vmwgfx_mob.c 17.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**************************************************************************
 *
 * Copyright © 2012 VMware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "vmwgfx_drv.h"

30 31 32 33
/*
 * If we set up the screen target otable, screen objects stop working.
 */

34 35
#define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE &&	\
			       (dev_priv->capabilities & SVGA_CAP_3D)) ? 0 : 1)
36

37 38 39 40 41 42
#ifdef CONFIG_64BIT
#define VMW_PPN_SIZE 8
#define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH64_0
#define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH64_1
#define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH64_2
#else
43
#define VMW_PPN_SIZE 4
44 45 46 47
#define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH_0
#define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH_1
#define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH_2
#endif
48 49 50 51 52 53 54

/*
 * struct vmw_mob - Structure containing page table and metadata for a
 * Guest Memory OBject.
 *
 * @num_pages       Number of pages that make up the page table.
 * @pt_level        The indirection level of the page table. 0-2.
55
 * @pt_root_page    DMA address of the level 0 page of the page table.
56 57 58 59 60
 */
struct vmw_mob {
	struct ttm_buffer_object *pt_bo;
	unsigned long num_pages;
	unsigned pt_level;
61
	dma_addr_t pt_root_page;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	uint32_t id;
};

/*
 * struct vmw_otable - Guest Memory OBject table metadata
 *
 * @size:           Size of the table (page-aligned).
 * @page_table:     Pointer to a struct vmw_mob holding the page table.
 */
struct vmw_otable {
	unsigned long size;
	struct vmw_mob *page_table;
};

static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
			       struct vmw_mob *mob);
static void vmw_mob_pt_setup(struct vmw_mob *mob,
79
			     struct vmw_piter data_iter,
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
			     unsigned long num_data_pages);

/*
 * vmw_setup_otable_base - Issue an object table base setup command to
 * the device
 *
 * @dev_priv:       Pointer to a device private structure
 * @type:           Type of object table base
 * @offset          Start of table offset into dev_priv::otable_bo
 * @otable          Pointer to otable metadata;
 *
 * This function returns -ENOMEM if it fails to reserve fifo space,
 * and may block waiting for fifo space.
 */
static int vmw_setup_otable_base(struct vmw_private *dev_priv,
				 SVGAOTableType type,
				 unsigned long offset,
				 struct vmw_otable *otable)
{
	struct {
		SVGA3dCmdHeader header;
101
		SVGA3dCmdSetOTableBase64 body;
102 103
	} *cmd;
	struct vmw_mob *mob;
104 105
	const struct vmw_sg_table *vsgt;
	struct vmw_piter iter;
106 107 108 109
	int ret;

	BUG_ON(otable->page_table != NULL);

110 111 112 113
	vsgt = vmw_bo_sg_table(dev_priv->otable_bo);
	vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
	WARN_ON(!vmw_piter_next(&iter));

114 115 116 117 118 119 120
	mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
	if (unlikely(mob == NULL)) {
		DRM_ERROR("Failed creating OTable page table.\n");
		return -ENOMEM;
	}

	if (otable->size <= PAGE_SIZE) {
121
		mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
122 123 124 125
		mob->pt_root_page = vmw_piter_dma_addr(&iter);
	} else if (vsgt->num_regions == 1) {
		mob->pt_level = SVGA3D_MOBFMT_RANGE;
		mob->pt_root_page = vmw_piter_dma_addr(&iter);
126 127 128 129 130
	} else {
		ret = vmw_mob_pt_populate(dev_priv, mob);
		if (unlikely(ret != 0))
			goto out_no_populate;

131
		vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
132
		mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
133 134 135 136 137
	}

	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Failed reserving FIFO space for OTable setup.\n");
138
		ret = -ENOMEM;
139 140 141 142
		goto out_no_fifo;
	}

	memset(cmd, 0, sizeof(*cmd));
143
	cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
144 145
	cmd->header.size = sizeof(cmd->body);
	cmd->body.type = type;
146
	cmd->body.baseAddress = cpu_to_le64(mob->pt_root_page >> PAGE_SHIFT);
