ttm_bo_util.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2007-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. #include <linux/export.h>
  32. #include <linux/swap.h>
  33. #include <linux/vmalloc.h>
  34. #include <drm/ttm/ttm_bo.h>
  35. #include <drm/ttm/ttm_placement.h>
  36. #include <drm/ttm/ttm_tt.h>
  37. #include <drm/drm_cache.h>
  38. #include "ttm_bo_internal.h"
  39. struct ttm_transfer_obj {
  40. struct ttm_buffer_object base;
  41. struct ttm_buffer_object *bo;
  42. };
  43. int ttm_mem_io_reserve(struct ttm_device *bdev,
  44. struct ttm_resource *mem)
  45. {
  46. if (mem->bus.offset || mem->bus.addr)
  47. return 0;
  48. mem->bus.is_iomem = false;
  49. if (!bdev->funcs->io_mem_reserve)
  50. return 0;
  51. return bdev->funcs->io_mem_reserve(bdev, mem);
  52. }
  53. void ttm_mem_io_free(struct ttm_device *bdev,
  54. struct ttm_resource *mem)
  55. {
  56. if (!mem)
  57. return;
  58. if (!mem->bus.offset && !mem->bus.addr)
  59. return;
  60. if (bdev->funcs->io_mem_free)
  61. bdev->funcs->io_mem_free(bdev, mem);
  62. mem->bus.offset = 0;
  63. mem->bus.addr = NULL;
  64. }
  65. /**
  66. * ttm_move_memcpy - Helper to perform a memcpy ttm move operation.
  67. * @clear: Whether to clear rather than copy.
  68. * @num_pages: Number of pages of the operation.
  69. * @dst_iter: A struct ttm_kmap_iter representing the destination resource.
  70. * @src_iter: A struct ttm_kmap_iter representing the source resource.
  71. *
  72. * This function is intended to be able to move out async under a
  73. * dma-fence if desired.
  74. */
  75. void ttm_move_memcpy(bool clear,
  76. u32 num_pages,
  77. struct ttm_kmap_iter *dst_iter,
  78. struct ttm_kmap_iter *src_iter)
  79. {
  80. const struct ttm_kmap_iter_ops *dst_ops = dst_iter->ops;
  81. const struct ttm_kmap_iter_ops *src_ops = src_iter->ops;
  82. struct iosys_map src_map, dst_map;
  83. pgoff_t i;
  84. /* Single TTM move. NOP */
  85. if (dst_ops->maps_tt && src_ops->maps_tt)
  86. return;
  87. /* Don't move nonexistent data. Clear destination instead. */
  88. if (clear) {
  89. for (i = 0; i < num_pages; ++i) {
  90. dst_ops->map_local(dst_iter, &dst_map, i);
  91. if (dst_map.is_iomem)
  92. memset_io(dst_map.vaddr_iomem, 0, PAGE_SIZE);
  93. else
  94. memset(dst_map.vaddr, 0, PAGE_SIZE);
  95. if (dst_ops->unmap_local)
  96. dst_ops->unmap_local(dst_iter, &dst_map);
  97. }
  98. return;
  99. }
  100. for (i = 0; i < num_pages; ++i) {
  101. dst_ops->map_local(dst_iter, &dst_map, i);
  102. src_ops->map_local(src_iter, &src_map, i);
  103. drm_memcpy_from_wc(&dst_map, &src_map, PAGE_SIZE);
  104. if (src_ops->unmap_local)
  105. src_ops->unmap_local(src_iter, &src_map);
  106. if (dst_ops->unmap_local)
  107. dst_ops->unmap_local(dst_iter, &dst_map);
  108. }
  109. }
  110. EXPORT_SYMBOL(ttm_move_memcpy);
  111. /**
  112. * ttm_bo_move_memcpy
  113. *
  114. * @bo: A pointer to a struct ttm_buffer_object.
  115. * @ctx: operation context
  116. * @dst_mem: struct ttm_resource indicating where to move.
  117. *
  118. * Fallback move function for a mappable buffer object in mappable memory.
  119. * The function will, if successful,
  120. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  121. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  122. * data remains untouched, and it's up to the caller to free the
  123. * memory space indicated by @new_mem.
  124. * Returns:
  125. * !0: Failure.
  126. */
  127. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  128. struct ttm_operation_ctx *ctx,
  129. struct ttm_resource *dst_mem)
  130. {
  131. struct ttm_device *bdev = bo->bdev;
  132. struct ttm_resource_manager *dst_man =
  133. ttm_manager_type(bo->bdev, dst_mem->mem_type);
  134. struct ttm_tt *ttm = bo->ttm;
  135. struct ttm_resource *src_mem = bo->resource;
  136. struct ttm_resource_manager *src_man;
  137. union {
  138. struct ttm_kmap_iter_tt tt;
  139. struct ttm_kmap_iter_linear_io io;
  140. } _dst_iter, _src_iter;
  141. struct ttm_kmap_iter *dst_iter, *src_iter;
  142. bool clear;
  143. int ret = 0;
  144. if (WARN_ON(!src_mem))
  145. return -EINVAL;
  146. src_man = ttm_manager_type(bdev, src_mem->mem_type);
  147. if (ttm && ((ttm->page_flags & TTM_TT_FLAG_SWAPPED) ||
  148. dst_man->use_tt)) {
  149. ret = ttm_bo_populate(bo, ctx);
  150. if (ret)
  151. return ret;
  152. }
  153. dst_iter = ttm_kmap_iter_linear_io_init(&_dst_iter.io, bdev, dst_mem);
  154. if (PTR_ERR(dst_iter) == -EINVAL && dst_man->use_tt)
  155. dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, ttm);
  156. if (IS_ERR(dst_iter))
  157. return PTR_ERR(dst_iter);
  158. src_iter = ttm_kmap_iter_linear_io_init(&_src_iter.io, bdev, src_mem);
  159. if (PTR_ERR(src_iter) == -EINVAL && src_man->use_tt)
  160. src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, ttm);
  161. if (IS_ERR(src_iter)) {
  162. ret = PTR_ERR(src_iter);
  163. goto out_src_iter;
  164. }
  165. clear = src_iter->ops->maps_tt && (!ttm || !ttm_tt_is_populated(ttm));
  166. if (!(clear && ttm && !(ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC)))
  167. ttm_move_memcpy(clear, PFN_UP(dst_mem->size), dst_iter, src_iter);
  168. if (!src_iter->ops->maps_tt)
  169. ttm_kmap_iter_linear_io_fini(&_src_iter.io, bdev, src_mem);
  170. ttm_bo_move_sync_cleanup(bo, dst_mem);
  171. out_src_iter:
  172. if (!dst_iter->ops->maps_tt)
  173. ttm_kmap_iter_linear_io_fini(&_dst_iter.io, bdev, dst_mem);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  177. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  178. {
  179. struct ttm_transfer_obj *fbo;
  180. fbo = container_of(bo, struct ttm_transfer_obj, base);
  181. dma_resv_fini(&fbo->base.base._resv);
  182. ttm_bo_put(fbo->bo);
  183. kfree(fbo);
  184. }
  185. /**
  186. * ttm_buffer_object_transfer
  187. *
  188. * @bo: A pointer to a struct ttm_buffer_object.
  189. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  190. * holding the data of @bo with the old placement.
  191. *
  192. * This is a utility function that may be called after an accelerated move
  193. * has been scheduled. A new buffer object is created as a placeholder for
  194. * the old data while it's being copied. When that buffer object is idle,
  195. * it can be destroyed, releasing the space of the old placement.
  196. * Returns:
  197. * !0: Failure.
  198. */
  199. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  200. struct ttm_buffer_object **new_obj)
  201. {
  202. struct ttm_transfer_obj *fbo;
  203. int ret;
  204. fbo = kmalloc_obj(*fbo);
  205. if (!fbo)
  206. return -ENOMEM;
  207. fbo->base = *bo;
  208. /**
  209. * Fix up members that we shouldn't copy directly:
  210. * TODO: Explicit member copy would probably be better here.
  211. */
  212. atomic_inc(&ttm_glob.bo_count);
  213. drm_vma_node_reset(&fbo->base.base.vma_node);
  214. kref_init(&fbo->base.kref);
  215. fbo->base.destroy = &ttm_transfered_destroy;
  216. fbo->base.pin_count = 0;
  217. if (bo->type != ttm_bo_type_sg)
  218. fbo->base.base.resv = &fbo->base.base._resv;
  219. dma_resv_init(&fbo->base.base._resv);
  220. fbo->base.base.dev = NULL;
  221. ret = dma_resv_trylock(&fbo->base.base._resv);
  222. WARN_ON(!ret);
  223. ret = dma_resv_reserve_fences(&fbo->base.base._resv, TTM_NUM_MOVE_FENCES);
  224. if (ret) {
  225. dma_resv_unlock(&fbo->base.base._resv);
  226. kfree(fbo);
  227. return ret;
  228. }
  229. if (fbo->base.resource) {
  230. ttm_resource_set_bo(fbo->base.resource, &fbo->base);
  231. bo->resource = NULL;
  232. ttm_bo_set_bulk_move(&fbo->base, NULL);
  233. } else {
  234. fbo->base.bulk_move = NULL;
  235. }
  236. ttm_bo_get(bo);
  237. fbo->bo = bo;
  238. ttm_bo_move_to_lru_tail_unlocked(&fbo->base);
  239. *new_obj = &fbo->base;
  240. return 0;
  241. }
  242. /**
  243. * ttm_io_prot
  244. *
  245. * @bo: ttm buffer object
  246. * @res: ttm resource object
  247. * @tmp: Page protection flag for a normal, cached mapping.
  248. *
  249. * Utility function that returns the pgprot_t that should be used for
  250. * setting up a PTE with the caching model indicated by @c_state.
  251. */
  252. pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
  253. pgprot_t tmp)
  254. {
  255. struct ttm_resource_manager *man;
  256. enum ttm_caching caching;
  257. man = ttm_manager_type(bo->bdev, res->mem_type);
  258. if (man->use_tt) {
  259. caching = bo->ttm->caching;
  260. if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
  261. tmp = pgprot_decrypted(tmp);
  262. } else {
  263. caching = res->bus.caching;
  264. }
  265. return ttm_prot_from_caching(caching, tmp);
  266. }
  267. EXPORT_SYMBOL(ttm_io_prot);
  268. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  269. unsigned long offset,
  270. unsigned long size,
  271. struct ttm_bo_kmap_obj *map)
  272. {
  273. struct ttm_resource *mem = bo->resource;
  274. if (mem->bus.addr) {
  275. map->bo_kmap_type = ttm_bo_map_premapped;
  276. map->virtual = ((u8 *)mem->bus.addr) + offset;
  277. } else {
  278. resource_size_t res = mem->bus.offset + offset;
  279. map->bo_kmap_type = ttm_bo_map_iomap;
  280. if (mem->bus.caching == ttm_write_combined)
  281. map->virtual = ioremap_wc(res, size);
  282. #ifdef CONFIG_X86
  283. else if (mem->bus.caching == ttm_cached)
  284. map->virtual = ioremap_cache(res, size);
  285. #endif
  286. else
  287. map->virtual = ioremap(res, size);
  288. }
  289. return (!map->virtual) ? -ENOMEM : 0;
  290. }
  291. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  292. unsigned long start_page,
  293. unsigned long num_pages,
  294. struct ttm_bo_kmap_obj *map)
  295. {
  296. struct ttm_resource *mem = bo->resource;
  297. struct ttm_operation_ctx ctx = { };
  298. struct ttm_tt *ttm = bo->ttm;
  299. struct ttm_resource_manager *man =
  300. ttm_manager_type(bo->bdev, mem->mem_type);
  301. pgprot_t prot;
  302. int ret;
  303. BUG_ON(!ttm);
  304. ret = ttm_bo_populate(bo, &ctx);
  305. if (ret)
  306. return ret;
  307. if (num_pages == 1 && ttm->caching == ttm_cached &&
  308. !(man->use_tt && (ttm->page_flags & TTM_TT_FLAG_DECRYPTED))) {
  309. /*
  310. * We're mapping a single page, and the desired
  311. * page protection is consistent with the bo.
  312. */
  313. map->bo_kmap_type = ttm_bo_map_kmap;
  314. map->page = ttm->pages[start_page];
  315. map->virtual = kmap(map->page);
  316. } else {
  317. /*
  318. * We need to use vmap to get the desired page protection
  319. * or to make the buffer object look contiguous.
  320. */
  321. prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
  322. map->bo_kmap_type = ttm_bo_map_vmap;
  323. map->virtual = vmap(ttm->pages + start_page, num_pages,
  324. 0, prot);
  325. }
  326. return (!map->virtual) ? -ENOMEM : 0;
  327. }
  328. /**
  329. * ttm_bo_kmap_try_from_panic
  330. *
  331. * @bo: The buffer object
  332. * @page: The page to map
  333. *
  334. * Sets up a kernel virtual mapping using kmap_local_page_try_from_panic().
  335. * This should only be called from the panic handler, if you make sure the bo
  336. * is the one being displayed, so is properly allocated, and protected.
  337. *
  338. * Returns the vaddr, that you can use to write to the bo, and that you should
  339. * pass to kunmap_local() when you're done with this page, or NULL if the bo
  340. * is in iomem.
  341. */
  342. void *ttm_bo_kmap_try_from_panic(struct ttm_buffer_object *bo, unsigned long page)
  343. {
  344. if (page + 1 > PFN_UP(bo->resource->size))
  345. return NULL;
  346. if (!bo->resource->bus.is_iomem && bo->ttm->pages && bo->ttm->pages[page])
  347. return kmap_local_page_try_from_panic(bo->ttm->pages[page]);
  348. return NULL;
  349. }
  350. EXPORT_SYMBOL(ttm_bo_kmap_try_from_panic);
  351. /**
  352. * ttm_bo_kmap
  353. *
  354. * @bo: The buffer object.
  355. * @start_page: The first page to map.
  356. * @num_pages: Number of pages to map.
  357. * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
  358. *
  359. * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
  360. * data in the buffer object. The ttm_kmap_obj_virtual function can then be
  361. * used to obtain a virtual address to the data.
  362. *
  363. * Returns
  364. * -ENOMEM: Out of memory.
  365. * -EINVAL: Invalid range.
  366. */
  367. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  368. unsigned long start_page, unsigned long num_pages,
  369. struct ttm_bo_kmap_obj *map)
  370. {
  371. struct ttm_resource *res = bo->resource;
  372. unsigned long offset, size;
  373. int ret;
  374. map->virtual = NULL;
  375. map->bo = bo;
  376. if (num_pages > PFN_UP(res->size))
  377. return -EINVAL;
  378. if ((start_page + num_pages) > PFN_UP(res->size))
  379. return -EINVAL;
  380. ret = ttm_mem_io_reserve(bo->bdev, res);
  381. if (ret)
  382. return ret;
  383. if (!res->bus.is_iomem) {
  384. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  385. } else {
  386. offset = start_page << PAGE_SHIFT;
  387. size = num_pages << PAGE_SHIFT;
  388. return ttm_bo_ioremap(bo, offset, size, map);
  389. }
  390. }
  391. EXPORT_SYMBOL(ttm_bo_kmap);
  392. /**
  393. * ttm_bo_kunmap
  394. *
  395. * @map: Object describing the map to unmap.
  396. *
  397. * Unmaps a kernel map set up by ttm_bo_kmap.
  398. */
  399. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  400. {
  401. if (!map->virtual)
  402. return;
  403. switch (map->bo_kmap_type) {
  404. case ttm_bo_map_iomap:
  405. iounmap(map->virtual);
  406. break;
  407. case ttm_bo_map_vmap:
  408. vunmap(map->virtual);
  409. break;
  410. case ttm_bo_map_kmap:
  411. kunmap(map->page);
  412. break;
  413. case ttm_bo_map_premapped:
  414. break;
  415. default:
  416. BUG();
  417. }
  418. ttm_mem_io_free(map->bo->bdev, map->bo->resource);
  419. map->virtual = NULL;
  420. map->page = NULL;
  421. }
  422. EXPORT_SYMBOL(ttm_bo_kunmap);
  423. /**
  424. * ttm_bo_vmap
  425. *
  426. * @bo: The buffer object.
  427. * @map: pointer to a struct iosys_map representing the map.
  428. *
  429. * Sets up a kernel virtual mapping, using ioremap or vmap to the
  430. * data in the buffer object. The parameter @map returns the virtual
  431. * address as struct iosys_map. Unmap the buffer with ttm_bo_vunmap().
  432. *
  433. * Returns
  434. * -ENOMEM: Out of memory.
  435. * -EINVAL: Invalid range.
  436. */
  437. int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map)
  438. {
  439. struct ttm_resource *mem = bo->resource;
  440. int ret;
  441. dma_resv_assert_held(bo->base.resv);
  442. ret = ttm_mem_io_reserve(bo->bdev, mem);
  443. if (ret)
  444. return ret;
  445. if (mem->bus.is_iomem) {
  446. void __iomem *vaddr_iomem;
  447. if (mem->bus.addr)
  448. vaddr_iomem = (void __iomem *)mem->bus.addr;
  449. else if (mem->bus.caching == ttm_write_combined)
  450. vaddr_iomem = ioremap_wc(mem->bus.offset,
  451. bo->base.size);
  452. #ifdef CONFIG_X86
  453. else if (mem->bus.caching == ttm_cached)
  454. vaddr_iomem = ioremap_cache(mem->bus.offset,
  455. bo->base.size);
  456. #endif
  457. else
  458. vaddr_iomem = ioremap(mem->bus.offset, bo->base.size);
  459. if (!vaddr_iomem)
  460. return -ENOMEM;
  461. iosys_map_set_vaddr_iomem(map, vaddr_iomem);
  462. } else {
  463. struct ttm_operation_ctx ctx = { };
  464. struct ttm_tt *ttm = bo->ttm;
  465. pgprot_t prot;
  466. void *vaddr;
  467. ret = ttm_bo_populate(bo, &ctx);
  468. if (ret)
  469. return ret;
  470. /*
  471. * We need to use vmap to get the desired page protection
  472. * or to make the buffer object look contiguous.
  473. */
  474. prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
  475. vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot);
  476. if (!vaddr)
  477. return -ENOMEM;
  478. iosys_map_set_vaddr(map, vaddr);
  479. }
  480. return 0;
  481. }
  482. EXPORT_SYMBOL(ttm_bo_vmap);
  483. /**
  484. * ttm_bo_vunmap
  485. *
  486. * @bo: The buffer object.
  487. * @map: Object describing the map to unmap.
  488. *
  489. * Unmaps a kernel map set up by ttm_bo_vmap().
  490. */
  491. void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map)
  492. {
  493. struct ttm_resource *mem = bo->resource;
  494. dma_resv_assert_held(bo->base.resv);
  495. if (iosys_map_is_null(map))
  496. return;
  497. if (!map->is_iomem)
  498. vunmap(map->vaddr);
  499. else if (!mem->bus.addr)
  500. iounmap(map->vaddr_iomem);
  501. iosys_map_clear(map);
  502. ttm_mem_io_free(bo->bdev, mem);
  503. }
  504. EXPORT_SYMBOL(ttm_bo_vunmap);
  505. static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
  506. bool dst_use_tt)
  507. {
  508. long ret;
  509. ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
  510. false, 15 * HZ);
  511. if (ret == 0)
  512. return -EBUSY;
  513. if (ret < 0)
  514. return ret;
  515. if (!dst_use_tt)
  516. ttm_bo_tt_destroy(bo);
  517. ttm_resource_free(bo, &bo->resource);
  518. return 0;
  519. }
  520. static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
  521. struct dma_fence *fence,
  522. bool dst_use_tt)
  523. {
  524. struct ttm_buffer_object *ghost_obj;
  525. int ret;
  526. /**
  527. * This should help pipeline ordinary buffer moves.
  528. *
  529. * Hang old buffer memory on a new buffer object,
  530. * and leave it to be released when the GPU
  531. * operation has completed.
  532. */
  533. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  534. if (ret)
  535. return ret;
  536. dma_resv_add_fence(&ghost_obj->base._resv, fence,
  537. DMA_RESV_USAGE_KERNEL);
  538. /**
  539. * If we're not moving to fixed memory, the TTM object
  540. * needs to stay alive. Otherwhise hang it on the ghost
  541. * bo to be unbound and destroyed.
  542. */
  543. if (dst_use_tt)
  544. ghost_obj->ttm = NULL;
  545. else
  546. bo->ttm = NULL;
  547. dma_resv_unlock(&ghost_obj->base._resv);
  548. ttm_bo_put(ghost_obj);
  549. return 0;
  550. }
  551. static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
  552. struct dma_fence *fence)
  553. {
  554. struct ttm_resource_manager *from;
  555. struct dma_fence *tmp;
  556. int i;
  557. from = ttm_manager_type(bo->bdev, bo->resource->mem_type);
  558. /**
  559. * BO doesn't have a TTM we need to bind/unbind. Just remember
  560. * this eviction and free up the allocation.
  561. * The fence will be saved in the first free slot or in the slot
  562. * already used to store a fence from the same context. Since
  563. * drivers can't use more than TTM_NUM_MOVE_FENCES contexts for
  564. * evictions we should always find a slot to use.
  565. */
  566. spin_lock(&from->eviction_lock);
  567. for (i = 0; i < TTM_NUM_MOVE_FENCES; i++) {
  568. tmp = from->eviction_fences[i];
  569. if (!tmp)
  570. break;
  571. if (fence->context != tmp->context)
  572. continue;
  573. if (dma_fence_is_later(fence, tmp)) {
  574. dma_fence_put(tmp);
  575. break;
  576. }
  577. goto unlock;
  578. }
  579. if (i < TTM_NUM_MOVE_FENCES) {
  580. from->eviction_fences[i] = dma_fence_get(fence);
  581. } else {
  582. WARN(1, "not enough fence slots for all fence contexts");
  583. spin_unlock(&from->eviction_lock);
  584. dma_fence_wait(fence, false);
  585. goto end;
  586. }
  587. unlock:
  588. spin_unlock(&from->eviction_lock);
  589. end:
  590. ttm_resource_free(bo, &bo->resource);
  591. }
  592. /**
  593. * ttm_bo_move_accel_cleanup - cleanup helper for hw copies
  594. *
  595. * @bo: A pointer to a struct ttm_buffer_object.
  596. * @fence: A fence object that signals when moving is complete.
  597. * @evict: This is an evict move. Don't return until the buffer is idle.
  598. * @pipeline: evictions are to be pipelined.
  599. * @new_mem: struct ttm_resource indicating where to move.
  600. *
  601. * Accelerated move function to be called when an accelerated move
  602. * has been scheduled. The function will create a new temporary buffer object
  603. * representing the old placement, and put the sync object on both buffer
  604. * objects. After that the newly created buffer object is unref'd to be
  605. * destroyed when the move is complete. This will help pipeline
  606. * buffer moves.
  607. */
  608. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  609. struct dma_fence *fence,
  610. bool evict,
  611. bool pipeline,
  612. struct ttm_resource *new_mem)
  613. {
  614. struct ttm_device *bdev = bo->bdev;
  615. struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->resource->mem_type);
  616. struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
  617. int ret = 0;
  618. dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
  619. if (!evict)
  620. ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
  621. else if (!from->use_tt && pipeline)
  622. ttm_bo_move_pipeline_evict(bo, fence);
  623. else
  624. ret = ttm_bo_wait_free_node(bo, man->use_tt);
  625. if (ret)
  626. return ret;
  627. ttm_bo_assign_mem(bo, new_mem);
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
  631. /**
  632. * ttm_bo_move_sync_cleanup - cleanup by waiting for the move to finish
  633. *
  634. * @bo: A pointer to a struct ttm_buffer_object.
  635. * @new_mem: struct ttm_resource indicating where to move.
  636. *
  637. * Special case of ttm_bo_move_accel_cleanup where the bo is guaranteed
  638. * by the caller to be idle. Typically used after memcpy buffer moves.
  639. */
  640. void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
  641. struct ttm_resource *new_mem)
  642. {
  643. struct ttm_resource_manager *man =
  644. ttm_manager_type(bo->bdev, new_mem->mem_type);
  645. int ret;
  646. ret = ttm_bo_wait_free_node(bo, man->use_tt);
  647. if (WARN_ON(ret))
  648. return;
  649. ttm_bo_assign_mem(bo, new_mem);
  650. }
  651. EXPORT_SYMBOL(ttm_bo_move_sync_cleanup);
  652. /**
  653. * ttm_bo_pipeline_gutting - purge the contents of a bo
  654. * @bo: The buffer object
  655. *
  656. * Purge the contents of a bo, async if the bo is not idle.
  657. * After a successful call, the bo is left unpopulated in
  658. * system placement. The function may wait uninterruptible
  659. * for idle on OOM.
  660. *
  661. * Return: 0 if successful, negative error code on failure.
  662. */
  663. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  664. {
  665. struct ttm_buffer_object *ghost;
  666. struct ttm_tt *ttm;
  667. int ret;
  668. /* If already idle, no need for ghost object dance. */
  669. if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP)) {
  670. if (!bo->ttm) {
  671. /* See comment below about clearing. */
  672. ret = ttm_tt_create(bo, true);
  673. if (ret)
  674. return ret;
  675. } else {
  676. ttm_tt_unpopulate(bo->bdev, bo->ttm);
  677. if (bo->type == ttm_bo_type_device)
  678. ttm_tt_mark_for_clear(bo->ttm);
  679. }
  680. ttm_resource_free(bo, &bo->resource);
  681. return 0;
  682. }
  683. /*
  684. * We need an unpopulated ttm_tt after giving our current one,
  685. * if any, to the ghost object. And we can't afford to fail
  686. * creating one *after* the operation. If the bo subsequently gets
  687. * resurrected, make sure it's cleared (if ttm_bo_type_device)
  688. * to avoid leaking sensitive information to user-space.
  689. */
  690. ttm = bo->ttm;
  691. bo->ttm = NULL;
  692. ret = ttm_tt_create(bo, true);
  693. swap(bo->ttm, ttm);
  694. if (ret)
  695. return ret;
  696. ret = ttm_buffer_object_transfer(bo, &ghost);
  697. if (ret)
  698. goto error_destroy_tt;
  699. ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
  700. /* Last resort, wait for the BO to be idle when we are OOM */
  701. if (ret) {
  702. dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
  703. false, MAX_SCHEDULE_TIMEOUT);
  704. }
  705. dma_resv_unlock(&ghost->base._resv);
  706. ttm_bo_put(ghost);
  707. bo->ttm = ttm;
  708. return 0;
  709. error_destroy_tt:
  710. ttm_tt_destroy(bo->bdev, ttm);
  711. return ret;
  712. }
  713. static bool ttm_lru_walk_trylock(struct ttm_bo_lru_cursor *curs,
  714. struct ttm_buffer_object *bo)
  715. {
  716. struct ttm_operation_ctx *ctx = curs->arg->ctx;
  717. curs->needs_unlock = false;
  718. if (dma_resv_trylock(bo->base.resv)) {
  719. curs->needs_unlock = true;
  720. return true;
  721. }
  722. if (bo->base.resv == ctx->resv && ctx->allow_res_evict) {
  723. dma_resv_assert_held(bo->base.resv);
  724. return true;
  725. }
  726. return false;
  727. }
  728. static int ttm_lru_walk_ticketlock(struct ttm_bo_lru_cursor *curs,
  729. struct ttm_buffer_object *bo)
  730. {
  731. struct ttm_lru_walk_arg *arg = curs->arg;
  732. int ret;
  733. if (arg->ctx->interruptible)
  734. ret = dma_resv_lock_interruptible(bo->base.resv, arg->ticket);
  735. else
  736. ret = dma_resv_lock(bo->base.resv, arg->ticket);
  737. if (!ret) {
  738. curs->needs_unlock = true;
  739. /*
  740. * Only a single ticketlock per loop. Ticketlocks are prone
  741. * to return -EDEADLK causing the eviction to fail, so
  742. * after waiting for the ticketlock, revert back to
  743. * trylocking for this walk.
  744. */
  745. arg->ticket = NULL;
  746. } else if (ret == -EDEADLK) {
  747. /* Caller needs to exit the ww transaction. */
  748. ret = -ENOSPC;
  749. }
  750. return ret;
  751. }
  752. /**
  753. * ttm_lru_walk_for_evict() - Perform a LRU list walk, with actions taken on
  754. * valid items.
  755. * @walk: describe the walks and actions taken
  756. * @bdev: The TTM device.
  757. * @man: The struct ttm_resource manager whose LRU lists we're walking.
  758. * @target: The end condition for the walk.
  759. *
  760. * The LRU lists of @man are walk, and for each struct ttm_resource encountered,
  761. * the corresponding ttm_buffer_object is locked and taken a reference on, and
  762. * the LRU lock is dropped. the LRU lock may be dropped before locking and, in
  763. * that case, it's verified that the item actually remains on the LRU list after
  764. * the lock, and that the buffer object didn't switch resource in between.
  765. *
  766. * With a locked object, the actions indicated by @walk->process_bo are
  767. * performed, and after that, the bo is unlocked, the refcount dropped and the
  768. * next struct ttm_resource is processed. Here, the walker relies on
  769. * TTM's restartable LRU list implementation.
  770. *
  771. * Typically @walk->process_bo() would return the number of pages evicted,
  772. * swapped or shrunken, so that when the total exceeds @target, or when the
  773. * LRU list has been walked in full, iteration is terminated. It's also terminated
  774. * on error. Note that the definition of @target is done by the caller, it
  775. * could have a different meaning than the number of pages.
  776. *
  777. * Note that the way dma_resv individualization is done, locking needs to be done
  778. * either with the LRU lock held (trylocking only) or with a reference on the
  779. * object.
  780. *
  781. * Return: The progress made towards target or negative error code on error.
  782. */
  783. s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
  784. struct ttm_resource_manager *man, s64 target)
  785. {
  786. struct ttm_bo_lru_cursor cursor;
  787. struct ttm_buffer_object *bo;
  788. s64 progress = 0;
  789. s64 lret;
  790. ttm_bo_lru_for_each_reserved_guarded(&cursor, man, &walk->arg, bo) {
  791. lret = walk->ops->process_bo(walk, bo);
  792. if (lret == -EBUSY || lret == -EALREADY)
  793. lret = 0;
  794. progress = (lret < 0) ? lret : progress + lret;
  795. if (progress < 0 || progress >= target)
  796. break;
  797. }
  798. if (IS_ERR(bo))
  799. return PTR_ERR(bo);
  800. return progress;
  801. }
  802. EXPORT_SYMBOL(ttm_lru_walk_for_evict);
  803. static void ttm_bo_lru_cursor_cleanup_bo(struct ttm_bo_lru_cursor *curs)
  804. {
  805. struct ttm_buffer_object *bo = curs->bo;
  806. if (bo) {
  807. if (curs->needs_unlock)
  808. dma_resv_unlock(bo->base.resv);
  809. ttm_bo_put(bo);
  810. curs->bo = NULL;
  811. }
  812. }
  813. /**
  814. * ttm_bo_lru_cursor_fini() - Stop using a struct ttm_bo_lru_cursor
  815. * and clean up any iteration it was used for.
  816. * @curs: The cursor.
  817. */
  818. void ttm_bo_lru_cursor_fini(struct ttm_bo_lru_cursor *curs)
  819. {
  820. spinlock_t *lru_lock = &curs->res_curs.man->bdev->lru_lock;
  821. ttm_bo_lru_cursor_cleanup_bo(curs);
  822. spin_lock(lru_lock);
  823. ttm_resource_cursor_fini(&curs->res_curs);
  824. spin_unlock(lru_lock);
  825. }
  826. EXPORT_SYMBOL(ttm_bo_lru_cursor_fini);
  827. /**
  828. * ttm_bo_lru_cursor_init() - Initialize a struct ttm_bo_lru_cursor
  829. * @curs: The ttm_bo_lru_cursor to initialize.
  830. * @man: The ttm resource_manager whose LRU lists to iterate over.
  831. * @arg: The ttm_lru_walk_arg to govern the walk.
  832. *
  833. * Initialize a struct ttm_bo_lru_cursor.
  834. *
  835. * Return: Pointer to @curs. The function does not fail.
  836. */
  837. struct ttm_bo_lru_cursor *
  838. ttm_bo_lru_cursor_init(struct ttm_bo_lru_cursor *curs,
  839. struct ttm_resource_manager *man,
  840. struct ttm_lru_walk_arg *arg)
  841. {
  842. memset(curs, 0, sizeof(*curs));
  843. ttm_resource_cursor_init(&curs->res_curs, man);
  844. curs->arg = arg;
  845. return curs;
  846. }
  847. EXPORT_SYMBOL(ttm_bo_lru_cursor_init);
  848. static struct ttm_buffer_object *
  849. __ttm_bo_lru_cursor_next(struct ttm_bo_lru_cursor *curs)
  850. {
  851. spinlock_t *lru_lock = &curs->res_curs.man->bdev->lru_lock;
  852. struct ttm_resource *res = NULL;
  853. struct ttm_buffer_object *bo;
  854. struct ttm_lru_walk_arg *arg = curs->arg;
  855. bool first = !curs->bo;
  856. ttm_bo_lru_cursor_cleanup_bo(curs);
  857. spin_lock(lru_lock);
  858. for (;;) {
  859. int mem_type, ret = 0;
  860. bool bo_locked = false;
  861. if (first) {
  862. res = ttm_resource_manager_first(&curs->res_curs);
  863. first = false;
  864. } else {
  865. res = ttm_resource_manager_next(&curs->res_curs);
  866. }
  867. if (!res)
  868. break;
  869. bo = res->bo;
  870. if (ttm_lru_walk_trylock(curs, bo))
  871. bo_locked = true;
  872. else if (!arg->ticket || arg->ctx->no_wait_gpu || arg->trylock_only)
  873. continue;
  874. if (!ttm_bo_get_unless_zero(bo)) {
  875. if (curs->needs_unlock)
  876. dma_resv_unlock(bo->base.resv);
  877. continue;
  878. }
  879. mem_type = res->mem_type;
  880. spin_unlock(lru_lock);
  881. if (!bo_locked)
  882. ret = ttm_lru_walk_ticketlock(curs, bo);
  883. /*
  884. * Note that in between the release of the lru lock and the
  885. * ticketlock, the bo may have switched resource,
  886. * and also memory type, since the resource may have been
  887. * freed and allocated again with a different memory type.
  888. * In that case, just skip it.
  889. */
  890. curs->bo = bo;
  891. if (!ret && bo->resource && bo->resource->mem_type == mem_type)
  892. return bo;
  893. ttm_bo_lru_cursor_cleanup_bo(curs);
  894. if (ret && ret != -EALREADY)
  895. return ERR_PTR(ret);
  896. spin_lock(lru_lock);
  897. }
  898. spin_unlock(lru_lock);
  899. return res ? bo : NULL;
  900. }
  901. /**
  902. * ttm_bo_lru_cursor_next() - Continue iterating a manager's LRU lists
  903. * to find and lock buffer object.
  904. * @curs: The cursor initialized using ttm_bo_lru_cursor_init() and
  905. * ttm_bo_lru_cursor_first().
  906. *
  907. * Return: A pointer to a locked and reference-counted buffer object,
  908. * or NULL if none could be found and looping should be terminated.
  909. */
  910. struct ttm_buffer_object *ttm_bo_lru_cursor_next(struct ttm_bo_lru_cursor *curs)
  911. {
  912. return __ttm_bo_lru_cursor_next(curs);
  913. }
  914. EXPORT_SYMBOL(ttm_bo_lru_cursor_next);
  915. /**
  916. * ttm_bo_lru_cursor_first() - Start iterating a manager's LRU lists
  917. * to find and lock buffer object.
  918. * @curs: The cursor initialized using ttm_bo_lru_cursor_init().
  919. *
  920. * Return: A pointer to a locked and reference-counted buffer object,
  921. * or NULL if none could be found and looping should be terminated.
  922. */
  923. struct ttm_buffer_object *ttm_bo_lru_cursor_first(struct ttm_bo_lru_cursor *curs)
  924. {
  925. ttm_bo_lru_cursor_cleanup_bo(curs);
  926. return __ttm_bo_lru_cursor_next(curs);
  927. }
  928. EXPORT_SYMBOL(ttm_bo_lru_cursor_first);
  929. /**
  930. * ttm_bo_shrink() - Helper to shrink a ttm buffer object.
  931. * @ctx: The struct ttm_operation_ctx used for the shrinking operation.
  932. * @bo: The buffer object.
  933. * @flags: Flags governing the shrinking behaviour.
  934. *
  935. * The function uses the ttm_tt_back_up functionality to back up or
  936. * purge a struct ttm_tt. If the bo is not in system, it's first
  937. * moved there.
  938. *
  939. * Return: The number of pages shrunken or purged, or
  940. * negative error code on failure.
  941. */
  942. long ttm_bo_shrink(struct ttm_operation_ctx *ctx, struct ttm_buffer_object *bo,
  943. const struct ttm_bo_shrink_flags flags)
  944. {
  945. static const struct ttm_place sys_placement_flags = {
  946. .fpfn = 0,
  947. .lpfn = 0,
  948. .mem_type = TTM_PL_SYSTEM,
  949. .flags = 0,
  950. };
  951. static struct ttm_placement sys_placement = {
  952. .num_placement = 1,
  953. .placement = &sys_placement_flags,
  954. };
  955. struct ttm_device *bdev = bo->bdev;
  956. long lret;
  957. dma_resv_assert_held(bo->base.resv);
  958. if (flags.allow_move && bo->resource->mem_type != TTM_PL_SYSTEM) {
  959. int ret = ttm_bo_validate(bo, &sys_placement, ctx);
  960. /* Consider -ENOMEM and -ENOSPC non-fatal. */
  961. if (ret) {
  962. if (ret == -ENOMEM || ret == -ENOSPC)
  963. ret = -EBUSY;
  964. return ret;
  965. }
  966. }
  967. ttm_bo_unmap_virtual(bo);
  968. lret = ttm_bo_wait_ctx(bo, ctx);
  969. if (lret < 0)
  970. return lret;
  971. if (bo->bulk_move) {
  972. spin_lock(&bdev->lru_lock);
  973. ttm_resource_del_bulk_move(bo->resource, bo);
  974. spin_unlock(&bdev->lru_lock);
  975. }
  976. lret = ttm_tt_backup(bdev, bo->ttm, (struct ttm_backup_flags)
  977. {.purge = flags.purge,
  978. .writeback = flags.writeback});
  979. if (lret <= 0 && bo->bulk_move) {
  980. spin_lock(&bdev->lru_lock);
  981. ttm_resource_add_bulk_move(bo->resource, bo);
  982. spin_unlock(&bdev->lru_lock);
  983. }
  984. if (lret < 0 && lret != -EINTR)
  985. return -EBUSY;
  986. return lret;
  987. }
  988. EXPORT_SYMBOL(ttm_bo_shrink);
  989. /**
  990. * ttm_bo_shrink_suitable() - Whether a bo is suitable for shinking
  991. * @ctx: The struct ttm_operation_ctx governing the shrinking.
  992. * @bo: The candidate for shrinking.
  993. *
  994. * Check whether the object, given the information available to TTM,
  995. * is suitable for shinking, This function can and should be used
  996. * before attempting to shrink an object.
  997. *
  998. * Return: true if suitable. false if not.
  999. */
  1000. bool ttm_bo_shrink_suitable(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
  1001. {
  1002. return bo->ttm && ttm_tt_is_populated(bo->ttm) && !bo->pin_count &&
  1003. (!ctx->no_wait_gpu ||
  1004. dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP));
  1005. }
  1006. EXPORT_SYMBOL(ttm_bo_shrink_suitable);
  1007. /**
  1008. * ttm_bo_shrink_avoid_wait() - Whether to avoid waiting for GPU
  1009. * during shrinking
  1010. *
  1011. * In some situations, like direct reclaim, waiting (in particular gpu waiting)
  1012. * should be avoided since it may stall a system that could otherwise make progress
  1013. * shrinking something else less time consuming.
  1014. *
  1015. * Return: true if gpu waiting should be avoided, false if not.
  1016. */
  1017. bool ttm_bo_shrink_avoid_wait(void)
  1018. {
  1019. return !current_is_kswapd();
  1020. }
  1021. EXPORT_SYMBOL(ttm_bo_shrink_avoid_wait);