1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
23530bdc3SThomas Hellstrom /**************************************************************************
33530bdc3SThomas Hellstrom  *
409881d29SZack Rusin  * Copyright 2012-2023 VMware, Inc., Palo Alto, CA., USA
53530bdc3SThomas Hellstrom  *
63530bdc3SThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
73530bdc3SThomas Hellstrom  * copy of this software and associated documentation files (the
83530bdc3SThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
93530bdc3SThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
103530bdc3SThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
113530bdc3SThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
123530bdc3SThomas Hellstrom  * the following conditions:
133530bdc3SThomas Hellstrom  *
143530bdc3SThomas Hellstrom  * The above copyright notice and this permission notice (including the
153530bdc3SThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
163530bdc3SThomas Hellstrom  * of the Software.
173530bdc3SThomas Hellstrom  *
183530bdc3SThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
193530bdc3SThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
203530bdc3SThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
213530bdc3SThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
223530bdc3SThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
233530bdc3SThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
243530bdc3SThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
253530bdc3SThomas Hellstrom  *
263530bdc3SThomas Hellstrom  **************************************************************************/
273530bdc3SThomas Hellstrom 
2809881d29SZack Rusin #include "vmwgfx_bo.h"
293530bdc3SThomas Hellstrom #include "vmwgfx_drv.h"
303530bdc3SThomas Hellstrom 
3109881d29SZack Rusin #include <linux/highmem.h>
3209881d29SZack Rusin 
33f2a0dcb1SThomas Hellstrom #ifdef CONFIG_64BIT
34f2a0dcb1SThomas Hellstrom #define VMW_PPN_SIZE 8
35ebc9ac7cSZack Rusin #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PT64_0
36ebc9ac7cSZack Rusin #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PT64_1
37ebc9ac7cSZack Rusin #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PT64_2
38f2a0dcb1SThomas Hellstrom #else
393530bdc3SThomas Hellstrom #define VMW_PPN_SIZE 4
40ebc9ac7cSZack Rusin #define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PT_0
41ebc9ac7cSZack Rusin #define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PT_1
42ebc9ac7cSZack Rusin #define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PT_2
43f2a0dcb1SThomas Hellstrom #endif
443530bdc3SThomas Hellstrom 
453530bdc3SThomas Hellstrom /*
463530bdc3SThomas Hellstrom  * struct vmw_mob - Structure containing page table and metadata for a
473530bdc3SThomas Hellstrom  * Guest Memory OBject.
483530bdc3SThomas Hellstrom  *
493530bdc3SThomas Hellstrom  * @num_pages       Number of pages that make up the page table.
503530bdc3SThomas Hellstrom  * @pt_level        The indirection level of the page table. 0-2.
510fd53cfbSThomas Hellstrom  * @pt_root_page    DMA address of the level 0 page of the page table.
523530bdc3SThomas Hellstrom  */
533530bdc3SThomas Hellstrom struct vmw_mob {
54*668b2066SZack Rusin 	struct vmw_bo *pt_bo;
553530bdc3SThomas Hellstrom 	unsigned long num_pages;
563530bdc3SThomas Hellstrom 	unsigned pt_level;
570fd53cfbSThomas Hellstrom 	dma_addr_t pt_root_page;
583530bdc3SThomas Hellstrom 	uint32_t id;
593530bdc3SThomas Hellstrom };
603530bdc3SThomas Hellstrom 
613530bdc3SThomas Hellstrom /*
623530bdc3SThomas Hellstrom  * struct vmw_otable - Guest Memory OBject table metadata
633530bdc3SThomas Hellstrom  *
643530bdc3SThomas Hellstrom  * @size:           Size of the table (page-aligned).
653530bdc3SThomas Hellstrom  * @page_table:     Pointer to a struct vmw_mob holding the page table.
663530bdc3SThomas Hellstrom  */
67d80efd5cSThomas Hellstrom static const struct vmw_otable pre_dx_tables[] = {
68ebc9ac7cSZack Rusin 	{VMWGFX_NUM_MOB * sizeof(SVGAOTableMobEntry), NULL, true},
69ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_SURFACE * sizeof(SVGAOTableSurfaceEntry), NULL, true},
70ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_CONTEXT * sizeof(SVGAOTableContextEntry), NULL, true},
71ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_SHADER * sizeof(SVGAOTableShaderEntry), NULL, true},
72ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_SCREEN_TARGET * sizeof(SVGAOTableScreenTargetEntry),
7311343099SZack Rusin 	 NULL, true}
74d80efd5cSThomas Hellstrom };
75d80efd5cSThomas Hellstrom 
76d80efd5cSThomas Hellstrom static const struct vmw_otable dx_tables[] = {
77ebc9ac7cSZack Rusin 	{VMWGFX_NUM_MOB * sizeof(SVGAOTableMobEntry), NULL, true},
78ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_SURFACE * sizeof(SVGAOTableSurfaceEntry), NULL, true},
79ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_CONTEXT * sizeof(SVGAOTableContextEntry), NULL, true},
80ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_SHADER * sizeof(SVGAOTableShaderEntry), NULL, true},
81ebc9ac7cSZack Rusin 	{VMWGFX_NUM_GB_SCREEN_TARGET * sizeof(SVGAOTableScreenTargetEntry),
8211343099SZack Rusin 	 NULL, true},
83d80efd5cSThomas Hellstrom 	{VMWGFX_NUM_DXCONTEXT * sizeof(SVGAOTableDXContextEntry), NULL, true},
843530bdc3SThomas Hellstrom };
853530bdc3SThomas Hellstrom 
863530bdc3SThomas Hellstrom static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
873530bdc3SThomas Hellstrom 			       struct vmw_mob *mob);
883530bdc3SThomas Hellstrom static void vmw_mob_pt_setup(struct vmw_mob *mob,
890fd53cfbSThomas Hellstrom 			     struct vmw_piter data_iter,
903530bdc3SThomas Hellstrom 			     unsigned long num_data_pages);
913530bdc3SThomas Hellstrom 
922ef4fb92SZack Rusin 
vmw_bo_unpin_unlocked(struct ttm_buffer_object * bo)932ef4fb92SZack Rusin static inline void vmw_bo_unpin_unlocked(struct ttm_buffer_object *bo)
942ef4fb92SZack Rusin {
952ef4fb92SZack Rusin 	int ret = ttm_bo_reserve(bo, false, true, NULL);
962ef4fb92SZack Rusin 	BUG_ON(ret != 0);
972ef4fb92SZack Rusin 	ttm_bo_unpin(bo);
982ef4fb92SZack Rusin 	ttm_bo_unreserve(bo);
992ef4fb92SZack Rusin }
1002ef4fb92SZack Rusin 
1012ef4fb92SZack Rusin 
1023530bdc3SThomas Hellstrom /*
1033530bdc3SThomas Hellstrom  * vmw_setup_otable_base - Issue an object table base setup command to
1043530bdc3SThomas Hellstrom  * the device
1053530bdc3SThomas Hellstrom  *
1063530bdc3SThomas Hellstrom  * @dev_priv:       Pointer to a device private structure
1073530bdc3SThomas Hellstrom  * @type:           Type of object table base
1083530bdc3SThomas Hellstrom  * @offset          Start of table offset into dev_priv::otable_bo
1093530bdc3SThomas Hellstrom  * @otable          Pointer to otable metadata;
1103530bdc3SThomas Hellstrom  *
1113530bdc3SThomas Hellstrom  * This function returns -ENOMEM if it fails to reserve fifo space,
1123530bdc3SThomas Hellstrom  * and may block waiting for fifo space.
1133530bdc3SThomas Hellstrom  */
vmw_setup_otable_base(struct vmw_private * dev_priv,SVGAOTableType type,struct ttm_buffer_object * otable_bo,unsigned long offset,struct vmw_otable * otable)1143530bdc3SThomas Hellstrom static int vmw_setup_otable_base(struct vmw_private *dev_priv,
1153530bdc3SThomas Hellstrom 				 SVGAOTableType type,
116d80efd5cSThomas Hellstrom 				 struct ttm_buffer_object *otable_bo,
1173530bdc3SThomas Hellstrom 				 unsigned long offset,
1183530bdc3SThomas Hellstrom 				 struct vmw_otable *otable)
1193530bdc3SThomas Hellstrom {
1203530bdc3SThomas Hellstrom 	struct {
1213530bdc3SThomas Hellstrom 		SVGA3dCmdHeader header;
1223e894a62SThomas Hellstrom 		SVGA3dCmdSetOTableBase64 body;
1233530bdc3SThomas Hellstrom 	} *cmd;
1243530bdc3SThomas Hellstrom 	struct vmw_mob *mob;
1250fd53cfbSThomas Hellstrom 	const struct vmw_sg_table *vsgt;
1260fd53cfbSThomas Hellstrom 	struct vmw_piter iter;
1273530bdc3SThomas Hellstrom 	int ret;
1283530bdc3SThomas Hellstrom 
1293530bdc3SThomas Hellstrom 	BUG_ON(otable->page_table != NULL);
1303530bdc3SThomas Hellstrom 
131d80efd5cSThomas Hellstrom 	vsgt = vmw_bo_sg_table(otable_bo);
1320fd53cfbSThomas Hellstrom 	vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
1330fd53cfbSThomas Hellstrom 	WARN_ON(!vmw_piter_next(&iter));
1340fd53cfbSThomas Hellstrom 
1353530bdc3SThomas Hellstrom 	mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
1363530bdc3SThomas Hellstrom 	if (unlikely(mob == NULL)) {
1373530bdc3SThomas Hellstrom 		DRM_ERROR("Failed creating OTable page table.\n");
1383530bdc3SThomas Hellstrom 		return -ENOMEM;
1393530bdc3SThomas Hellstrom 	}
1403530bdc3SThomas Hellstrom 
1413530bdc3SThomas Hellstrom 	if (otable->size <= PAGE_SIZE) {
142f2a0dcb1SThomas Hellstrom 		mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
1430fd53cfbSThomas Hellstrom 		mob->pt_root_page = vmw_piter_dma_addr(&iter);
1443530bdc3SThomas Hellstrom 	} else {
1453530bdc3SThomas Hellstrom 		ret = vmw_mob_pt_populate(dev_priv, mob);
1463530bdc3SThomas Hellstrom 		if (unlikely(ret != 0))
1473530bdc3SThomas Hellstrom 			goto out_no_populate;
1483530bdc3SThomas Hellstrom 
1490fd53cfbSThomas Hellstrom 		vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
150ebc9ac7cSZack Rusin 		mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PT_1;
1513530bdc3SThomas Hellstrom 	}
1523530bdc3SThomas Hellstrom 
1538426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
1543530bdc3SThomas Hellstrom 	if (unlikely(cmd == NULL)) {
155cd9a21a8SDave Jones 		ret = -ENOMEM;
1563530bdc3SThomas Hellstrom 		goto out_no_fifo;
1573530bdc3SThomas Hellstrom 	}
1583530bdc3SThomas Hellstrom 
1593530bdc3SThomas Hellstrom 	memset(cmd, 0, sizeof(*cmd));
1603e894a62SThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
1613530bdc3SThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
1623530bdc3SThomas Hellstrom 	cmd->body.type = type;
163b9eb1a61SThomas Hellstrom 	cmd->body.baseAddress = mob->pt_root_page >> PAGE_SHIFT;
1643530bdc3SThomas Hellstrom 	cmd->body.sizeInBytes = otable->size;
1653530bdc3SThomas Hellstrom 	cmd->body.validSizeInBytes = 0;
1663530bdc3SThomas Hellstrom 	cmd->body.ptDepth = mob->pt_level;
1673530bdc3SThomas Hellstrom 
168f2a0dcb1SThomas Hellstrom 	/*
169f2a0dcb1SThomas Hellstrom 	 * The device doesn't support this, But the otable size is
170f2a0dcb1SThomas Hellstrom 	 * determined at compile-time, so this BUG shouldn't trigger
171f2a0dcb1SThomas Hellstrom 	 * randomly.
172f2a0dcb1SThomas Hellstrom 	 */
173f2a0dcb1SThomas Hellstrom 	BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);
174f2a0dcb1SThomas Hellstrom 
1758426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
1763530bdc3SThomas Hellstrom 	otable->page_table = mob;
1773530bdc3SThomas Hellstrom 
1783530bdc3SThomas Hellstrom 	return 0;
1793530bdc3SThomas Hellstrom 
1803530bdc3SThomas Hellstrom out_no_fifo:
1813530bdc3SThomas Hellstrom out_no_populate:
1823530bdc3SThomas Hellstrom 	vmw_mob_destroy(mob);
1833530bdc3SThomas Hellstrom 	return ret;
1843530bdc3SThomas Hellstrom }
1853530bdc3SThomas Hellstrom 
1863530bdc3SThomas Hellstrom /*
1873530bdc3SThomas Hellstrom  * vmw_takedown_otable_base - Issue an object table base takedown command
1883530bdc3SThomas Hellstrom  * to the device
1893530bdc3SThomas Hellstrom  *
1903530bdc3SThomas Hellstrom  * @dev_priv:       Pointer to a device private structure
1913530bdc3SThomas Hellstrom  * @type:           Type of object table base
1923530bdc3SThomas Hellstrom  *
1933530bdc3SThomas Hellstrom  */
vmw_takedown_otable_base(struct vmw_private * dev_priv,SVGAOTableType type,struct vmw_otable * otable)1943530bdc3SThomas Hellstrom static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
1953530bdc3SThomas Hellstrom 				     SVGAOTableType type,
1963530bdc3SThomas Hellstrom 				     struct vmw_otable *otable)
1973530bdc3SThomas Hellstrom {
1983530bdc3SThomas Hellstrom 	struct {
1993530bdc3SThomas Hellstrom 		SVGA3dCmdHeader header;
2003530bdc3SThomas Hellstrom 		SVGA3dCmdSetOTableBase body;
2013530bdc3SThomas Hellstrom 	} *cmd;
2023e894a62SThomas Hellstrom 	struct ttm_buffer_object *bo;
2033530bdc3SThomas Hellstrom 
2043530bdc3SThomas Hellstrom 	if (otable->page_table == NULL)
2053530bdc3SThomas Hellstrom 		return;
2063530bdc3SThomas Hellstrom 
207*668b2066SZack Rusin 	bo = &otable->page_table->pt_bo->tbo;
2088426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
20911c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
21013eec7eaSThomas Hellstrom 		return;
21113eec7eaSThomas Hellstrom 
2123530bdc3SThomas Hellstrom 	memset(cmd, 0, sizeof(*cmd));
2133530bdc3SThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE;
2143530bdc3SThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
2153530bdc3SThomas Hellstrom 	cmd->body.type = type;
2163530bdc3SThomas Hellstrom 	cmd->body.baseAddress = 0;
2173530bdc3SThomas Hellstrom 	cmd->body.sizeInBytes = 0;
2183530bdc3SThomas Hellstrom 	cmd->body.validSizeInBytes = 0;
2193530bdc3SThomas Hellstrom 	cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID;
2208426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
2213530bdc3SThomas Hellstrom 
2223530bdc3SThomas Hellstrom 	if (bo) {
2233530bdc3SThomas Hellstrom 		int ret;
2243530bdc3SThomas Hellstrom 
225dfd5e50eSChristian König 		ret = ttm_bo_reserve(bo, false, true, NULL);
2263530bdc3SThomas Hellstrom 		BUG_ON(ret != 0);
2273530bdc3SThomas Hellstrom 
228e9431ea5SThomas Hellstrom 		vmw_bo_fence_single(bo, NULL);
2293530bdc3SThomas Hellstrom 		ttm_bo_unreserve(bo);
2303530bdc3SThomas Hellstrom 	}
2313530bdc3SThomas Hellstrom 
2323530bdc3SThomas Hellstrom 	vmw_mob_destroy(otable->page_table);
2333530bdc3SThomas Hellstrom 	otable->page_table = NULL;
2343530bdc3SThomas Hellstrom }
2353530bdc3SThomas Hellstrom 
236d80efd5cSThomas Hellstrom 
vmw_otable_batch_setup(struct vmw_private * dev_priv,struct vmw_otable_batch * batch)237d80efd5cSThomas Hellstrom static int vmw_otable_batch_setup(struct vmw_private *dev_priv,
238d80efd5cSThomas Hellstrom 				  struct vmw_otable_batch *batch)
2393530bdc3SThomas Hellstrom {
2403530bdc3SThomas Hellstrom 	unsigned long offset;
2413530bdc3SThomas Hellstrom 	unsigned long bo_size;
242d80efd5cSThomas Hellstrom 	struct vmw_otable *otables = batch->otables;
2433530bdc3SThomas Hellstrom 	SVGAOTableType i;
2443530bdc3SThomas Hellstrom 	int ret;
2453530bdc3SThomas Hellstrom 
2463530bdc3SThomas Hellstrom 	bo_size = 0;
247d80efd5cSThomas Hellstrom 	for (i = 0; i < batch->num_otables; ++i) {
248d80efd5cSThomas Hellstrom 		if (!otables[i].enabled)
249d80efd5cSThomas Hellstrom 			continue;
250d80efd5cSThomas Hellstrom 
251bc65754cSCai Huoqing 		otables[i].size = PFN_ALIGN(otables[i].size);
2523530bdc3SThomas Hellstrom 		bo_size += otables[i].size;
2533530bdc3SThomas Hellstrom 	}
2543530bdc3SThomas Hellstrom 
255*668b2066SZack Rusin 	ret = vmw_bo_create_and_populate(dev_priv, bo_size,
256*668b2066SZack Rusin 					 VMW_BO_DOMAIN_WAITABLE_SYS,
257*668b2066SZack Rusin 					 &batch->otable_bo);
2583530bdc3SThomas Hellstrom 	if (unlikely(ret != 0))
25956dc01f1SDave Airlie 		return ret;
2603530bdc3SThomas Hellstrom 
2613530bdc3SThomas Hellstrom 	offset = 0;
262d80efd5cSThomas Hellstrom 	for (i = 0; i < batch->num_otables; ++i) {
263d80efd5cSThomas Hellstrom 		if (!batch->otables[i].enabled)
264d80efd5cSThomas Hellstrom 			continue;
265d80efd5cSThomas Hellstrom 
266*668b2066SZack Rusin 		ret = vmw_setup_otable_base(dev_priv, i,
267*668b2066SZack Rusin 					    &batch->otable_bo->tbo,
268d80efd5cSThomas Hellstrom 					    offset,
2693530bdc3SThomas Hellstrom 					    &otables[i]);
2703530bdc3SThomas Hellstrom 		if (unlikely(ret != 0))
2713530bdc3SThomas Hellstrom 			goto out_no_setup;
2723530bdc3SThomas Hellstrom 		offset += otables[i].size;
2733530bdc3SThomas Hellstrom 	}
2743530bdc3SThomas Hellstrom 
2753530bdc3SThomas Hellstrom 	return 0;
2763530bdc3SThomas Hellstrom 
2773530bdc3SThomas Hellstrom out_no_setup:
278d80efd5cSThomas Hellstrom 	for (i = 0; i < batch->num_otables; ++i) {
279d80efd5cSThomas Hellstrom 		if (batch->otables[i].enabled)
280d80efd5cSThomas Hellstrom 			vmw_takedown_otable_base(dev_priv, i,
281d80efd5cSThomas Hellstrom 						 &batch->otables[i]);
282d80efd5cSThomas Hellstrom 	}
2833530bdc3SThomas Hellstrom 
284*668b2066SZack Rusin 	vmw_bo_unpin_unlocked(&batch->otable_bo->tbo);
285*668b2066SZack Rusin 	ttm_bo_put(&batch->otable_bo->tbo);
2866034d9d4SThomas Zimmermann 	batch->otable_bo = NULL;
2873530bdc3SThomas Hellstrom 	return ret;
2883530bdc3SThomas Hellstrom }
2893530bdc3SThomas Hellstrom 
290d80efd5cSThomas Hellstrom /*
291d80efd5cSThomas Hellstrom  * vmw_otables_setup - Set up guest backed memory object tables
292d80efd5cSThomas Hellstrom  *
293d80efd5cSThomas Hellstrom  * @dev_priv:       Pointer to a device private structure
294d80efd5cSThomas Hellstrom  *
295d80efd5cSThomas Hellstrom  * Takes care of the device guest backed surface
296d80efd5cSThomas Hellstrom  * initialization, by setting up the guest backed memory object tables.
297d80efd5cSThomas Hellstrom  * Returns 0 on success and various error codes on failure. A successful return
298d80efd5cSThomas Hellstrom  * means the object tables can be taken down using the vmw_otables_takedown
299d80efd5cSThomas Hellstrom  * function.
300d80efd5cSThomas Hellstrom  */
vmw_otables_setup(struct vmw_private * dev_priv)301d80efd5cSThomas Hellstrom int vmw_otables_setup(struct vmw_private *dev_priv)
302d80efd5cSThomas Hellstrom {
303d80efd5cSThomas Hellstrom 	struct vmw_otable **otables = &dev_priv->otable_batch.otables;
304d80efd5cSThomas Hellstrom 	int ret;
305d80efd5cSThomas Hellstrom 
306878c6ecdSDeepak Rawat 	if (has_sm4_context(dev_priv)) {
3075d25fde2SShyam Saini 		*otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
3081a4adb05SRavikant B Sharma 		if (!(*otables))
309d80efd5cSThomas Hellstrom 			return -ENOMEM;
310d80efd5cSThomas Hellstrom 
311d80efd5cSThomas Hellstrom 		dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
312d80efd5cSThomas Hellstrom 	} else {
3135d25fde2SShyam Saini 		*otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
3145d25fde2SShyam Saini 				   GFP_KERNEL);
3151a4adb05SRavikant B Sharma 		if (!(*otables))
316d80efd5cSThomas Hellstrom 			return -ENOMEM;
317d80efd5cSThomas Hellstrom 
318d80efd5cSThomas Hellstrom 		dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
319d80efd5cSThomas Hellstrom 	}
320d80efd5cSThomas Hellstrom 
321d80efd5cSThomas Hellstrom 	ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
322d80efd5cSThomas Hellstrom 	if (unlikely(ret != 0))
323d80efd5cSThomas Hellstrom 		goto out_setup;
324d80efd5cSThomas Hellstrom 
325d80efd5cSThomas Hellstrom 	return 0;
326d80efd5cSThomas Hellstrom 
327d80efd5cSThomas Hellstrom out_setup:
328d80efd5cSThomas Hellstrom 	kfree(*otables);
329d80efd5cSThomas Hellstrom 	return ret;
330d80efd5cSThomas Hellstrom }
331d80efd5cSThomas Hellstrom 
vmw_otable_batch_takedown(struct vmw_private * dev_priv,struct vmw_otable_batch * batch)332d80efd5cSThomas Hellstrom static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
333d80efd5cSThomas Hellstrom 			       struct vmw_otable_batch *batch)
334d80efd5cSThomas Hellstrom {
335d80efd5cSThomas Hellstrom 	SVGAOTableType i;
336*668b2066SZack Rusin 	struct ttm_buffer_object *bo = &batch->otable_bo->tbo;
337d80efd5cSThomas Hellstrom 	int ret;
338d80efd5cSThomas Hellstrom 
339d80efd5cSThomas Hellstrom 	for (i = 0; i < batch->num_otables; ++i)
340d80efd5cSThomas Hellstrom 		if (batch->otables[i].enabled)
341d80efd5cSThomas Hellstrom 			vmw_takedown_otable_base(dev_priv, i,
342d80efd5cSThomas Hellstrom 						 &batch->otables[i]);
343d80efd5cSThomas Hellstrom 
344dfd5e50eSChristian König 	ret = ttm_bo_reserve(bo, false, true, NULL);
345d80efd5cSThomas Hellstrom 	BUG_ON(ret != 0);
346d80efd5cSThomas Hellstrom 
347e9431ea5SThomas Hellstrom 	vmw_bo_fence_single(bo, NULL);
3482ef4fb92SZack Rusin 	ttm_bo_unpin(bo);
349d80efd5cSThomas Hellstrom 	ttm_bo_unreserve(bo);
350d80efd5cSThomas Hellstrom 
351*668b2066SZack Rusin 	vmw_bo_unreference(&batch->otable_bo);
352d80efd5cSThomas Hellstrom }
3533530bdc3SThomas Hellstrom 
3543530bdc3SThomas Hellstrom /*
3553530bdc3SThomas Hellstrom  * vmw_otables_takedown - Take down guest backed memory object tables
3563530bdc3SThomas Hellstrom  *
3573530bdc3SThomas Hellstrom  * @dev_priv:       Pointer to a device private structure
3583530bdc3SThomas Hellstrom  *
3593530bdc3SThomas Hellstrom  * Take down the Guest Memory Object tables.
3603530bdc3SThomas Hellstrom  */
vmw_otables_takedown(struct vmw_private * dev_priv)3613530bdc3SThomas Hellstrom void vmw_otables_takedown(struct vmw_private *dev_priv)
3623530bdc3SThomas Hellstrom {
363d80efd5cSThomas Hellstrom 	vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
364d80efd5cSThomas Hellstrom 	kfree(dev_priv->otable_batch.otables);
3653530bdc3SThomas Hellstrom }
3663530bdc3SThomas Hellstrom 
3673530bdc3SThomas Hellstrom /*
3683530bdc3SThomas Hellstrom  * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
3693530bdc3SThomas Hellstrom  * needed for a guest backed memory object.
3703530bdc3SThomas Hellstrom  *
3713530bdc3SThomas Hellstrom  * @data_pages:  Number of data pages in the memory object buffer.
3723530bdc3SThomas Hellstrom  */
vmw_mob_calculate_pt_pages(unsigned long data_pages)3733530bdc3SThomas Hellstrom static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
3743530bdc3SThomas Hellstrom {
3753530bdc3SThomas Hellstrom 	unsigned long data_size = data_pages * PAGE_SIZE;
3763530bdc3SThomas Hellstrom 	unsigned long tot_size = 0;
3773530bdc3SThomas Hellstrom 
3783530bdc3SThomas Hellstrom 	while (likely(data_size > PAGE_SIZE)) {
3793530bdc3SThomas Hellstrom 		data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
3803530bdc3SThomas Hellstrom 		data_size *= VMW_PPN_SIZE;
381bc65754cSCai Huoqing 		tot_size += PFN_ALIGN(data_size);
3823530bdc3SThomas Hellstrom 	}
3833530bdc3SThomas Hellstrom 
3843530bdc3SThomas Hellstrom 	return tot_size >> PAGE_SHIFT;
3853530bdc3SThomas Hellstrom }
3863530bdc3SThomas Hellstrom 
3873530bdc3SThomas Hellstrom /*
3883530bdc3SThomas Hellstrom  * vmw_mob_create - Create a mob, but don't populate it.
3893530bdc3SThomas Hellstrom  *
3903530bdc3SThomas Hellstrom  * @data_pages:  Number of data pages of the underlying buffer object.
3913530bdc3SThomas Hellstrom  */
vmw_mob_create(unsigned long data_pages)3923530bdc3SThomas Hellstrom struct vmw_mob *vmw_mob_create(unsigned long data_pages)
3933530bdc3SThomas Hellstrom {
3943530bdc3SThomas Hellstrom 	struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
3953530bdc3SThomas Hellstrom 
3961a4adb05SRavikant B Sharma 	if (unlikely(!mob))
3973530bdc3SThomas Hellstrom 		return NULL;
3983530bdc3SThomas Hellstrom 
3993530bdc3SThomas Hellstrom 	mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
4003530bdc3SThomas Hellstrom 
4013530bdc3SThomas Hellstrom 	return mob;
4023530bdc3SThomas Hellstrom }
4033530bdc3SThomas Hellstrom 
4043530bdc3SThomas Hellstrom /*
4053530bdc3SThomas Hellstrom  * vmw_mob_pt_populate - Populate the mob pagetable
4063530bdc3SThomas Hellstrom  *
4073530bdc3SThomas Hellstrom  * @mob:         Pointer to the mob the pagetable of which we want to
4083530bdc3SThomas Hellstrom  *               populate.
4093530bdc3SThomas Hellstrom  *
4108aadeb8aSZack Rusin  * This function allocates memory to be used for the pagetable.
4118aadeb8aSZack Rusin  * Returns ENOMEM if memory resources aren't sufficient and may
4128aadeb8aSZack Rusin  * cause TTM buffer objects to be swapped out.
4133530bdc3SThomas Hellstrom  */
vmw_mob_pt_populate(struct vmw_private * dev_priv,struct vmw_mob * mob)4143530bdc3SThomas Hellstrom static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
4153530bdc3SThomas Hellstrom 			       struct vmw_mob *mob)
4163530bdc3SThomas Hellstrom {
4173530bdc3SThomas Hellstrom 	BUG_ON(mob->pt_bo != NULL);
4183530bdc3SThomas Hellstrom 
419*668b2066SZack Rusin 	return vmw_bo_create_and_populate(dev_priv, mob->num_pages * PAGE_SIZE,
420*668b2066SZack Rusin 					  VMW_BO_DOMAIN_WAITABLE_SYS,
421*668b2066SZack Rusin 					  &mob->pt_bo);
4223530bdc3SThomas Hellstrom }
4233530bdc3SThomas Hellstrom 
424f2a0dcb1SThomas Hellstrom /**
425f2a0dcb1SThomas Hellstrom  * vmw_mob_assign_ppn - Assign a value to a page table entry
426f2a0dcb1SThomas Hellstrom  *
427f2a0dcb1SThomas Hellstrom  * @addr: Pointer to pointer to page table entry.
428f2a0dcb1SThomas Hellstrom  * @val: The page table entry
429f2a0dcb1SThomas Hellstrom  *
430f2a0dcb1SThomas Hellstrom  * Assigns a value to a page table entry pointed to by *@addr and increments
431f2a0dcb1SThomas Hellstrom  * *@addr according to the page table entry size.
432f2a0dcb1SThomas Hellstrom  */
433f2a0dcb1SThomas Hellstrom #if (VMW_PPN_SIZE == 8)
vmw_mob_assign_ppn(u32 ** addr,dma_addr_t val)434b9eb1a61SThomas Hellstrom static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
435f2a0dcb1SThomas Hellstrom {
436b9eb1a61SThomas Hellstrom 	*((u64 *) *addr) = val >> PAGE_SHIFT;
437f2a0dcb1SThomas Hellstrom 	*addr += 2;
438f2a0dcb1SThomas Hellstrom }
439f2a0dcb1SThomas Hellstrom #else
vmw_mob_assign_ppn(u32 ** addr,dma_addr_t val)440b9eb1a61SThomas Hellstrom static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
441f2a0dcb1SThomas Hellstrom {
442b9eb1a61SThomas Hellstrom 	*(*addr)++ = val >> PAGE_SHIFT;
443f2a0dcb1SThomas Hellstrom }
444f2a0dcb1SThomas Hellstrom #endif
4453530bdc3SThomas Hellstrom 
4463530bdc3SThomas Hellstrom /*
4473530bdc3SThomas Hellstrom  * vmw_mob_build_pt - Build a pagetable
4483530bdc3SThomas Hellstrom  *
4490fd53cfbSThomas Hellstrom  * @data_addr:      Array of DMA addresses to the underlying buffer
4503530bdc3SThomas Hellstrom  *                  object's data pages.
4513530bdc3SThomas Hellstrom  * @num_data_pages: Number of buffer object data pages.
4523530bdc3SThomas Hellstrom  * @pt_pages:       Array of page pointers to the page table pages.
4533530bdc3SThomas Hellstrom  *
4543530bdc3SThomas Hellstrom  * Returns the number of page table pages actually used.
4553530bdc3SThomas Hellstrom  * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
4563530bdc3SThomas Hellstrom  */
vmw_mob_build_pt(struct vmw_piter * data_iter,unsigned long num_data_pages,struct vmw_piter * pt_iter)4570fd53cfbSThomas Hellstrom static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
4583530bdc3SThomas Hellstrom 				      unsigned long num_data_pages,
4590fd53cfbSThomas Hellstrom 				      struct vmw_piter *pt_iter)
4603530bdc3SThomas Hellstrom {
4613530bdc3SThomas Hellstrom 	unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
4623530bdc3SThomas Hellstrom 	unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
4630fd53cfbSThomas Hellstrom 	unsigned long pt_page;
464b9eb1a61SThomas Hellstrom 	u32 *addr, *save_addr;
4653530bdc3SThomas Hellstrom 	unsigned long i;
4660fd53cfbSThomas Hellstrom 	struct page *page;
4673530bdc3SThomas Hellstrom 
4683530bdc3SThomas Hellstrom 	for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
4690fd53cfbSThomas Hellstrom 		page = vmw_piter_page(pt_iter);
4700fd53cfbSThomas Hellstrom 
4710fd53cfbSThomas Hellstrom 		save_addr = addr = kmap_atomic(page);
4723530bdc3SThomas Hellstrom 
4733530bdc3SThomas Hellstrom 		for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
474f2a0dcb1SThomas Hellstrom 			vmw_mob_assign_ppn(&addr,
475f2a0dcb1SThomas Hellstrom 					   vmw_piter_dma_addr(data_iter));
4760fd53cfbSThomas Hellstrom 			if (unlikely(--num_data_pages == 0))
4773530bdc3SThomas Hellstrom 				break;
4780fd53cfbSThomas Hellstrom 			WARN_ON(!vmw_piter_next(data_iter));
4793530bdc3SThomas Hellstrom 		}
4803530bdc3SThomas Hellstrom 		kunmap_atomic(save_addr);
4810fd53cfbSThomas Hellstrom 		vmw_piter_next(pt_iter);
4823530bdc3SThomas Hellstrom 	}
4833530bdc3SThomas Hellstrom 
4843530bdc3SThomas Hellstrom 	return num_pt_pages;
4853530bdc3SThomas Hellstrom }
4863530bdc3SThomas Hellstrom 
4873530bdc3SThomas Hellstrom /*
4883530bdc3SThomas Hellstrom  * vmw_mob_build_pt - Set up a multilevel mob pagetable
4893530bdc3SThomas Hellstrom  *
4903530bdc3SThomas Hellstrom  * @mob:            Pointer to a mob whose page table needs setting up.
4910fd53cfbSThomas Hellstrom  * @data_addr       Array of DMA addresses to the buffer object's data
4923530bdc3SThomas Hellstrom  *                  pages.
4933530bdc3SThomas Hellstrom  * @num_data_pages: Number of buffer object data pages.
4943530bdc3SThomas Hellstrom  *
4953530bdc3SThomas Hellstrom  * Uses tail recursion to set up a multilevel mob page table.
4963530bdc3SThomas Hellstrom  */
vmw_mob_pt_setup(struct vmw_mob * mob,struct vmw_piter data_iter,unsigned long num_data_pages)4973530bdc3SThomas Hellstrom static void vmw_mob_pt_setup(struct vmw_mob *mob,
4980fd53cfbSThomas Hellstrom 			     struct vmw_piter data_iter,
4993530bdc3SThomas Hellstrom 			     unsigned long num_data_pages)
5003530bdc3SThomas Hellstrom {
5013530bdc3SThomas Hellstrom 	unsigned long num_pt_pages = 0;
502*668b2066SZack Rusin 	struct ttm_buffer_object *bo = &mob->pt_bo->tbo;
50374231041SZack Rusin 	struct vmw_piter save_pt_iter = {0};
5040fd53cfbSThomas Hellstrom 	struct vmw_piter pt_iter;
5050fd53cfbSThomas Hellstrom 	const struct vmw_sg_table *vsgt;
5063530bdc3SThomas Hellstrom 	int ret;
5073530bdc3SThomas Hellstrom 
50874231041SZack Rusin 	BUG_ON(num_data_pages == 0);
50974231041SZack Rusin 
510dfd5e50eSChristian König 	ret = ttm_bo_reserve(bo, false, true, NULL);
5113530bdc3SThomas Hellstrom 	BUG_ON(ret != 0);
5123530bdc3SThomas Hellstrom 
5130fd53cfbSThomas Hellstrom 	vsgt = vmw_bo_sg_table(bo);
5140fd53cfbSThomas Hellstrom 	vmw_piter_start(&pt_iter, vsgt, 0);
5150fd53cfbSThomas Hellstrom 	BUG_ON(!vmw_piter_next(&pt_iter));
5163530bdc3SThomas Hellstrom 	mob->pt_level = 0;
5173530bdc3SThomas Hellstrom 	while (likely(num_data_pages > 1)) {
5183530bdc3SThomas Hellstrom 		++mob->pt_level;
5193530bdc3SThomas Hellstrom 		BUG_ON(mob->pt_level > 2);
5200fd53cfbSThomas Hellstrom 		save_pt_iter = pt_iter;
5210fd53cfbSThomas Hellstrom 		num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
5220fd53cfbSThomas Hellstrom 						&pt_iter);
5230fd53cfbSThomas Hellstrom 		data_iter = save_pt_iter;
5243530bdc3SThomas Hellstrom 		num_data_pages = num_pt_pages;
5253530bdc3SThomas Hellstrom 	}
5263530bdc3SThomas Hellstrom 
5270fd53cfbSThomas Hellstrom 	mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
5283530bdc3SThomas Hellstrom 	ttm_bo_unreserve(bo);
5293530bdc3SThomas Hellstrom }
5303530bdc3SThomas Hellstrom 
5313530bdc3SThomas Hellstrom /*
5323530bdc3SThomas Hellstrom  * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
5333530bdc3SThomas Hellstrom  *
5343530bdc3SThomas Hellstrom  * @mob:            Pointer to a mob to destroy.
5353530bdc3SThomas Hellstrom  */
vmw_mob_destroy(struct vmw_mob * mob)5363530bdc3SThomas Hellstrom void vmw_mob_destroy(struct vmw_mob *mob)
5373530bdc3SThomas Hellstrom {
5386034d9d4SThomas Zimmermann 	if (mob->pt_bo) {
539*668b2066SZack Rusin 		vmw_bo_unpin_unlocked(&mob->pt_bo->tbo);
540*668b2066SZack Rusin 		vmw_bo_unreference(&mob->pt_bo);
5416034d9d4SThomas Zimmermann 	}
5423530bdc3SThomas Hellstrom 	kfree(mob);
5433530bdc3SThomas Hellstrom }
5443530bdc3SThomas Hellstrom 
5453530bdc3SThomas Hellstrom /*
5463530bdc3SThomas Hellstrom  * vmw_mob_unbind - Hide a mob from the device.
5473530bdc3SThomas Hellstrom  *
5483530bdc3SThomas Hellstrom  * @dev_priv:       Pointer to a device private.
5493530bdc3SThomas Hellstrom  * @mob_id:         Device id of the mob to unbind.
5503530bdc3SThomas Hellstrom  */
vmw_mob_unbind(struct vmw_private * dev_priv,struct vmw_mob * mob)5513530bdc3SThomas Hellstrom void vmw_mob_unbind(struct vmw_private *dev_priv,
5523530bdc3SThomas Hellstrom 		    struct vmw_mob *mob)
5533530bdc3SThomas Hellstrom {
5543530bdc3SThomas Hellstrom 	struct {
5553530bdc3SThomas Hellstrom 		SVGA3dCmdHeader header;
5563530bdc3SThomas Hellstrom 		SVGA3dCmdDestroyGBMob body;
5573530bdc3SThomas Hellstrom 	} *cmd;
5583530bdc3SThomas Hellstrom 	int ret;
559*668b2066SZack Rusin 	struct ttm_buffer_object *bo = &mob->pt_bo->tbo;
5603530bdc3SThomas Hellstrom 
5613530bdc3SThomas Hellstrom 	if (bo) {
562dfd5e50eSChristian König 		ret = ttm_bo_reserve(bo, false, true, NULL);
5633530bdc3SThomas Hellstrom 		/*
5643530bdc3SThomas Hellstrom 		 * Noone else should be using this buffer.
5653530bdc3SThomas Hellstrom 		 */
5663530bdc3SThomas Hellstrom 		BUG_ON(ret != 0);
5673530bdc3SThomas Hellstrom 	}
5683530bdc3SThomas Hellstrom 
5698426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
57011c45419SDeepak Rawat 	if (cmd) {
5713530bdc3SThomas Hellstrom 		cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
5723530bdc3SThomas Hellstrom 		cmd->header.size = sizeof(cmd->body);
5733530bdc3SThomas Hellstrom 		cmd->body.mobid = mob->id;
5748426ed9cSZack Rusin 		vmw_cmd_commit(dev_priv, sizeof(*cmd));
5756950e23eSAlexey Khoroshilov 	}
57611c45419SDeepak Rawat 
5773530bdc3SThomas Hellstrom 	if (bo) {
578e9431ea5SThomas Hellstrom 		vmw_bo_fence_single(bo, NULL);
5793530bdc3SThomas Hellstrom 		ttm_bo_unreserve(bo);
5803530bdc3SThomas Hellstrom 	}
581153b3d5bSThomas Hellstrom 	vmw_fifo_resource_dec(dev_priv);
5823530bdc3SThomas Hellstrom }
5833530bdc3SThomas Hellstrom 
5843530bdc3SThomas Hellstrom /*
5853530bdc3SThomas Hellstrom  * vmw_mob_bind - Make a mob visible to the device after first
5863530bdc3SThomas Hellstrom  *                populating it if necessary.
5873530bdc3SThomas Hellstrom  *
5883530bdc3SThomas Hellstrom  * @dev_priv:       Pointer to a device private.
5893530bdc3SThomas Hellstrom  * @mob:            Pointer to the mob we're making visible.
5900fd53cfbSThomas Hellstrom  * @data_addr:      Array of DMA addresses to the data pages of the underlying
5913530bdc3SThomas Hellstrom  *                  buffer object.
5923530bdc3SThomas Hellstrom  * @num_data_pages: Number of data pages of the underlying buffer
5933530bdc3SThomas Hellstrom  *                  object.
5943530bdc3SThomas Hellstrom  * @mob_id:         Device id of the mob to bind
5953530bdc3SThomas Hellstrom  *
5963530bdc3SThomas Hellstrom  * This function is intended to be interfaced with the ttm_tt backend
5973530bdc3SThomas Hellstrom  * code.
5983530bdc3SThomas Hellstrom  */
vmw_mob_bind(struct vmw_private * dev_priv,struct vmw_mob * mob,const struct vmw_sg_table * vsgt,unsigned long num_data_pages,int32_t mob_id)5993530bdc3SThomas Hellstrom int vmw_mob_bind(struct vmw_private *dev_priv,
6003530bdc3SThomas Hellstrom 		 struct vmw_mob *mob,
6010fd53cfbSThomas Hellstrom 		 const struct vmw_sg_table *vsgt,
6023530bdc3SThomas Hellstrom 		 unsigned long num_data_pages,
6033530bdc3SThomas Hellstrom 		 int32_t mob_id)
6043530bdc3SThomas Hellstrom {
6053530bdc3SThomas Hellstrom 	int ret;
6063530bdc3SThomas Hellstrom 	bool pt_set_up = false;
6070fd53cfbSThomas Hellstrom 	struct vmw_piter data_iter;
6083530bdc3SThomas Hellstrom 	struct {
6093530bdc3SThomas Hellstrom 		SVGA3dCmdHeader header;
6103e894a62SThomas Hellstrom 		SVGA3dCmdDefineGBMob64 body;
6113530bdc3SThomas Hellstrom 	} *cmd;
6123530bdc3SThomas Hellstrom 
6133530bdc3SThomas Hellstrom 	mob->id = mob_id;
6140fd53cfbSThomas Hellstrom 	vmw_piter_start(&data_iter, vsgt, 0);
6150fd53cfbSThomas Hellstrom 	if (unlikely(!vmw_piter_next(&data_iter)))
6160fd53cfbSThomas Hellstrom 		return 0;
6170fd53cfbSThomas Hellstrom 
6183530bdc3SThomas Hellstrom 	if (likely(num_data_pages == 1)) {
619f2a0dcb1SThomas Hellstrom 		mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
6200fd53cfbSThomas Hellstrom 		mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
6213530bdc3SThomas Hellstrom 	} else if (unlikely(mob->pt_bo == NULL)) {
6223530bdc3SThomas Hellstrom 		ret = vmw_mob_pt_populate(dev_priv, mob);
6233530bdc3SThomas Hellstrom 		if (unlikely(ret != 0))
6243530bdc3SThomas Hellstrom 			return ret;
6253530bdc3SThomas Hellstrom 
6260fd53cfbSThomas Hellstrom 		vmw_mob_pt_setup(mob, data_iter, num_data_pages);
6273530bdc3SThomas Hellstrom 		pt_set_up = true;
628ebc9ac7cSZack Rusin 		mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PT_1;
6293530bdc3SThomas Hellstrom 	}
6303530bdc3SThomas Hellstrom 
631153b3d5bSThomas Hellstrom 	vmw_fifo_resource_inc(dev_priv);
6323530bdc3SThomas Hellstrom 
6338426ed9cSZack Rusin 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
63411c45419SDeepak Rawat 	if (unlikely(cmd == NULL))
6353530bdc3SThomas Hellstrom 		goto out_no_cmd_space;
6363530bdc3SThomas Hellstrom 
6373e894a62SThomas Hellstrom 	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
6383530bdc3SThomas Hellstrom 	cmd->header.size = sizeof(cmd->body);
6393530bdc3SThomas Hellstrom 	cmd->body.mobid = mob_id;
6403530bdc3SThomas Hellstrom 	cmd->body.ptDepth = mob->pt_level;
641b9eb1a61SThomas Hellstrom 	cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
6423530bdc3SThomas Hellstrom 	cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
6433530bdc3SThomas Hellstrom 
6448426ed9cSZack Rusin 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
6453530bdc3SThomas Hellstrom 
6463530bdc3SThomas Hellstrom 	return 0;
6473530bdc3SThomas Hellstrom 
6483530bdc3SThomas Hellstrom out_no_cmd_space:
649153b3d5bSThomas Hellstrom 	vmw_fifo_resource_dec(dev_priv);
6506034d9d4SThomas Zimmermann 	if (pt_set_up) {
651*668b2066SZack Rusin 		vmw_bo_unpin_unlocked(&mob->pt_bo->tbo);
652*668b2066SZack Rusin 		vmw_bo_unreference(&mob->pt_bo);
6536034d9d4SThomas Zimmermann 	}
6543530bdc3SThomas Hellstrom 
6553530bdc3SThomas Hellstrom 	return -ENOMEM;
6563530bdc3SThomas Hellstrom }
657