147 148 149 150
	cmd->body.sizeInBytes = otable->size;
	cmd->body.validSizeInBytes = 0;
	cmd->body.ptDepth = mob->pt_level;

151 152 153 154 155 156 157
	/*
	 * The device doesn't support this, But the otable size is
	 * determined at compile-time, so this BUG shouldn't trigger
	 * randomly.
	 */
	BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	vmw_fifo_commit(dev_priv, sizeof(*cmd));
	otable->page_table = mob;

	return 0;

out_no_fifo:
out_no_populate:
	vmw_mob_destroy(mob);
	return ret;
}

/*
 * vmw_takedown_otable_base - Issue an object table base takedown command
 * to the device
 *
 * @dev_priv:       Pointer to a device private structure
 * @type:           Type of object table base
 *
 */
static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
				     SVGAOTableType type,
				     struct vmw_otable *otable)
{
	struct {
		SVGA3dCmdHeader header;
		SVGA3dCmdSetOTableBase body;
	} *cmd;
185
	struct ttm_buffer_object *bo;
186 187 188 189

	if (otable->page_table == NULL)
		return;

190
	bo = otable->page_table->pt_bo;
191
	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
192 193 194
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Failed reserving FIFO space for OTable "
			  "takedown.\n");
195
		return;
196
	}
197 198 199 200 201 202 203 204 205 206
 
	memset(cmd, 0, sizeof(*cmd));
	cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE;
	cmd->header.size = sizeof(cmd->body);
	cmd->body.type = type;
	cmd->body.baseAddress = 0;
	cmd->body.sizeInBytes = 0;
	cmd->body.validSizeInBytes = 0;
	cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID;
	vmw_fifo_commit(dev_priv, sizeof(*cmd));
207 208 209 210

	if (bo) {
		int ret;

211
		ret = ttm_bo_reserve(bo, false, true, false, NULL);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		BUG_ON(ret != 0);

		vmw_fence_single_bo(bo, NULL);
		ttm_bo_unreserve(bo);
	}

	vmw_mob_destroy(otable->page_table);
	otable->page_table = NULL;
}

/*
 * vmw_otables_setup - Set up guest backed memory object tables
 *
 * @dev_priv:       Pointer to a device private structure
 *
 * Takes care of the device guest backed surface
 * initialization, by setting up the guest backed memory object tables.
 * Returns 0 on success and various error codes on failure. A succesful return
 * means the object tables can be taken down using the vmw_otables_takedown
 * function.
 */
int vmw_otables_setup(struct vmw_private *dev_priv)
{
	unsigned long offset;
	unsigned long bo_size;
	struct vmw_otable *otables;
	SVGAOTableType i;
	int ret;

241
	otables = kzalloc(SVGA_OTABLE_DX9_MAX * sizeof(*otables),
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
			  GFP_KERNEL);
	if (unlikely(otables == NULL)) {
		DRM_ERROR("Failed to allocate space for otable "
			  "metadata.\n");
		return -ENOMEM;
	}

	otables[SVGA_OTABLE_MOB].size =
		VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE;
	otables[SVGA_OTABLE_SURFACE].size =
		VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE;
	otables[SVGA_OTABLE_CONTEXT].size =
		VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE;
	otables[SVGA_OTABLE_SHADER].size =
		VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE;
257 258 259
	otables[SVGA_OTABLE_SCREEN_TARGET].size =
		VMWGFX_NUM_GB_SCREEN_TARGET *
		SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE;
260 261

	bo_size = 0;
262
	for (i = 0; i < SVGA_OTABLE_DX9_MAX; ++i) {
263 264 265 266 267 268 269 270 271 272 273 274 275 276
		otables[i].size =
			(otables[i].size + PAGE_SIZE - 1) & PAGE_MASK;
		bo_size += otables[i].size;
	}

	ret = ttm_bo_create(&dev_priv->bdev, bo_size,
			    ttm_bo_type_device,
			    &vmw_sys_ne_placement,
			    0, false, NULL,
			    &dev_priv->otable_bo);

	if (unlikely(ret != 0))
		goto out_no_bo;

277
	ret = ttm_bo_reserve(dev_priv->otable_bo, false, true, false, NULL);
278 279 280
	BUG_ON(ret != 0);
	ret = vmw_bo_driver.ttm_tt_populate(dev_priv->otable_bo->ttm);
	if (unlikely(ret != 0))
281 282 283 284 285 286
		goto out_unreserve;
	ret = vmw_bo_map_dma(dev_priv->otable_bo);
	if (unlikely(ret != 0))
		goto out_unreserve;

	ttm_bo_unreserve(dev_priv->otable_bo);
287 288

	offset = 0;
289
	for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i) {
290 291 292 293 294 295 296 297 298 299
		ret = vmw_setup_otable_base(dev_priv, i, offset,
					    &otables[i]);
		if (unlikely(ret != 0))
			goto out_no_setup;
		offset += otables[i].size;
	}

	dev_priv->otables = otables;
	return 0;

