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