vmwgfx_ttm_buffer.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-2023 VMware, Inc., Palo Alto, CA., USA
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_bo.h"
  28. #include "vmwgfx_drv.h"
  29. #include <drm/ttm/ttm_placement.h>
  30. static const struct ttm_place vram_placement_flags = {
  31. .fpfn = 0,
  32. .lpfn = 0,
  33. .mem_type = TTM_PL_VRAM,
  34. .flags = 0
  35. };
  36. static const struct ttm_place sys_placement_flags = {
  37. .fpfn = 0,
  38. .lpfn = 0,
  39. .mem_type = TTM_PL_SYSTEM,
  40. .flags = 0
  41. };
  42. struct ttm_placement vmw_vram_placement = {
  43. .num_placement = 1,
  44. .placement = &vram_placement_flags,
  45. };
  46. struct ttm_placement vmw_sys_placement = {
  47. .num_placement = 1,
  48. .placement = &sys_placement_flags,
  49. };
  50. const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
  51. /**
  52. * __vmw_piter_non_sg_next: Helper functions to advance
  53. * a struct vmw_piter iterator.
  54. *
  55. * @viter: Pointer to the iterator.
  56. *
  57. * These functions return false if past the end of the list,
  58. * true otherwise. Functions are selected depending on the current
  59. * DMA mapping mode.
  60. */
  61. static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
  62. {
  63. return ++(viter->i) < viter->num_pages;
  64. }
  65. static bool __vmw_piter_sg_next(struct vmw_piter *viter)
  66. {
  67. bool ret = __vmw_piter_non_sg_next(viter);
  68. return __sg_page_iter_dma_next(&viter->iter) && ret;
  69. }
  70. static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
  71. {
  72. return viter->addrs[viter->i];
  73. }
  74. static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
  75. {
  76. return sg_page_iter_dma_address(&viter->iter);
  77. }
  78. /**
  79. * vmw_piter_start - Initialize a struct vmw_piter.
  80. *
  81. * @viter: Pointer to the iterator to initialize
  82. * @vsgt: Pointer to a struct vmw_sg_table to initialize from
  83. * @p_offset: Pointer offset used to update current array position
  84. *
  85. * Note that we're following the convention of __sg_page_iter_start, so that
  86. * the iterator doesn't point to a valid page after initialization; it has
  87. * to be advanced one step first.
  88. */
  89. void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
  90. unsigned long p_offset)
  91. {
  92. viter->i = p_offset - 1;
  93. viter->num_pages = vsgt->num_pages;
  94. viter->pages = vsgt->pages;
  95. switch (vsgt->mode) {
  96. case vmw_dma_alloc_coherent:
  97. viter->next = &__vmw_piter_non_sg_next;
  98. viter->dma_address = &__vmw_piter_dma_addr;
  99. viter->addrs = vsgt->addrs;
  100. break;
  101. case vmw_dma_map_populate:
  102. case vmw_dma_map_bind:
  103. viter->next = &__vmw_piter_sg_next;
  104. viter->dma_address = &__vmw_piter_sg_addr;
  105. __sg_page_iter_start(&viter->iter.base, vsgt->sgt->sgl,
  106. vsgt->sgt->orig_nents, p_offset);
  107. break;
  108. default:
  109. BUG();
  110. }
  111. }
  112. /**
  113. * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
  114. * TTM pages
  115. *
  116. * @vmw_tt: Pointer to a struct vmw_ttm_backend
  117. *
  118. * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
  119. */
  120. static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
  121. {
  122. struct device *dev = vmw_tt->dev_priv->drm.dev;
  123. dma_unmap_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
  124. vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
  125. }
  126. /**
  127. * vmw_ttm_map_for_dma - map TTM pages to get device addresses
  128. *
  129. * @vmw_tt: Pointer to a struct vmw_ttm_backend
  130. *
  131. * This function is used to get device addresses from the kernel DMA layer.
  132. * However, it's violating the DMA API in that when this operation has been
  133. * performed, it's illegal for the CPU to write to the pages without first
  134. * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
  135. * therefore only legal to call this function if we know that the function
  136. * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
  137. * a CPU write buffer flush.
  138. */
  139. static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
  140. {
  141. struct device *dev = vmw_tt->dev_priv->drm.dev;
  142. return dma_map_sgtable(dev, &vmw_tt->sgt, DMA_BIDIRECTIONAL, 0);
  143. }
  144. /**
  145. * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
  146. *
  147. * @vmw_tt: Pointer to a struct vmw_ttm_tt
  148. *
  149. * Select the correct function for and make sure the TTM pages are
  150. * visible to the device. Allocate storage for the device mappings.
  151. * If a mapping has already been performed, indicated by the storage
  152. * pointer being non NULL, the function returns success.
  153. */
  154. static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
  155. {
  156. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  157. struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
  158. int ret = 0;
  159. if (vmw_tt->mapped)
  160. return 0;
  161. vsgt->mode = dev_priv->map_mode;
  162. vsgt->pages = vmw_tt->dma_ttm.pages;
  163. vsgt->num_pages = vmw_tt->dma_ttm.num_pages;
  164. vsgt->addrs = vmw_tt->dma_ttm.dma_address;
  165. vsgt->sgt = NULL;
  166. switch (dev_priv->map_mode) {
  167. case vmw_dma_map_bind:
  168. case vmw_dma_map_populate:
  169. if (vmw_tt->dma_ttm.page_flags & TTM_TT_FLAG_EXTERNAL) {
  170. vsgt->sgt = vmw_tt->dma_ttm.sg;
  171. } else {
  172. vsgt->sgt = &vmw_tt->sgt;
  173. ret = sg_alloc_table_from_pages_segment(&vmw_tt->sgt,
  174. vsgt->pages, vsgt->num_pages, 0,
  175. (unsigned long)vsgt->num_pages << PAGE_SHIFT,
  176. dma_get_max_seg_size(dev_priv->drm.dev),
  177. GFP_KERNEL);
  178. if (ret)
  179. goto out_sg_alloc_fail;
  180. }
  181. ret = vmw_ttm_map_for_dma(vmw_tt);
  182. if (unlikely(ret != 0))
  183. goto out_map_fail;
  184. break;
  185. default:
  186. break;
  187. }
  188. vmw_tt->mapped = true;
  189. return 0;
  190. out_map_fail:
  191. drm_warn(&dev_priv->drm, "VSG table map failed!");
  192. sg_free_table(vsgt->sgt);
  193. vsgt->sgt = NULL;
  194. out_sg_alloc_fail:
  195. return ret;
  196. }
  197. /**
  198. * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
  199. *
  200. * @vmw_tt: Pointer to a struct vmw_ttm_tt
  201. *
  202. * Tear down any previously set up device DMA mappings and free
  203. * any storage space allocated for them. If there are no mappings set up,
  204. * this function is a NOP.
  205. */
  206. static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
  207. {
  208. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  209. if (!vmw_tt->vsgt.sgt)
  210. return;
  211. switch (dev_priv->map_mode) {
  212. case vmw_dma_map_bind:
  213. case vmw_dma_map_populate:
  214. vmw_ttm_unmap_from_dma(vmw_tt);
  215. sg_free_table(vmw_tt->vsgt.sgt);
  216. vmw_tt->vsgt.sgt = NULL;
  217. break;
  218. default:
  219. break;
  220. }
  221. vmw_tt->mapped = false;
  222. }
  223. /**
  224. * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
  225. * TTM buffer object
  226. *
  227. * @bo: Pointer to a struct ttm_buffer_object
  228. *
  229. * Returns a pointer to a struct vmw_sg_table object. The object should
  230. * not be freed after use.
  231. * Note that for the device addresses to be valid, the buffer object must
  232. * either be reserved or pinned.
  233. */
  234. const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
  235. {
  236. struct vmw_ttm_tt *vmw_tt =
  237. container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm);
  238. return &vmw_tt->vsgt;
  239. }
  240. static int vmw_ttm_bind(struct ttm_device *bdev,
  241. struct ttm_tt *ttm, struct ttm_resource *bo_mem)
  242. {
  243. struct vmw_ttm_tt *vmw_be =
  244. container_of(ttm, struct vmw_ttm_tt, dma_ttm);
  245. int ret = 0;
  246. if (!bo_mem)
  247. return -EINVAL;
  248. if (vmw_be->bound)
  249. return 0;
  250. ret = vmw_ttm_map_dma(vmw_be);
  251. if (unlikely(ret != 0))
  252. return ret;
  253. vmw_be->gmr_id = bo_mem->start;
  254. vmw_be->mem_type = bo_mem->mem_type;
  255. switch (bo_mem->mem_type) {
  256. case VMW_PL_GMR:
  257. ret = vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
  258. ttm->num_pages, vmw_be->gmr_id);
  259. break;
  260. case VMW_PL_MOB:
  261. if (unlikely(vmw_be->mob == NULL)) {
  262. vmw_be->mob =
  263. vmw_mob_create(ttm->num_pages);
  264. if (unlikely(vmw_be->mob == NULL))
  265. return -ENOMEM;
  266. }
  267. ret = vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
  268. &vmw_be->vsgt, ttm->num_pages,
  269. vmw_be->gmr_id);
  270. break;
  271. case VMW_PL_SYSTEM:
  272. /* Nothing to be done for a system bind */
  273. break;
  274. default:
  275. BUG();
  276. }
  277. vmw_be->bound = true;
  278. return ret;
  279. }
  280. static void vmw_ttm_unbind(struct ttm_device *bdev,
  281. struct ttm_tt *ttm)
  282. {
  283. struct vmw_ttm_tt *vmw_be =
  284. container_of(ttm, struct vmw_ttm_tt, dma_ttm);
  285. if (!vmw_be->bound)
  286. return;
  287. switch (vmw_be->mem_type) {
  288. case VMW_PL_GMR:
  289. vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
  290. break;
  291. case VMW_PL_MOB:
  292. vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
  293. break;
  294. case VMW_PL_SYSTEM:
  295. break;
  296. default:
  297. BUG();
  298. }
  299. if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
  300. vmw_ttm_unmap_dma(vmw_be);
  301. vmw_be->bound = false;
  302. }
  303. static void vmw_ttm_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
  304. {
  305. struct vmw_ttm_tt *vmw_be =
  306. container_of(ttm, struct vmw_ttm_tt, dma_ttm);
  307. vmw_ttm_unmap_dma(vmw_be);
  308. ttm_tt_fini(ttm);
  309. if (vmw_be->mob)
  310. vmw_mob_destroy(vmw_be->mob);
  311. kfree(vmw_be);
  312. }
  313. static int vmw_ttm_populate(struct ttm_device *bdev,
  314. struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  315. {
  316. bool external = (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
  317. if (ttm_tt_is_populated(ttm))
  318. return 0;
  319. if (external && ttm->sg)
  320. return drm_prime_sg_to_dma_addr_array(ttm->sg,
  321. ttm->dma_address,
  322. ttm->num_pages);
  323. return ttm_pool_alloc(&bdev->pool, ttm, ctx);
  324. }
  325. static void vmw_ttm_unpopulate(struct ttm_device *bdev,
  326. struct ttm_tt *ttm)
  327. {
  328. struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
  329. dma_ttm);
  330. bool external = (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) != 0;
  331. if (external)
  332. return;
  333. vmw_ttm_unbind(bdev, ttm);
  334. if (vmw_tt->mob) {
  335. vmw_mob_destroy(vmw_tt->mob);
  336. vmw_tt->mob = NULL;
  337. }
  338. vmw_ttm_unmap_dma(vmw_tt);
  339. ttm_pool_free(&bdev->pool, ttm);
  340. }
  341. static struct ttm_tt *vmw_ttm_tt_create(struct ttm_buffer_object *bo,
  342. uint32_t page_flags)
  343. {
  344. struct vmw_ttm_tt *vmw_be;
  345. int ret;
  346. bool external = bo->type == ttm_bo_type_sg;
  347. vmw_be = kzalloc_obj(*vmw_be);
  348. if (!vmw_be)
  349. return NULL;
  350. vmw_be->dev_priv = vmw_priv_from_ttm(bo->bdev);
  351. vmw_be->mob = NULL;
  352. if (external)
  353. page_flags |= TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_EXTERNAL_MAPPABLE;
  354. if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent || external)
  355. ret = ttm_sg_tt_init(&vmw_be->dma_ttm, bo, page_flags,
  356. ttm_cached);
  357. else
  358. ret = ttm_tt_init(&vmw_be->dma_ttm, bo, page_flags,
  359. ttm_cached, 0);
  360. if (unlikely(ret != 0))
  361. goto out_no_init;
  362. return &vmw_be->dma_ttm;
  363. out_no_init:
  364. kfree(vmw_be);
  365. return NULL;
  366. }
  367. static void vmw_evict_flags(struct ttm_buffer_object *bo,
  368. struct ttm_placement *placement)
  369. {
  370. *placement = vmw_sys_placement;
  371. }
  372. static int vmw_ttm_io_mem_reserve(struct ttm_device *bdev, struct ttm_resource *mem)
  373. {
  374. struct vmw_private *dev_priv = vmw_priv_from_ttm(bdev);
  375. switch (mem->mem_type) {
  376. case TTM_PL_SYSTEM:
  377. case VMW_PL_SYSTEM:
  378. case VMW_PL_GMR:
  379. case VMW_PL_MOB:
  380. return 0;
  381. case TTM_PL_VRAM:
  382. mem->bus.offset = (mem->start << PAGE_SHIFT) +
  383. dev_priv->vram_start;
  384. mem->bus.is_iomem = true;
  385. mem->bus.caching = ttm_cached;
  386. break;
  387. default:
  388. return -EINVAL;
  389. }
  390. return 0;
  391. }
  392. /**
  393. * vmw_move_notify - TTM move_notify_callback
  394. *
  395. * @bo: The TTM buffer object about to move.
  396. * @old_mem: The old memory where we move from
  397. * @new_mem: The struct ttm_resource indicating to what memory
  398. * region the move is taking place.
  399. *
  400. * Calls move_notify for all subsystems needing it.
  401. * (currently only resources).
  402. */
  403. static void vmw_move_notify(struct ttm_buffer_object *bo,
  404. struct ttm_resource *old_mem,
  405. struct ttm_resource *new_mem)
  406. {
  407. vmw_bo_move_notify(bo, new_mem);
  408. vmw_query_move_notify(bo, old_mem, new_mem);
  409. }
  410. /**
  411. * vmw_swap_notify - TTM move_notify_callback
  412. *
  413. * @bo: The TTM buffer object about to be swapped out.
  414. */
  415. static void vmw_swap_notify(struct ttm_buffer_object *bo)
  416. {
  417. vmw_bo_swap_notify(bo);
  418. (void) ttm_bo_wait(bo, false, false);
  419. }
  420. static bool vmw_memtype_is_system(uint32_t mem_type)
  421. {
  422. return mem_type == TTM_PL_SYSTEM || mem_type == VMW_PL_SYSTEM;
  423. }
  424. static int vmw_move(struct ttm_buffer_object *bo,
  425. bool evict,
  426. struct ttm_operation_ctx *ctx,
  427. struct ttm_resource *new_mem,
  428. struct ttm_place *hop)
  429. {
  430. struct ttm_resource_manager *new_man;
  431. struct ttm_resource_manager *old_man = NULL;
  432. int ret = 0;
  433. new_man = ttm_manager_type(bo->bdev, new_mem->mem_type);
  434. if (bo->resource)
  435. old_man = ttm_manager_type(bo->bdev, bo->resource->mem_type);
  436. if (new_man->use_tt && !vmw_memtype_is_system(new_mem->mem_type)) {
  437. ret = vmw_ttm_bind(bo->bdev, bo->ttm, new_mem);
  438. if (ret)
  439. return ret;
  440. }
  441. if (!bo->resource || (bo->resource->mem_type == TTM_PL_SYSTEM &&
  442. bo->ttm == NULL)) {
  443. ttm_bo_move_null(bo, new_mem);
  444. return 0;
  445. }
  446. vmw_move_notify(bo, bo->resource, new_mem);
  447. if (old_man && old_man->use_tt && new_man->use_tt) {
  448. if (vmw_memtype_is_system(bo->resource->mem_type)) {
  449. ttm_bo_move_null(bo, new_mem);
  450. return 0;
  451. }
  452. ret = ttm_bo_wait_ctx(bo, ctx);
  453. if (ret)
  454. goto fail;
  455. vmw_ttm_unbind(bo->bdev, bo->ttm);
  456. ttm_resource_free(bo, &bo->resource);
  457. ttm_bo_assign_mem(bo, new_mem);
  458. return 0;
  459. } else {
  460. ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
  461. if (ret)
  462. goto fail;
  463. }
  464. return 0;
  465. fail:
  466. vmw_move_notify(bo, new_mem, bo->resource);
  467. return ret;
  468. }
  469. struct ttm_device_funcs vmw_bo_driver = {
  470. .ttm_tt_create = &vmw_ttm_tt_create,
  471. .ttm_tt_populate = &vmw_ttm_populate,
  472. .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
  473. .ttm_tt_destroy = &vmw_ttm_destroy,
  474. .eviction_valuable = ttm_bo_eviction_valuable,
  475. .evict_flags = vmw_evict_flags,
  476. .move = vmw_move,
  477. .swap_notify = vmw_swap_notify,
  478. .io_mem_reserve = &vmw_ttm_io_mem_reserve,
  479. };
  480. int vmw_bo_create_and_populate(struct vmw_private *dev_priv,
  481. size_t bo_size, u32 domain,
  482. struct vmw_bo **bo_p)
  483. {
  484. struct ttm_operation_ctx ctx = {
  485. .interruptible = false,
  486. .no_wait_gpu = false
  487. };
  488. struct vmw_bo *vbo;
  489. int ret;
  490. struct vmw_bo_params bo_params = {
  491. .domain = domain,
  492. .busy_domain = domain,
  493. .bo_type = ttm_bo_type_kernel,
  494. .size = bo_size,
  495. .pin = true,
  496. .keep_resv = true,
  497. };
  498. ret = vmw_bo_create(dev_priv, &bo_params, &vbo);
  499. if (unlikely(ret != 0))
  500. return ret;
  501. ret = vmw_ttm_populate(vbo->tbo.bdev, vbo->tbo.ttm, &ctx);
  502. if (likely(ret == 0)) {
  503. struct vmw_ttm_tt *vmw_tt =
  504. container_of(vbo->tbo.ttm, struct vmw_ttm_tt, dma_ttm);
  505. ret = vmw_ttm_map_dma(vmw_tt);
  506. }
  507. ttm_bo_unreserve(&vbo->tbo);
  508. if (likely(ret == 0))
  509. *bo_p = vbo;
  510. return ret;
  511. }