300 301
out_unreserve:
	ttm_bo_unreserve(dev_priv->otable_bo);
302
out_no_setup:
303
	for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		vmw_takedown_otable_base(dev_priv, i, &otables[i]);

	ttm_bo_unref(&dev_priv->otable_bo);
out_no_bo:
	kfree(otables);
	return ret;
}


/*
 * vmw_otables_takedown - Take down guest backed memory object tables
 *
 * @dev_priv:       Pointer to a device private structure
 *
 * Take down the Guest Memory Object tables.
 */
void vmw_otables_takedown(struct vmw_private *dev_priv)
{
	SVGAOTableType i;
	struct ttm_buffer_object *bo = dev_priv->otable_bo;
	int ret;

326
	for (i = 0; i < SVGA_OTABLE_DX9_MAX - VMW_OTABLE_SETUP_SUB; ++i)
327 328 329
		vmw_takedown_otable_base(dev_priv, i,
					 &dev_priv->otables[i]);

330
	ret = ttm_bo_reserve(bo, false, true, false, NULL);
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
	BUG_ON(ret != 0);

	vmw_fence_single_bo(bo, NULL);
	ttm_bo_unreserve(bo);

	ttm_bo_unref(&dev_priv->otable_bo);
	kfree(dev_priv->otables);
	dev_priv->otables = NULL;
}


/*
 * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
 * needed for a guest backed memory object.
 *
 * @data_pages:  Number of data pages in the memory object buffer.
 */
static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
{
	unsigned long data_size = data_pages * PAGE_SIZE;
	unsigned long tot_size = 0;

	while (likely(data_size > PAGE_SIZE)) {
		data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
		data_size *= VMW_PPN_SIZE;
		tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
	}

	return tot_size >> PAGE_SHIFT;
}

/*
 * vmw_mob_create - Create a mob, but don't populate it.
 *
 * @data_pages:  Number of data pages of the underlying buffer object.
 */
struct vmw_mob *vmw_mob_create(unsigned long data_pages)
{
	struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);

	if (unlikely(mob == NULL))
		return NULL;

	mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);

	return mob;
}

/*
 * vmw_mob_pt_populate - Populate the mob pagetable
 *
 * @mob:         Pointer to the mob the pagetable of which we want to
 *               populate.
 *
 * This function allocates memory to be used for the pagetable, and
 * adjusts TTM memory accounting accordingly. Returns ENOMEM if
 * memory resources aren't sufficient and may cause TTM buffer objects
 * to be swapped out by using the TTM memory accounting function.
 */
static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
			       struct vmw_mob *mob)
{
	int ret;
	BUG_ON(mob->pt_bo != NULL);

	ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE,
			    ttm_bo_type_device,
			    &vmw_sys_ne_placement,
			    0, false, NULL, &mob->pt_bo);
	if (unlikely(ret != 0))
		return ret;

403
	ret = ttm_bo_reserve(mob->pt_bo, false, true, false, NULL);
404 405 406 407

	BUG_ON(ret != 0);
	ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm);
	if (unlikely(ret != 0))
408 409 410 411 412 413 414 415 416 417 418 419
		goto out_unreserve;
	ret = vmw_bo_map_dma(mob->pt_bo);
	if (unlikely(ret != 0))
		goto out_unreserve;

	ttm_bo_unreserve(mob->pt_bo);
	
	return 0;

