Linux Audio

Check our new training course

Loading...
v5.4
  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 */
 58struct 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 */
 72static 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
 81static 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
 91static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
 92			       struct vmw_mob *mob);
 93static 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 */
109static 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_FIFO_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_fifo_commit(dev_priv, sizeof(*cmd));
174	otable->page_table = mob;
175
176	return 0;
177
178out_no_fifo:
179out_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 */
192static 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_FIFO_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_fifo_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
235static 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	struct ttm_operation_ctx ctx = {
242		.interruptible = false,
243		.no_wait_gpu = false
244	};
245	SVGAOTableType i;
246	int ret;
247
248	bo_size = 0;
249	for (i = 0; i < batch->num_otables; ++i) {
250		if (!otables[i].enabled)
251			continue;
252
253		otables[i].size =
254			(otables[i].size + PAGE_SIZE - 1) & PAGE_MASK;
255		bo_size += otables[i].size;
256	}
257
258	ret = ttm_bo_create(&dev_priv->bdev, bo_size,
259			    ttm_bo_type_device,
260			    &vmw_sys_ne_placement,
261			    0, false, &batch->otable_bo);
262
263	if (unlikely(ret != 0))
264		goto out_no_bo;
265
266	ret = ttm_bo_reserve(batch->otable_bo, false, true, NULL);
267	BUG_ON(ret != 0);
268	ret = vmw_bo_driver.ttm_tt_populate(batch->otable_bo->ttm, &ctx);
269	if (unlikely(ret != 0))
270		goto out_unreserve;
271	ret = vmw_bo_map_dma(batch->otable_bo);
272	if (unlikely(ret != 0))
273		goto out_unreserve;
274
275	ttm_bo_unreserve(batch->otable_bo);
276
277	offset = 0;
278	for (i = 0; i < batch->num_otables; ++i) {
279		if (!batch->otables[i].enabled)
280			continue;
281
282		ret = vmw_setup_otable_base(dev_priv, i, batch->otable_bo,
 
283					    offset,
284					    &otables[i]);
285		if (unlikely(ret != 0))
286			goto out_no_setup;
287		offset += otables[i].size;
288	}
289
290	return 0;
291
292out_unreserve:
293	ttm_bo_unreserve(batch->otable_bo);
294out_no_setup:
295	for (i = 0; i < batch->num_otables; ++i) {
296		if (batch->otables[i].enabled)
297			vmw_takedown_otable_base(dev_priv, i,
298						 &batch->otables[i]);
299	}
300
301	ttm_bo_put(batch->otable_bo);
 
