vmwgfx_buffer.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  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_drv.h"
  28. #include <drm/ttm/ttm_bo_driver.h>
  29. #include <drm/ttm/ttm_placement.h>
  30. #include <drm/ttm/ttm_page_alloc.h>
  31. static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
  32. TTM_PL_FLAG_CACHED;
  33. static uint32_t vram_ne_placement_flags = TTM_PL_FLAG_VRAM |
  34. TTM_PL_FLAG_CACHED |
  35. TTM_PL_FLAG_NO_EVICT;
  36. static uint32_t sys_placement_flags = TTM_PL_FLAG_SYSTEM |
  37. TTM_PL_FLAG_CACHED;
  38. static uint32_t gmr_placement_flags = VMW_PL_FLAG_GMR |
  39. TTM_PL_FLAG_CACHED;
  40. static uint32_t gmr_ne_placement_flags = VMW_PL_FLAG_GMR |
  41. TTM_PL_FLAG_CACHED |
  42. TTM_PL_FLAG_NO_EVICT;
  43. struct ttm_placement vmw_vram_placement = {
  44. .fpfn = 0,
  45. .lpfn = 0,
  46. .num_placement = 1,
  47. .placement = &vram_placement_flags,
  48. .num_busy_placement = 1,
  49. .busy_placement = &vram_placement_flags
  50. };
  51. static uint32_t vram_gmr_placement_flags[] = {
  52. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
  53. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  54. };
  55. static uint32_t gmr_vram_placement_flags[] = {
  56. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED,
  57. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
  58. };
  59. struct ttm_placement vmw_vram_gmr_placement = {
  60. .fpfn = 0,
  61. .lpfn = 0,
  62. .num_placement = 2,
  63. .placement = vram_gmr_placement_flags,
  64. .num_busy_placement = 1,
  65. .busy_placement = &gmr_placement_flags
  66. };
  67. static uint32_t vram_gmr_ne_placement_flags[] = {
  68. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT,
  69. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
  70. };
  71. struct ttm_placement vmw_vram_gmr_ne_placement = {
  72. .fpfn = 0,
  73. .lpfn = 0,
  74. .num_placement = 2,
  75. .placement = vram_gmr_ne_placement_flags,
  76. .num_busy_placement = 1,
  77. .busy_placement = &gmr_ne_placement_flags
  78. };
  79. struct ttm_placement vmw_vram_sys_placement = {
  80. .fpfn = 0,
  81. .lpfn = 0,
  82. .num_placement = 1,
  83. .placement = &vram_placement_flags,
  84. .num_busy_placement = 1,
  85. .busy_placement = &sys_placement_flags
  86. };
  87. struct ttm_placement vmw_vram_ne_placement = {
  88. .fpfn = 0,
  89. .lpfn = 0,
  90. .num_placement = 1,
  91. .placement = &vram_ne_placement_flags,
  92. .num_busy_placement = 1,
  93. .busy_placement = &vram_ne_placement_flags
  94. };
  95. struct ttm_placement vmw_sys_placement = {
  96. .fpfn = 0,
  97. .lpfn = 0,
  98. .num_placement = 1,
  99. .placement = &sys_placement_flags,
  100. .num_busy_placement = 1,
  101. .busy_placement = &sys_placement_flags
  102. };
  103. static uint32_t evictable_placement_flags[] = {
  104. TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED,
  105. TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
  106. VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  107. };
  108. struct ttm_placement vmw_evictable_placement = {
  109. .fpfn = 0,
  110. .lpfn = 0,
  111. .num_placement = 3,
  112. .placement = evictable_placement_flags,
  113. .num_busy_placement = 1,
  114. .busy_placement = &sys_placement_flags
  115. };
  116. struct ttm_placement vmw_srf_placement = {
  117. .fpfn = 0,
  118. .lpfn = 0,
  119. .num_placement = 1,
  120. .num_busy_placement = 2,
  121. .placement = &gmr_placement_flags,
  122. .busy_placement = gmr_vram_placement_flags
  123. };
  124. struct vmw_ttm_tt {
  125. struct ttm_dma_tt dma_ttm;
  126. struct vmw_private *dev_priv;
  127. int gmr_id;
  128. struct sg_table sgt;
  129. struct vmw_sg_table vsgt;
  130. uint64_t sg_alloc_size;
  131. bool mapped;
  132. };
  133. /**
  134. * Helper functions to advance a struct vmw_piter iterator.
  135. *
  136. * @viter: Pointer to the iterator.
  137. *
  138. * These functions return false if past the end of the list,
  139. * true otherwise. Functions are selected depending on the current
  140. * DMA mapping mode.
  141. */
  142. static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
  143. {
  144. return ++(viter->i) < viter->num_pages;
  145. }
  146. static bool __vmw_piter_sg_next(struct vmw_piter *viter)
  147. {
  148. return __sg_page_iter_next(&viter->iter);
  149. }
  150. /**
  151. * Helper functions to return a pointer to the current page.
  152. *
  153. * @viter: Pointer to the iterator
  154. *
  155. * These functions return a pointer to the page currently
  156. * pointed to by @viter. Functions are selected depending on the
  157. * current mapping mode.
  158. */
  159. static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
  160. {
  161. return viter->pages[viter->i];
  162. }
  163. static struct page *__vmw_piter_sg_page(struct vmw_piter *viter)
  164. {
  165. return sg_page_iter_page(&viter->iter);
  166. }
  167. /**
  168. * Helper functions to return the DMA address of the current page.
  169. *
  170. * @viter: Pointer to the iterator
  171. *
  172. * These functions return the DMA address of the page currently
  173. * pointed to by @viter. Functions are selected depending on the
  174. * current mapping mode.
  175. */
  176. static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
  177. {
  178. return page_to_phys(viter->pages[viter->i]);
  179. }
  180. static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
  181. {
  182. return viter->addrs[viter->i];
  183. }
  184. static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
  185. {
  186. return sg_page_iter_dma_address(&viter->iter);
  187. }
  188. /**
  189. * vmw_piter_start - Initialize a struct vmw_piter.
  190. *
  191. * @viter: Pointer to the iterator to initialize
  192. * @vsgt: Pointer to a struct vmw_sg_table to initialize from
  193. *
  194. * Note that we're following the convention of __sg_page_iter_start, so that
  195. * the iterator doesn't point to a valid page after initialization; it has
  196. * to be advanced one step first.
  197. */
  198. void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
  199. unsigned long p_offset)
  200. {
  201. viter->i = p_offset - 1;
  202. viter->num_pages = vsgt->num_pages;
  203. switch (vsgt->mode) {
  204. case vmw_dma_phys:
  205. viter->next = &__vmw_piter_non_sg_next;
  206. viter->dma_address = &__vmw_piter_phys_addr;
  207. viter->page = &__vmw_piter_non_sg_page;
  208. viter->pages = vsgt->pages;
  209. break;
  210. case vmw_dma_alloc_coherent:
  211. viter->next = &__vmw_piter_non_sg_next;
  212. viter->dma_address = &__vmw_piter_dma_addr;
  213. viter->page = &__vmw_piter_non_sg_page;
  214. viter->addrs = vsgt->addrs;
  215. break;
  216. case vmw_dma_map_populate:
  217. case vmw_dma_map_bind:
  218. viter->next = &__vmw_piter_sg_next;
  219. viter->dma_address = &__vmw_piter_sg_addr;
  220. viter->page = &__vmw_piter_sg_page;
  221. __sg_page_iter_start(&viter->iter, vsgt->sgt->sgl,
  222. vsgt->sgt->orig_nents, p_offset);
  223. break;
  224. default:
  225. BUG();
  226. }
  227. }
  228. /**
  229. * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
  230. * TTM pages
  231. *
  232. * @vmw_tt: Pointer to a struct vmw_ttm_backend
  233. *
  234. * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
  235. */
  236. static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
  237. {
  238. struct device *dev = vmw_tt->dev_priv->dev->dev;
  239. dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,
  240. DMA_BIDIRECTIONAL);
  241. vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
  242. }
  243. /**
  244. * vmw_ttm_map_for_dma - map TTM pages to get device addresses
  245. *
  246. * @vmw_tt: Pointer to a struct vmw_ttm_backend
  247. *
  248. * This function is used to get device addresses from the kernel DMA layer.
  249. * However, it's violating the DMA API in that when this operation has been
  250. * performed, it's illegal for the CPU to write to the pages without first
  251. * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
  252. * therefore only legal to call this function if we know that the function
  253. * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
  254. * a CPU write buffer flush.
  255. */
  256. static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
  257. {
  258. struct device *dev = vmw_tt->dev_priv->dev->dev;
  259. int ret;
  260. ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,
  261. DMA_BIDIRECTIONAL);
  262. if (unlikely(ret == 0))
  263. return -ENOMEM;
  264. vmw_tt->sgt.nents = ret;
  265. return 0;
  266. }
  267. /**
  268. * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
  269. *
  270. * @vmw_tt: Pointer to a struct vmw_ttm_tt
  271. *
  272. * Select the correct function for and make sure the TTM pages are
  273. * visible to the device. Allocate storage for the device mappings.
  274. * If a mapping has already been performed, indicated by the storage
  275. * pointer being non NULL, the function returns success.
  276. */
  277. static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
  278. {
  279. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  280. struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
  281. struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
  282. struct vmw_piter iter;
  283. dma_addr_t old;
  284. int ret = 0;
  285. static size_t sgl_size;
  286. static size_t sgt_size;
  287. if (vmw_tt->mapped)
  288. return 0;
  289. vsgt->mode = dev_priv->map_mode;
  290. vsgt->pages = vmw_tt->dma_ttm.ttm.pages;
  291. vsgt->num_pages = vmw_tt->dma_ttm.ttm.num_pages;
  292. vsgt->addrs = vmw_tt->dma_ttm.dma_address;
  293. vsgt->sgt = &vmw_tt->sgt;
  294. switch (dev_priv->map_mode) {
  295. case vmw_dma_map_bind:
  296. case vmw_dma_map_populate:
  297. if (unlikely(!sgl_size)) {
  298. sgl_size = ttm_round_pot(sizeof(struct scatterlist));
  299. sgt_size = ttm_round_pot(sizeof(struct sg_table));
  300. }
  301. vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
  302. ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, false,
  303. true);
  304. if (unlikely(ret != 0))
  305. return ret;
  306. ret = sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
  307. vsgt->num_pages, 0,
  308. (unsigned long)
  309. vsgt->num_pages << PAGE_SHIFT,
  310. GFP_KERNEL);
  311. if (unlikely(ret != 0))
  312. goto out_sg_alloc_fail;
  313. if (vsgt->num_pages > vmw_tt->sgt.nents) {
  314. uint64_t over_alloc =
  315. sgl_size * (vsgt->num_pages -
  316. vmw_tt->sgt.nents);
  317. ttm_mem_global_free(glob, over_alloc);
  318. vmw_tt->sg_alloc_size -= over_alloc;
  319. }
  320. ret = vmw_ttm_map_for_dma(vmw_tt);
  321. if (unlikely(ret != 0))
  322. goto out_map_fail;
  323. break;
  324. default:
  325. break;
  326. }
  327. old = ~((dma_addr_t) 0);
  328. vmw_tt->vsgt.num_regions = 0;
  329. for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
  330. dma_addr_t cur = vmw_piter_dma_addr(&iter);
  331. if (cur != old + PAGE_SIZE)
  332. vmw_tt->vsgt.num_regions++;
  333. old = cur;
  334. }
  335. vmw_tt->mapped = true;
  336. return 0;
  337. out_map_fail:
  338. sg_free_table(vmw_tt->vsgt.sgt);
  339. vmw_tt->vsgt.sgt = NULL;
  340. out_sg_alloc_fail:
  341. ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
  342. return ret;
  343. }
  344. /**
  345. * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
  346. *
  347. * @vmw_tt: Pointer to a struct vmw_ttm_tt
  348. *
  349. * Tear down any previously set up device DMA mappings and free
  350. * any storage space allocated for them. If there are no mappings set up,
  351. * this function is a NOP.
  352. */
  353. static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
  354. {
  355. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  356. if (!vmw_tt->vsgt.sgt)
  357. return;
  358. switch (dev_priv->map_mode) {
  359. case vmw_dma_map_bind:
  360. case vmw_dma_map_populate:
  361. vmw_ttm_unmap_from_dma(vmw_tt);
  362. sg_free_table(vmw_tt->vsgt.sgt);
  363. vmw_tt->vsgt.sgt = NULL;
  364. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  365. vmw_tt->sg_alloc_size);
  366. break;
  367. default:
  368. break;
  369. }
  370. vmw_tt->mapped = false;
  371. }
  372. static int vmw_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  373. {
  374. struct vmw_ttm_tt *vmw_be =
  375. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  376. int ret;
  377. ret = vmw_ttm_map_dma(vmw_be);
  378. if (unlikely(ret != 0))
  379. return ret;
  380. vmw_be->gmr_id = bo_mem->start;
  381. return vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
  382. ttm->num_pages, vmw_be->gmr_id);
  383. }
  384. static int vmw_ttm_unbind(struct ttm_tt *ttm)
  385. {
  386. struct vmw_ttm_tt *vmw_be =
  387. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  388. vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
  389. if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
  390. vmw_ttm_unmap_dma(vmw_be);
  391. return 0;
  392. }
  393. static void vmw_ttm_destroy(struct ttm_tt *ttm)
  394. {
  395. struct vmw_ttm_tt *vmw_be =
  396. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  397. vmw_ttm_unmap_dma(vmw_be);
  398. if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
  399. ttm_dma_tt_fini(&vmw_be->dma_ttm);
  400. else
  401. ttm_tt_fini(ttm);
  402. kfree(vmw_be);
  403. }
  404. static int vmw_ttm_populate(struct ttm_tt *ttm)
  405. {
  406. struct vmw_ttm_tt *vmw_tt =
  407. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  408. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  409. struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
  410. int ret;
  411. if (ttm->state != tt_unpopulated)
  412. return 0;
  413. if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
  414. size_t size =
  415. ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
  416. ret = ttm_mem_global_alloc(glob, size, false, true);
  417. if (unlikely(ret != 0))
  418. return ret;
  419. ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
  420. if (unlikely(ret != 0))
  421. ttm_mem_global_free(glob, size);
  422. } else
  423. ret = ttm_pool_populate(ttm);
  424. return ret;
  425. }
  426. static void vmw_ttm_unpopulate(struct ttm_tt *ttm)
  427. {
  428. struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
  429. dma_ttm.ttm);
  430. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  431. struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
  432. vmw_ttm_unmap_dma(vmw_tt);
  433. if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
  434. size_t size =
  435. ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
  436. ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
  437. ttm_mem_global_free(glob, size);
  438. } else
  439. ttm_pool_unpopulate(ttm);
  440. }
  441. static struct ttm_backend_func vmw_ttm_func = {
  442. .bind = vmw_ttm_bind,
  443. .unbind = vmw_ttm_unbind,
  444. .destroy = vmw_ttm_destroy,
  445. };
  446. struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
  447. unsigned long size, uint32_t page_flags,
  448. struct page *dummy_read_page)
  449. {
  450. struct vmw_ttm_tt *vmw_be;
  451. int ret;
  452. vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
  453. if (!vmw_be)
  454. return NULL;
  455. vmw_be->dma_ttm.ttm.func = &vmw_ttm_func;
  456. vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
  457. if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
  458. ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bdev, size, page_flags,
  459. dummy_read_page);
  460. else
  461. ret = ttm_tt_init(&vmw_be->dma_ttm.ttm, bdev, size, page_flags,
  462. dummy_read_page);
  463. if (unlikely(ret != 0))
  464. goto out_no_init;
  465. return &vmw_be->dma_ttm.ttm;
  466. out_no_init:
  467. kfree(vmw_be);
  468. return NULL;
  469. }
  470. int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  471. {
  472. return 0;
  473. }
  474. int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  475. struct ttm_mem_type_manager *man)
  476. {
  477. switch (type) {
  478. case TTM_PL_SYSTEM:
  479. /* System memory */
  480. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  481. man->available_caching = TTM_PL_FLAG_CACHED;
  482. man->default_caching = TTM_PL_FLAG_CACHED;
  483. break;
  484. case TTM_PL_VRAM:
  485. /* "On-card" video ram */
  486. man->func = &ttm_bo_manager_func;
  487. man->gpu_offset = 0;
  488. man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
  489. man->available_caching = TTM_PL_FLAG_CACHED;
  490. man->default_caching = TTM_PL_FLAG_CACHED;
  491. break;
  492. case VMW_PL_GMR:
  493. /*
  494. * "Guest Memory Regions" is an aperture like feature with
  495. * one slot per bo. There is an upper limit of the number of
  496. * slots as well as the bo size.
  497. */
  498. man->func = &vmw_gmrid_manager_func;
  499. man->gpu_offset = 0;
  500. man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
  501. man->available_caching = TTM_PL_FLAG_CACHED;
  502. man->default_caching = TTM_PL_FLAG_CACHED;
  503. break;
  504. default:
  505. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  506. return -EINVAL;
  507. }
  508. return 0;
  509. }
  510. void vmw_evict_flags(struct ttm_buffer_object *bo,
  511. struct ttm_placement *placement)
  512. {
  513. *placement = vmw_sys_placement;
  514. }
  515. static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  516. {
  517. struct ttm_object_file *tfile =
  518. vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
  519. return vmw_user_dmabuf_verify_access(bo, tfile);
  520. }
  521. static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  522. {
  523. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  524. struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
  525. mem->bus.addr = NULL;
  526. mem->bus.is_iomem = false;
  527. mem->bus.offset = 0;
  528. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  529. mem->bus.base = 0;
  530. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  531. return -EINVAL;
  532. switch (mem->mem_type) {
  533. case TTM_PL_SYSTEM:
  534. case VMW_PL_GMR:
  535. return 0;
  536. case TTM_PL_VRAM:
  537. mem->bus.offset = mem->start << PAGE_SHIFT;
  538. mem->bus.base = dev_priv->vram_start;
  539. mem->bus.is_iomem = true;
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. return 0;
  545. }
  546. static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  547. {
  548. }
  549. static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  550. {
  551. return 0;
  552. }
  553. /**
  554. * FIXME: We're using the old vmware polling method to sync.
  555. * Do this with fences instead.
  556. */
  557. static void *vmw_sync_obj_ref(void *sync_obj)
  558. {
  559. return (void *)
  560. vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
  561. }
  562. static void vmw_sync_obj_unref(void **sync_obj)
  563. {
  564. vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
  565. }
  566. static int vmw_sync_obj_flush(void *sync_obj)
  567. {
  568. vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
  569. return 0;
  570. }
  571. static bool vmw_sync_obj_signaled(void *sync_obj)
  572. {
  573. return vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
  574. DRM_VMW_FENCE_FLAG_EXEC);
  575. }
  576. static int vmw_sync_obj_wait(void *sync_obj, bool lazy, bool interruptible)
  577. {
  578. return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
  579. DRM_VMW_FENCE_FLAG_EXEC,
  580. lazy, interruptible,
  581. VMW_FENCE_WAIT_TIMEOUT);
  582. }
  583. struct ttm_bo_driver vmw_bo_driver = {
  584. .ttm_tt_create = &vmw_ttm_tt_create,
  585. .ttm_tt_populate = &vmw_ttm_populate,
  586. .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
  587. .invalidate_caches = vmw_invalidate_caches,
  588. .init_mem_type = vmw_init_mem_type,
  589. .evict_flags = vmw_evict_flags,
  590. .move = NULL,
  591. .verify_access = vmw_verify_access,
  592. .sync_obj_signaled = vmw_sync_obj_signaled,
  593. .sync_obj_wait = vmw_sync_obj_wait,
  594. .sync_obj_flush = vmw_sync_obj_flush,
  595. .sync_obj_unref = vmw_sync_obj_unref,
  596. .sync_obj_ref = vmw_sync_obj_ref,
  597. .move_notify = NULL,
  598. .swap_notify = NULL,
  599. .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
  600. .io_mem_reserve = &vmw_ttm_io_mem_reserve,
  601. .io_mem_free = &vmw_ttm_io_mem_free,
  602. };