ttm_bo_vm.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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(struct vm_area_struct *vma, struct vm_fault *vmf)
  41. {
  42. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
  43. vma->vm_private_data;
  44. struct ttm_bo_device *bdev = bo->bdev;
  45. unsigned long page_offset;
  46. unsigned long page_last;
  47. unsigned long pfn;
  48. struct ttm_tt *ttm = NULL;
  49. struct page *page;
  50. int ret;
  51. int i;
  52. unsigned long address = (unsigned long)vmf->virtual_address;
  53. int retval = VM_FAULT_NOPAGE;
  54. struct ttm_mem_type_manager *man =
  55. &bdev->man[bo->mem.mem_type];
  56. /*
  57. * Work around locking order reversal in fault / nopfn
  58. * between mmap_sem and bo_reserve: Perform a trylock operation
  59. * for reserve, and if it fails, retry the fault after scheduling.
  60. */
  61. ret = ttm_bo_reserve(bo, true, true, false, 0);
  62. if (unlikely(ret != 0)) {
  63. if (ret == -EBUSY)
  64. set_need_resched();
  65. return VM_FAULT_NOPAGE;
  66. }
  67. if (bdev->driver->fault_reserve_notify) {
  68. ret = bdev->driver->fault_reserve_notify(bo);
  69. switch (ret) {
  70. case 0:
  71. break;
  72. case -EBUSY:
  73. set_need_resched();
  74. case -ERESTARTSYS:
  75. retval = VM_FAULT_NOPAGE;
  76. goto out_unlock;
  77. default:
  78. retval = VM_FAULT_SIGBUS;
  79. goto out_unlock;
  80. }
  81. }
  82. /*
  83. * Wait for buffer data in transit, due to a pipelined
  84. * move.
  85. */
  86. spin_lock(&bdev->fence_lock);
  87. if (test_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags)) {
  88. ret = ttm_bo_wait(bo, false, true, false);
  89. spin_unlock(&bdev->fence_lock);
  90. if (unlikely(ret != 0)) {
  91. retval = (ret != -ERESTARTSYS) ?
  92. VM_FAULT_SIGBUS : VM_FAULT_NOPAGE;
  93. goto out_unlock;
  94. }
  95. } else
  96. spin_unlock(&bdev->fence_lock);
  97. ret = ttm_mem_io_lock(man, true);
  98. if (unlikely(ret != 0)) {
  99. retval = VM_FAULT_NOPAGE;
  100. goto out_unlock;
  101. }
  102. ret = ttm_mem_io_reserve_vm(bo);
  103. if (unlikely(ret != 0)) {
  104. retval = VM_FAULT_SIGBUS;
  105. goto out_io_unlock;
  106. }
  107. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  108. drm_vma_node_start(&bo->vma_node) - vma->vm_pgoff;
  109. page_last = vma_pages(vma) +
  110. drm_vma_node_start(&bo->vma_node) - vma->vm_pgoff;
  111. if (unlikely(page_offset >= bo->num_pages)) {
  112. retval = VM_FAULT_SIGBUS;
  113. goto out_io_unlock;
  114. }
  115. /*
  116. * Strictly, we're not allowed to modify vma->vm_page_prot here,
  117. * since the mmap_sem is only held in read mode. However, we
  118. * modify only the caching bits of vma->vm_page_prot and
  119. * consider those bits protected by
  120. * the bo->mutex, as we should be the only writers.
  121. * There shouldn't really be any readers of these bits except
  122. * within vm_insert_mixed()? fork?
  123. *
  124. * TODO: Add a list of vmas to the bo, and change the
  125. * vma->vm_page_prot when the object changes caching policy, with
  126. * the correct locks held.
  127. */
  128. if (bo->mem.bus.is_iomem) {
  129. vma->vm_page_prot = ttm_io_prot(bo->mem.placement,
  130. vma->vm_page_prot);
  131. } else {
  132. ttm = bo->ttm;
  133. vma->vm_page_prot = (bo->mem.placement & TTM_PL_FLAG_CACHED) ?
  134. vm_get_page_prot(vma->vm_flags) :
  135. ttm_io_prot(bo->mem.placement, vma->vm_page_prot);
  136. /* Allocate all page at once, most common usage */
  137. if (ttm->bdev->driver->ttm_tt_populate(ttm)) {
  138. retval = VM_FAULT_OOM;
  139. goto out_io_unlock;
  140. }
  141. }
  142. /*
  143. * Speculatively prefault a number of pages. Only error on
  144. * first page.
  145. */
  146. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  147. if (bo->mem.bus.is_iomem)
  148. pfn = ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT) + page_offset;
  149. else {
  150. page = ttm->pages[page_offset];
  151. if (unlikely(!page && i == 0)) {
  152. retval = VM_FAULT_OOM;
  153. goto out_io_unlock;
  154. } else if (unlikely(!page)) {
  155. break;
  156. }
  157. pfn = page_to_pfn(page);
  158. }
  159. ret = vm_insert_mixed(vma, address, pfn);
  160. /*
  161. * Somebody beat us to this PTE or prefaulting to
  162. * an already populated PTE, or prefaulting error.
  163. */
  164. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  165. break;
  166. else if (unlikely(ret != 0)) {
  167. retval =
  168. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  169. goto out_io_unlock;
  170. }
  171. address += PAGE_SIZE;
  172. if (unlikely(++page_offset >= page_last))
  173. break;
  174. }
  175. out_io_unlock:
  176. ttm_mem_io_unlock(man);
  177. out_unlock:
  178. ttm_bo_unreserve(bo);
  179. return retval;
  180. }
  181. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  182. {
  183. struct ttm_buffer_object *bo =
  184. (struct ttm_buffer_object *)vma->vm_private_data;
  185. (void)ttm_bo_reference(bo);
  186. }
  187. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  188. {
  189. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
  190. ttm_bo_unref(&bo);
  191. vma->vm_private_data = NULL;
  192. }
  193. static const struct vm_operations_struct ttm_bo_vm_ops = {
  194. .fault = ttm_bo_vm_fault,
  195. .open = ttm_bo_vm_open,
  196. .close = ttm_bo_vm_close
  197. };
  198. static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
  199. unsigned long offset,
  200. unsigned long pages)
  201. {
  202. struct drm_vma_offset_node *node;
  203. struct ttm_buffer_object *bo = NULL;
  204. drm_vma_offset_lock_lookup(&bdev->vma_manager);
  205. node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
  206. if (likely(node)) {
  207. bo = container_of(node, struct ttm_buffer_object, vma_node);
  208. if (!kref_get_unless_zero(&bo->kref))
  209. bo = NULL;
  210. }
  211. drm_vma_offset_unlock_lookup(&bdev->vma_manager);
  212. if (!bo)
  213. pr_err("Could not find buffer object to map\n");
  214. return bo;
  215. }
  216. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  217. struct ttm_bo_device *bdev)
  218. {
  219. struct ttm_bo_driver *driver;
  220. struct ttm_buffer_object *bo;
  221. int ret;
  222. bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
  223. if (unlikely(!bo))
  224. return -EINVAL;
  225. driver = bo->bdev->driver;
  226. if (unlikely(!driver->verify_access)) {
  227. ret = -EPERM;
  228. goto out_unref;
  229. }
  230. ret = driver->verify_access(bo, filp);
  231. if (unlikely(ret != 0))
  232. goto out_unref;
  233. vma->vm_ops = &ttm_bo_vm_ops;
  234. /*
  235. * Note: We're transferring the bo reference to
  236. * vma->vm_private_data here.
  237. */
  238. vma->vm_private_data = bo;
  239. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
  240. return 0;
  241. out_unref:
  242. ttm_bo_unref(&bo);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL(ttm_bo_mmap);
  246. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  247. {
  248. if (vma->vm_pgoff != 0)
  249. return -EACCES;
  250. vma->vm_ops = &ttm_bo_vm_ops;
  251. vma->vm_private_data = ttm_bo_reference(bo);
  252. vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND;
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(ttm_fbdev_mmap);