302	batch->otable_bo = NULL;
303out_no_bo:
304	return ret;
305}
306
307/*
308 * vmw_otables_setup - Set up guest backed memory object tables
309 *
310 * @dev_priv:       Pointer to a device private structure
311 *
312 * Takes care of the device guest backed surface
313 * initialization, by setting up the guest backed memory object tables.
314 * Returns 0 on success and various error codes on failure. A successful return
315 * means the object tables can be taken down using the vmw_otables_takedown
316 * function.
317 */
318int vmw_otables_setup(struct vmw_private *dev_priv)
319{
320	struct vmw_otable **otables = &dev_priv->otable_batch.otables;
321	int ret;
322
323	if (dev_priv->has_dx) {
324		*otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
325		if (!(*otables))
326			return -ENOMEM;
327
328		dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
329	} else {
330		*otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
331				   GFP_KERNEL);
332		if (!(*otables))
333			return -ENOMEM;
334
335		dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
336	}
337
338	ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
339	if (unlikely(ret != 0))
340		goto out_setup;
341
342	return 0;
343
344out_setup:
345	kfree(*otables);
346	return ret;
347}
348
349static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
350			       struct vmw_otable_batch *batch)
351{
352	SVGAOTableType i;
353	struct ttm_buffer_object *bo = batch->otable_bo;
354	int ret;
355
356	for (i = 0; i < batch->num_otables; ++i)
357		if (batch->otables[i].enabled)
358			vmw_takedown_otable_base(dev_priv, i,
359						 &batch->otables[i]);
360
361	ret = ttm_bo_reserve(bo, false, true, NULL);
362	BUG_ON(ret != 0);
363
364	vmw_bo_fence_single(bo, NULL);
 
365	ttm_bo_unreserve(bo);
366
367	ttm_bo_put(batch->otable_bo);
368	batch->otable_bo = NULL;
369}
370
371/*
372 * vmw_otables_takedown - Take down guest backed memory object tables
373 *
374 * @dev_priv:       Pointer to a device private structure
375 *
376 * Take down the Guest Memory Object tables.
377 */
378void vmw_otables_takedown(struct vmw_private *dev_priv)
379{
380	vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
381	kfree(dev_priv->otable_batch.otables);
382}
383
384/*
385 * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
386 * needed for a guest backed memory object.
387 *
388 * @data_pages:  Number of data pages in the memory object buffer.
389 */
390static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
391{
392	unsigned long data_size = data_pages * PAGE_SIZE;
393	unsigned long tot_size = 0;
394
395	while (likely(data_size > PAGE_SIZE)) {
396		data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
397		data_size *= VMW_PPN_SIZE;
398		tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
399	}
400
401	return tot_size >> PAGE_SHIFT;
402}
403
404/*
405 * vmw_mob_create - Create a mob, but don't populate it.
406 *
407 * @data_pages:  Number of data pages of the underlying buffer object.
408 */
409struct vmw_mob *vmw_mob_create(unsigned long data_pages)
410{
411	struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
412
413	if (unlikely(!mob))
414		return NULL;
415
416	mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
417
418	return mob;
419}
420
421/*
422 * vmw_mob_pt_populate - Populate the mob pagetable
423 *
424 * @mob:         Pointer to the mob the pagetable of which we want to
425 *               populate.
426 *
427 * This function allocates memory to be used for the pagetable, and
428 * adjusts TTM memory accounting accordingly. Returns ENOMEM if
429 * memory resources aren't sufficient and may cause TTM buffer objects
430 * to be swapped out by using the TTM memory accounting function.
431 */
432static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
433			       struct vmw_mob *mob)
434{
435	int ret;
436	struct ttm_operation_ctx ctx = {
437		.interruptible = false,
438		.no_wait_gpu = false
439	};
440
441	BUG_ON(mob->pt_bo != NULL);
442
443	ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE,
444			    ttm_bo_type_device,
445			    &vmw_sys_ne_placement,
446			    0, false, &mob->pt_bo);
447	if (unlikely(ret != 0))
448		return ret;
449
450	ret = ttm_bo_reserve(mob->pt_bo, false, true, NULL);
451
452	BUG_ON(ret != 0);
453	ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm, &ctx);
454	if (unlikely(ret != 0))
455		goto out_unreserve;
456	ret = vmw_bo_map_dma(mob->pt_bo);
457	if (unlikely(ret != 0))
458		goto out_unreserve;
459
460	ttm_bo_unreserve(mob->pt_bo);
461
462	return 0;
463
464out_unreserve:
465	ttm_bo_unreserve(mob->pt_bo);
466	ttm_bo_put(mob->pt_bo);
467	mob->pt_bo = NULL;
468
469	return ret;
470}
471
472/**
473 * vmw_mob_assign_ppn - Assign a value to a page table entry
474 *
475 * @addr: Pointer to pointer to page table entry.
476 * @val: The page table entry
477 *
478 * Assigns a value to a page table entry pointed to by *@addr and increments
479 * *@addr according to the page table entry size.
480 */
481#if (VMW_PPN_SIZE == 8)
482static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
483{
484	*((u64 *) *addr) = val >> PAGE_SHIFT;
485	*addr += 2;
486}
487#else
488static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
489{
490	*(*addr)++ = val >> PAGE_SHIFT;
491}
492#endif
493
494/*
495 * vmw_mob_build_pt - Build a pagetable
496 *
497 * @data_addr:      Array of DMA addresses to the underlying buffer
498 *                  object's data pages.
499 * @num_data_pages: Number of buffer object data pages.
500 * @pt_pages:       Array of page pointers to the page table pages.
501 *
502 * Returns the number of page table pages actually used.
503 * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
504 */
505static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
506				      unsigned long num_data_pages,
507				      struct vmw_piter *pt_iter)
508{
509	unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
510	unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
511	unsigned long pt_page;
512	u32 *addr, *save_addr;
513	unsigned long i;
514	struct page *page;
515
516	for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
517		page = vmw_piter_page(pt_iter);
518
519		save_addr = addr = kmap_atomic(page);
520
521		for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
522			vmw_mob_assign_ppn(&addr,
523					   vmw_piter_dma_addr(data_iter));
524			if (unlikely(--num_data_pages == 0))
525				break;
526			WARN_ON(!vmw_piter_next(data_iter));
527		}
528		kunmap_atomic(save_addr);
529		vmw_piter_next(pt_iter);
530	}
531
532	return num_pt_pages;
533}
534
535/*
536 * vmw_mob_build_pt - Set up a multilevel mob pagetable
537 *
538 * @mob:            Pointer to a mob whose page table needs setting up.
539 * @data_addr       Array of DMA addresses to the buffer object's data
540 *                  pages.
541 * @num_data_pages: Number of buffer object data pages.
542 *
543 * Uses tail recursion to set up a multilevel mob page table.
544 */
545static void vmw_mob_pt_setup(struct vmw_mob *mob,
546			     struct vmw_piter data_iter,
547			     unsigned long num_data_pages)
548{
549	unsigned long num_pt_pages = 0;
550	struct ttm_buffer_object *bo = mob->pt_bo;
551	struct vmw_piter save_pt_iter;
552	struct vmw_piter pt_iter;
553	const struct vmw_sg_table *vsgt;
554	int ret;
555
 
 
556	ret = ttm_bo_reserve(bo, false, true, NULL);
557	BUG_ON(ret != 0);
558
559	vsgt = vmw_bo_sg_table(bo);
560	vmw_piter_start(&pt_iter, vsgt, 0);
561	BUG_ON(!vmw_piter_next(&pt_iter));
562	mob->pt_level = 0;
563	while (likely(num_data_pages > 1)) {
564		++mob->pt_level;
565		BUG_ON(mob->pt_level > 2);
566		save_pt_iter = pt_iter;
567		num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
568						&pt_iter);
569		data_iter = save_pt_iter;
570		num_data_pages = num_pt_pages;
571	}
572
573	mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
574	ttm_bo_unreserve(bo);
575}
576
577/*
578 * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
579 *
580 * @mob:            Pointer to a mob to destroy.
581 */
582void vmw_mob_destroy(struct vmw_mob *mob)
583{
584	if (mob->pt_bo) {
585		ttm_bo_put(mob->pt_bo);
586		mob->pt_bo = NULL;
587	}
588	kfree(mob);
589}
590
591/*
592 * vmw_mob_unbind - Hide a mob from the device.
593 *
594 * @dev_priv:       Pointer to a device private.
595 * @mob_id:         Device id of the mob to unbind.
596 */
597void vmw_mob_unbind(struct vmw_private *dev_priv,
598		    struct vmw_mob *mob)
599{
600	struct {
601		SVGA3dCmdHeader header;
602		SVGA3dCmdDestroyGBMob body;
603	} *cmd;
604	int ret;
605	struct ttm_buffer_object *bo = mob->pt_bo;
606
607	if (bo) {
608		ret = ttm_bo_reserve(bo, false, true, NULL);
609		/*
610		 * Noone else should be using this buffer.
611		 */
612		BUG_ON(ret != 0);
613	}
614
615	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
616	if (cmd) {
617		cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
618		cmd->header.size = sizeof(cmd->body);
619		cmd->body.mobid = mob->id;
620		vmw_fifo_commit(dev_priv, sizeof(*cmd));
621	}
622
623	if (bo) {
624		vmw_bo_fence_single(bo, NULL);
625		ttm_bo_unreserve(bo);
626	}
627	vmw_fifo_resource_dec(dev_priv);
628}
629
630/*
631 * vmw_mob_bind - Make a mob visible to the device after first
632 *                populating it if necessary.
633 *
634 * @dev_priv:       Pointer to a device private.
635 * @mob:            Pointer to the mob we're making visible.
636 * @data_addr:      Array of DMA addresses to the data pages of the underlying
637 *                  buffer object.
638 * @num_data_pages: Number of data pages of the underlying buffer
639 *                  object.
640 * @mob_id:         Device id of the mob to bind
641 *
642 * This function is intended to be interfaced with the ttm_tt backend
643 * code.
644 */
645int vmw_mob_bind(struct vmw_private *dev_priv,
646		 struct vmw_mob *mob,
647		 const struct vmw_sg_table *vsgt,
648		 unsigned long num_data_pages,
649		 int32_t mob_id)
650{
651	int ret;
652	bool pt_set_up = false;
653	struct vmw_piter data_iter;
654	struct {
655		SVGA3dCmdHeader header;
656		SVGA3dCmdDefineGBMob64 body;
657	} *cmd;
658
659	mob->id = mob_id;
660	vmw_piter_start(&data_iter, vsgt, 0);
661	if (unlikely(!vmw_piter_next(&data_iter)))
662		return 0;
663
664	if (likely(num_data_pages == 1)) {
665		mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
666		mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
667	} else if (vsgt->num_regions == 1) {
668		mob->pt_level = SVGA3D_MOBFMT_RANGE;
669		mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
670	} else if (unlikely(mob->pt_bo == NULL)) {
671		ret = vmw_mob_pt_populate(dev_priv, mob);
672		if (unlikely(ret != 0))
673			return ret;
674
675		vmw_mob_pt_setup(mob, data_iter, num_data_pages);
676		pt_set_up = true;
677		mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
678	}
679
680	vmw_fifo_resource_inc(dev_priv);
681
682	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
683	if (unlikely(cmd == NULL))
684		goto out_no_cmd_space;
685
686	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
687	cmd->header.size = sizeof(cmd->body);
688	cmd->body.mobid = mob_id;
689	cmd->body.ptDepth = mob->pt_level;
690	cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
691	cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
692
693	vmw_fifo_commit(dev_priv, sizeof(*cmd));
694
695	return 0;
696
697out_no_cmd_space:
698	vmw_fifo_resource_dec(dev_priv);
699	if (pt_set_up) {
700		ttm_bo_put(mob->pt_bo);
701		mob->pt_bo = NULL;
702	}
703
704	return -ENOMEM;
705}
v6.13.7
  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 */
 53struct 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 */
 67static 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
 76static 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
 86static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
 87			       struct vmw_mob *mob);
 88static void vmw_mob_pt_setup(struct vmw_mob *mob,
 89			     struct vmw_piter data_iter,
 90			     unsigned long num_data_pages);
 91
 92
 93static 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 */
