ttm_bo_util.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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/vmalloc.h>
  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_cache.h>
  36. struct ttm_transfer_obj {
  37. struct ttm_buffer_object base;
  38. struct ttm_buffer_object *bo;
  39. };
  40. int ttm_mem_io_reserve(struct ttm_device *bdev,
  41. struct ttm_resource *mem)
  42. {
  43. if (mem->bus.offset || mem->bus.addr)
  44. return 0;
  45. mem->bus.is_iomem = false;
  46. if (!bdev->funcs->io_mem_reserve)
  47. return 0;
  48. return bdev->funcs->io_mem_reserve(bdev, mem);
  49. }
  50. void ttm_mem_io_free(struct ttm_device *bdev,
  51. struct ttm_resource *mem)
  52. {
  53. if (!mem)
  54. return;
  55. if (!mem->bus.offset && !mem->bus.addr)
  56. return;
  57. if (bdev->funcs->io_mem_free)
  58. bdev->funcs->io_mem_free(bdev, mem);
  59. mem->bus.offset = 0;
  60. mem->bus.addr = NULL;
  61. }
  62. /**
  63. * ttm_move_memcpy - Helper to perform a memcpy ttm move operation.
  64. * @clear: Whether to clear rather than copy.
  65. * @num_pages: Number of pages of the operation.
  66. * @dst_iter: A struct ttm_kmap_iter representing the destination resource.
  67. * @src_iter: A struct ttm_kmap_iter representing the source resource.
  68. *
  69. * This function is intended to be able to move out async under a
  70. * dma-fence if desired.
  71. */
  72. void ttm_move_memcpy(bool clear,
  73. u32 num_pages,
  74. struct ttm_kmap_iter *dst_iter,
  75. struct ttm_kmap_iter *src_iter)
  76. {
  77. const struct ttm_kmap_iter_ops *dst_ops = dst_iter->ops;
  78. const struct ttm_kmap_iter_ops *src_ops = src_iter->ops;
  79. struct iosys_map src_map, dst_map;
  80. pgoff_t i;
  81. /* Single TTM move. NOP */
  82. if (dst_ops->maps_tt && src_ops->maps_tt)
  83. return;
  84. /* Don't move nonexistent data. Clear destination instead. */
  85. if (clear) {
  86. for (i = 0; i < num_pages; ++i) {
  87. dst_ops->map_local(dst_iter, &dst_map, i);
  88. if (dst_map.is_iomem)
  89. memset_io(dst_map.vaddr_iomem, 0, PAGE_SIZE);
  90. else
  91. memset(dst_map.vaddr, 0, PAGE_SIZE);
  92. if (dst_ops->unmap_local)
  93. dst_ops->unmap_local(dst_iter, &dst_map);
  94. }
  95. return;
  96. }
  97. for (i = 0; i < num_pages; ++i) {
  98. dst_ops->map_local(dst_iter, &dst_map, i);
  99. src_ops->map_local(src_iter, &src_map, i);
  100. drm_memcpy_from_wc(&dst_map, &src_map, PAGE_SIZE);
  101. if (src_ops->unmap_local)
  102. src_ops->unmap_local(src_iter, &src_map);
  103. if (dst_ops->unmap_local)
  104. dst_ops->unmap_local(dst_iter, &dst_map);
  105. }
  106. }
  107. EXPORT_SYMBOL(ttm_move_memcpy);
  108. /**
  109. * ttm_bo_move_memcpy
  110. *
  111. * @bo: A pointer to a struct ttm_buffer_object.
  112. * @ctx: operation context
  113. * @dst_mem: struct ttm_resource indicating where to move.
  114. *
  115. * Fallback move function for a mappable buffer object in mappable memory.
  116. * The function will, if successful,
  117. * free any old aperture space, and set (@new_mem)->mm_node to NULL,
  118. * and update the (@bo)->mem placement flags. If unsuccessful, the old
  119. * data remains untouched, and it's up to the caller to free the
  120. * memory space indicated by @new_mem.
  121. * Returns:
  122. * !0: Failure.
  123. */
  124. int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
  125. struct ttm_operation_ctx *ctx,
  126. struct ttm_resource *dst_mem)
  127. {
  128. struct ttm_device *bdev = bo->bdev;
  129. struct ttm_resource_manager *dst_man =
  130. ttm_manager_type(bo->bdev, dst_mem->mem_type);
  131. struct ttm_tt *ttm = bo->ttm;
  132. struct ttm_resource *src_mem = bo->resource;
  133. struct ttm_resource_manager *src_man;
  134. union {
  135. struct ttm_kmap_iter_tt tt;
  136. struct ttm_kmap_iter_linear_io io;
  137. } _dst_iter, _src_iter;
  138. struct ttm_kmap_iter *dst_iter, *src_iter;
  139. bool clear;
  140. int ret = 0;
  141. if (WARN_ON(!src_mem))
  142. return -EINVAL;
  143. src_man = ttm_manager_type(bdev, src_mem->mem_type);
  144. if (ttm && ((ttm->page_flags & TTM_TT_FLAG_SWAPPED) ||
  145. dst_man->use_tt)) {
  146. ret = ttm_tt_populate(bdev, ttm, ctx);
  147. if (ret)
  148. return ret;
  149. }
  150. dst_iter = ttm_kmap_iter_linear_io_init(&_dst_iter.io, bdev, dst_mem);
  151. if (PTR_ERR(dst_iter) == -EINVAL && dst_man->use_tt)
  152. dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, bo->ttm);
  153. if (IS_ERR(dst_iter))
  154. return PTR_ERR(dst_iter);
  155. src_iter = ttm_kmap_iter_linear_io_init(&_src_iter.io, bdev, src_mem);
  156. if (PTR_ERR(src_iter) == -EINVAL && src_man->use_tt)
  157. src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm);
  158. if (IS_ERR(src_iter)) {
  159. ret = PTR_ERR(src_iter);
  160. goto out_src_iter;
  161. }
  162. clear = src_iter->ops->maps_tt && (!ttm || !ttm_tt_is_populated(ttm));
  163. if (!(clear && ttm && !(ttm->page_flags & TTM_TT_FLAG_ZERO_ALLOC)))
  164. ttm_move_memcpy(clear, PFN_UP(dst_mem->size), dst_iter, src_iter);
  165. if (!src_iter->ops->maps_tt)
  166. ttm_kmap_iter_linear_io_fini(&_src_iter.io, bdev, src_mem);
  167. ttm_bo_move_sync_cleanup(bo, dst_mem);
  168. out_src_iter:
  169. if (!dst_iter->ops->maps_tt)
  170. ttm_kmap_iter_linear_io_fini(&_dst_iter.io, bdev, dst_mem);
  171. return ret;
  172. }
  173. EXPORT_SYMBOL(ttm_bo_move_memcpy);
  174. static void ttm_transfered_destroy(struct ttm_buffer_object *bo)
  175. {
  176. struct ttm_transfer_obj *fbo;
  177. fbo = container_of(bo, struct ttm_transfer_obj, base);
  178. dma_resv_fini(&fbo->base.base._resv);
  179. ttm_bo_put(fbo->bo);
  180. kfree(fbo);
  181. }
  182. /**
  183. * ttm_buffer_object_transfer
  184. *
  185. * @bo: A pointer to a struct ttm_buffer_object.
  186. * @new_obj: A pointer to a pointer to a newly created ttm_buffer_object,
  187. * holding the data of @bo with the old placement.
  188. *
  189. * This is a utility function that may be called after an accelerated move
  190. * has been scheduled. A new buffer object is created as a placeholder for
  191. * the old data while it's being copied. When that buffer object is idle,
  192. * it can be destroyed, releasing the space of the old placement.
  193. * Returns:
  194. * !0: Failure.
  195. */
  196. static int ttm_buffer_object_transfer(struct ttm_buffer_object *bo,
  197. struct ttm_buffer_object **new_obj)
  198. {
  199. struct ttm_transfer_obj *fbo;
  200. int ret;
  201. fbo = kmalloc(sizeof(*fbo), GFP_KERNEL);
  202. if (!fbo)
  203. return -ENOMEM;
  204. fbo->base = *bo;
  205. /**
  206. * Fix up members that we shouldn't copy directly:
  207. * TODO: Explicit member copy would probably be better here.
  208. */
  209. atomic_inc(&ttm_glob.bo_count);
  210. drm_vma_node_reset(&fbo->base.base.vma_node);
  211. kref_init(&fbo->base.kref);
  212. fbo->base.destroy = &ttm_transfered_destroy;
  213. fbo->base.pin_count = 0;
  214. if (bo->type != ttm_bo_type_sg)
  215. fbo->base.base.resv = &fbo->base.base._resv;
  216. dma_resv_init(&fbo->base.base._resv);
  217. fbo->base.base.dev = NULL;
  218. ret = dma_resv_trylock(&fbo->base.base._resv);
  219. WARN_ON(!ret);
  220. ret = dma_resv_reserve_fences(&fbo->base.base._resv, 1);
  221. if (ret) {
  222. dma_resv_unlock(&fbo->base.base._resv);
  223. kfree(fbo);
  224. return ret;
  225. }
  226. if (fbo->base.resource) {
  227. ttm_resource_set_bo(fbo->base.resource, &fbo->base);
  228. bo->resource = NULL;
  229. ttm_bo_set_bulk_move(&fbo->base, NULL);
  230. } else {
  231. fbo->base.bulk_move = NULL;
  232. }
  233. ttm_bo_get(bo);
  234. fbo->bo = bo;
  235. ttm_bo_move_to_lru_tail_unlocked(&fbo->base);
  236. *new_obj = &fbo->base;
  237. return 0;
  238. }
  239. /**
  240. * ttm_io_prot
  241. *
  242. * @bo: ttm buffer object
  243. * @res: ttm resource object
  244. * @tmp: Page protection flag for a normal, cached mapping.
  245. *
  246. * Utility function that returns the pgprot_t that should be used for
  247. * setting up a PTE with the caching model indicated by @c_state.
  248. */
  249. pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
  250. pgprot_t tmp)
  251. {
  252. struct ttm_resource_manager *man;
  253. enum ttm_caching caching;
  254. man = ttm_manager_type(bo->bdev, res->mem_type);
  255. if (man->use_tt) {
  256. caching = bo->ttm->caching;
  257. if (bo->ttm->page_flags & TTM_TT_FLAG_DECRYPTED)
  258. tmp = pgprot_decrypted(tmp);
  259. } else {
  260. caching = res->bus.caching;
  261. }
  262. return ttm_prot_from_caching(caching, tmp);
  263. }
  264. EXPORT_SYMBOL(ttm_io_prot);
  265. static int ttm_bo_ioremap(struct ttm_buffer_object *bo,
  266. unsigned long offset,
  267. unsigned long size,
  268. struct ttm_bo_kmap_obj *map)
  269. {
  270. struct ttm_resource *mem = bo->resource;
  271. if (bo->resource->bus.addr) {
  272. map->bo_kmap_type = ttm_bo_map_premapped;
  273. map->virtual = ((u8 *)bo->resource->bus.addr) + offset;
  274. } else {
  275. resource_size_t res = bo->resource->bus.offset + offset;
  276. map->bo_kmap_type = ttm_bo_map_iomap;
  277. if (mem->bus.caching == ttm_write_combined)
  278. map->virtual = ioremap_wc(res, size);
  279. #ifdef CONFIG_X86
  280. else if (mem->bus.caching == ttm_cached)
  281. map->virtual = ioremap_cache(res, size);
  282. #endif
  283. else
  284. map->virtual = ioremap(res, size);
  285. }
  286. return (!map->virtual) ? -ENOMEM : 0;
  287. }
  288. static int ttm_bo_kmap_ttm(struct ttm_buffer_object *bo,
  289. unsigned long start_page,
  290. unsigned long num_pages,
  291. struct ttm_bo_kmap_obj *map)
  292. {
  293. struct ttm_resource *mem = bo->resource;
  294. struct ttm_operation_ctx ctx = {
  295. .interruptible = false,
  296. .no_wait_gpu = false
  297. };
  298. struct ttm_tt *ttm = bo->ttm;
  299. struct ttm_resource_manager *man =
  300. ttm_manager_type(bo->bdev, bo->resource->mem_type);
  301. pgprot_t prot;
  302. int ret;
  303. BUG_ON(!ttm);
  304. ret = ttm_tt_populate(bo->bdev, ttm, &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
  330. *
  331. * @bo: The buffer object.
  332. * @start_page: The first page to map.
  333. * @num_pages: Number of pages to map.
  334. * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
  335. *
  336. * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
  337. * data in the buffer object. The ttm_kmap_obj_virtual function can then be
  338. * used to obtain a virtual address to the data.
  339. *
  340. * Returns
  341. * -ENOMEM: Out of memory.
  342. * -EINVAL: Invalid range.
  343. */
  344. int ttm_bo_kmap(struct ttm_buffer_object *bo,
  345. unsigned long start_page, unsigned long num_pages,
  346. struct ttm_bo_kmap_obj *map)
  347. {
  348. unsigned long offset, size;
  349. int ret;
  350. map->virtual = NULL;
  351. map->bo = bo;
  352. if (num_pages > PFN_UP(bo->resource->size))
  353. return -EINVAL;
  354. if ((start_page + num_pages) > PFN_UP(bo->resource->size))
  355. return -EINVAL;
  356. ret = ttm_mem_io_reserve(bo->bdev, bo->resource);
  357. if (ret)
  358. return ret;
  359. if (!bo->resource->bus.is_iomem) {
  360. return ttm_bo_kmap_ttm(bo, start_page, num_pages, map);
  361. } else {
  362. offset = start_page << PAGE_SHIFT;
  363. size = num_pages << PAGE_SHIFT;
  364. return ttm_bo_ioremap(bo, offset, size, map);
  365. }
  366. }
  367. EXPORT_SYMBOL(ttm_bo_kmap);
  368. /**
  369. * ttm_bo_kunmap
  370. *
  371. * @map: Object describing the map to unmap.
  372. *
  373. * Unmaps a kernel map set up by ttm_bo_kmap.
  374. */
  375. void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map)
  376. {
  377. if (!map->virtual)
  378. return;
  379. switch (map->bo_kmap_type) {
  380. case ttm_bo_map_iomap:
  381. iounmap(map->virtual);
  382. break;
  383. case ttm_bo_map_vmap:
  384. vunmap(map->virtual);
  385. break;
  386. case ttm_bo_map_kmap:
  387. kunmap(map->page);
  388. break;
  389. case ttm_bo_map_premapped:
  390. break;
  391. default:
  392. BUG();
  393. }
  394. ttm_mem_io_free(map->bo->bdev, map->bo->resource);
  395. map->virtual = NULL;
  396. map->page = NULL;
  397. }
  398. EXPORT_SYMBOL(ttm_bo_kunmap);
  399. /**
  400. * ttm_bo_vmap
  401. *
  402. * @bo: The buffer object.
  403. * @map: pointer to a struct iosys_map representing the map.
  404. *
  405. * Sets up a kernel virtual mapping, using ioremap or vmap to the
  406. * data in the buffer object. The parameter @map returns the virtual
  407. * address as struct iosys_map. Unmap the buffer with ttm_bo_vunmap().
  408. *
  409. * Returns
  410. * -ENOMEM: Out of memory.
  411. * -EINVAL: Invalid range.
  412. */
  413. int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map)
  414. {
  415. struct ttm_resource *mem = bo->resource;
  416. int ret;
  417. dma_resv_assert_held(bo->base.resv);
  418. ret = ttm_mem_io_reserve(bo->bdev, mem);
  419. if (ret)
  420. return ret;
  421. if (mem->bus.is_iomem) {
  422. void __iomem *vaddr_iomem;
  423. if (mem->bus.addr)
  424. vaddr_iomem = (void __iomem *)mem->bus.addr;
  425. else if (mem->bus.caching == ttm_write_combined)
  426. vaddr_iomem = ioremap_wc(mem->bus.offset,
  427. bo->base.size);
  428. #ifdef CONFIG_X86
  429. else if (mem->bus.caching == ttm_cached)
  430. vaddr_iomem = ioremap_cache(mem->bus.offset,
  431. bo->base.size);
  432. #endif
  433. else
  434. vaddr_iomem = ioremap(mem->bus.offset, bo->base.size);
  435. if (!vaddr_iomem)
  436. return -ENOMEM;
  437. iosys_map_set_vaddr_iomem(map, vaddr_iomem);
  438. } else {
  439. struct ttm_operation_ctx ctx = {
  440. .interruptible = false,
  441. .no_wait_gpu = false
  442. };
  443. struct ttm_tt *ttm = bo->ttm;
  444. pgprot_t prot;
  445. void *vaddr;
  446. ret = ttm_tt_populate(bo->bdev, ttm, &ctx);
  447. if (ret)
  448. return ret;
  449. /*
  450. * We need to use vmap to get the desired page protection
  451. * or to make the buffer object look contiguous.
  452. */
  453. prot = ttm_io_prot(bo, mem, PAGE_KERNEL);
  454. vaddr = vmap(ttm->pages, ttm->num_pages, 0, prot);
  455. if (!vaddr)
  456. return -ENOMEM;
  457. iosys_map_set_vaddr(map, vaddr);
  458. }
  459. return 0;
  460. }
  461. EXPORT_SYMBOL(ttm_bo_vmap);
  462. /**
  463. * ttm_bo_vunmap
  464. *
  465. * @bo: The buffer object.
  466. * @map: Object describing the map to unmap.
  467. *
  468. * Unmaps a kernel map set up by ttm_bo_vmap().
  469. */
  470. void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map)
  471. {
  472. struct ttm_resource *mem = bo->resource;
  473. dma_resv_assert_held(bo->base.resv);
  474. if (iosys_map_is_null(map))
  475. return;
  476. if (!map->is_iomem)
  477. vunmap(map->vaddr);
  478. else if (!mem->bus.addr)
  479. iounmap(map->vaddr_iomem);
  480. iosys_map_clear(map);
  481. ttm_mem_io_free(bo->bdev, bo->resource);
  482. }
  483. EXPORT_SYMBOL(ttm_bo_vunmap);
  484. static int ttm_bo_wait_free_node(struct ttm_buffer_object *bo,
  485. bool dst_use_tt)
  486. {
  487. long ret;
  488. ret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
  489. false, 15 * HZ);
  490. if (ret == 0)
  491. return -EBUSY;
  492. if (ret < 0)
  493. return ret;
  494. if (!dst_use_tt)
  495. ttm_bo_tt_destroy(bo);
  496. ttm_resource_free(bo, &bo->resource);
  497. return 0;
  498. }
  499. static int ttm_bo_move_to_ghost(struct ttm_buffer_object *bo,
  500. struct dma_fence *fence,
  501. bool dst_use_tt)
  502. {
  503. struct ttm_buffer_object *ghost_obj;
  504. int ret;
  505. /**
  506. * This should help pipeline ordinary buffer moves.
  507. *
  508. * Hang old buffer memory on a new buffer object,
  509. * and leave it to be released when the GPU
  510. * operation has completed.
  511. */
  512. ret = ttm_buffer_object_transfer(bo, &ghost_obj);
  513. if (ret)
  514. return ret;
  515. dma_resv_add_fence(&ghost_obj->base._resv, fence,
  516. DMA_RESV_USAGE_KERNEL);
  517. /**
  518. * If we're not moving to fixed memory, the TTM object
  519. * needs to stay alive. Otherwhise hang it on the ghost
  520. * bo to be unbound and destroyed.
  521. */
  522. if (dst_use_tt)
  523. ghost_obj->ttm = NULL;
  524. else
  525. bo->ttm = NULL;
  526. dma_resv_unlock(&ghost_obj->base._resv);
  527. ttm_bo_put(ghost_obj);
  528. return 0;
  529. }
  530. static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo,
  531. struct dma_fence *fence)
  532. {
  533. struct ttm_device *bdev = bo->bdev;
  534. struct ttm_resource_manager *from;
  535. from = ttm_manager_type(bdev, bo->resource->mem_type);
  536. /**
  537. * BO doesn't have a TTM we need to bind/unbind. Just remember
  538. * this eviction and free up the allocation
  539. */
  540. spin_lock(&from->move_lock);
  541. if (!from->move || dma_fence_is_later(fence, from->move)) {
  542. dma_fence_put(from->move);
  543. from->move = dma_fence_get(fence);
  544. }
  545. spin_unlock(&from->move_lock);
  546. ttm_resource_free(bo, &bo->resource);
  547. }
  548. /**
  549. * ttm_bo_move_accel_cleanup - cleanup helper for hw copies
  550. *
  551. * @bo: A pointer to a struct ttm_buffer_object.
  552. * @fence: A fence object that signals when moving is complete.
  553. * @evict: This is an evict move. Don't return until the buffer is idle.
  554. * @pipeline: evictions are to be pipelined.
  555. * @new_mem: struct ttm_resource indicating where to move.
  556. *
  557. * Accelerated move function to be called when an accelerated move
  558. * has been scheduled. The function will create a new temporary buffer object
  559. * representing the old placement, and put the sync object on both buffer
  560. * objects. After that the newly created buffer object is unref'd to be
  561. * destroyed when the move is complete. This will help pipeline
  562. * buffer moves.
  563. */
  564. int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
  565. struct dma_fence *fence,
  566. bool evict,
  567. bool pipeline,
  568. struct ttm_resource *new_mem)
  569. {
  570. struct ttm_device *bdev = bo->bdev;
  571. struct ttm_resource_manager *from = ttm_manager_type(bdev, bo->resource->mem_type);
  572. struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
  573. int ret = 0;
  574. dma_resv_add_fence(bo->base.resv, fence, DMA_RESV_USAGE_KERNEL);
  575. if (!evict)
  576. ret = ttm_bo_move_to_ghost(bo, fence, man->use_tt);
  577. else if (!from->use_tt && pipeline)
  578. ttm_bo_move_pipeline_evict(bo, fence);
  579. else
  580. ret = ttm_bo_wait_free_node(bo, man->use_tt);
  581. if (ret)
  582. return ret;
  583. ttm_bo_assign_mem(bo, new_mem);
  584. return 0;
  585. }
  586. EXPORT_SYMBOL(ttm_bo_move_accel_cleanup);
  587. /**
  588. * ttm_bo_move_sync_cleanup - cleanup by waiting for the move to finish
  589. *
  590. * @bo: A pointer to a struct ttm_buffer_object.
  591. * @new_mem: struct ttm_resource indicating where to move.
  592. *
  593. * Special case of ttm_bo_move_accel_cleanup where the bo is guaranteed
  594. * by the caller to be idle. Typically used after memcpy buffer moves.
  595. */
  596. void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
  597. struct ttm_resource *new_mem)
  598. {
  599. struct ttm_device *bdev = bo->bdev;
  600. struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type);
  601. int ret;
  602. ret = ttm_bo_wait_free_node(bo, man->use_tt);
  603. if (WARN_ON(ret))
  604. return;
  605. ttm_bo_assign_mem(bo, new_mem);
  606. }
  607. EXPORT_SYMBOL(ttm_bo_move_sync_cleanup);
  608. /**
  609. * ttm_bo_pipeline_gutting - purge the contents of a bo
  610. * @bo: The buffer object
  611. *
  612. * Purge the contents of a bo, async if the bo is not idle.
  613. * After a successful call, the bo is left unpopulated in
  614. * system placement. The function may wait uninterruptible
  615. * for idle on OOM.
  616. *
  617. * Return: 0 if successful, negative error code on failure.
  618. */
  619. int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo)
  620. {
  621. struct ttm_buffer_object *ghost;
  622. struct ttm_tt *ttm;
  623. int ret;
  624. /* If already idle, no need for ghost object dance. */
  625. if (dma_resv_test_signaled(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP)) {
  626. if (!bo->ttm) {
  627. /* See comment below about clearing. */
  628. ret = ttm_tt_create(bo, true);
  629. if (ret)
  630. return ret;
  631. } else {
  632. ttm_tt_unpopulate(bo->bdev, bo->ttm);
  633. if (bo->type == ttm_bo_type_device)
  634. ttm_tt_mark_for_clear(bo->ttm);
  635. }
  636. ttm_resource_free(bo, &bo->resource);
  637. return 0;
  638. }
  639. /*
  640. * We need an unpopulated ttm_tt after giving our current one,
  641. * if any, to the ghost object. And we can't afford to fail
  642. * creating one *after* the operation. If the bo subsequently gets
  643. * resurrected, make sure it's cleared (if ttm_bo_type_device)
  644. * to avoid leaking sensitive information to user-space.
  645. */
  646. ttm = bo->ttm;
  647. bo->ttm = NULL;
  648. ret = ttm_tt_create(bo, true);
  649. swap(bo->ttm, ttm);
  650. if (ret)
  651. return ret;
  652. ret = ttm_buffer_object_transfer(bo, &ghost);
  653. if (ret)
  654. goto error_destroy_tt;
  655. ret = dma_resv_copy_fences(&ghost->base._resv, bo->base.resv);
  656. /* Last resort, wait for the BO to be idle when we are OOM */
  657. if (ret) {
  658. dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_BOOKKEEP,
  659. false, MAX_SCHEDULE_TIMEOUT);
  660. }
  661. dma_resv_unlock(&ghost->base._resv);
  662. ttm_bo_put(ghost);
  663. bo->ttm = ttm;
  664. return 0;
  665. error_destroy_tt:
  666. ttm_tt_destroy(bo->bdev, ttm);
  667. return ret;
  668. }
  669. static bool ttm_lru_walk_trylock(struct ttm_lru_walk *walk,
  670. struct ttm_buffer_object *bo,
  671. bool *needs_unlock)
  672. {
  673. struct ttm_operation_ctx *ctx = walk->ctx;
  674. *needs_unlock = false;
  675. if (dma_resv_trylock(bo->base.resv)) {
  676. *needs_unlock = true;
  677. return true;
  678. }
  679. if (bo->base.resv == ctx->resv && ctx->allow_res_evict) {
  680. dma_resv_assert_held(bo->base.resv);
  681. return true;
  682. }
  683. return false;
  684. }
  685. static int ttm_lru_walk_ticketlock(struct ttm_lru_walk *walk,
  686. struct ttm_buffer_object *bo,
  687. bool *needs_unlock)
  688. {
  689. struct dma_resv *resv = bo->base.resv;
  690. int ret;
  691. if (walk->ctx->interruptible)
  692. ret = dma_resv_lock_interruptible(resv, walk->ticket);
  693. else
  694. ret = dma_resv_lock(resv, walk->ticket);
  695. if (!ret) {
  696. *needs_unlock = true;
  697. /*
  698. * Only a single ticketlock per loop. Ticketlocks are prone
  699. * to return -EDEADLK causing the eviction to fail, so
  700. * after waiting for the ticketlock, revert back to
  701. * trylocking for this walk.
  702. */
  703. walk->ticket = NULL;
  704. } else if (ret == -EDEADLK) {
  705. /* Caller needs to exit the ww transaction. */
  706. ret = -ENOSPC;
  707. }
  708. return ret;
  709. }
  710. static void ttm_lru_walk_unlock(struct ttm_buffer_object *bo, bool locked)
  711. {
  712. if (locked)
  713. dma_resv_unlock(bo->base.resv);
  714. }
  715. /**
  716. * ttm_lru_walk_for_evict() - Perform a LRU list walk, with actions taken on
  717. * valid items.
  718. * @walk: describe the walks and actions taken
  719. * @bdev: The TTM device.
  720. * @man: The struct ttm_resource manager whose LRU lists we're walking.
  721. * @target: The end condition for the walk.
  722. *
  723. * The LRU lists of @man are walk, and for each struct ttm_resource encountered,
  724. * the corresponding ttm_buffer_object is locked and taken a reference on, and
  725. * the LRU lock is dropped. the LRU lock may be dropped before locking and, in
  726. * that case, it's verified that the item actually remains on the LRU list after
  727. * the lock, and that the buffer object didn't switch resource in between.
  728. *
  729. * With a locked object, the actions indicated by @walk->process_bo are
  730. * performed, and after that, the bo is unlocked, the refcount dropped and the
  731. * next struct ttm_resource is processed. Here, the walker relies on
  732. * TTM's restartable LRU list implementation.
  733. *
  734. * Typically @walk->process_bo() would return the number of pages evicted,
  735. * swapped or shrunken, so that when the total exceeds @target, or when the
  736. * LRU list has been walked in full, iteration is terminated. It's also terminated
  737. * on error. Note that the definition of @target is done by the caller, it
  738. * could have a different meaning than the number of pages.
  739. *
  740. * Note that the way dma_resv individualization is done, locking needs to be done
  741. * either with the LRU lock held (trylocking only) or with a reference on the
  742. * object.
  743. *
  744. * Return: The progress made towards target or negative error code on error.
  745. */
  746. s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
  747. struct ttm_resource_manager *man, s64 target)
  748. {
  749. struct ttm_resource_cursor cursor;
  750. struct ttm_resource *res;
  751. s64 progress = 0;
  752. s64 lret;
  753. spin_lock(&bdev->lru_lock);
  754. ttm_resource_manager_for_each_res(man, &cursor, res) {
  755. struct ttm_buffer_object *bo = res->bo;
  756. bool bo_needs_unlock = false;
  757. bool bo_locked = false;
  758. int mem_type;
  759. /*
  760. * Attempt a trylock before taking a reference on the bo,
  761. * since if we do it the other way around, and the trylock fails,
  762. * we need to drop the lru lock to put the bo.
  763. */
  764. if (ttm_lru_walk_trylock(walk, bo, &bo_needs_unlock))
  765. bo_locked = true;
  766. else if (!walk->ticket || walk->ctx->no_wait_gpu ||
  767. walk->trylock_only)
  768. continue;
  769. if (!ttm_bo_get_unless_zero(bo)) {
  770. ttm_lru_walk_unlock(bo, bo_needs_unlock);
  771. continue;
  772. }
  773. mem_type = res->mem_type;
  774. spin_unlock(&bdev->lru_lock);
  775. lret = 0;
  776. if (!bo_locked)
  777. lret = ttm_lru_walk_ticketlock(walk, bo, &bo_needs_unlock);
  778. /*
  779. * Note that in between the release of the lru lock and the
  780. * ticketlock, the bo may have switched resource,
  781. * and also memory type, since the resource may have been
  782. * freed and allocated again with a different memory type.
  783. * In that case, just skip it.
  784. */
  785. if (!lret && bo->resource && bo->resource->mem_type == mem_type)
  786. lret = walk->ops->process_bo(walk, bo);
  787. ttm_lru_walk_unlock(bo, bo_needs_unlock);
  788. ttm_bo_put(bo);
  789. if (lret == -EBUSY || lret == -EALREADY)
  790. lret = 0;
  791. progress = (lret < 0) ? lret : progress + lret;
  792. spin_lock(&bdev->lru_lock);
  793. if (progress < 0 || progress >= target)
  794. break;
  795. }
  796. ttm_resource_cursor_fini(&cursor);
  797. spin_unlock(&bdev->lru_lock);
  798. return progress;
  799. }