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, false, 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, false, 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 = kmalloc(sizeof(dx_tables), GFP_KERNEL);
  280. if (*otables == NULL)
  281. return -ENOMEM;
  282. memcpy(*otables, dx_tables, sizeof(dx_tables));
  283. dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
  284. } else {
  285. *otables = kmalloc(sizeof(pre_dx_tables), GFP_KERNEL);
  286. if (*otables == NULL)
  287. return -ENOMEM;
  288. memcpy(*otables, pre_dx_tables, sizeof(pre_dx_tables));
  289. dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
  290. }
  291. ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
  292. if (unlikely(ret != 0))
  293. goto out_setup;
  294. return 0;
  295. out_setup:
  296. kfree(*otables);
  297. return ret;
  298. }
  299. static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
  300. struct vmw_otable_batch *batch)
  301. {
  302. SVGAOTableType i;
  303. struct ttm_buffer_object *bo = batch->otable_bo;
  304. int ret;
  305. for (i = 0; i < batch->num_otables; ++i)
  306. if (batch->otables[i].enabled)
  307. vmw_takedown_otable_base(dev_priv, i,
  308. &batch->otables[i]);
  309. ret = ttm_bo_reserve(bo, false, true, false, NULL);
  310. BUG_ON(ret != 0);
  311. vmw_fence_single_bo(bo, NULL);
  312. ttm_bo_unreserve(bo);
  313. ttm_bo_unref(&batch->otable_bo);
  314. }
  315. /*
  316. * vmw_otables_takedown - Take down guest backed memory object tables
  317. *
  318. * @dev_priv: Pointer to a device private structure
  319. *
  320. * Take down the Guest Memory Object tables.
  321. */
  322. void vmw_otables_takedown(struct vmw_private *dev_priv)
  323. {
  324. vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
  325. kfree(dev_priv->otable_batch.otables);
  326. }
  327. /*
  328. * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
  329. * needed for a guest backed memory object.
  330. *
  331. * @data_pages: Number of data pages in the memory object buffer.
  332. */
  333. static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
  334. {
  335. unsigned long data_size = data_pages * PAGE_SIZE;
  336. unsigned long tot_size = 0;
  337. while (likely(data_size > PAGE_SIZE)) {
  338. data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
  339. data_size *= VMW_PPN_SIZE;
  340. tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
  341. }
  342. return tot_size >> PAGE_SHIFT;
  343. }
  344. /*
  345. * vmw_mob_create - Create a mob, but don't populate it.
  346. *
  347. * @data_pages: Number of data pages of the underlying buffer object.
  348. */
  349. struct vmw_mob *vmw_mob_create(unsigned long data_pages)
  350. {
  351. struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
  352. if (unlikely(mob == NULL))
  353. return NULL;
  354. mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
  355. return mob;
  356. }
  357. /*
  358. * vmw_mob_pt_populate - Populate the mob pagetable
  359. *
  360. * @mob: Pointer to the mob the pagetable of which we want to
  361. * populate.
  362. *
  363. * This function allocates memory to be used for the pagetable, and
  364. * adjusts TTM memory accounting accordingly. Returns ENOMEM if
  365. * memory resources aren't sufficient and may cause TTM buffer objects
  366. * to be swapped out by using the TTM memory accounting function.
  367. */
  368. static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
  369. struct vmw_mob *mob)
  370. {
  371. int ret;
  372. BUG_ON(mob->pt_bo != NULL);
  373. ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE,
  374. ttm_bo_type_device,
  375. &vmw_sys_ne_placement,
  376. 0, false, NULL, &mob->pt_bo);
  377. if (unlikely(ret != 0))
  378. return ret;
  379. ret = ttm_bo_reserve(mob->pt_bo, false, true, false, NULL);
  380. BUG_ON(ret != 0);
  381. ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm);
  382. if (unlikely(ret != 0))
  383. goto out_unreserve;
  384. ret = vmw_bo_map_dma(mob->pt_bo);
  385. if (unlikely(ret != 0))
  386. goto out_unreserve;
  387. ttm_bo_unreserve(mob->pt_bo);
  388. return 0;
  389. out_unreserve:
  390. ttm_bo_unreserve(mob->pt_bo);
  391. ttm_bo_unref(&mob->pt_bo);
  392. return ret;
  393. }
  394. /**
  395. * vmw_mob_assign_ppn - Assign a value to a page table entry
  396. *
  397. * @addr: Pointer to pointer to page table entry.
  398. * @val: The page table entry
  399. *
  400. * Assigns a value to a page table entry pointed to by *@addr and increments
  401. * *@addr according to the page table entry size.
  402. */
  403. #if (VMW_PPN_SIZE == 8)
  404. static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
  405. {
  406. *((u64 *) *addr) = val >> PAGE_SHIFT;
  407. *addr += 2;
  408. }
  409. #else
  410. static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
  411. {
  412. *(*addr)++ = val >> PAGE_SHIFT;
  413. }
  414. #endif
  415. /*
  416. * vmw_mob_build_pt - Build a pagetable
  417. *
  418. * @data_addr: Array of DMA addresses to the underlying buffer
  419. * object's data pages.
  420. * @num_data_pages: Number of buffer object data pages.
  421. * @pt_pages: Array of page pointers to the page table pages.
  422. *
  423. * Returns the number of page table pages actually used.
  424. * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
  425. */
  426. static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
  427. unsigned long num_data_pages,
  428. struct vmw_piter *pt_iter)
  429. {
  430. unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
  431. unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
  432. unsigned long pt_page;
  433. u32 *addr, *save_addr;
  434. unsigned long i;
  435. struct page *page;
  436. for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
  437. page = vmw_piter_page(pt_iter);
  438. save_addr = addr = kmap_atomic(page);
  439. for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
  440. vmw_mob_assign_ppn(&addr,
  441. vmw_piter_dma_addr(data_iter));
  442. if (unlikely(--num_data_pages == 0))
  443. break;
  444. WARN_ON(!vmw_piter_next(data_iter));
  445. }
  446. kunmap_atomic(save_addr);
  447. vmw_piter_next(pt_iter);
  448. }
  449. return num_pt_pages;
  450. }
  451. /*
  452. * vmw_mob_build_pt - Set up a multilevel mob pagetable
  453. *
  454. * @mob: Pointer to a mob whose page table needs setting up.
  455. * @data_addr Array of DMA addresses to the buffer object's data
  456. * pages.
  457. * @num_data_pages: Number of buffer object data pages.
  458. *
  459. * Uses tail recursion to set up a multilevel mob page table.
  460. */
  461. static void vmw_mob_pt_setup(struct vmw_mob *mob,
  462. struct vmw_piter data_iter,
  463. unsigned long num_data_pages)
  464. {
  465. unsigned long num_pt_pages = 0;
  466. struct ttm_buffer_object *bo = mob->pt_bo;
  467. struct vmw_piter save_pt_iter;
  468. struct vmw_piter pt_iter;
  469. const struct vmw_sg_table *vsgt;
  470. int ret;
  471. ret = ttm_bo_reserve(bo, false, true, false, NULL);
  472. BUG_ON(ret != 0);
  473. vsgt = vmw_bo_sg_table(bo);
  474. vmw_piter_start(&pt_iter, vsgt, 0);
  475. BUG_ON(!vmw_piter_next(&pt_iter));
  476. mob->pt_level = 0;
  477. while (likely(num_data_pages > 1)) {
  478. ++mob->pt_level;
  479. BUG_ON(mob->pt_level > 2);
  480. save_pt_iter = pt_iter;
  481. num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
  482. &pt_iter);
  483. data_iter = save_pt_iter;
  484. num_data_pages = num_pt_pages;
  485. }
  486. mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
  487. ttm_bo_unreserve(bo);
  488. }
  489. /*
  490. * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
  491. *
  492. * @mob: Pointer to a mob to destroy.
  493. */
  494. void vmw_mob_destroy(struct vmw_mob *mob)
  495. {
  496. if (mob->pt_bo)
  497. ttm_bo_unref(&mob->pt_bo);
  498. kfree(mob);
  499. }
  500. /*
  501. * vmw_mob_unbind - Hide a mob from the device.
  502. *
  503. * @dev_priv: Pointer to a device private.
  504. * @mob_id: Device id of the mob to unbind.
  505. */
  506. void vmw_mob_unbind(struct vmw_private *dev_priv,
  507. struct vmw_mob *mob)
  508. {
  509. struct {
  510. SVGA3dCmdHeader header;
  511. SVGA3dCmdDestroyGBMob body;
  512. } *cmd;
  513. int ret;
  514. struct ttm_buffer_object *bo = mob->pt_bo;
  515. if (bo) {
  516. ret = ttm_bo_reserve(bo, false, true, false, NULL);
  517. /*
  518. * Noone else should be using this buffer.
  519. */
  520. BUG_ON(ret != 0);
  521. }
  522. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  523. if (unlikely(cmd == NULL)) {
  524. DRM_ERROR("Failed reserving FIFO space for Memory "
  525. "Object unbinding.\n");
  526. } else {
  527. cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
  528. cmd->header.size = sizeof(cmd->body);
  529. cmd->body.mobid = mob->id;
  530. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  531. }
  532. if (bo) {
  533. vmw_fence_single_bo(bo, NULL);
  534. ttm_bo_unreserve(bo);
  535. }
  536. vmw_fifo_resource_dec(dev_priv);
  537. }
  538. /*
  539. * vmw_mob_bind - Make a mob visible to the device after first
  540. * populating it if necessary.
  541. *
  542. * @dev_priv: Pointer to a device private.
  543. * @mob: Pointer to the mob we're making visible.
  544. * @data_addr: Array of DMA addresses to the data pages of the underlying
  545. * buffer object.
  546. * @num_data_pages: Number of data pages of the underlying buffer
  547. * object.
  548. * @mob_id: Device id of the mob to bind
  549. *
  550. * This function is intended to be interfaced with the ttm_tt backend
  551. * code.
  552. */
  553. int vmw_mob_bind(struct vmw_private *dev_priv,
  554. struct vmw_mob *mob,
  555. const struct vmw_sg_table *vsgt,
  556. unsigned long num_data_pages,
  557. int32_t mob_id)
  558. {
  559. int ret;
  560. bool pt_set_up = false;
  561. struct vmw_piter data_iter;
  562. struct {
  563. SVGA3dCmdHeader header;
  564. SVGA3dCmdDefineGBMob64 body;
  565. } *cmd;
  566. mob->id = mob_id;
  567. vmw_piter_start(&data_iter, vsgt, 0);
  568. if (unlikely(!vmw_piter_next(&data_iter)))
  569. return 0;
  570. if (likely(num_data_pages == 1)) {
  571. mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
  572. mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
  573. } else if (vsgt->num_regions == 1) {
  574. mob->pt_level = SVGA3D_MOBFMT_RANGE;
  575. mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
  576. } else if (unlikely(mob->pt_bo == NULL)) {
  577. ret = vmw_mob_pt_populate(dev_priv, mob);
  578. if (unlikely(ret != 0))
  579. return ret;
  580. vmw_mob_pt_setup(mob, data_iter, num_data_pages);
  581. pt_set_up = true;
  582. mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
  583. }
  584. vmw_fifo_resource_inc(dev_priv);
  585. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  586. if (unlikely(cmd == NULL)) {
  587. DRM_ERROR("Failed reserving FIFO space for Memory "
  588. "Object binding.\n");
  589. goto out_no_cmd_space;
  590. }
  591. cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
  592. cmd->header.size = sizeof(cmd->body);
  593. cmd->body.mobid = mob_id;
  594. cmd->body.ptDepth = mob->pt_level;
  595. cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
  596. cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
  597. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  598. return 0;
  599. out_no_cmd_space:
  600. vmw_fifo_resource_dec(dev_priv);
  601. if (pt_set_up)
  602. ttm_bo_unref(&mob->pt_bo);
  603. return -ENOMEM;
  604. }