vmwgfx_mob.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /**************************************************************************
  2. *
  3. * Copyright © 2012-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. /*
  29. * If we set up the screen target otable, screen objects stop working.
  30. */
  31. #define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE ? 0 : 1))
  32. #ifdef CONFIG_64BIT
  33. #define VMW_PPN_SIZE 8
  34. #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH64_0
  35. #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH64_1
  36. #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH64_2
  37. #else
  38. #define VMW_PPN_SIZE 4
  39. #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH_0
  40. #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH_1
  41. #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH_2
  42. #endif
  43. /*
  44. * struct vmw_mob - Structure containing page table and metadata for a
  45. * Guest Memory OBject.
  46. *
  47. * @num_pages Number of pages that make up the page table.
  48. * @pt_level The indirection level of the page table. 0-2.
  49. * @pt_root_page DMA address of the level 0 page of the page table.
  50. */
  51. struct vmw_mob {
  52. struct ttm_buffer_object *pt_bo;
  53. unsigned long num_pages;
  54. unsigned pt_level;
  55. dma_addr_t pt_root_page;
  56. uint32_t id;
  57. };
  58. /*
  59. * struct vmw_otable - Guest Memory OBject table metadata
  60. *
  61. * @size: Size of the table (page-aligned).
  62. * @page_table: Pointer to a struct vmw_mob holding the page table.
  63. */
  64. static const struct vmw_otable pre_dx_tables[] = {
  65. {VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE, NULL, true},
  66. {VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, NULL, true},
  67. {VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE, NULL, true},
  68. {VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE, NULL, true},
  69. {VMWGFX_NUM_GB_SCREEN_TARGET * SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE,
  70. NULL, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE}
  71. };
  72. static const struct vmw_otable dx_tables[] = {
  73. {VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE, NULL, true},
  74. {VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, NULL, true},
  75. {VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE, NULL, true},
  76. {VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE, NULL, true},
  77. {VMWGFX_NUM_GB_SCREEN_TARGET * SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE,
  78. NULL, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE},
  79. {VMWGFX_NUM_DXCONTEXT * sizeof(SVGAOTableDXContextEntry), NULL, true},
  80. };
  81. static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
  82. struct vmw_mob *mob);
  83. static void vmw_mob_pt_setup(struct vmw_mob *mob,
  84. struct vmw_piter data_iter,
  85. unsigned long num_data_pages);
  86. /*
  87. * vmw_setup_otable_base - Issue an object table base setup command to
  88. * the device
  89. *
  90. * @dev_priv: Pointer to a device private structure
  91. * @type: Type of object table base
  92. * @offset Start of table offset into dev_priv::otable_bo
  93. * @otable Pointer to otable metadata;
  94. *
  95. * This function returns -ENOMEM if it fails to reserve fifo space,
  96. * and may block waiting for fifo space.
  97. */
  98. static int vmw_setup_otable_base(struct vmw_private *dev_priv,
  99. SVGAOTableType type,
  100. struct ttm_buffer_object *otable_bo,
  101. unsigned long offset,
  102. struct vmw_otable *otable)
  103. {
  104. struct {
  105. SVGA3dCmdHeader header;
  106. SVGA3dCmdSetOTableBase64 body;
  107. } *cmd;
  108. struct vmw_mob *mob;
  109. const struct vmw_sg_table *vsgt;
  110. struct vmw_piter iter;
  111. int ret;
  112. BUG_ON(otable->page_table != NULL);
  113. vsgt = vmw_bo_sg_table(otable_bo);
  114. vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
  115. WARN_ON(!vmw_piter_next(&iter));
  116. mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
  117. if (unlikely(mob == NULL)) {
  118. DRM_ERROR("Failed creating OTable page table.\n");
  119. return -ENOMEM;
  120. }
  121. if (otable->size <= PAGE_SIZE) {
  122. mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
  123. mob->pt_root_page = vmw_piter_dma_addr(&iter);
  124. } else if (vsgt->num_regions == 1) {
  125. mob->pt_level = SVGA3D_MOBFMT_RANGE;
  126. mob->pt_root_page = vmw_piter_dma_addr(&iter);
  127. } else {
  128. ret = vmw_mob_pt_populate(dev_priv, mob);
  129. if (unlikely(ret != 0))
  130. goto out_no_populate;
  131. vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
  132. mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
  133. }
  134. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  135. if (unlikely(cmd == NULL)) {
  136. DRM_ERROR("Failed reserving FIFO space for OTable setup.\n");
  137. ret = -ENOMEM;
  138. goto out_no_fifo;
  139. }
  140. memset(cmd, 0, sizeof(*cmd));
  141. cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
  142. cmd->header.size = sizeof(cmd->body);
  143. cmd->body.type = type;
  144. cmd->body.baseAddress = mob->pt_root_page >> PAGE_SHIFT;
  145. cmd->body.sizeInBytes = otable->size;
  146. cmd->body.validSizeInBytes = 0;
  147. cmd->body.ptDepth = mob->pt_level;
  148. /*
  149. * The device doesn't support this, But the otable size is
  150. * determined at compile-time, so this BUG shouldn't trigger
  151. * randomly.
  152. */
  153. BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);
  154. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  155. otable->page_table = mob;
  156. return 0;
  157. out_no_fifo:
  158. out_no_populate:
  159. vmw_mob_destroy(mob);
  160. return ret;
  161. }
  162. /*
  163. * vmw_takedown_otable_base - Issue an object table base takedown command
  164. * to the device
  165. *
  166. * @dev_priv: Pointer to a device private structure
  167. * @type: Type of object table base
  168. *
  169. */
  170. static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
  171. SVGAOTableType type,
  172. struct vmw_otable *otable)
  173. {
  174. struct {
  175. SVGA3dCmdHeader header;
  176. SVGA3dCmdSetOTableBase body;
  177. } *cmd;
  178. struct ttm_buffer_object *bo;
  179. if (otable->page_table == NULL)
  180. return;
  181. bo = otable->page_table->pt_bo;
  182. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  183. if (unlikely(cmd == NULL)) {
  184. DRM_ERROR("Failed reserving FIFO space for OTable "
  185. "takedown.\n");
  186. return;
  187. }
  188. memset(cmd, 0, sizeof(*cmd));
  189. cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE;
  190. cmd->header.size = sizeof(cmd->body);
  191. cmd->body.type = type;
  192. cmd->body.baseAddress = 0;
  193. cmd->body.sizeInBytes = 0;
  194. cmd->body.validSizeInBytes = 0;
  195. cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID;
  196. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  197. if (bo) {
  198. int ret;
  199. ret = ttm_bo_reserve(bo, false, true, NULL);
  200. BUG_ON(ret != 0);
  201. vmw_fence_single_bo(bo, NULL);
  202. ttm_bo_unreserve(bo);
  203. }
  204. vmw_mob_destroy(otable->page_table);
  205. otable->page_table = NULL;
  206. }
  207. static int vmw_otable_batch_setup(struct vmw_private *dev_priv,
  208. struct vmw_otable_batch *batch)
  209. {
  210. unsigned long offset;
  211. unsigned long bo_size;
  212. struct vmw_otable *otables = batch->otables;
  213. SVGAOTableType i;
  214. int ret;
  215. bo_size = 0;
  216. for (i = 0; i < batch->num_otables; ++i) {
  217. if (!otables[i].enabled)
  218. continue;
  219. otables[i].size =
  220. (otables[i].size + PAGE_SIZE - 1) & PAGE_MASK;
  221. bo_size += otables[i].size;
  222. }
  223. ret = ttm_bo_create(&dev_priv->bdev, bo_size,
  224. ttm_bo_type_device,
  225. &vmw_sys_ne_placement,
  226. 0, false, NULL,
  227. &batch->otable_bo);
  228. if (unlikely(ret != 0))
  229. goto out_no_bo;
  230. ret = ttm_bo_reserve(batch->otable_bo, false, true, NULL);
  231. BUG_ON(ret != 0);
  232. ret = vmw_bo_driver.ttm_tt_populate(batch->otable_bo->ttm);
  233. if (unlikely(ret != 0))
  234. goto out_unreserve;
  235. ret = vmw_bo_map_dma(batch->otable_bo);
  236. if (unlikely(ret != 0))
  237. goto out_unreserve;
  238. ttm_bo_unreserve(batch->otable_bo);
  239. offset = 0;
  240. for (i = 0; i < batch->num_otables; ++i) {
  241. if (!batch->otables[i].enabled)
  242. continue;
  243. ret = vmw_setup_otable_base(dev_priv, i, batch->otable_bo,
  244. offset,
  245. &otables[i]);
  246. if (unlikely(ret != 0))
  247. goto out_no_setup;
  248. offset += otables[i].size;
  249. }
  250. return 0;
  251. out_unreserve:
  252. ttm_bo_unreserve(batch->otable_bo);
  253. out_no_setup:
  254. for (i = 0; i < batch->num_otables; ++i) {
  255. if (batch->otables[i].enabled)
  256. vmw_takedown_otable_base(dev_priv, i,
  257. &batch->otables[i]);
  258. }
  259. ttm_bo_unref(&batch->otable_bo);
  260. out_no_bo:
  261. return ret;
  262. }
  263. /*
  264. * vmw_otables_setup - Set up guest backed memory object tables
  265. *
  266. * @dev_priv: Pointer to a device private structure
  267. *
  268. * Takes care of the device guest backed surface
  269. * initialization, by setting up the guest backed memory object tables.
  270. * Returns 0 on success and various error codes on failure. A successful return
  271. * means the object tables can be taken down using the vmw_otables_takedown
  272. * function.
  273. */
  274. int vmw_otables_setup(struct vmw_private *dev_priv)
  275. {
  276. struct vmw_otable **otables = &dev_priv->otable_batch.otables;
  277. int ret;
  278. if (dev_priv->has_dx) {
  279. *otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
  280. if (!(*otables))
  281. return -ENOMEM;
  282. dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
  283. } else {
  284. *otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
  285. GFP_KERNEL);
  286. if (!(*otables))
  287. return -ENOMEM;
  288. dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
  289. }
  290. ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
  291. if (unlikely(ret != 0))
  292. goto out_setup;
  293. return 0;
  294. out_setup:
  295. kfree(*otables);
  296. return ret;
  297. }
  298. static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
  299. struct vmw_otable_batch *batch)
  300. {
  301. SVGAOTableType i;
  302. struct ttm_buffer_object *bo = batch->otable_bo;
  303. int ret;
  304. for (i = 0; i < batch->num_otables; ++i)
  305. if (batch->otables[i].enabled)
  306. vmw_takedown_otable_base(dev_priv, i,
  307. &batch->otables[i]);
  308. ret = ttm_bo_reserve(bo, false, true, NULL);
  309. BUG_ON(ret != 0);
  310. vmw_fence_single_bo(bo, NULL);
  311. ttm_bo_unreserve(bo);
  312. ttm_bo_unref(&batch->otable_bo);
  313. }
  314. /*
  315. * vmw_otables_takedown - Take down guest backed memory object tables
  316. *
  317. * @dev_priv: Pointer to a device private structure
  318. *
  319. * Take down the Guest Memory Object tables.
  320. */
  321. void vmw_otables_takedown(struct vmw_private *dev_priv)
  322. {
  323. vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
  324. kfree(dev_priv->otable_batch.otables);
  325. }
  326. /*
  327. * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
  328. * needed for a guest backed memory object.
  329. *
  330. * @data_pages: Number of data pages in the memory object buffer.
  331. */
  332. static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
  333. {
  334. unsigned long data_size = data_pages * PAGE_SIZE;
  335. unsigned long tot_size = 0;
  336. while (likely(data_size > PAGE_SIZE)) {
  337. data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
  338. data_size *= VMW_PPN_SIZE;
  339. tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
  340. }
  341. return tot_size >> PAGE_SHIFT;
  342. }
  343. /*
  344. * vmw_mob_create - Create a mob, but don't populate it.
  345. *
  346. * @data_pages: Number of data pages of the underlying buffer object.
  347. */
  348. struct vmw_mob *vmw_mob_create(unsigned long data_pages)
  349. {
  350. struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
  351. if (unlikely(!mob))
  352. return NULL;
  353. mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
  354. return mob;
  355. }
  356. /*
  357. * vmw_mob_pt_populate - Populate the mob pagetable
  358. *
  359. * @mob: Pointer to the mob the pagetable of which we want to
  360. * populate.
  361. *
  362. * This function allocates memory to be used for the pagetable, and
  363. * adjusts TTM memory accounting accordingly. Returns ENOMEM if
  364. * memory resources aren't sufficient and may cause TTM buffer objects
  365. * to be swapped out by using the TTM memory accounting function.
  366. */
  367. static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
  368. struct vmw_mob *mob)
  369. {
  370. int ret;
  371. BUG_ON(mob->pt_bo != NULL);
  372. ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE,
  373. ttm_bo_type_device,
  374. &vmw_sys_ne_placement,
  375. 0, false, NULL, &mob->pt_bo);
  376. if (unlikely(ret != 0))
  377. return ret;
  378. ret = ttm_bo_reserve(mob->pt_bo, false, true, NULL);
  379. BUG_ON(ret != 0);
  380. ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm);
  381. if (unlikely(ret != 0))
  382. goto out_unreserve;
  383. ret = vmw_bo_map_dma(mob->pt_bo);
  384. if (unlikely(ret != 0))
  385. goto out_unreserve;
  386. ttm_bo_unreserve(mob->pt_bo);
  387. return 0;
  388. out_unreserve:
  389. ttm_bo_unreserve(mob->pt_bo);
  390. ttm_bo_unref(&mob->pt_bo);
  391. return ret;
  392. }
  393. /**
  394. * vmw_mob_assign_ppn - Assign a value to a page table entry
  395. *
  396. * @addr: Pointer to pointer to page table entry.
  397. * @val: The page table entry
  398. *
  399. * Assigns a value to a page table entry pointed to by *@addr and increments
  400. * *@addr according to the page table entry size.
  401. */
  402. #if (VMW_PPN_SIZE == 8)
  403. static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
  404. {
  405. *((u64 *) *addr) = val >> PAGE_SHIFT;
  406. *addr += 2;
  407. }
  408. #else
  409. static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
  410. {
  411. *(*addr)++ = val >> PAGE_SHIFT;
  412. }
  413. #endif
  414. /*
  415. * vmw_mob_build_pt - Build a pagetable
  416. *
  417. * @data_addr: Array of DMA addresses to the underlying buffer
  418. * object's data pages.
  419. * @num_data_pages: Number of buffer object data pages.
  420. * @pt_pages: Array of page pointers to the page table pages.
  421. *
  422. * Returns the number of page table pages actually used.
  423. * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
  424. */
  425. static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
  426. unsigned long num_data_pages,
  427. struct vmw_piter *pt_iter)
  428. {
  429. unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
  430. unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
  431. unsigned long pt_page;
  432. u32 *addr, *save_addr;
  433. unsigned long i;
  434. struct page *page;
  435. for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
  436. page = vmw_piter_page(pt_iter);
  437. save_addr = addr = kmap_atomic(page);
  438. for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
  439. vmw_mob_assign_ppn(&addr,
  440. vmw_piter_dma_addr(data_iter));
  441. if (unlikely(--num_data_pages == 0))
  442. break;
  443. WARN_ON(!vmw_piter_next(data_iter));
  444. }
  445. kunmap_atomic(save_addr);
  446. vmw_piter_next(pt_iter);
  447. }
  448. return num_pt_pages;
  449. }
  450. /*
  451. * vmw_mob_build_pt - Set up a multilevel mob pagetable
  452. *
  453. * @mob: Pointer to a mob whose page table needs setting up.
  454. * @data_addr Array of DMA addresses to the buffer object's data
  455. * pages.
  456. * @num_data_pages: Number of buffer object data pages.
  457. *
  458. * Uses tail recursion to set up a multilevel mob page table.
  459. */
  460. static void vmw_mob_pt_setup(struct vmw_mob *mob,
  461. struct vmw_piter data_iter,
  462. unsigned long num_data_pages)
  463. {
  464. unsigned long num_pt_pages = 0;
  465. struct ttm_buffer_object *bo = mob->pt_bo;
  466. struct vmw_piter save_pt_iter;
  467. struct vmw_piter pt_iter;
  468. const struct vmw_sg_table *vsgt;
  469. int ret;
  470. ret = ttm_bo_reserve(bo, false, true, NULL);
  471. BUG_ON(ret != 0);
  472. vsgt = vmw_bo_sg_table(bo);
  473. vmw_piter_start(&pt_iter, vsgt, 0);
  474. BUG_ON(!vmw_piter_next(&pt_iter));
  475. mob->pt_level = 0;
  476. while (likely(num_data_pages > 1)) {
  477. ++mob->pt_level;
  478. BUG_ON(mob->pt_level > 2);
  479. save_pt_iter = pt_iter;
  480. num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
  481. &pt_iter);
  482. data_iter = save_pt_iter;
  483. num_data_pages = num_pt_pages;
  484. }
  485. mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
  486. ttm_bo_unreserve(bo);
  487. }
  488. /*
  489. * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
  490. *
  491. * @mob: Pointer to a mob to destroy.
  492. */
  493. void vmw_mob_destroy(struct vmw_mob *mob)
  494. {
  495. if (mob->pt_bo)
  496. ttm_bo_unref(&mob->pt_bo);
  497. kfree(mob);
  498. }
  499. /*
  500. * vmw_mob_unbind - Hide a mob from the device.
  501. *
  502. * @dev_priv: Pointer to a device private.
  503. * @mob_id: Device id of the mob to unbind.
  504. */
  505. void vmw_mob_unbind(struct vmw_private *dev_priv,
  506. struct vmw_mob *mob)
  507. {
  508. struct {
  509. SVGA3dCmdHeader header;
  510. SVGA3dCmdDestroyGBMob body;
  511. } *cmd;
  512. int ret;
  513. struct ttm_buffer_object *bo = mob->pt_bo;
  514. if (bo) {
  515. ret = ttm_bo_reserve(bo, false, true, NULL);
  516. /*
  517. * Noone else should be using this buffer.
  518. */
  519. BUG_ON(ret != 0);
  520. }
  521. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  522. if (unlikely(cmd == NULL)) {
  523. DRM_ERROR("Failed reserving FIFO space for Memory "
  524. "Object unbinding.\n");
  525. } else {
  526. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
  527. cmd->header.size = sizeof(cmd->body);
  528. cmd->body.mobid = mob->id;
  529. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  530. }
  531. if (bo) {
  532. vmw_fence_single_bo(bo, NULL);
  533. ttm_bo_unreserve(bo);
  534. }
  535. vmw_fifo_resource_dec(dev_priv);
  536. }
  537. /*
  538. * vmw_mob_bind - Make a mob visible to the device after first
  539. * populating it if necessary.
  540. *
  541. * @dev_priv: Pointer to a device private.
  542. * @mob: Pointer to the mob we're making visible.
  543. * @data_addr: Array of DMA addresses to the data pages of the underlying
  544. * buffer object.
  545. * @num_data_pages: Number of data pages of the underlying buffer
  546. * object.
  547. * @mob_id: Device id of the mob to bind
  548. *
  549. * This function is intended to be interfaced with the ttm_tt backend
  550. * code.
  551. */
  552. int vmw_mob_bind(struct vmw_private *dev_priv,
  553. struct vmw_mob *mob,
  554. const struct vmw_sg_table *vsgt,
  555. unsigned long num_data_pages,
  556. int32_t mob_id)
  557. {
  558. int ret;
  559. bool pt_set_up = false;
  560. struct vmw_piter data_iter;
  561. struct {
  562. SVGA3dCmdHeader header;
  563. SVGA3dCmdDefineGBMob64 body;
  564. } *cmd;
  565. mob->id = mob_id;
  566. vmw_piter_start(&data_iter, vsgt, 0);
  567. if (unlikely(!vmw_piter_next(&data_iter)))
  568. return 0;
  569. if (likely(num_data_pages == 1)) {
  570. mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
  571. mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
  572. } else if (vsgt->num_regions == 1) {
  573. mob->pt_level = SVGA3D_MOBFMT_RANGE;
  574. mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
  575. } else if (unlikely(mob->pt_bo == NULL)) {
  576. ret = vmw_mob_pt_populate(dev_priv, mob);
  577. if (unlikely(ret != 0))
  578. return ret;
  579. vmw_mob_pt_setup(mob, data_iter, num_data_pages);
  580. pt_set_up = true;
  581. mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
  582. }
  583. vmw_fifo_resource_inc(dev_priv);
  584. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  585. if (unlikely(cmd == NULL)) {
  586. DRM_ERROR("Failed reserving FIFO space for Memory "
  587. "Object binding.\n");
  588. goto out_no_cmd_space;
  589. }
  590. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
  591. cmd->header.size = sizeof(cmd->body);
  592. cmd->body.mobid = mob_id;
  593. cmd->body.ptDepth = mob->pt_level;
  594. cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
  595. cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
  596. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  597. return 0;
  598. out_no_cmd_space:
  599. vmw_fifo_resource_dec(dev_priv);
  600. if (pt_set_up)
  601. ttm_bo_unref(&mob->pt_bo);
  602. return -ENOMEM;
  603. }