ttm_bo_vm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/rbtree.h>
  37. #include <linux/module.h>
  38. #include <linux/uaccess.h>
  39. #define TTM_BO_VM_NUM_PREFAULT 16
  40. static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  41. struct vm_area_struct *vma,
  42. struct vm_fault *vmf)
  43. {
  44. struct ttm_bo_device *bdev = bo->bdev;
  45. int ret = 0;
  46. spin_lock(&bdev->fence_lock);
  47. if (likely(!test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)))
  48. goto out_unlock;
  49. /*
  50. * Quick non-stalling check for idle.
  51. */
  52. ret = ttm_bo_wait(bo, false, false, true);
  53. if (likely(ret == 0))
  54. goto out_unlock;
  55. /*
  56. * If possible, avoid waiting for GPU with mmap_sem
  57. * held.
  58. */
  59. if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  60. ret = VM_FAULT_RETRY;
  61. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  62. goto out_unlock;
  63. up_read(&vma->vm_mm->mmap_sem);
  64. (void) ttm_bo_wait(bo, false, true, false);
  65. goto out_unlock;
  66. }
  67. /*
  68. * Ordinary wait.
  69. */
  70. ret = ttm_bo_wait(bo, false, true, false);
  71. if (unlikely(ret != 0))
  72. ret = (ret != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
  73. VM_FAULT_NOPAGE;
  74. out_unlock:
  75. spin_unlock(&bdev->fence_lock);
  76. return ret;
  77. }
  78. static int ttm_bo_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  79. {
  80. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
  81. vma->vm_private_data;
  82. struct ttm_bo_device *bdev = bo->bdev;
  83. unsigned long page_offset;
  84. unsigned long page_last;
  85. unsigned long pfn;
  86. struct ttm_tt *ttm = NULL;
  87. struct page *page;
  88. int ret;
  89. int i;
  90. unsigned long address = (unsigned long)vmf->virtual_address;
  91. int retval = VM_FAULT_NOPAGE;
  92. struct ttm_mem_type_manager *man =
  93. &bdev->man[bo->mem.mem_type];
  94. struct vm_area_struct cvma;
  95. /*
  96. * Work around locking order reversal in fault / nopfn
  97. * between mmap_sem and bo_reserve: Perform a trylock operation
  98. * for reserve, and if it fails, retry the fault after waiting
  99. * for the buffer to become unreserved.
  100. */
  101. ret = ttm_bo_reserve(bo, true, true, false, NULL);
  102. if (unlikely(ret != 0)) {
  103. if (ret != -EBUSY)
  104. return VM_FAULT_NOPAGE;
  105. if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  106. if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  107. up_read(&vma->vm_mm->mmap_sem);
  108. (void) ttm_bo_wait_unreserved(bo);
  109. }
  110. return VM_FAULT_RETRY;
  111. }
  112. /*
  113. * If we'd want to change locking order to
  114. * mmap_sem -> bo::reserve, we'd use a blocking reserve here
  115. * instead of retrying the fault...
  116. */
  117. return VM_FAULT_NOPAGE;
  118. }
  119. if (bdev->driver->fault_reserve_notify) {
  120. ret = bdev->driver->fault_reserve_notify(bo);
  121. switch (ret) {
  122. case 0:
  123. break;
  124. case -EBUSY:
  125. case -ERESTARTSYS:
  126. retval = VM_FAULT_NOPAGE;
  127. goto out_unlock;
  128. default:
  129. retval = VM_FAULT_SIGBUS;
  130. goto out_unlock;
  131. }
  132. }
  133. /*
  134. * Wait for buffer data in transit, due to a pipelined
  135. * move.
  136. */
  137. ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
  138. if (unlikely(ret != 0)) {
  139. retval = ret;
  140. goto out_unlock;
  141. }
  142. ret = ttm_mem_io_lock(man, true);
  143. if (unlikely(ret != 0)) {
  144. retval = VM_FAULT_NOPAGE;
  145. goto out_unlock;
  146. }
  147. ret = ttm_mem_io_reserve_vm(bo);
  148. if (unlikely(ret != 0)) {
  149. retval = VM_FAULT_SIGBUS;
  150. goto out_io_unlock;
  151. }
  152. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  153. vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
  154. page_last = vma_pages(vma) + vma->vm_pgoff -
  155. drm_vma_node_start(&bo->vma_node);
  156. if (unlikely(page_offset >= bo->num_pages)) {
  157. retval = VM_FAULT_SIGBUS;
  158. goto out_io_unlock;
  159. }
  160. /*
  161. * Make a local vma copy to modify the page_prot member
  162. * and vm_flags if necessary. The vma parameter is protected
  163. * by mmap_sem in write mode.
  164. */
  165. cvma = *vma;
  166. cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
  167. if (bo->mem.bus.is_iomem) {
  168. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  169. cvma.vm_page_prot);
  170. } else {
  171. ttm = bo->ttm;
  172. if (!(bo->mem.placement & TTM_PL_FLAG_CACHED))
  173. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  174. cvma.vm_page_prot);
  175. /* Allocate all page at once, most common usage */
  176. if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
  177. retval = VM_FAULT_OOM;
  178. goto out_io_unlock;
  179. }
  180. }
  181. /*
  182. * Speculatively prefault a number of pages. Only error on
  183. * first page.
  184. */
  185. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  186. if (bo->mem.bus.is_iomem)
  187. pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
  188. else {
  189. page = ttm->pages[page_offset];
  190. if (unlikely(!page && i == 0)) {
  191. retval = VM_FAULT_OOM;
  192. goto out_io_unlock;
  193. } else if (unlikely(!page)) {
  194. break;
  195. }
  196. pfn = page_to_pfn(page);
  197. }
  198. ret = vm_insert_mixed(&cvma, address, pfn);
  199. /*
  200. * Somebody beat us to this PTE or prefaulting to
  201. * an already populated PTE, or prefaulting error.
  202. */
  203. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  204. break;
  205. else if (unlikely(ret != 0)) {
  206. retval =
  207. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  208. goto out_io_unlock;
  209. }
  210. address += PAGE_SIZE;
  211. if (unlikely(++page_offset >= page_last))
  212. break;
  213. }
  214. out_io_unlock:
  215. ttm_mem_io_unlock(man);
  216. out_unlock:
  217. ttm_bo_unreserve(bo);
  218. return retval;
  219. }
  220. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  221. {
  222. struct ttm_buffer_object *bo =
  223. (struct ttm_buffer_object *)vma->vm_private_data;
  224. (void)ttm_bo_reference(bo);
  225. }
  226. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  227. {
  228. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
  229. ttm_bo_unref(&bo);
  230. vma->vm_private_data = NULL;
  231. }
  232. static const struct vm_operations_struct ttm_bo_vm_ops = {
  233. .fault = ttm_bo_vm_fault,
  234. .open = ttm_bo_vm_open,
  235. .close = ttm_bo_vm_close
  236. };
  237. static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
  238. unsigned long offset,
  239. unsigned long pages)
  240. {
  241. struct drm_vma_offset_node *node;
  242. struct ttm_buffer_object *bo = NULL;
  243. drm_vma_offset_lock_lookup(&bdev->vma_manager);
  244. node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
  245. if (likely(node)) {
  246. bo = container_of(node, struct ttm_buffer_object, vma_node);
  247. if (!kref_get_unless_zero(&bo->kref))
  248. bo = NULL;
  249. }
  250. drm_vma_offset_unlock_lookup(&bdev->vma_manager);
  251. if (!bo)
  252. pr_err("Could not find buffer object to map\n");
  253. return bo;
  254. }
  255. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  256. struct ttm_bo_device *bdev)
  257. {
  258. struct ttm_bo_driver *driver;
  259. struct ttm_buffer_object *bo;
  260. int ret;
  261. bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
  262. if (unlikely(!bo))
  263. return -EINVAL;
  264. driver = bo->bdev->driver;
  265. if (unlikely(!driver->verify_access)) {
  266. ret = -EPERM;
  267. goto out_unref;
  268. }
  269. ret = driver->verify_access(bo, filp);
  270. if (unlikely(ret != 0))
  271. goto out_unref;
  272. vma->vm_ops = &ttm_bo_vm_ops;
  273. /*
  274. * Note: We're transferring the bo reference to
  275. * vma->vm_private_data here.
  276. */
  277. vma->vm_private_data = bo;
  278. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
  279. return 0;
  280. out_unref:
  281. ttm_bo_unref(&bo);
  282. return ret;
  283. }
  284. EXPORT_SYMBOL(ttm_bo_mmap);
  285. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  286. {
  287. if (vma->vm_pgoff != 0)
  288. return -EACCES;
  289. vma->vm_ops = &ttm_bo_vm_ops;
  290. vma->vm_private_data = ttm_bo_reference(bo);
  291. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(ttm_fbdev_mmap);