114static 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
180out_no_fifo:
181out_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 */
194static 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
237static 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
 
 
277out_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	ttm_bo_put(&batch->otable_bo->tbo);
286	batch->otable_bo = NULL;
 
287	return ret;
288}
289
290/*
291 * vmw_otables_setup - Set up guest backed memory object tables
292 *
293 * @dev_priv:       Pointer to a device private structure
294 *
295 * Takes care of the device guest backed surface
296 * initialization, by setting up the guest backed memory object tables.
297 * Returns 0 on success and various error codes on failure. A successful return
298 * means the object tables can be taken down using the vmw_otables_takedown
299 * function.
300 */
301int vmw_otables_setup(struct vmw_private *dev_priv)
302{
303	struct vmw_otable **otables = &dev_priv->otable_batch.otables;
304	int ret;
305
306	if (has_sm4_context(dev_priv)) {
307		*otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
308		if (!(*otables))
309			return -ENOMEM;
310
311		dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
312	} else {
313		*otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
314				   GFP_KERNEL);
315		if (!(*otables))
316			return -ENOMEM;
317
318		dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
319	}
320
321	ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
322	if (unlikely(ret != 0))
323		goto out_setup;
324
325	return 0;
326
327out_setup:
328	kfree(*otables);
329	return ret;
330}
331
332static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
333			       struct vmw_otable_batch *batch)
334{
335	SVGAOTableType i;
336	struct ttm_buffer_object *bo = &batch->otable_bo->tbo;
337	int ret;
338
339	for (i = 0; i < batch->num_otables; ++i)
340		if (batch->otables[i].enabled)
341			vmw_takedown_otable_base(dev_priv, i,
342						 &batch->otables[i]);
343
344	ret = ttm_bo_reserve(bo, false, true, NULL);
345	BUG_ON(ret != 0);
346
347	vmw_bo_fence_single(bo, NULL);
348	ttm_bo_unpin(bo);
349	ttm_bo_unreserve(bo);
350
351	vmw_bo_unreference(&batch->otable_bo);
 
352}
353
354/*
355 * vmw_otables_takedown - Take down guest backed memory object tables
356 *
357 * @dev_priv:       Pointer to a device private structure
358 *
359 * Take down the Guest Memory Object tables.
360 */
361void vmw_otables_takedown(struct vmw_private *dev_priv)
362{
363	vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
364	kfree(dev_priv->otable_batch.otables);
365}
366
367/*
368 * vmw_mob_calculate_pt_pages - Calculate the number of page table pages
369 * needed for a guest backed memory object.
370 *
371 * @data_pages:  Number of data pages in the memory object buffer.
372 */
373static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
374{
375	unsigned long data_size = data_pages * PAGE_SIZE;
376	unsigned long tot_size = 0;
377
378	while (likely(data_size > PAGE_SIZE)) {
379		data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
380		data_size *= VMW_PPN_SIZE;
381		tot_size += PFN_ALIGN(data_size);
382	}
383
384	return tot_size >> PAGE_SHIFT;
385}
386
387/*
388 * vmw_mob_create - Create a mob, but don't populate it.
389 *
390 * @data_pages:  Number of data pages of the underlying buffer object.
391 */
392struct vmw_mob *vmw_mob_create(unsigned long data_pages)
393{
394	struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
395
396	if (unlikely(!mob))
397		return NULL;
398
399	mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
400
401	return mob;
402}
403
404/*
405 * vmw_mob_pt_populate - Populate the mob pagetable
406 *
407 * @mob:         Pointer to the mob the pagetable of which we want to
408 *               populate.
409 *
410 * This function allocates memory to be used for the pagetable.
411 * Returns ENOMEM if memory resources aren't sufficient and may
412 * cause TTM buffer objects to be swapped out.
 
413 */
414static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
415			       struct vmw_mob *mob)
416{
 
 
 
 
 
 
417	BUG_ON(mob->pt_bo != NULL);
418
419	return vmw_bo_create_and_populate(dev_priv, mob->num_pages * PAGE_SIZE,
420					  VMW_BO_DOMAIN_WAITABLE_SYS,
421					  &mob->pt_bo);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422}
423
424/**
425 * vmw_mob_assign_ppn - Assign a value to a page table entry
426 *
427 * @addr: Pointer to pointer to page table entry.
428 * @val: The page table entry
429 *
430 * Assigns a value to a page table entry pointed to by *@addr and increments
431 * *@addr according to the page table entry size.
432 */
433#if (VMW_PPN_SIZE == 8)
434static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
435{
436	*((u64 *) *addr) = val >> PAGE_SHIFT;
437	*addr += 2;
438}
439#else
440static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
441{
442	*(*addr)++ = val >> PAGE_SHIFT;
443}
444#endif
445
446/*
447 * vmw_mob_build_pt - Build a pagetable
448 *
449 * @data_addr:      Array of DMA addresses to the underlying buffer
450 *                  object's data pages.
451 * @num_data_pages: Number of buffer object data pages.
452 * @pt_pages:       Array of page pointers to the page table pages.
453 *
454 * Returns the number of page table pages actually used.
455 * Uses atomic kmaps of highmem pages to avoid TLB thrashing.
456 */
457static 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	unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
462	unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
463	unsigned long pt_page;
464	u32 *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		page = vmw_piter_page(pt_iter);
470
471		save_addr = addr = kmap_atomic(page);
472
473		for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
474			vmw_mob_assign_ppn(&addr,
475					   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	return num_pt_pages;
485}
486
487/*
488 * vmw_mob_build_pt - Set up a multilevel mob pagetable
489 *
490 * @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 *                  pages.
493 * @num_data_pages: Number of buffer object data pages.
494 *
495 * Uses tail recursion to set up a multilevel mob page table.
496 */
497static void vmw_mob_pt_setup(struct vmw_mob *mob,
498			     struct vmw_piter data_iter,
499			     unsigned long num_data_pages)
500{
501	unsigned long num_pt_pages = 0;
502	struct ttm_buffer_object *bo = &mob->pt_bo->tbo;
503	struct vmw_piter save_pt_iter = {0};
504	struct vmw_piter pt_iter;
505	const struct vmw_sg_table *vsgt;
506	int ret;
507
508	BUG_ON(num_data_pages == 0);
509
510	ret = ttm_bo_reserve(bo, false, true, NULL);
511	BUG_ON(ret != 0);
512
513	vsgt = vmw_bo_sg_table(bo);
514	vmw_piter_start(&pt_iter, vsgt, 0);
515	BUG_ON(!vmw_piter_next(&pt_iter));
516	mob->pt_level = 0;
517	while (likely(num_data_pages > 1)) {
518		++mob->pt_level;
519		BUG_ON(mob->pt_level > 2);
520		save_pt_iter = pt_iter;
521		num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
522						&pt_iter);
523		data_iter = save_pt_iter;
524		num_data_pages = num_pt_pages;
525	}
526
527	mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
528	ttm_bo_unreserve(bo);
529}
530
531/*
532 * vmw_mob_destroy - Destroy a mob, unpopulating first if necessary.
533 *
534 * @mob:            Pointer to a mob to destroy.
535 */
536void vmw_mob_destroy(struct vmw_mob *mob)
537{
538	if (mob->pt_bo) {
539		vmw_bo_unpin_unlocked(&mob->pt_bo->tbo);
540		vmw_bo_unreference(&mob->pt_bo);
541	}
542	kfree(mob);
543}
544
545/*
546 * vmw_mob_unbind - Hide a mob from the device.
547 *
548 * @dev_priv:       Pointer to a device private.
549 * @mob_id:         Device id of the mob to unbind.
550 */
551void vmw_mob_unbind(struct vmw_private *dev_priv,
552		    struct vmw_mob *mob)
553{
554	struct {
555		SVGA3dCmdHeader header;
556		SVGA3dCmdDestroyGBMob body;
557	} *cmd;
558	int ret;
559	struct ttm_buffer_object *bo = &mob->pt_bo->tbo;
560
561	if (bo) {
562		ret = ttm_bo_reserve(bo, false, true, NULL);
563		/*
564		 * Noone else should be using this buffer.
565		 */
566		BUG_ON(ret != 0);
567	}
568
569	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
570	if (cmd) {
571		cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
572		cmd->header.size = sizeof(cmd->body);
573		cmd->body.mobid = mob->id;
574		vmw_cmd_commit(dev_priv, sizeof(*cmd));
575	}
576
577	if (bo) {
578		vmw_bo_fence_single(bo, NULL);
579		ttm_bo_unreserve(bo);
580	}
581	vmw_fifo_resource_dec(dev_priv);
582}
583
584/*
585 * vmw_mob_bind - Make a mob visible to the device after first
586 *                populating it if necessary.
587 *
588 * @dev_priv:       Pointer to a device private.
589 * @mob:            Pointer to the mob we're making visible.
590 * @data_addr:      Array of DMA addresses to the data pages of the underlying
591 *                  buffer object.
592 * @num_data_pages: Number of data pages of the underlying buffer
593 *                  object.
594 * @mob_id:         Device id of the mob to bind
595 *
596 * This function is intended to be interfaced with the ttm_tt backend
597 * code.
598 */
599int vmw_mob_bind(struct vmw_private *dev_priv,
600		 struct vmw_mob *mob,
601		 const struct vmw_sg_table *vsgt,
602		 unsigned long num_data_pages,
603		 int32_t mob_id)
604{
605	int ret;
606	bool pt_set_up = false;
607	struct vmw_piter data_iter;
608	struct {
609		SVGA3dCmdHeader header;
610		SVGA3dCmdDefineGBMob64 body;
611	} *cmd;
612
613	mob->id = mob_id;
614	vmw_piter_start(&data_iter, vsgt, 0);
615	if (unlikely(!vmw_piter_next(&data_iter)))
616		return 0;
617
618	if (likely(num_data_pages == 1)) {
619		mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
620		mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
 
 
 
621	} else if (unlikely(mob->pt_bo == NULL)) {
622		ret = vmw_mob_pt_populate(dev_priv, mob);
623		if (unlikely(ret != 0))
624			return ret;
625
626		vmw_mob_pt_setup(mob, data_iter, num_data_pages);
627		pt_set_up = true;
628		mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PT_1;
629	}
630
631	vmw_fifo_resource_inc(dev_priv);
632
633	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
634	if (unlikely(cmd == NULL))
635		goto out_no_cmd_space;
636
637	cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
638	cmd->header.size = sizeof(cmd->body);
639	cmd->body.mobid = mob_id;
640	cmd->body.ptDepth = mob->pt_level;
641	cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
642	cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
643
644	vmw_cmd_commit(dev_priv, sizeof(*cmd));
645
646	return 0;
647
648out_no_cmd_space:
649	vmw_fifo_resource_dec(dev_priv);
650	if (pt_set_up) {
651		vmw_bo_unpin_unlocked(&mob->pt_bo->tbo);
652		vmw_bo_unreference(&mob->pt_bo);
653	}
654
655	return -ENOMEM;
656}