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