vmwgfx_mob.c 19 KB

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