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