ttm_bo_vm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. /*
  29. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  30. */
  31. #define pr_fmt(fmt) "[TTM] " fmt
  32. #include <linux/export.h>
  33. #include <drm/ttm/ttm_bo.h>
  34. #include <drm/ttm/ttm_placement.h>
  35. #include <drm/ttm/ttm_tt.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_managed.h>
  38. static vm_fault_t ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  39. struct vm_fault *vmf)
  40. {
  41. long err = 0;
  42. /*
  43. * Quick non-stalling check for idle.
  44. */
  45. if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_KERNEL))
  46. return 0;
  47. /*
  48. * If possible, avoid waiting for GPU with mmap_lock
  49. * held. We only do this if the fault allows retry and this
  50. * is the first attempt.
  51. */
  52. if (fault_flag_allow_retry_first(vmf->flags)) {
  53. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  54. return VM_FAULT_RETRY;
  55. drm_gem_object_get(&bo->base);
  56. mmap_read_unlock(vmf->vma->vm_mm);
  57. (void)dma_resv_wait_timeout(bo->base.resv,
  58. DMA_RESV_USAGE_KERNEL, true,
  59. MAX_SCHEDULE_TIMEOUT);
  60. dma_resv_unlock(bo->base.resv);
  61. drm_gem_object_put(&bo->base);
  62. return VM_FAULT_RETRY;
  63. }
  64. /*
  65. * Ordinary wait.
  66. */
  67. err = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_KERNEL, true,
  68. MAX_SCHEDULE_TIMEOUT);
  69. if (unlikely(err < 0)) {
  70. return (err != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
  71. VM_FAULT_NOPAGE;
  72. }
  73. return 0;
  74. }
  75. static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo,
  76. unsigned long page_offset)
  77. {
  78. struct ttm_device *bdev = bo->bdev;
  79. if (bdev->funcs->io_mem_pfn)
  80. return bdev->funcs->io_mem_pfn(bo, page_offset);
  81. return (bo->resource->bus.offset >> PAGE_SHIFT) + page_offset;
  82. }
  83. /**
  84. * ttm_bo_vm_reserve - Reserve a buffer object in a retryable vm callback
  85. * @bo: The buffer object
  86. * @vmf: The fault structure handed to the callback
  87. *
  88. * vm callbacks like fault() and *_mkwrite() allow for the mmap_lock to be dropped
  89. * during long waits, and after the wait the callback will be restarted. This
  90. * is to allow other threads using the same virtual memory space concurrent
  91. * access to map(), unmap() completely unrelated buffer objects. TTM buffer
  92. * object reservations sometimes wait for GPU and should therefore be
  93. * considered long waits. This function reserves the buffer object interruptibly
  94. * taking this into account. Starvation is avoided by the vm system not
  95. * allowing too many repeated restarts.
  96. * This function is intended to be used in customized fault() and _mkwrite()
  97. * handlers.
  98. *
  99. * Return:
  100. * 0 on success and the bo was reserved.
  101. * VM_FAULT_RETRY if blocking wait.
  102. * VM_FAULT_NOPAGE if blocking wait and retrying was not allowed.
  103. */
  104. vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
  105. struct vm_fault *vmf)
  106. {
  107. /*
  108. * Work around locking order reversal in fault / nopfn
  109. * between mmap_lock and bo_reserve: Perform a trylock operation
  110. * for reserve, and if it fails, retry the fault after waiting
  111. * for the buffer to become unreserved.
  112. */
  113. if (unlikely(!dma_resv_trylock(bo->base.resv))) {
  114. /*
  115. * If the fault allows retry and this is the first
  116. * fault attempt, we try to release the mmap_lock
  117. * before waiting
  118. */
  119. if (fault_flag_allow_retry_first(vmf->flags)) {
  120. if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  121. drm_gem_object_get(&bo->base);
  122. mmap_read_unlock(vmf->vma->vm_mm);
  123. if (!dma_resv_lock_interruptible(bo->base.resv,
  124. NULL))
  125. dma_resv_unlock(bo->base.resv);
  126. drm_gem_object_put(&bo->base);
  127. }
  128. return VM_FAULT_RETRY;
  129. }
  130. if (dma_resv_lock_interruptible(bo->base.resv, NULL))
  131. return VM_FAULT_NOPAGE;
  132. }
  133. /*
  134. * Refuse to fault imported pages. This should be handled
  135. * (if at all) by redirecting mmap to the exporter.
  136. */
  137. if (bo->ttm && (bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL)) {
  138. if (!(bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) {
  139. dma_resv_unlock(bo->base.resv);
  140. return VM_FAULT_SIGBUS;
  141. }
  142. }
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(ttm_bo_vm_reserve);
  146. /**
  147. * ttm_bo_vm_fault_reserved - TTM fault helper
  148. * @vmf: The struct vm_fault given as argument to the fault callback
  149. * @prot: The page protection to be used for this memory area.
  150. * @num_prefault: Maximum number of prefault pages. The caller may want to
  151. * specify this based on madvice settings and the size of the GPU object
  152. * backed by the memory.
  153. *
  154. * This function inserts one or more page table entries pointing to the
  155. * memory backing the buffer object, and then returns a return code
  156. * instructing the caller to retry the page access.
  157. *
  158. * Return:
  159. * VM_FAULT_NOPAGE on success or pending signal
  160. * VM_FAULT_SIGBUS on unspecified error
  161. * VM_FAULT_OOM on out-of-memory
  162. * VM_FAULT_RETRY if retryable wait
  163. */
  164. vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
  165. pgprot_t prot,
  166. pgoff_t num_prefault)
  167. {
  168. struct vm_area_struct *vma = vmf->vma;
  169. struct ttm_buffer_object *bo = vma->vm_private_data;
  170. unsigned long page_offset;
  171. unsigned long page_last;
  172. unsigned long pfn;
  173. struct ttm_tt *ttm = NULL;
  174. struct page *page;
  175. int err;
  176. pgoff_t i;
  177. vm_fault_t ret = VM_FAULT_NOPAGE;
  178. unsigned long address = vmf->address;
  179. /*
  180. * Wait for buffer data in transit, due to a pipelined
  181. * move.
  182. */
  183. ret = ttm_bo_vm_fault_idle(bo, vmf);
  184. if (unlikely(ret != 0))
  185. return ret;
  186. err = ttm_mem_io_reserve(bo->bdev, bo->resource);
  187. if (unlikely(err != 0))
  188. return VM_FAULT_SIGBUS;
  189. page_offset = ((address - vma->vm_start) >> PAGE_SHIFT) +
  190. vma->vm_pgoff - drm_vma_node_start(&bo->base.vma_node);
  191. page_last = vma_pages(vma) + vma->vm_pgoff -
  192. drm_vma_node_start(&bo->base.vma_node);
  193. if (unlikely(page_offset >= PFN_UP(bo->base.size)))
  194. return VM_FAULT_SIGBUS;
  195. prot = ttm_io_prot(bo, bo->resource, prot);
  196. if (!bo->resource->bus.is_iomem) {
  197. struct ttm_operation_ctx ctx = {
  198. .interruptible = true,
  199. .no_wait_gpu = false,
  200. };
  201. ttm = bo->ttm;
  202. err = ttm_bo_populate(bo, &ctx);
  203. if (err) {
  204. if (err == -EINTR || err == -ERESTARTSYS ||
  205. err == -EAGAIN)
  206. return VM_FAULT_NOPAGE;
  207. pr_debug("TTM fault hit %pe.\n", ERR_PTR(err));
  208. return VM_FAULT_SIGBUS;
  209. }
  210. } else {
  211. /* Iomem should not be marked encrypted */
  212. prot = pgprot_decrypted(prot);
  213. }
  214. /*
  215. * Speculatively prefault a number of pages. Only error on
  216. * first page.
  217. */
  218. for (i = 0; i < num_prefault; ++i) {
  219. if (bo->resource->bus.is_iomem) {
  220. pfn = ttm_bo_io_mem_pfn(bo, page_offset);
  221. } else {
  222. page = ttm->pages[page_offset];
  223. if (unlikely(!page && i == 0)) {
  224. return VM_FAULT_OOM;
  225. } else if (unlikely(!page)) {
  226. break;
  227. }
  228. pfn = page_to_pfn(page);
  229. }
  230. /*
  231. * Note that the value of @prot at this point may differ from
  232. * the value of @vma->vm_page_prot in the caching- and
  233. * encryption bits. This is because the exact location of the
  234. * data may not be known at mmap() time and may also change
  235. * at arbitrary times while the data is mmap'ed.
  236. * See vmf_insert_pfn_prot() for a discussion.
  237. */
  238. ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
  239. /* Never error on prefaulted PTEs */
  240. if (unlikely((ret & VM_FAULT_ERROR))) {
  241. if (i == 0)
  242. return VM_FAULT_NOPAGE;
  243. else
  244. break;
  245. }
  246. address += PAGE_SIZE;
  247. if (unlikely(++page_offset >= page_last))
  248. break;
  249. }
  250. return ret;
  251. }
  252. EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
  253. static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
  254. {
  255. struct page *dummy_page = (struct page *)res;
  256. __free_page(dummy_page);
  257. }
  258. vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
  259. {
  260. struct vm_area_struct *vma = vmf->vma;
  261. struct ttm_buffer_object *bo = vma->vm_private_data;
  262. vm_fault_t ret = VM_FAULT_NOPAGE;
  263. unsigned long address;
  264. unsigned long pfn;
  265. struct page *page;
  266. /* Allocate new dummy page to map all the VA range in this VMA to it*/
  267. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  268. if (!page)
  269. return VM_FAULT_OOM;
  270. /* Set the page to be freed using drmm release action */
  271. if (drmm_add_action_or_reset(bo->base.dev, ttm_bo_release_dummy_page,
  272. page))
  273. return VM_FAULT_OOM;
  274. pfn = page_to_pfn(page);
  275. /* Prefault the entire VMA range right away to avoid further faults */
  276. for (address = vma->vm_start; address < vma->vm_end;
  277. address += PAGE_SIZE)
  278. ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
  279. return ret;
  280. }
  281. EXPORT_SYMBOL(ttm_bo_vm_dummy_page);
  282. vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
  283. {
  284. struct vm_area_struct *vma = vmf->vma;
  285. struct ttm_buffer_object *bo = vma->vm_private_data;
  286. vm_fault_t ret;
  287. pgprot_t prot;
  288. int idx;
  289. ret = ttm_bo_vm_reserve(bo, vmf);
  290. if (ret)
  291. return ret;
  292. prot = vma->vm_page_prot;
  293. if (drm_dev_enter(bo->base.dev, &idx)) {
  294. ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT);
  295. drm_dev_exit(idx);
  296. } else {
  297. ret = ttm_bo_vm_dummy_page(vmf, prot);
  298. }
  299. if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
  300. return ret;
  301. dma_resv_unlock(bo->base.resv);
  302. return ret;
  303. }
  304. EXPORT_SYMBOL(ttm_bo_vm_fault);
  305. void ttm_bo_vm_open(struct vm_area_struct *vma)
  306. {
  307. struct ttm_buffer_object *bo = vma->vm_private_data;
  308. WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
  309. drm_gem_object_get(&bo->base);
  310. }
  311. EXPORT_SYMBOL(ttm_bo_vm_open);
  312. void ttm_bo_vm_close(struct vm_area_struct *vma)
  313. {
  314. struct ttm_buffer_object *bo = vma->vm_private_data;
  315. drm_gem_object_put(&bo->base);
  316. vma->vm_private_data = NULL;
  317. }
  318. EXPORT_SYMBOL(ttm_bo_vm_close);
  319. static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo,
  320. unsigned long offset,
  321. uint8_t *buf, int len, int write)
  322. {
  323. unsigned long page = offset >> PAGE_SHIFT;
  324. unsigned long bytes_left = len;
  325. int ret;
  326. /* Copy a page at a time, that way no extra virtual address
  327. * mapping is needed
  328. */
  329. offset -= page << PAGE_SHIFT;
  330. do {
  331. unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
  332. struct ttm_bo_kmap_obj map;
  333. void *ptr;
  334. bool is_iomem;
  335. ret = ttm_bo_kmap(bo, page, 1, &map);
  336. if (ret)
  337. return ret;
  338. ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset;
  339. WARN_ON_ONCE(is_iomem);
  340. if (write)
  341. memcpy(ptr, buf, bytes);
  342. else
  343. memcpy(buf, ptr, bytes);
  344. ttm_bo_kunmap(&map);
  345. page++;
  346. buf += bytes;
  347. bytes_left -= bytes;
  348. offset = 0;
  349. } while (bytes_left);
  350. return len;
  351. }
  352. /**
  353. * ttm_bo_access - Helper to access a buffer object
  354. *
  355. * @bo: ttm buffer object
  356. * @offset: access offset into buffer object
  357. * @buf: pointer to caller memory to read into or write from
  358. * @len: length of access
  359. * @write: write access
  360. *
  361. * Utility function to access a buffer object. Useful when buffer object cannot
  362. * be easily mapped (non-contiguous, non-visible, etc...). Should not directly
  363. * be exported to user space via a peak / poke interface.
  364. *
  365. * Returns:
  366. * @len if successful, negative error code on failure.
  367. */
  368. int ttm_bo_access(struct ttm_buffer_object *bo, unsigned long offset,
  369. void *buf, int len, int write)
  370. {
  371. int ret;
  372. if (len < 1 || (offset + len) > bo->base.size)
  373. return -EIO;
  374. ret = ttm_bo_reserve(bo, true, false, NULL);
  375. if (ret)
  376. return ret;
  377. if (!bo->resource) {
  378. ret = -ENODATA;
  379. goto unlock;
  380. }
  381. switch (bo->resource->mem_type) {
  382. case TTM_PL_SYSTEM:
  383. fallthrough;
  384. case TTM_PL_TT:
  385. ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write);
  386. break;
  387. default:
  388. if (bo->bdev->funcs->access_memory)
  389. ret = bo->bdev->funcs->access_memory
  390. (bo, offset, buf, len, write);
  391. else
  392. ret = -EIO;
  393. }
  394. unlock:
  395. ttm_bo_unreserve(bo);
  396. return ret;
  397. }
  398. EXPORT_SYMBOL(ttm_bo_access);
  399. int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  400. void *buf, int len, int write)
  401. {
  402. struct ttm_buffer_object *bo = vma->vm_private_data;
  403. unsigned long offset = (addr) - vma->vm_start +
  404. ((vma->vm_pgoff - drm_vma_node_start(&bo->base.vma_node))
  405. << PAGE_SHIFT);
  406. return ttm_bo_access(bo, offset, buf, len, write);
  407. }
  408. EXPORT_SYMBOL(ttm_bo_vm_access);
  409. static const struct vm_operations_struct ttm_bo_vm_ops = {
  410. .fault = ttm_bo_vm_fault,
  411. .open = ttm_bo_vm_open,
  412. .close = ttm_bo_vm_close,
  413. .access = ttm_bo_vm_access,
  414. };
  415. /**
  416. * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
  417. *
  418. * @vma: vma as input from the fbdev mmap method.
  419. * @bo: The bo backing the address space.
  420. *
  421. * Maps a buffer object.
  422. */
  423. int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  424. {
  425. /* Enforce no COW since would have really strange behavior with it. */
  426. if (is_cow_mapping(vma->vm_flags))
  427. return -EINVAL;
  428. drm_gem_object_get(&bo->base);
  429. /*
  430. * Drivers may want to override the vm_ops field. Otherwise we
  431. * use TTM's default callbacks.
  432. */
  433. if (!vma->vm_ops)
  434. vma->vm_ops = &ttm_bo_vm_ops;
  435. /*
  436. * Note: We're transferring the bo reference to
  437. * vma->vm_private_data here.
  438. */
  439. vma->vm_private_data = bo;
  440. vm_flags_set(vma, VM_PFNMAP | VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
  441. return 0;
  442. }
  443. EXPORT_SYMBOL(ttm_bo_mmap_obj);