ttm_bo_vm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 scheduling.
  99. */
  100. ret = ttm_bo_reserve(bo, true, true, false, 0);
  101. if (unlikely(ret != 0)) {
  102. if (ret == -EBUSY)
  103. set_need_resched();
  104. return VM_FAULT_NOPAGE;
  105. }
  106. if (bdev->driver->fault_reserve_notify) {
  107. ret = bdev->driver->fault_reserve_notify(bo);
  108. switch (ret) {
  109. case 0:
  110. break;
  111. case -EBUSY:
  112. set_need_resched();
  113. case -ERESTARTSYS:
  114. retval = VM_FAULT_NOPAGE;
  115. goto out_unlock;
  116. default:
  117. retval = VM_FAULT_SIGBUS;
  118. goto out_unlock;
  119. }
  120. }
  121. /*
  122. * Wait for buffer data in transit, due to a pipelined
  123. * move.
  124. */
  125. ret = ttm_bo_vm_fault_idle(bo, vma, vmf);
  126. if (unlikely(ret != 0)) {
  127. retval = ret;
  128. goto out_unlock;
  129. }
  130. ret = ttm_mem_io_lock(man, true);
  131. if (unlikely(ret != 0)) {
  132. retval = VM_FAULT_NOPAGE;
  133. goto out_unlock;
  134. }
  135. ret = ttm_mem_io_reserve_vm(bo);
  136. if (unlikely(ret != 0)) {
  137. retval = VM_FAULT_SIGBUS;
  138. goto out_io_unlock;
  139. }
  140. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  141. drm_vma_node_start(&bo->vma_node) - vma->vm_pgoff;
  142. page_last = vma_pages(vma) +
  143. drm_vma_node_start(&bo->vma_node) - vma->vm_pgoff;
  144. if (unlikely(page_offset >= bo->num_pages)) {
  145. retval = VM_FAULT_SIGBUS;
  146. goto out_io_unlock;
  147. }
  148. /*
  149. * Make a local vma copy to modify the page_prot member
  150. * and vm_flags if necessary. The vma parameter is protected
  151. * by mmap_sem in write mode.
  152. */
  153. cvma = *vma;
  154. cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
  155. if (bo->mem.bus.is_iomem) {
  156. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  157. cvma.vm_page_prot);
  158. } else {
  159. ttm = bo->ttm;
  160. if (!(bo->mem.placement & TTM_PL_FLAG_CACHED))
  161. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  162. cvma.vm_page_prot);
  163. /* Allocate all page at once, most common usage */
  164. if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
  165. retval = VM_FAULT_OOM;
  166. goto out_io_unlock;
  167. }
  168. }
  169. /*
  170. * Speculatively prefault a number of pages. Only error on
  171. * first page.
  172. */
  173. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  174. if (bo->mem.bus.is_iomem)
  175. pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
  176. else {
  177. page = ttm->pages[page_offset];
  178. if (unlikely(!page && i == 0)) {
  179. retval = VM_FAULT_OOM;
  180. goto out_io_unlock;
  181. } else if (unlikely(!page)) {
  182. break;
  183. }
  184. pfn = page_to_pfn(page);
  185. }
  186. ret = vm_insert_mixed(&cvma, address, pfn);
  187. /*
  188. * Somebody beat us to this PTE or prefaulting to
  189. * an already populated PTE, or prefaulting error.
  190. */
  191. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  192. break;
  193. else if (unlikely(ret != 0)) {
  194. retval =
  195. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  196. goto out_io_unlock;
  197. }
  198. address += PAGE_SIZE;
  199. if (unlikely(++page_offset >= page_last))
  200. break;
  201. }
  202. out_io_unlock:
  203. ttm_mem_io_unlock(man);
  204. out_unlock:
  205. ttm_bo_unreserve(bo);
  206. return retval;
  207. }
  208. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  209. {
  210. struct ttm_buffer_object *bo =
  211. (struct ttm_buffer_object *)vma->vm_private_data;
  212. (void)ttm_bo_reference(bo);
  213. }
  214. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  215. {
  216. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
  217. ttm_bo_unref(&bo);
  218. vma->vm_private_data = NULL;
  219. }
  220. static const struct vm_operations_struct ttm_bo_vm_ops = {
  221. .fault = ttm_bo_vm_fault,
  222. .open = ttm_bo_vm_open,
  223. .close = ttm_bo_vm_close
  224. };
  225. static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
  226. unsigned long offset,
  227. unsigned long pages)
  228. {
  229. struct drm_vma_offset_node *node;
  230. struct ttm_buffer_object *bo = NULL;
  231. drm_vma_offset_lock_lookup(&bdev->vma_manager);
  232. node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
  233. if (likely(node)) {
  234. bo = container_of(node, struct ttm_buffer_object, vma_node);
  235. if (!kref_get_unless_zero(&bo->kref))
  236. bo = NULL;
  237. }
  238. drm_vma_offset_unlock_lookup(&bdev->vma_manager);
  239. if (!bo)
  240. pr_err("Could not find buffer object to map\n");
  241. return bo;
  242. }
  243. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  244. struct ttm_bo_device *bdev)
  245. {
  246. struct ttm_bo_driver *driver;
  247. struct ttm_buffer_object *bo;
  248. int ret;
  249. bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
  250. if (unlikely(!bo))
  251. return -EINVAL;
  252. driver = bo->bdev->driver;
  253. if (unlikely(!driver->verify_access)) {
  254. ret = -EPERM;
  255. goto out_unref;
  256. }
  257. ret = driver->verify_access(bo, filp);
  258. if (unlikely(ret != 0))
  259. goto out_unref;
  260. vma->vm_ops = &ttm_bo_vm_ops;
  261. /*
  262. * Note: We're transferring the bo reference to
  263. * vma->vm_private_data here.
  264. */
  265. vma->vm_private_data = bo;
  266. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
  267. return 0;
  268. out_unref:
  269. ttm_bo_unref(&bo);
  270. return ret;
  271. }
  272. EXPORT_SYMBOL(ttm_bo_mmap);
  273. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  274. {
  275. if (vma->vm_pgoff != 0)
  276. return -EACCES;
  277. vma->vm_ops = &ttm_bo_vm_ops;
  278. vma->vm_private_data = ttm_bo_reference(bo);
  279. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  280. return 0;
  281. }
  282. EXPORT_SYMBOL(ttm_fbdev_mmap);