vmwgfx_buffer.c 18 KB

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