vmwgfx_mob.c 17 KB

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