out_unreserve:
	ttm_bo_unreserve(mob->pt_bo);
	ttm_bo_unref(&mob->pt_bo);
420 421 422 423

	return ret;
}

424 425 426 427 428 429 430 431 432 433
/**
 * vmw_mob_assign_ppn - Assign a value to a page table entry
 *
 * @addr: Pointer to pointer to page table entry.
 * @val: The page table entry
 *
 * Assigns a value to a page table entry pointed to by *@addr and increments
 * *@addr according to the page table entry size.
 */
#if (VMW_PPN_SIZE == 8)
434
static void vmw_mob_assign_ppn(__le32 **addr, dma_addr_t val)
435
{
436
	*((__le64 *) *addr) = cpu_to_le64(val >> PAGE_SHIFT);
437 438 439
	*addr += 2;
}
#else
440
static void vmw_mob_assign_ppn(__le32 **addr, dma_addr_t val)
441
{
442
	*(*addr)++ = cpu_to_le32(val >> PAGE_SHIFT);
443 444
}
#endif
445 446 447 448

/*
 * vmw_mob_build_pt - Build a pagetable
 *
449
 * @data_addr:      Array of DMA addresses to the underlying buffer
450 451 452 453 454 455 456
 *                  object's data pages.
 * @num_data_pages: Number of buffer object data pages.
 * @pt_pages:       Array of page pointers to the page table pages.
 *
 * Returns the number of page table pages actually used.
 * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
 */
457
static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
458
				      unsigned long num_data_pages,
459
				      struct vmw_piter *pt_iter)
460 461 462
{
	unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
	unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
463
	unsigned long pt_page;
464
	__le32 *addr, *save_addr;
465
	unsigned long i;
466
	struct page *page;
467 468

	for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
469 470 471
		page = vmw_piter_page(pt_iter);

		save_addr = addr = kmap_atomic(page);
472 473

		for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
474 475
			vmw_mob_assign_ppn(&addr,
					   vmw_piter_dma_addr(data_iter));
476
			if (unlikely(--num_data_pages == 0))
477
				break;
478
			WARN_ON(!vmw_piter_next(data_iter));
479 480
		}
		kunmap_atomic(save_addr);
481
		vmw_piter_next(pt_iter);
482 483 484 485 486 487 488 489 490
	}

	return num_pt_pages;
}

/*
 * vmw_mob_build_pt - Set up a multilevel mob pagetable
 *
 * @mob:            Pointer to a mob whose page table needs setting up.
491
 * @data_addr       Array of DMA addresses to the buffer object's data
492 493 494 495 496 497
 *                  pages.
 * @num_data_pages: Number of buffer object data pages.
 *
 * Uses tail recursion to set up a multilevel mob page table.
 */
static void vmw_mob_pt_setup(struct vmw_mob *mob,
498
			     struct vmw_piter data_iter,
499 500 501 502
			     unsigned long num_data_pages)
{
	unsigned long num_pt_pages = 0;
	struct ttm_buffer_object *bo = mob->pt_bo;
503 504 505
	struct vmw_piter save_pt_iter;
	struct vmw_piter pt_iter;
	const struct vmw_sg_table *vsgt;
506 507
	int ret;

508
	ret = ttm_bo_reserve(bo, false, true, false, NULL);
509 510
	BUG_ON(ret != 0);

511 512 513
	vsgt = vmw_bo_sg_table(bo);
	vmw_piter_start(&pt_iter, vsgt, 0);
	BUG_ON(!vmw_piter_next(&pt_iter));
514 515 516 517
	mob->pt_level = 0;
	while (likely(num_data_pages > 1)) {
		++mob->pt_level;
		BUG_ON(mob->pt_level > 2);
518 519 520 521
		save_pt_iter = pt_iter;
		num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
						&pt_iter);
		data_iter = save_pt_iter;
522 523 524
		num_data_pages = num_pt_pages;
	}

525
	mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	ttm_bo_unreserve(bo);
}

/*
 * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
 *
 * @mob:            Pointer to a mob to destroy.
 */
void vmw_mob_destroy(struct vmw_mob *mob)
{
	if (mob->pt_bo)
		ttm_bo_unref(&mob->pt_bo);
	kfree(mob);
}

