ttm_bo_vm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 <drm/ttm/ttm_bo.h>
  33. #include <drm/ttm/ttm_placement.h>
  34. #include <drm/ttm/ttm_tt.h>
  35. #include <drm/drm_drv.h>
  36. #include <drm/drm_managed.h>
  37. static vm_fault_t ttm_bo_vm_fault_idle(struct ttm_buffer_object *bo,
  38. struct vm_fault *vmf)
  39. {
  40. long err = 0;
  41. /*
  42. * Quick non-stalling check for idle.
  43. */
  44. if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_KERNEL))
  45. return 0;
  46. /*
  47. * If possible, avoid waiting for GPU with mmap_lock
  48. * held. We only do this if the fault allows retry and this
  49. * is the first attempt.
  50. */
  51. if (fault_flag_allow_retry_first(vmf->flags)) {
  52. if (vmf->flags & FAULT_FLAG_RETRY_NOWAIT)
  53. return VM_FAULT_RETRY;
  54. ttm_bo_get(bo);
  55. mmap_read_unlock(vmf->vma->vm_mm);
  56. (void)dma_resv_wait_timeout(bo->base.resv,
  57. DMA_RESV_USAGE_KERNEL, true,
  58. MAX_SCHEDULE_TIMEOUT);
  59. dma_resv_unlock(bo->base.resv);
  60. ttm_bo_put(bo);
  61. return VM_FAULT_RETRY;
  62. }
  63. /*
  64. * Ordinary wait.
  65. */
  66. err = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_KERNEL, true,
  67. MAX_SCHEDULE_TIMEOUT);
  68. if (unlikely(err < 0)) {
  69. return (err != -ERESTARTSYS) ? VM_FAULT_SIGBUS :
  70. VM_FAULT_NOPAGE;
  71. }
  72. return 0;
  73. }
  74. static unsigned long ttm_bo_io_mem_pfn(struct ttm_buffer_object *bo,
  75. unsigned long page_offset)
  76. {
  77. struct ttm_device *bdev = bo->bdev;
  78. if (bdev->funcs->io_mem_pfn)
  79. return bdev->funcs->io_mem_pfn(bo, page_offset);
  80. return (bo->resource->bus.offset >> PAGE_SHIFT) + page_offset;
  81. }
  82. /**
  83. * ttm_bo_vm_reserve - Reserve a buffer object in a retryable vm callback
  84. * @bo: The buffer object
  85. * @vmf: The fault structure handed to the callback
  86. *
  87. * vm callbacks like fault() and *_mkwrite() allow for the mmap_lock to be dropped
  88. * during long waits, and after the wait the callback will be restarted. This
  89. * is to allow other threads using the same virtual memory space concurrent
  90. * access to map(), unmap() completely unrelated buffer objects. TTM buffer
  91. * object reservations sometimes wait for GPU and should therefore be
  92. * considered long waits. This function reserves the buffer object interruptibly
  93. * taking this into account. Starvation is avoided by the vm system not
  94. * allowing too many repeated restarts.
  95. * This function is intended to be used in customized fault() and _mkwrite()
  96. * handlers.
  97. *
  98. * Return:
  99. * 0 on success and the bo was reserved.
  100. * VM_FAULT_RETRY if blocking wait.
  101. * VM_FAULT_NOPAGE if blocking wait and retrying was not allowed.
  102. */
  103. vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
  104. struct vm_fault *vmf)
  105. {
  106. /*
  107. * Work around locking order reversal in fault / nopfn
  108. * between mmap_lock and bo_reserve: Perform a trylock operation
  109. * for reserve, and if it fails, retry the fault after waiting
  110. * for the buffer to become unreserved.
  111. */
  112. if (unlikely(!dma_resv_trylock(bo->base.resv))) {
  113. /*
  114. * If the fault allows retry and this is the first
  115. * fault attempt, we try to release the mmap_lock
  116. * before waiting
  117. */
  118. if (fault_flag_allow_retry_first(vmf->flags)) {
  119. if (!(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
  120. ttm_bo_get(bo);
  121. mmap_read_unlock(vmf->vma->vm_mm);
  122. if (!dma_resv_lock_interruptible(bo->base.resv,
  123. NULL))
  124. dma_resv_unlock(bo->base.resv);
  125. ttm_bo_put(bo);
  126. }
  127. return VM_FAULT_RETRY;
  128. }
  129. if (dma_resv_lock_interruptible(bo->base.resv, NULL))
  130. return VM_FAULT_NOPAGE;
  131. }
  132. /*
  133. * Refuse to fault imported pages. This should be handled
  134. * (if at all) by redirecting mmap to the exporter.
  135. */
  136. if (bo->ttm && (bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL)) {
  137. if (!(bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL_MAPPABLE)) {
  138. dma_resv_unlock(bo->base.resv);
  139. return VM_FAULT_SIGBUS;
  140. }
  141. }
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(ttm_bo_vm_reserve);
  145. /**
  146. * ttm_bo_vm_fault_reserved - TTM fault helper
  147. * @vmf: The struct vm_fault given as argument to the fault callback
  148. * @prot: The page protection to be used for this memory area.
  149. * @num_prefault: Maximum number of prefault pages. The caller may want to
  150. * specify this based on madvice settings and the size of the GPU object
  151. * backed by the memory.
  152. *
  153. * This function inserts one or more page table entries pointing to the
  154. * memory backing the buffer object, and then returns a return code
  155. * instructing the caller to retry the page access.
  156. *
  157. * Return:
  158. * VM_FAULT_NOPAGE on success or pending signal
  159. * VM_FAULT_SIGBUS on unspecified error
  160. * VM_FAULT_OOM on out-of-memory
  161. * VM_FAULT_RETRY if retryable wait
  162. */
  163. vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
  164. pgprot_t prot,
  165. pgoff_t num_prefault)
  166. {
  167. struct vm_area_struct *vma = vmf->vma;
  168. struct ttm_buffer_object *bo = vma->vm_private_data;
  169. struct ttm_device *bdev = bo->bdev;
  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(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. .force_alloc = true
  201. };
  202. ttm = bo->ttm;
  203. err = ttm_tt_populate(bdev, bo->ttm, &ctx);
  204. if (err) {
  205. if (err == -EINTR || err == -ERESTARTSYS ||
  206. err == -EAGAIN)
  207. return VM_FAULT_NOPAGE;
  208. pr_debug("TTM fault hit %pe.\n", ERR_PTR(err));
  209. return VM_FAULT_SIGBUS;
  210. }
  211. } else {
  212. /* Iomem should not be marked encrypted */
  213. prot = pgprot_decrypted(prot);
  214. }
  215. /*
  216. * Speculatively prefault a number of pages. Only error on
  217. * first page.
  218. */
  219. for (i = 0; i < num_prefault; ++i) {
  220. if (bo->resource->bus.is_iomem) {
  221. pfn = ttm_bo_io_mem_pfn(bo, page_offset);
  222. } else {
  223. page = ttm->pages[page_offset];
  224. if (unlikely(!page && i == 0)) {
  225. return VM_FAULT_OOM;
  226. } else if (unlikely(!page)) {
  227. break;
  228. }
  229. pfn = page_to_pfn(page);
  230. }
  231. /*
  232. * Note that the value of @prot at this point may differ from
  233. * the value of @vma->vm_page_prot in the caching- and
  234. * encryption bits. This is because the exact location of the
  235. * data may not be known at mmap() time and may also change
  236. * at arbitrary times while the data is mmap'ed.
  237. * See vmf_insert_pfn_prot() for a discussion.
  238. */
  239. ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
  240. /* Never error on prefaulted PTEs */
  241. if (unlikely((ret & VM_FAULT_ERROR))) {
  242. if (i == 0)
  243. return VM_FAULT_NOPAGE;
  244. else
  245. break;
  246. }
  247. address += PAGE_SIZE;
  248. if (unlikely(++page_offset >= page_last))
  249. break;
  250. }
  251. return ret;
  252. }
  253. EXPORT_SYMBOL(ttm_bo_vm_fault_reserved);
  254. static void ttm_bo_release_dummy_page(struct drm_device *dev, void *res)
  255. {
  256. struct page *dummy_page = (struct page *)res;
  257. __free_page(dummy_page);
  258. }
  259. vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot)
  260. {
  261. struct vm_area_struct *vma = vmf->vma;
  262. struct ttm_buffer_object *bo = vma->vm_private_data;
  263. struct drm_device *ddev = bo->base.dev;
  264. vm_fault_t ret = VM_FAULT_NOPAGE;
  265. unsigned long address;
  266. unsigned long pfn;
  267. struct page *page;
  268. /* Allocate new dummy page to map all the VA range in this VMA to it*/
  269. page = alloc_page(GFP_KERNEL | __GFP_ZERO);
  270. if (!page)
  271. return VM_FAULT_OOM;
  272. /* Set the page to be freed using drmm release action */
  273. if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page))
  274. return VM_FAULT_OOM;
  275. pfn = page_to_pfn(page);
  276. /* Prefault the entire VMA range right away to avoid further faults */
  277. for (address = vma->vm_start; address < vma->vm_end;
  278. address += PAGE_SIZE)
  279. ret = vmf_insert_pfn_prot(vma, address, pfn, prot);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL(ttm_bo_vm_dummy_page);
  283. vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf)
  284. {
  285. struct vm_area_struct *vma = vmf->vma;
  286. pgprot_t prot;
  287. struct ttm_buffer_object *bo = vma->vm_private_data;
  288. struct drm_device *ddev = bo->base.dev;
  289. vm_fault_t ret;
  290. int idx;
  291. ret = ttm_bo_vm_reserve(bo, vmf);
  292. if (ret)
  293. return ret;
  294. prot = vma->vm_page_prot;
  295. if (drm_dev_enter(ddev, &idx)) {
  296. ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT);
  297. drm_dev_exit(idx);
  298. } else {
  299. ret = ttm_bo_vm_dummy_page(vmf, prot);
  300. }
  301. if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
  302. return ret;
  303. dma_resv_unlock(bo->base.resv);
  304. return ret;
  305. }
  306. EXPORT_SYMBOL(ttm_bo_vm_fault);
  307. void ttm_bo_vm_open(struct vm_area_struct *vma)
  308. {
  309. struct ttm_buffer_object *bo = vma->vm_private_data;
  310. WARN_ON(bo->bdev->dev_mapping != vma->vm_file->f_mapping);
  311. ttm_bo_get(bo);
  312. }
  313. EXPORT_SYMBOL(ttm_bo_vm_open);
  314. void ttm_bo_vm_close(struct vm_area_struct *vma)
  315. {
  316. struct ttm_buffer_object *bo = vma->vm_private_data;
  317. ttm_bo_put(bo);
  318. vma->vm_private_data = NULL;
  319. }
  320. EXPORT_SYMBOL(ttm_bo_vm_close);
  321. static int ttm_bo_vm_access_kmap(struct ttm_buffer_object *bo,
  322. unsigned long offset,
  323. uint8_t *buf, int len, int write)
  324. {
  325. unsigned long page = offset >> PAGE_SHIFT;
  326. unsigned long bytes_left = len;
  327. int ret;
  328. /* Copy a page at a time, that way no extra virtual address
  329. * mapping is needed
  330. */
  331. offset -= page << PAGE_SHIFT;
  332. do {
  333. unsigned long bytes = min(bytes_left, PAGE_SIZE - offset);
  334. struct ttm_bo_kmap_obj map;
  335. void *ptr;
  336. bool is_iomem;
  337. ret = ttm_bo_kmap(bo, page, 1, &map);
  338. if (ret)
  339. return ret;
  340. ptr = (uint8_t *)ttm_kmap_obj_virtual(&map, &is_iomem) + offset;
  341. WARN_ON_ONCE(is_iomem);
  342. if (write)
  343. memcpy(ptr, buf, bytes);
  344. else
  345. memcpy(buf, ptr, bytes);
  346. ttm_bo_kunmap(&map);
  347. page++;
  348. buf += bytes;
  349. bytes_left -= bytes;
  350. offset = 0;
  351. } while (bytes_left);
  352. return len;
  353. }
  354. int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
  355. void *buf, int len, int write)
  356. {
  357. struct ttm_buffer_object *bo = vma->vm_private_data;
  358. unsigned long offset = (addr) - vma->vm_start +
  359. ((vma->vm_pgoff - drm_vma_node_start(&bo->base.vma_node))
  360. << PAGE_SHIFT);
  361. int ret;
  362. if (len < 1 || (offset + len) > bo->base.size)
  363. return -EIO;
  364. ret = ttm_bo_reserve(bo, true, false, NULL);
  365. if (ret)
  366. return ret;
  367. switch (bo->resource->mem_type) {
  368. case TTM_PL_SYSTEM:
  369. fallthrough;
  370. case TTM_PL_TT:
  371. ret = ttm_bo_vm_access_kmap(bo, offset, buf, len, write);
  372. break;
  373. default:
  374. if (bo->bdev->funcs->access_memory)
  375. ret = bo->bdev->funcs->access_memory(
  376. bo, offset, buf, len, write);
  377. else
  378. ret = -EIO;
  379. }
  380. ttm_bo_unreserve(bo);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL(ttm_bo_vm_access);
  384. static const struct vm_operations_struct ttm_bo_vm_ops = {
  385. .fault = ttm_bo_vm_fault,
  386. .open = ttm_bo_vm_open,
  387. .close = ttm_bo_vm_close,
  388. .access = ttm_bo_vm_access,
  389. };
  390. /**
  391. * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
  392. *
  393. * @vma: vma as input from the fbdev mmap method.
  394. * @bo: The bo backing the address space.
  395. *
  396. * Maps a buffer object.
  397. */
  398. int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo)
  399. {
  400. /* Enforce no COW since would have really strange behavior with it. */
  401. if (is_cow_mapping(vma->vm_flags))
  402. return -EINVAL;
  403. ttm_bo_get(bo);
  404. /*
  405. * Drivers may want to override the vm_ops field. Otherwise we
  406. * use TTM's default callbacks.
  407. */
  408. if (!vma->vm_ops)
  409. vma->vm_ops = &ttm_bo_vm_ops;
  410. /*
  411. * Note: We're transferring the bo reference to
  412. * vma->vm_private_data here.
  413. */
  414. vma->vm_private_data = bo;
  415. vm_flags_set(vma, VM_PFNMAP | VM_IO | VM_DONTEXPAND | VM_DONTDUMP);
  416. return 0;
  417. }
  418. EXPORT_SYMBOL(ttm_bo_mmap_obj);