ttm_bo_vm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 <drm/ttm/ttm_module.h>
  32. #include <drm/ttm/ttm_bo_driver.h>
  33. #include <drm/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. #include <linux/mem_encrypt.h>
  41. #define TTM_BO_VM_NUM_PREFAULT 16
  42. static int ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  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 (dma_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(&vmf->vma->vm_mm->mmap_sem);
  63. (void) dma_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 = dma_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. dma_fence_put(bo->moving);
  79. bo->moving = NULL;
  80. out_unlock:
  81. return ret;
  82. }
  83. static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo,
  84. unsigned long page_offset)
  85. {
  86. struct ttm_bo_device *bdev = bo->bdev;
  87. if (bdev->driver->io_mem_pfn)
  88. return bdev->driver->io_mem_pfn(bo, page_offset);
  89. return ((bo->mem.bus.base + bo->mem.bus.offset) >> PAGE_SHIFT)
  90. + page_offset;
  91. }
  92. static int ttm_bo_vm_fault(struct vm_fault *vmf)
  93. {
  94. struct vm_area_struct *vma = vmf->vma;
  95. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)
  96. vma->vm_private_data;
  97. struct ttm_bo_device *bdev = bo->bdev;
  98. unsigned long page_offset;
  99. unsigned long page_last;
  100. unsigned long pfn;
  101. struct ttm_tt *ttm = NULL;
  102. struct page *page;
  103. int ret;
  104. int i;
  105. unsigned long address = vmf->address;
  106. int retval = VM_FAULT_NOPAGE;
  107. struct ttm_mem_type_manager *man =
  108. &bdev->man[bo->mem.mem_type];
  109. struct vm_area_struct cvma;
  110. /*
  111. * Work around locking order reversal in fault / nopfn
  112. * between mmap_sem and bo_reserve: Perform a trylock operation
  113. * for reserve, and if it fails, retry the fault after waiting
  114. * for the buffer to become unreserved.
  115. */
  116. ret = ttm_bo_reserve(bo, true, true, NULL);
  117. if (unlikely(ret != 0)) {
  118. if (ret != -EBUSY)
  119. return VM_FAULT_NOPAGE;
  120. if (vmf->flags & FAULT_FLAG_ALLOW_RETRY) {
  121. if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  122. ttm_bo_reference(bo);
  123. up_read(&vmf->vma->vm_mm->mmap_sem);
  124. (void) ttm_bo_wait_unreserved(bo);
  125. ttm_bo_unref(&bo);
  126. }
  127. return VM_FAULT_RETRY;
  128. }
  129. /*
  130. * If we'd want to change locking order to
  131. * mmap_sem -> bo::reserve, we'd use a blocking reserve here
  132. * instead of retrying the fault...
  133. */
  134. return VM_FAULT_NOPAGE;
  135. }
  136. /*
  137. * Refuse to fault imported pages. This should be handled
  138. * (if at all) by redirecting mmap to the exporter.
  139. */
  140. if (bo->ttm && (bo->ttm->page_flags & TTM_PAGE_FLAG_SG)) {
  141. retval = VM_FAULT_SIGBUS;
  142. goto out_unlock;
  143. }
  144. if (bdev->driver->fault_reserve_notify) {
  145. ret = bdev->driver->fault_reserve_notify(bo);
  146. switch (ret) {
  147. case 0:
  148. break;
  149. case -EBUSY:
  150. case -ERESTARTSYS:
  151. retval = VM_FAULT_NOPAGE;
  152. goto out_unlock;
  153. default:
  154. retval = VM_FAULT_SIGBUS;
  155. goto out_unlock;
  156. }
  157. }
  158. /*
  159. * Wait for buffer data in transit, due to a pipelined
  160. * move.
  161. */
  162. ret = ttm_bo_vm_fault_idle(bo, vmf);
  163. if (unlikely(ret != 0)) {
  164. retval = ret;
  165. if (retval == VM_FAULT_RETRY &&
  166. !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  167. /* The BO has already been unreserved. */
  168. return retval;
  169. }
  170. goto out_unlock;
  171. }
  172. ret = ttm_mem_io_lock(man, true);
  173. if (unlikely(ret != 0)) {
  174. retval = VM_FAULT_NOPAGE;
  175. goto out_unlock;
  176. }
  177. ret = ttm_mem_io_reserve_vm(bo);
  178. if (unlikely(ret != 0)) {
  179. retval = VM_FAULT_SIGBUS;
  180. goto out_io_unlock;
  181. }
  182. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  183. vma->vm_pgoff - drm_vma_node_start(&bo->vma_node);
  184. page_last = vma_pages(vma) + vma->vm_pgoff -
  185. drm_vma_node_start(&bo->vma_node);
  186. if (unlikely(page_offset >= bo->num_pages)) {
  187. retval = VM_FAULT_SIGBUS;
  188. goto out_io_unlock;
  189. }
  190. /*
  191. * Make a local vma copy to modify the page_prot member
  192. * and vm_flags if necessary. The vma parameter is protected
  193. * by mmap_sem in write mode.
  194. */
  195. cvma = *vma;
  196. cvma.vm_page_prot = vm_get_page_prot(cvma.vm_flags);
  197. if (bo->mem.bus.is_iomem) {
  198. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  199. cvma.vm_page_prot);
  200. } else {
  201. struct ttm_operation_ctx ctx = {
  202. .interruptible = false,
  203. .no_wait_gpu = false
  204. };
  205. ttm = bo->ttm;
  206. cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  207. cvma.vm_page_prot);
  208. /* Allocate all page at once, most common usage */
  209. if (ttm->bdev->driver->ttm_tt_populate(ttm, &ctx)) {
  210. retval = VM_FAULT_OOM;
  211. goto out_io_unlock;
  212. }
  213. }
  214. /*
  215. * Speculatively prefault a number of pages. Only error on
  216. * first page.
  217. */
  218. for (i = 0; i < TTM_BO_VM_NUM_PREFAULT; ++i) {
  219. if (bo->mem.bus.is_iomem) {
  220. /* Iomem should not be marked encrypted */
  221. cvma.vm_page_prot = pgprot_decrypted(cvma.vm_page_prot);
  222. pfn = ttm_bo_io_mem_pfn(bo, page_offset);
  223. } else {
  224. page = ttm->pages[page_offset];
  225. if (unlikely(!page && i == 0)) {
  226. retval = VM_FAULT_OOM;
  227. goto out_io_unlock;
  228. } else if (unlikely(!page)) {
  229. break;
  230. }
  231. page->mapping = vma->vm_file->f_mapping;
  232. page->index = drm_vma_node_start(&bo->vma_node) +
  233. page_offset;
  234. pfn = page_to_pfn(page);
  235. }
  236. if (vma->vm_flags & VM_MIXEDMAP)
  237. ret = vm_insert_mixed(&cvma, address,
  238. __pfn_to_pfn_t(pfn, PFN_DEV));
  239. else
  240. ret = vm_insert_pfn(&cvma, address, pfn);
  241. /*
  242. * Somebody beat us to this PTE or prefaulting to
  243. * an already populated PTE, or prefaulting error.
  244. */
  245. if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0)))
  246. break;
  247. else if (unlikely(ret != 0)) {
  248. retval =
  249. (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
  250. goto out_io_unlock;
  251. }
  252. address += PAGE_SIZE;
  253. if (unlikely(++page_offset >= page_last))
  254. break;
  255. }
  256. out_io_unlock:
  257. ttm_mem_io_unlock(man);
  258. out_unlock:
  259. ttm_bo_unreserve(bo);
  260. return retval;
  261. }
  262. static void ttm_bo_vm_open(struct vm_area_struct *vma)
  263. {
  264. struct ttm_buffer_object *bo =
  265. (struct ttm_buffer_object *)vma->vm_private_data;
  266. WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
  267. (void)ttm_bo_reference(bo);
  268. }
  269. static void ttm_bo_vm_close(struct vm_area_struct *vma)
  270. {
  271. struct ttm_buffer_object *bo = (struct ttm_buffer_object *)vma->vm_private_data;
  272. ttm_bo_unref(&bo);
  273. vma->vm_private_data = NULL;
  274. }
  275. static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo,
  276. unsigned long offset,
  277. void *buf, int len, int write)
  278. {
  279. unsigned long page = offset >> PAGE_SHIFT;
  280. unsigned long bytes_left = len;
  281. int ret;
  282. /* Copy a page at a time, that way no extra virtual address
  283. * mapping is needed
  284. */
  285. offset -= page << PAGE_SHIFT;
  286. do {
  287. unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
  288. struct ttm_bo_kmap_obj map;
  289. void *ptr;
  290. bool is_iomem;
  291. ret = ttm_bo_kmap(bo, page, 1, &map);
  292. if (ret)
  293. return ret;
  294. ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset;
  295. WARN_ON_ONCE(is_iomem);
  296. if (write)
  297. memcpy(ptr, buf, bytes);
  298. else
  299. memcpy(buf, ptr, bytes);
  300. ttm_bo_kunmap(&map);
  301. page++;
  302. bytes_left -= bytes;
  303. offset = 0;
  304. } while (bytes_left);
  305. return len;
  306. }
  307. static int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  308. void *buf, int len, int write)
  309. {
  310. unsigned long offset = (addr) - vma->vm_start;
  311. struct ttm_buffer_object *bo = vma->vm_private_data;
  312. int ret;
  313. if (len < 1 || (offset + len) >> PAGE_SHIFT > bo->num_pages)
  314. return -EIO;
  315. ret = ttm_bo_reserve(bo, true, false, NULL);
  316. if (ret)
  317. return ret;
  318. switch (bo->mem.mem_type) {
  319. case TTM_PL_SYSTEM:
  320. if (unlikely(bo->ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  321. ret = ttm_tt_swapin(bo->ttm);
  322. if (unlikely(ret != 0))
  323. return ret;
  324. }
  325. /* fall through */
  326. case TTM_PL_TT:
  327. ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write);
  328. break;
  329. default:
  330. if (bo->bdev->driver->access_memory)
  331. ret = bo->bdev->driver->access_memory(
  332. bo, offset, buf, len, write);
  333. else
  334. ret = -EIO;
  335. }
  336. ttm_bo_unreserve(bo);
  337. return ret;
  338. }
  339. static const struct vm_operations_struct ttm_bo_vm_ops = {
  340. .fault = ttm_bo_vm_fault,
  341. .open = ttm_bo_vm_open,
  342. .close = ttm_bo_vm_close,
  343. .access = ttm_bo_vm_access
  344. };
  345. static struct ttm_buffer_object *ttm_bo_vm_lookup(struct ttm_bo_device *bdev,
  346. unsigned long offset,
  347. unsigned long pages)
  348. {
  349. struct drm_vma_offset_node *node;
  350. struct ttm_buffer_object *bo = NULL;
  351. drm_vma_offset_lock_lookup(&bdev->vma_manager);
  352. node = drm_vma_offset_lookup_locked(&bdev->vma_manager, offset, pages);
  353. if (likely(node)) {
  354. bo = container_of(node, struct ttm_buffer_object, vma_node);
  355. if (!kref_get_unless_zero(&bo->kref))
  356. bo = NULL;
  357. }
  358. drm_vma_offset_unlock_lookup(&bdev->vma_manager);
  359. if (!bo)
  360. pr_err("Could not find buffer object to map\n");
  361. return bo;
  362. }
  363. int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
  364. struct ttm_bo_device *bdev)
  365. {
  366. struct ttm_bo_driver *driver;
  367. struct ttm_buffer_object *bo;
  368. int ret;
  369. bo = ttm_bo_vm_lookup(bdev, vma->vm_pgoff, vma_pages(vma));
  370. if (unlikely(!bo))
  371. return -EINVAL;
  372. driver = bo->bdev->driver;
  373. if (unlikely(!driver->verify_access)) {
  374. ret = -EPERM;
  375. goto out_unref;
  376. }
  377. ret = driver->verify_access(bo, filp);
  378. if (unlikely(ret != 0))
  379. goto out_unref;
  380. vma->vm_ops = &ttm_bo_vm_ops;
  381. /*
  382. * Note: We're transferring the bo reference to
  383. * vma->vm_private_data here.
  384. */
  385. vma->vm_private_data = bo;
  386. /*
  387. * We'd like to use VM_PFNMAP on shared mappings, where
  388. * (vma->vm_flags & VM_SHARED) != 0, for performance reasons,
  389. * but for some reason VM_PFNMAP + x86 PAT + write-combine is very
  390. * bad for performance. Until that has been sorted out, use
  391. * VM_MIXEDMAP on all mappings. See freedesktop.org bug #75719
  392. */
  393. vma->vm_flags |= VM_MIXEDMAP;
  394. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  395. return 0;
  396. out_unref:
  397. ttm_bo_unref(&bo);
  398. return ret;
  399. }
  400. EXPORT_SYMBOL(ttm_bo_mmap);
  401. int ttm_fbdev_mmap(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  402. {
  403. if (vma->vm_pgoff != 0)
  404. return -EACCES;
  405. vma->vm_ops = &ttm_bo_vm_ops;
  406. vma->vm_private_data = ttm_bo_reference(bo);
  407. vma->vm_flags |= VM_MIXEDMAP;
  408. vma->vm_flags |= VM_IO | VM_DONTEXPAND;
  409. return 0;
  410. }
  411. EXPORT_SYMBOL(ttm_fbdev_mmap);