/*
 * vmw_mob_unbind - Hide a mob from the device.
 *
 * @dev_priv:       Pointer to a device private.
 * @mob_id:         Device id of the mob to unbind.
 */
void vmw_mob_unbind(struct vmw_private *dev_priv,
		    struct vmw_mob *mob)
{
	struct {
		SVGA3dCmdHeader header;
		SVGA3dCmdDestroyGBMob body;
	} *cmd;
	int ret;
	struct ttm_buffer_object *bo = mob->pt_bo;

	if (bo) {
558
		ret = ttm_bo_reserve(bo, false, true, false, NULL);
559 560 561 562 563 564 565 566 567 568
		/*
		 * Noone else should be using this buffer.
		 */
		BUG_ON(ret != 0);
	}

	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Failed reserving FIFO space for Memory "
			  "Object unbinding.\n");
569 570 571 572 573
	} else {
		cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
		cmd->header.size = sizeof(cmd->body);
		cmd->body.mobid = mob->id;
		vmw_fifo_commit(dev_priv, sizeof(*cmd));
574 575 576 577 578
	}
	if (bo) {
		vmw_fence_single_bo(bo, NULL);
		ttm_bo_unreserve(bo);
	}
579
	vmw_fifo_resource_dec(dev_priv);
580 581 582 583 584 585 586 587
}

/*
 * vmw_mob_bind - Make a mob visible to the device after first
 *                populating it if necessary.
 *
 * @dev_priv:       Pointer to a device private.
 * @mob:            Pointer to the mob we're making visible.
588
 * @data_addr:      Array of DMA addresses to the data pages of the underlying
589 590 591 592 593 594 595 596 597 598
 *                  buffer object.
 * @num_data_pages: Number of data pages of the underlying buffer
 *                  object.
 * @mob_id:         Device id of the mob to bind
 *
 * This function is intended to be interfaced with the ttm_tt backend
 * code.
 */
int vmw_mob_bind(struct vmw_private *dev_priv,
		 struct vmw_mob *mob,
599
		 const struct vmw_sg_table *vsgt,
600 601 602 603 604
		 unsigned long num_data_pages,
		 int32_t mob_id)
{
	int ret;
	bool pt_set_up = false;
605
	struct vmw_piter data_iter;
606 607
	struct {
		SVGA3dCmdHeader header;
608
		SVGA3dCmdDefineGBMob64 body;
609 610 611
	} *cmd;

	mob->id = mob_id;
612 613 614 615
	vmw_piter_start(&data_iter, vsgt, 0);
	if (unlikely(!vmw_piter_next(&data_iter)))
		return 0;

616
	if (likely(num_data_pages == 1)) {
617
		mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
618 619 620 621
		mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
	} else if (vsgt->num_regions == 1) {
		mob->pt_level = SVGA3D_MOBFMT_RANGE;
		mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
622 623 624 625 626
	} else if (unlikely(mob->pt_bo == NULL)) {
		ret = vmw_mob_pt_populate(dev_priv, mob);
		if (unlikely(ret != 0))
			return ret;

627
		vmw_mob_pt_setup(mob, data_iter, num_data_pages);
628
		pt_set_up = true;
629
		mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
630 631
	}

632
	vmw_fifo_resource_inc(dev_priv);
633 634 635 636 637 638 639 640

	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
	if (unlikely(cmd == NULL)) {
		DRM_ERROR("Failed reserving FIFO space for Memory "
			  "Object binding.\n");
		goto out_no_cmd_space;
	}

641
	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
642 643 644
	cmd->header.size = sizeof(cmd->body);
	cmd->body.mobid = mob_id;
	cmd->body.ptDepth = mob->pt_level;
645
	cmd->body.base = cpu_to_le64(mob->pt_root_page >> PAGE_SHIFT);
646 647 648 649 650 651 652
	cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;

	vmw_fifo_commit(dev_priv, sizeof(*cmd));

	return 0;

out_no_cmd_space:
653
	vmw_fifo_resource_dec(dev_priv);
654 655 656 657 658
	if (pt_set_up)
		ttm_bo_unref(&mob->pt_bo);

	return -ENOMEM;
}