ttm_bo_vm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #define pr_fmt(fmt) "[TTM] " fmt
  31. #include <ttm/ttm_module.h>
  32. #include <ttm/ttm_bo_driver.h>
  33. #include <ttm/ttm_placement.h>
  34. #include <drm/drm_vma_manager.h>
  35. #include <linux/mm.h>
  36. #include <linux/pfn_t.h>
  37. #include <linux/rbtree.h>
  38. #include <linux/module.h>
  39. #include <linux/uaccess.h>
  40. #define TTM_BO_VM_NUM_PREFAULT 16
  41. static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  42. struct vm_area_struct *vma,
  43. struct vm_fault *vmf)
  44. {
  45. int ret = 0;
  46. if (likely(!bo->moving))
  47. goto out_unlock;
  48. /*
  49. * Quick non-stalling check for idle.
  50. */
  51. if (fence_is_signaled(bo->moving))
  52. goto out_clear;
  53. /*
  54. * If possible, avoid waiting for GPU with mmap_sem
  55. * held.
  56. */
  57. if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  58. ret = VM_FAULT_RETRY;
  59. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  60. goto out_unlock;
  61. ttm_bo_reference(bo);
  62. up_read(&vma->vm_mm->mmap_sem);
  63. (void) fence_wait(bo->moving, true);
  64. ttm_bo_unreserve(bo);
  65. ttm_bo_unref(&bo);
  66. goto out_unlock;
  67. }
  68. /*
  69. * Ordinary wait.
  70. */
  71. ret = fence_wait(bo->moving, true);
  72. if (unlikely(ret != 0)) {
  73. ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
  74. VM_FAULT_NOPAGE;
  75. goto out_unlock;
  76. }
  77. out_clear:
  78. fence_put(bo->moving);
  79. bo->moving = NULL;
  80. out_unlock:
  81. return ret;
  82. }
  83. static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  84. {
  85. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
  86. vma->vm_private_data;
  87. struct ttm_bo_device *bdev = bo->bdev;
  88. unsigned long page_offset;
  89. unsigned long page_last;
  90. unsigned long pfn;
  91. struct ttm_tt *ttm = NULL;
  92. struct page *page;
  93. int ret;
  94. int i;
  95. unsigned long address = (unsigned long)vmf->virtual_address;
  96. int retval = VM_FAULT_NOPAGE;
  97. struct ttm_mem_type_manager *man =
  98. &bdev->man[bo->mem.mem_type];
  99. struct vm_area_struct cvma;
  100. /*
  101. * Work around locking order reversal in fault / nopfn
  102. * between mmap_sem and bo_reserve: Perform a trylock operation
  103. * for reserve, and if it fails, retry the fault after waiting
  104. * for the buffer to become unreserved.
  105. */
  106. ret = ttm_bo_reserve(bo, true, true, NULL);
  107. if (unlikely(ret != 0)) {
  108. if (ret != -EBUSY)
  109. return VM_FAULT_NOPAGE;
  110. if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  111. if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  112. ttm_bo_reference(bo);
  113. up_read(&vma->vm_mm->mmap_sem);
  114. (void) ttm_bo_wait_unreserved(bo);
  115. ttm_bo_unref(&bo);
  116. }
  117. return VM_FAULT_RETRY;
  118. }
  119. /*
  120. * If we'd want to change locking order to
  121. * mmap_sem -> bo::reserve, we'd use a blocking reserve here
  122. * instead of retrying the fault...
  123. */
  124. return VM_FAULT_NOPAGE;
  125. }
  126. /*
  127. * Refuse to fault imported pages. This should be handled
  128. * (if at all) by redirecting mmap to the exporter.
  129. */
  130. if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
  131. retval = VM_FAULT_SIGBUS;
  132. goto out_unlock;
  133. }
  134. if (bdev->driver->fault_reserve_notify) {
  135. ret = bdev->driver->fault_reserve_notify(bo);
  136. switch (ret) {
  137. case 0:
  138. break;
  139. case -EBUSY:
  140. case -ERESTARTSYS:
  141. retval = VM_FAULT_NOPAGE;
  142. goto out_unlock;
  143. default:
  144. retval = VM_FAULT_SIGBUS;
  145. goto out_unlock;
  146. }
  147. }
  148. /*
  149. * Wait for buffer data in transit, due to a pipelined
  150. * move.
  151. */
  152. ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
  153. if (unlikely(ret != 0)) {
  154. retval = ret;
  155. if (retval == VM_FAULT_RETRY &&
  156. !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  157. /* The BO has already been unreserved. */
  158. return retval;
  159. }
  160. goto out_unlock;
  161. }
  162. ret = ttm_mem_io_lock(man, true);
  163. if (unlikely(ret != 0)) {
  164. retval = VM_FAULT_NOPAGE;
  165. goto out_unlock;
  166. }
  167. ret = ttm_mem_io_reserve_vm(bo);
  168. if (unlikely(ret != 0)) {
  169. retval = VM_FAULT_SIGBUS;
  170. goto out_io_unlock;
  171. }
  172. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  173. vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
  174. page_last = vma_pages(vma) + vma->vm_pgoff -
  175. drm_vma_node_start(&bo->vma_node);
  176. if (unlikely(page_offset >= bo->num_pages)) {
  177. retval = VM_FAULT_SIGBUS;
  178. goto out_io_unlock;
  179. }
  180. /*
  181. * Make a local vma copy to modify the page_prot member
  182. * and vm_flags if necessary. The vma parameter is protected
  183. * by mmap_sem in write mode.
  184. */
  185. cvma = *vma;
  186. cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
  187. if (bo->mem.bus.is_iomem) {
  188. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  189. cvma.vm_page_prot);
  190. } else {
  191. ttm = bo->ttm;
  192. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  193. cvma.vm_page_prot);
  194. /* Allocate all page at once, most common usage */
  195. if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
  196. retval = VM_FAULT_OOM;
  197. goto out_io_unlock;
  198. }
  199. }
  200. /*
  201. * Speculatively prefault a number of pages. Only error on
  202. * first page.
  203. */
  204. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  205. if (bo->mem.bus.is_iomem)
  206. pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
  207. else {
  208. page = ttm->pages[page_offset];
  209. if (unlikely(!page && i == 0)) {
  210. retval = VM_FAULT_OOM;
  211. goto out_io_unlock;
  212. } else if (unlikely(!page)) {
  213. break;
  214. }
  215. page->mapping = vma->vm_file->f_mapping;
  216. page->index = drm_vma_node_start(&bo->vma_node) +
  217. page_offset;
  218. pfn = page_to_pfn(page);
  219. }
  220. if (vma->vm_flags & VM_MIXEDMAP)
  221. ret = vm_insert_mixed(&cvma, address,
  222. __pfn_to_pfn_t(pfn, PFN_DEV));
  223. else
  224. ret = vm_insert_pfn(&cvma, address, pfn);
  225. /*
  226. * Somebody beat us to this PTE or prefaulting to
  227. * an already populated PTE, or prefaulting error.
  228. */
  229. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  230. break;
  231. else if (unlikely(ret != 0)) {
  232. retval =
  233. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  234. goto out_io_unlock;
  235. }
  236. address += PAGE_SIZE;
  237. if (unlikely(++page_offset >= page_last))
  238. break;
  239. }
  240. out_io_unlock:
  241. ttm_mem_io_unlock(man);
  242. out_unlock:
  243. ttm_bo_unreserve(bo);
  244. return retval;
  245. }
  246. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  247. {
  248. struct ttm_buffer_object *bo =
  249. (struct ttm_buffer_object *)vma->vm_private_data;
  250. WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
  251. (void)ttm_bo_reference(bo);
  252. }
  253. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  254. {
  255. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
  256. ttm_bo_unref(&bo);
  257. vma->vm_private_data = NULL;
  258. }
  259. static const struct vm_operations_struct ttm_bo_vm_ops = {
  260. .fault = ttm_bo_vm_fault,
  261. .open = ttm_bo_vm_open,
  262. .close = ttm_bo_vm_close
  263. };
  264. static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
  265. unsigned long offset,
  266. unsigned long pages)
  267. {
  268. struct drm_vma_offset_node *node;
  269. struct ttm_buffer_object *bo = NULL;
  270. drm_vma_offset_lock_lookup(&bdev->vma_manager);
  271. node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
  272. if (likely(node)) {
  273. bo = container_of(node, struct ttm_buffer_object, vma_node);
  274. if (!kref_get_unless_zero(&bo->kref))
  275. bo = NULL;
  276. }
  277. drm_vma_offset_unlock_lookup(&bdev->vma_manager);
  278. if (!bo)
  279. pr_err("Could not find buffer object to map\n");
  280. return bo;
  281. }
  282. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  283. struct ttm_bo_device *bdev)
  284. {
  285. struct ttm_bo_driver *driver;
  286. struct ttm_buffer_object *bo;
  287. int ret;
  288. bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
  289. if (unlikely(!bo))
  290. return -EINVAL;
  291. driver = bo->bdev->driver;
  292. if (unlikely(!driver->verify_access)) {
  293. ret = -EPERM;
  294. goto out_unref;
  295. }
  296. ret = driver->verify_access(bo, filp);
  297. if (unlikely(ret != 0))
  298. goto out_unref;
  299. vma->vm_ops = &ttm_bo_vm_ops;
  300. /*
  301. * Note: We're transferring the bo reference to
  302. * vma->vm_private_data here.
  303. */
  304. vma->vm_private_data = bo;
  305. /*
  306. * We'd like to use VM_PFNMAP on shared mappings, where
  307. * (vma->vm_flags & VM_SHARED) != 0, for performance reasons,
  308. * but for some reason VM_PFNMAP + x86 PAT + write-combine is very
  309. * bad for performance. Until that has been sorted out, use
  310. * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719
  311. */
  312. vma->vm_flags |= VM_MIXEDMAP;
  313. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  314. return 0;
  315. out_unref:
  316. ttm_bo_unref(&bo);
  317. return ret;
  318. }
  319. EXPORT_SYMBOL(ttm_bo_mmap);
  320. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  321. {
  322. if (vma->vm_pgoff != 0)
  323. return -EACCES;
  324. vma->vm_ops = &ttm_bo_vm_ops;
  325. vma->vm_private_data = ttm_bo_reference(bo);
  326. vma->vm_flags |= VM_MIXEDMAP;
  327. vma->vm_flags |= VM_IO | VM_DONTEXPAND;
  328. return 0;
  329. }
  330. EXPORT_SYMBOL(ttm_fbdev_mmap);