vmwgfx_mob.c 17 KB

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