vmwgfx_buffer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 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 struct ttm_place vram_placement_flags = {
  32. .fpfn = 0,
  33. .lpfn = 0,
  34. .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
  35. };
  36. static struct ttm_place vram_ne_placement_flags = {
  37. .fpfn = 0,
  38. .lpfn = 0,
  39. .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
  40. };
  41. static struct ttm_place sys_placement_flags = {
  42. .fpfn = 0,
  43. .lpfn = 0,
  44. .flags = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED
  45. };
  46. static struct ttm_place sys_ne_placement_flags = {
  47. .fpfn = 0,
  48. .lpfn = 0,
  49. .flags = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
  50. };
  51. static struct ttm_place gmr_placement_flags = {
  52. .fpfn = 0,
  53. .lpfn = 0,
  54. .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  55. };
  56. static struct ttm_place gmr_ne_placement_flags = {
  57. .fpfn = 0,
  58. .lpfn = 0,
  59. .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
  60. };
  61. static struct ttm_place mob_placement_flags = {
  62. .fpfn = 0,
  63. .lpfn = 0,
  64. .flags = VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED
  65. };
  66. static struct ttm_place mob_ne_placement_flags = {
  67. .fpfn = 0,
  68. .lpfn = 0,
  69. .flags = VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
  70. };
  71. struct ttm_placement vmw_vram_placement = {
  72. .num_placement = 1,
  73. .placement = &vram_placement_flags,
  74. .num_busy_placement = 1,
  75. .busy_placement = &vram_placement_flags
  76. };
  77. static struct ttm_place vram_gmr_placement_flags[] = {
  78. {
  79. .fpfn = 0,
  80. .lpfn = 0,
  81. .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
  82. }, {
  83. .fpfn = 0,
  84. .lpfn = 0,
  85. .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  86. }
  87. };
  88. static struct ttm_place gmr_vram_placement_flags[] = {
  89. {
  90. .fpfn = 0,
  91. .lpfn = 0,
  92. .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  93. }, {
  94. .fpfn = 0,
  95. .lpfn = 0,
  96. .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
  97. }
  98. };
  99. struct ttm_placement vmw_vram_gmr_placement = {
  100. .num_placement = 2,
  101. .placement = vram_gmr_placement_flags,
  102. .num_busy_placement = 1,
  103. .busy_placement = &gmr_placement_flags
  104. };
  105. static struct ttm_place vram_gmr_ne_placement_flags[] = {
  106. {
  107. .fpfn = 0,
  108. .lpfn = 0,
  109. .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED |
  110. TTM_PL_FLAG_NO_EVICT
  111. }, {
  112. .fpfn = 0,
  113. .lpfn = 0,
  114. .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED |
  115. TTM_PL_FLAG_NO_EVICT
  116. }
  117. };
  118. struct ttm_placement vmw_vram_gmr_ne_placement = {
  119. .num_placement = 2,
  120. .placement = vram_gmr_ne_placement_flags,
  121. .num_busy_placement = 1,
  122. .busy_placement = &gmr_ne_placement_flags
  123. };
  124. struct ttm_placement vmw_vram_sys_placement = {
  125. .num_placement = 1,
  126. .placement = &vram_placement_flags,
  127. .num_busy_placement = 1,
  128. .busy_placement = &sys_placement_flags
  129. };
  130. struct ttm_placement vmw_vram_ne_placement = {
  131. .num_placement = 1,
  132. .placement = &vram_ne_placement_flags,
  133. .num_busy_placement = 1,
  134. .busy_placement = &vram_ne_placement_flags
  135. };
  136. struct ttm_placement vmw_sys_placement = {
  137. .num_placement = 1,
  138. .placement = &sys_placement_flags,
  139. .num_busy_placement = 1,
  140. .busy_placement = &sys_placement_flags
  141. };
  142. struct ttm_placement vmw_sys_ne_placement = {
  143. .num_placement = 1,
  144. .placement = &sys_ne_placement_flags,
  145. .num_busy_placement = 1,
  146. .busy_placement = &sys_ne_placement_flags
  147. };
  148. static struct ttm_place evictable_placement_flags[] = {
  149. {
  150. .fpfn = 0,
  151. .lpfn = 0,
  152. .flags = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED
  153. }, {
  154. .fpfn = 0,
  155. .lpfn = 0,
  156. .flags = TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
  157. }, {
  158. .fpfn = 0,
  159. .lpfn = 0,
  160. .flags = VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
  161. }, {
  162. .fpfn = 0,
  163. .lpfn = 0,
  164. .flags = VMW_PL_FLAG_MOB | TTM_PL_FLAG_CACHED
  165. }
  166. };
  167. struct ttm_placement vmw_evictable_placement = {
  168. .num_placement = 4,
  169. .placement = evictable_placement_flags,
  170. .num_busy_placement = 1,
  171. .busy_placement = &sys_placement_flags
  172. };
  173. struct ttm_placement vmw_srf_placement = {
  174. .num_placement = 1,
  175. .num_busy_placement = 2,
  176. .placement = &gmr_placement_flags,
  177. .busy_placement = gmr_vram_placement_flags
  178. };
  179. struct ttm_placement vmw_mob_placement = {
  180. .num_placement = 1,
  181. .num_busy_placement = 1,
  182. .placement = &mob_placement_flags,
  183. .busy_placement = &mob_placement_flags
  184. };
  185. struct ttm_placement vmw_mob_ne_placement = {
  186. .num_placement = 1,
  187. .num_busy_placement = 1,
  188. .placement = &mob_ne_placement_flags,
  189. .busy_placement = &mob_ne_placement_flags
  190. };
  191. struct vmw_ttm_tt {
  192. struct ttm_dma_tt dma_ttm;
  193. struct vmw_private *dev_priv;
  194. int gmr_id;
  195. struct vmw_mob *mob;
  196. int mem_type;
  197. struct sg_table sgt;
  198. struct vmw_sg_table vsgt;
  199. uint64_t sg_alloc_size;
  200. bool mapped;
  201. };
  202. const size_t vmw_tt_size = sizeof(struct vmw_ttm_tt);
  203. /**
  204. * Helper functions to advance a struct vmw_piter iterator.
  205. *
  206. * @viter: Pointer to the iterator.
  207. *
  208. * These functions return false if past the end of the list,
  209. * true otherwise. Functions are selected depending on the current
  210. * DMA mapping mode.
  211. */
  212. static bool __vmw_piter_non_sg_next(struct vmw_piter *viter)
  213. {
  214. return ++(viter->i) < viter->num_pages;
  215. }
  216. static bool __vmw_piter_sg_next(struct vmw_piter *viter)
  217. {
  218. return __sg_page_iter_next(&viter->iter);
  219. }
  220. /**
  221. * Helper functions to return a pointer to the current page.
  222. *
  223. * @viter: Pointer to the iterator
  224. *
  225. * These functions return a pointer to the page currently
  226. * pointed to by @viter. Functions are selected depending on the
  227. * current mapping mode.
  228. */
  229. static struct page *__vmw_piter_non_sg_page(struct vmw_piter *viter)
  230. {
  231. return viter->pages[viter->i];
  232. }
  233. static struct page *__vmw_piter_sg_page(struct vmw_piter *viter)
  234. {
  235. return sg_page_iter_page(&viter->iter);
  236. }
  237. /**
  238. * Helper functions to return the DMA address of the current page.
  239. *
  240. * @viter: Pointer to the iterator
  241. *
  242. * These functions return the DMA address of the page currently
  243. * pointed to by @viter. Functions are selected depending on the
  244. * current mapping mode.
  245. */
  246. static dma_addr_t __vmw_piter_phys_addr(struct vmw_piter *viter)
  247. {
  248. return page_to_phys(viter->pages[viter->i]);
  249. }
  250. static dma_addr_t __vmw_piter_dma_addr(struct vmw_piter *viter)
  251. {
  252. return viter->addrs[viter->i];
  253. }
  254. static dma_addr_t __vmw_piter_sg_addr(struct vmw_piter *viter)
  255. {
  256. return sg_page_iter_dma_address(&viter->iter);
  257. }
  258. /**
  259. * vmw_piter_start - Initialize a struct vmw_piter.
  260. *
  261. * @viter: Pointer to the iterator to initialize
  262. * @vsgt: Pointer to a struct vmw_sg_table to initialize from
  263. *
  264. * Note that we're following the convention of __sg_page_iter_start, so that
  265. * the iterator doesn't point to a valid page after initialization; it has
  266. * to be advanced one step first.
  267. */
  268. void vmw_piter_start(struct vmw_piter *viter, const struct vmw_sg_table *vsgt,
  269. unsigned long p_offset)
  270. {
  271. viter->i = p_offset - 1;
  272. viter->num_pages = vsgt->num_pages;
  273. switch (vsgt->mode) {
  274. case vmw_dma_phys:
  275. viter->next = &__vmw_piter_non_sg_next;
  276. viter->dma_address = &__vmw_piter_phys_addr;
  277. viter->page = &__vmw_piter_non_sg_page;
  278. viter->pages = vsgt->pages;
  279. break;
  280. case vmw_dma_alloc_coherent:
  281. viter->next = &__vmw_piter_non_sg_next;
  282. viter->dma_address = &__vmw_piter_dma_addr;
  283. viter->page = &__vmw_piter_non_sg_page;
  284. viter->addrs = vsgt->addrs;
  285. viter->pages = vsgt->pages;
  286. break;
  287. case vmw_dma_map_populate:
  288. case vmw_dma_map_bind:
  289. viter->next = &__vmw_piter_sg_next;
  290. viter->dma_address = &__vmw_piter_sg_addr;
  291. viter->page = &__vmw_piter_sg_page;
  292. __sg_page_iter_start(&viter->iter, vsgt->sgt->sgl,
  293. vsgt->sgt->orig_nents, p_offset);
  294. break;
  295. default:
  296. BUG();
  297. }
  298. }
  299. /**
  300. * vmw_ttm_unmap_from_dma - unmap device addresses previsouly mapped for
  301. * TTM pages
  302. *
  303. * @vmw_tt: Pointer to a struct vmw_ttm_backend
  304. *
  305. * Used to free dma mappings previously mapped by vmw_ttm_map_for_dma.
  306. */
  307. static void vmw_ttm_unmap_from_dma(struct vmw_ttm_tt *vmw_tt)
  308. {
  309. struct device *dev = vmw_tt->dev_priv->dev->dev;
  310. dma_unmap_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.nents,
  311. DMA_BIDIRECTIONAL);
  312. vmw_tt->sgt.nents = vmw_tt->sgt.orig_nents;
  313. }
  314. /**
  315. * vmw_ttm_map_for_dma - map TTM pages to get device addresses
  316. *
  317. * @vmw_tt: Pointer to a struct vmw_ttm_backend
  318. *
  319. * This function is used to get device addresses from the kernel DMA layer.
  320. * However, it's violating the DMA API in that when this operation has been
  321. * performed, it's illegal for the CPU to write to the pages without first
  322. * unmapping the DMA mappings, or calling dma_sync_sg_for_cpu(). It is
  323. * therefore only legal to call this function if we know that the function
  324. * dma_sync_sg_for_cpu() is a NOP, and dma_sync_sg_for_device() is at most
  325. * a CPU write buffer flush.
  326. */
  327. static int vmw_ttm_map_for_dma(struct vmw_ttm_tt *vmw_tt)
  328. {
  329. struct device *dev = vmw_tt->dev_priv->dev->dev;
  330. int ret;
  331. ret = dma_map_sg(dev, vmw_tt->sgt.sgl, vmw_tt->sgt.orig_nents,
  332. DMA_BIDIRECTIONAL);
  333. if (unlikely(ret == 0))
  334. return -ENOMEM;
  335. vmw_tt->sgt.nents = ret;
  336. return 0;
  337. }
  338. /**
  339. * vmw_ttm_map_dma - Make sure TTM pages are visible to the device
  340. *
  341. * @vmw_tt: Pointer to a struct vmw_ttm_tt
  342. *
  343. * Select the correct function for and make sure the TTM pages are
  344. * visible to the device. Allocate storage for the device mappings.
  345. * If a mapping has already been performed, indicated by the storage
  346. * pointer being non NULL, the function returns success.
  347. */
  348. static int vmw_ttm_map_dma(struct vmw_ttm_tt *vmw_tt)
  349. {
  350. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  351. struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
  352. struct vmw_sg_table *vsgt = &vmw_tt->vsgt;
  353. struct vmw_piter iter;
  354. dma_addr_t old;
  355. int ret = 0;
  356. static size_t sgl_size;
  357. static size_t sgt_size;
  358. if (vmw_tt->mapped)
  359. return 0;
  360. vsgt->mode = dev_priv->map_mode;
  361. vsgt->pages = vmw_tt->dma_ttm.ttm.pages;
  362. vsgt->num_pages = vmw_tt->dma_ttm.ttm.num_pages;
  363. vsgt->addrs = vmw_tt->dma_ttm.dma_address;
  364. vsgt->sgt = &vmw_tt->sgt;
  365. switch (dev_priv->map_mode) {
  366. case vmw_dma_map_bind:
  367. case vmw_dma_map_populate:
  368. if (unlikely(!sgl_size)) {
  369. sgl_size = ttm_round_pot(sizeof(struct scatterlist));
  370. sgt_size = ttm_round_pot(sizeof(struct sg_table));
  371. }
  372. vmw_tt->sg_alloc_size = sgt_size + sgl_size * vsgt->num_pages;
  373. ret = ttm_mem_global_alloc(glob, vmw_tt->sg_alloc_size, false,
  374. true);
  375. if (unlikely(ret != 0))
  376. return ret;
  377. ret = sg_alloc_table_from_pages(&vmw_tt->sgt, vsgt->pages,
  378. vsgt->num_pages, 0,
  379. (unsigned long)
  380. vsgt->num_pages << PAGE_SHIFT,
  381. GFP_KERNEL);
  382. if (unlikely(ret != 0))
  383. goto out_sg_alloc_fail;
  384. if (vsgt->num_pages > vmw_tt->sgt.nents) {
  385. uint64_t over_alloc =
  386. sgl_size * (vsgt->num_pages -
  387. vmw_tt->sgt.nents);
  388. ttm_mem_global_free(glob, over_alloc);
  389. vmw_tt->sg_alloc_size -= over_alloc;
  390. }
  391. ret = vmw_ttm_map_for_dma(vmw_tt);
  392. if (unlikely(ret != 0))
  393. goto out_map_fail;
  394. break;
  395. default:
  396. break;
  397. }
  398. old = ~((dma_addr_t) 0);
  399. vmw_tt->vsgt.num_regions = 0;
  400. for (vmw_piter_start(&iter, vsgt, 0); vmw_piter_next(&iter);) {
  401. dma_addr_t cur = vmw_piter_dma_addr(&iter);
  402. if (cur != old + PAGE_SIZE)
  403. vmw_tt->vsgt.num_regions++;
  404. old = cur;
  405. }
  406. vmw_tt->mapped = true;
  407. return 0;
  408. out_map_fail:
  409. sg_free_table(vmw_tt->vsgt.sgt);
  410. vmw_tt->vsgt.sgt = NULL;
  411. out_sg_alloc_fail:
  412. ttm_mem_global_free(glob, vmw_tt->sg_alloc_size);
  413. return ret;
  414. }
  415. /**
  416. * vmw_ttm_unmap_dma - Tear down any TTM page device mappings
  417. *
  418. * @vmw_tt: Pointer to a struct vmw_ttm_tt
  419. *
  420. * Tear down any previously set up device DMA mappings and free
  421. * any storage space allocated for them. If there are no mappings set up,
  422. * this function is a NOP.
  423. */
  424. static void vmw_ttm_unmap_dma(struct vmw_ttm_tt *vmw_tt)
  425. {
  426. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  427. if (!vmw_tt->vsgt.sgt)
  428. return;
  429. switch (dev_priv->map_mode) {
  430. case vmw_dma_map_bind:
  431. case vmw_dma_map_populate:
  432. vmw_ttm_unmap_from_dma(vmw_tt);
  433. sg_free_table(vmw_tt->vsgt.sgt);
  434. vmw_tt->vsgt.sgt = NULL;
  435. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  436. vmw_tt->sg_alloc_size);
  437. break;
  438. default:
  439. break;
  440. }
  441. vmw_tt->mapped = false;
  442. }
  443. /**
  444. * vmw_bo_map_dma - Make sure buffer object pages are visible to the device
  445. *
  446. * @bo: Pointer to a struct ttm_buffer_object
  447. *
  448. * Wrapper around vmw_ttm_map_dma, that takes a TTM buffer object pointer
  449. * instead of a pointer to a struct vmw_ttm_backend as argument.
  450. * Note that the buffer object must be either pinned or reserved before
  451. * calling this function.
  452. */
  453. int vmw_bo_map_dma(struct ttm_buffer_object *bo)
  454. {
  455. struct vmw_ttm_tt *vmw_tt =
  456. container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  457. return vmw_ttm_map_dma(vmw_tt);
  458. }
  459. /**
  460. * vmw_bo_unmap_dma - Make sure buffer object pages are visible to the device
  461. *
  462. * @bo: Pointer to a struct ttm_buffer_object
  463. *
  464. * Wrapper around vmw_ttm_unmap_dma, that takes a TTM buffer object pointer
  465. * instead of a pointer to a struct vmw_ttm_backend as argument.
  466. */
  467. void vmw_bo_unmap_dma(struct ttm_buffer_object *bo)
  468. {
  469. struct vmw_ttm_tt *vmw_tt =
  470. container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  471. vmw_ttm_unmap_dma(vmw_tt);
  472. }
  473. /**
  474. * vmw_bo_sg_table - Return a struct vmw_sg_table object for a
  475. * TTM buffer object
  476. *
  477. * @bo: Pointer to a struct ttm_buffer_object
  478. *
  479. * Returns a pointer to a struct vmw_sg_table object. The object should
  480. * not be freed after use.
  481. * Note that for the device addresses to be valid, the buffer object must
  482. * either be reserved or pinned.
  483. */
  484. const struct vmw_sg_table *vmw_bo_sg_table(struct ttm_buffer_object *bo)
  485. {
  486. struct vmw_ttm_tt *vmw_tt =
  487. container_of(bo->ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  488. return &vmw_tt->vsgt;
  489. }
  490. static int vmw_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
  491. {
  492. struct vmw_ttm_tt *vmw_be =
  493. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  494. int ret;
  495. ret = vmw_ttm_map_dma(vmw_be);
  496. if (unlikely(ret != 0))
  497. return ret;
  498. vmw_be->gmr_id = bo_mem->start;
  499. vmw_be->mem_type = bo_mem->mem_type;
  500. switch (bo_mem->mem_type) {
  501. case VMW_PL_GMR:
  502. return vmw_gmr_bind(vmw_be->dev_priv, &vmw_be->vsgt,
  503. ttm->num_pages, vmw_be->gmr_id);
  504. case VMW_PL_MOB:
  505. if (unlikely(vmw_be->mob == NULL)) {
  506. vmw_be->mob =
  507. vmw_mob_create(ttm->num_pages);
  508. if (unlikely(vmw_be->mob == NULL))
  509. return -ENOMEM;
  510. }
  511. return vmw_mob_bind(vmw_be->dev_priv, vmw_be->mob,
  512. &vmw_be->vsgt, ttm->num_pages,
  513. vmw_be->gmr_id);
  514. default:
  515. BUG();
  516. }
  517. return 0;
  518. }
  519. static int vmw_ttm_unbind(struct ttm_tt *ttm)
  520. {
  521. struct vmw_ttm_tt *vmw_be =
  522. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  523. switch (vmw_be->mem_type) {
  524. case VMW_PL_GMR:
  525. vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
  526. break;
  527. case VMW_PL_MOB:
  528. vmw_mob_unbind(vmw_be->dev_priv, vmw_be->mob);
  529. break;
  530. default:
  531. BUG();
  532. }
  533. if (vmw_be->dev_priv->map_mode == vmw_dma_map_bind)
  534. vmw_ttm_unmap_dma(vmw_be);
  535. return 0;
  536. }
  537. static void vmw_ttm_destroy(struct ttm_tt *ttm)
  538. {
  539. struct vmw_ttm_tt *vmw_be =
  540. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  541. vmw_ttm_unmap_dma(vmw_be);
  542. if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
  543. ttm_dma_tt_fini(&vmw_be->dma_ttm);
  544. else
  545. ttm_tt_fini(ttm);
  546. if (vmw_be->mob)
  547. vmw_mob_destroy(vmw_be->mob);
  548. kfree(vmw_be);
  549. }
  550. static int vmw_ttm_populate(struct ttm_tt *ttm)
  551. {
  552. struct vmw_ttm_tt *vmw_tt =
  553. container_of(ttm, struct vmw_ttm_tt, dma_ttm.ttm);
  554. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  555. struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
  556. int ret;
  557. if (ttm->state != tt_unpopulated)
  558. return 0;
  559. if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
  560. size_t size =
  561. ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
  562. ret = ttm_mem_global_alloc(glob, size, false, true);
  563. if (unlikely(ret != 0))
  564. return ret;
  565. ret = ttm_dma_populate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
  566. if (unlikely(ret != 0))
  567. ttm_mem_global_free(glob, size);
  568. } else
  569. ret = ttm_pool_populate(ttm);
  570. return ret;
  571. }
  572. static void vmw_ttm_unpopulate(struct ttm_tt *ttm)
  573. {
  574. struct vmw_ttm_tt *vmw_tt = container_of(ttm, struct vmw_ttm_tt,
  575. dma_ttm.ttm);
  576. struct vmw_private *dev_priv = vmw_tt->dev_priv;
  577. struct ttm_mem_global *glob = vmw_mem_glob(dev_priv);
  578. if (vmw_tt->mob) {
  579. vmw_mob_destroy(vmw_tt->mob);
  580. vmw_tt->mob = NULL;
  581. }
  582. vmw_ttm_unmap_dma(vmw_tt);
  583. if (dev_priv->map_mode == vmw_dma_alloc_coherent) {
  584. size_t size =
  585. ttm_round_pot(ttm->num_pages * sizeof(dma_addr_t));
  586. ttm_dma_unpopulate(&vmw_tt->dma_ttm, dev_priv->dev->dev);
  587. ttm_mem_global_free(glob, size);
  588. } else
  589. ttm_pool_unpopulate(ttm);
  590. }
  591. static struct ttm_backend_func vmw_ttm_func = {
  592. .bind = vmw_ttm_bind,
  593. .unbind = vmw_ttm_unbind,
  594. .destroy = vmw_ttm_destroy,
  595. };
  596. static struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
  597. unsigned long size, uint32_t page_flags,
  598. struct page *dummy_read_page)
  599. {
  600. struct vmw_ttm_tt *vmw_be;
  601. int ret;
  602. vmw_be = kzalloc(sizeof(*vmw_be), GFP_KERNEL);
  603. if (!vmw_be)
  604. return NULL;
  605. vmw_be->dma_ttm.ttm.func = &vmw_ttm_func;
  606. vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
  607. vmw_be->mob = NULL;
  608. if (vmw_be->dev_priv->map_mode == vmw_dma_alloc_coherent)
  609. ret = ttm_dma_tt_init(&vmw_be->dma_ttm, bdev, size, page_flags,
  610. dummy_read_page);
  611. else
  612. ret = ttm_tt_init(&vmw_be->dma_ttm.ttm, bdev, size, page_flags,
  613. dummy_read_page);
  614. if (unlikely(ret != 0))
  615. goto out_no_init;
  616. return &vmw_be->dma_ttm.ttm;
  617. out_no_init:
  618. kfree(vmw_be);
  619. return NULL;
  620. }
  621. static int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
  622. {
  623. return 0;
  624. }
  625. static int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  626. struct ttm_mem_type_manager *man)
  627. {
  628. switch (type) {
  629. case TTM_PL_SYSTEM:
  630. /* System memory */
  631. man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  632. man->available_caching = TTM_PL_FLAG_CACHED;
  633. man->default_caching = TTM_PL_FLAG_CACHED;
  634. break;
  635. case TTM_PL_VRAM:
  636. /* "On-card" video ram */
  637. man->func = &ttm_bo_manager_func;
  638. man->gpu_offset = 0;
  639. man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
  640. man->available_caching = TTM_PL_FLAG_CACHED;
  641. man->default_caching = TTM_PL_FLAG_CACHED;
  642. break;
  643. case VMW_PL_GMR:
  644. case VMW_PL_MOB:
  645. /*
  646. * "Guest Memory Regions" is an aperture like feature with
  647. * one slot per bo. There is an upper limit of the number of
  648. * slots as well as the bo size.
  649. */
  650. man->func = &vmw_gmrid_manager_func;
  651. man->gpu_offset = 0;
  652. man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
  653. man->available_caching = TTM_PL_FLAG_CACHED;
  654. man->default_caching = TTM_PL_FLAG_CACHED;
  655. break;
  656. default:
  657. DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. static void vmw_evict_flags(struct ttm_buffer_object *bo,
  663. struct ttm_placement *placement)
  664. {
  665. *placement = vmw_sys_placement;
  666. }
  667. static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
  668. {
  669. struct ttm_object_file *tfile =
  670. vmw_fpriv((struct drm_file *)filp->private_data)->tfile;
  671. return vmw_user_dmabuf_verify_access(bo, tfile);
  672. }
  673. static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  674. {
  675. struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
  676. struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
  677. mem->bus.addr = NULL;
  678. mem->bus.is_iomem = false;
  679. mem->bus.offset = 0;
  680. mem->bus.size = mem->num_pages << PAGE_SHIFT;
  681. mem->bus.base = 0;
  682. if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
  683. return -EINVAL;
  684. switch (mem->mem_type) {
  685. case TTM_PL_SYSTEM:
  686. case VMW_PL_GMR:
  687. case VMW_PL_MOB:
  688. return 0;
  689. case TTM_PL_VRAM:
  690. mem->bus.offset = mem->start << PAGE_SHIFT;
  691. mem->bus.base = dev_priv->vram_start;
  692. mem->bus.is_iomem = true;
  693. break;
  694. default:
  695. return -EINVAL;
  696. }
  697. return 0;
  698. }
  699. static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
  700. {
  701. }
  702. static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
  703. {
  704. return 0;
  705. }
  706. /**
  707. * vmw_move_notify - TTM move_notify_callback
  708. *
  709. * @bo: The TTM buffer object about to move.
  710. * @mem: The struct ttm_mem_reg indicating to what memory
  711. * region the move is taking place.
  712. *
  713. * Calls move_notify for all subsystems needing it.
  714. * (currently only resources).
  715. */
  716. static void vmw_move_notify(struct ttm_buffer_object *bo,
  717. struct ttm_mem_reg *mem)
  718. {
  719. vmw_resource_move_notify(bo, mem);
  720. vmw_query_move_notify(bo, mem);
  721. }
  722. /**
  723. * vmw_swap_notify - TTM move_notify_callback
  724. *
  725. * @bo: The TTM buffer object about to be swapped out.
  726. */
  727. static void vmw_swap_notify(struct ttm_buffer_object *bo)
  728. {
  729. ttm_bo_wait(bo, false, false, false);
  730. }
  731. struct ttm_bo_driver vmw_bo_driver = {
  732. .ttm_tt_create = &vmw_ttm_tt_create,
  733. .ttm_tt_populate = &vmw_ttm_populate,
  734. .ttm_tt_unpopulate = &vmw_ttm_unpopulate,
  735. .invalidate_caches = vmw_invalidate_caches,
  736. .init_mem_type = vmw_init_mem_type,
  737. .evict_flags = vmw_evict_flags,
  738. .move = NULL,
  739. .verify_access = vmw_verify_access,
  740. .move_notify = vmw_move_notify,
  741. .swap_notify = vmw_swap_notify,
  742. .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
  743. .io_mem_reserve = &vmw_ttm_io_mem_reserve,
  744. .io_mem_free = &vmw_ttm_io_mem_free,
  745. };