Home | History | Annotate | Line # | Download | only in vmwgfx
      1 /*	$NetBSD: vmwgfx_cotable.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
      2 
      3 // SPDX-License-Identifier: GPL-2.0 OR MIT
      4 /**************************************************************************
      5  *
      6  * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sub license, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice (including the
     17  * next paragraph) shall be included in all copies or substantial portions
     18  * of the Software.
     19  *
     20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  *
     28  **************************************************************************/
     29 /*
     30  * Treat context OTables as resources to make use of the resource
     31  * backing MOB eviction mechanism, that is used to read back the COTable
     32  * whenever the backing MOB is evicted.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_cotable.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
     37 
     38 #include <drm/ttm/ttm_placement.h>
     39 
     40 #include "vmwgfx_drv.h"
     41 #include "vmwgfx_resource_priv.h"
     42 #include "vmwgfx_so.h"
     43 
     44 /**
     45  * struct vmw_cotable - Context Object Table resource
     46  *
     47  * @res: struct vmw_resource we are deriving from.
     48  * @ctx: non-refcounted pointer to the owning context.
     49  * @size_read_back: Size of data read back during eviction.
     50  * @seen_entries: Seen entries in command stream for this cotable.
     51  * @type: The cotable type.
     52  * @scrubbed: Whether the cotable has been scrubbed.
     53  * @resource_list: List of resources in the cotable.
     54  */
     55 struct vmw_cotable {
     56 	struct vmw_resource res;
     57 	struct vmw_resource *ctx;
     58 	size_t size_read_back;
     59 	int seen_entries;
     60 	u32 type;
     61 	bool scrubbed;
     62 	struct list_head resource_list;
     63 };
     64 
     65 /**
     66  * struct vmw_cotable_info - Static info about cotable types
     67  *
     68  * @min_initial_entries: Min number of initial intries at cotable allocation
     69  * for this cotable type.
     70  * @size: Size of each entry.
     71  */
     72 struct vmw_cotable_info {
     73 	u32 min_initial_entries;
     74 	u32 size;
     75 	void (*unbind_func)(struct vmw_private *, struct list_head *,
     76 			    bool);
     77 };
     78 
     79 static const struct vmw_cotable_info co_info[] = {
     80 	{1, sizeof(SVGACOTableDXRTViewEntry), &vmw_view_cotable_list_destroy},
     81 	{1, sizeof(SVGACOTableDXDSViewEntry), &vmw_view_cotable_list_destroy},
     82 	{1, sizeof(SVGACOTableDXSRViewEntry), &vmw_view_cotable_list_destroy},
     83 	{1, sizeof(SVGACOTableDXElementLayoutEntry), NULL},
     84 	{1, sizeof(SVGACOTableDXBlendStateEntry), NULL},
     85 	{1, sizeof(SVGACOTableDXDepthStencilEntry), NULL},
     86 	{1, sizeof(SVGACOTableDXRasterizerStateEntry), NULL},
     87 	{1, sizeof(SVGACOTableDXSamplerEntry), NULL},
     88 	{1, sizeof(SVGACOTableDXStreamOutputEntry), NULL},
     89 	{1, sizeof(SVGACOTableDXQueryEntry), NULL},
     90 	{1, sizeof(SVGACOTableDXShaderEntry), &vmw_dx_shader_cotable_list_scrub}
     91 };
     92 
     93 /*
     94  * Cotables with bindings that we remove must be scrubbed first,
     95  * otherwise, the device will swap in an invalid context when we remove
     96  * bindings before scrubbing a cotable...
     97  */
     98 const SVGACOTableType vmw_cotable_scrub_order[] = {
     99 	SVGA_COTABLE_RTVIEW,
    100 	SVGA_COTABLE_DSVIEW,
    101 	SVGA_COTABLE_SRVIEW,
    102 	SVGA_COTABLE_DXSHADER,
    103 	SVGA_COTABLE_ELEMENTLAYOUT,
    104 	SVGA_COTABLE_BLENDSTATE,
    105 	SVGA_COTABLE_DEPTHSTENCIL,
    106 	SVGA_COTABLE_RASTERIZERSTATE,
    107 	SVGA_COTABLE_SAMPLER,
    108 	SVGA_COTABLE_STREAMOUTPUT,
    109 	SVGA_COTABLE_DXQUERY,
    110 };
    111 
    112 static int vmw_cotable_bind(struct vmw_resource *res,
    113 			    struct ttm_validate_buffer *val_buf);
    114 static int vmw_cotable_unbind(struct vmw_resource *res,
    115 			      bool readback,
    116 			      struct ttm_validate_buffer *val_buf);
    117 static int vmw_cotable_create(struct vmw_resource *res);
    118 static int vmw_cotable_destroy(struct vmw_resource *res);
    119 
    120 static const struct vmw_res_func vmw_cotable_func = {
    121 	.res_type = vmw_res_cotable,
    122 	.needs_backup = true,
    123 	.may_evict = true,
    124 	.prio = 3,
    125 	.dirty_prio = 3,
    126 	.type_name = "context guest backed object tables",
    127 	.backup_placement = &vmw_mob_placement,
    128 	.create = vmw_cotable_create,
    129 	.destroy = vmw_cotable_destroy,
    130 	.bind = vmw_cotable_bind,
    131 	.unbind = vmw_cotable_unbind,
    132 };
    133 
    134 /**
    135  * vmw_cotable - Convert a struct vmw_resource pointer to a struct
    136  * vmw_cotable pointer
    137  *
    138  * @res: Pointer to the resource.
    139  */
    140 static struct vmw_cotable *vmw_cotable(struct vmw_resource *res)
    141 {
    142 	return container_of(res, struct vmw_cotable, res);
    143 }
    144 
    145 /**
    146  * vmw_cotable_destroy - Cotable resource destroy callback
    147  *
    148  * @res: Pointer to the cotable resource.
    149  *
    150  * There is no device cotable destroy command, so this function only
    151  * makes sure that the resource id is set to invalid.
    152  */
    153 static int vmw_cotable_destroy(struct vmw_resource *res)
    154 {
    155 	res->id = -1;
    156 	return 0;
    157 }
    158 
    159 /**
    160  * vmw_cotable_unscrub - Undo a cotable unscrub operation
    161  *
    162  * @res: Pointer to the cotable resource
    163  *
    164  * This function issues commands to (re)bind the cotable to
    165  * its backing mob, which needs to be validated and reserved at this point.
    166  * This is identical to bind() except the function interface looks different.
    167  */
    168 static int vmw_cotable_unscrub(struct vmw_resource *res)
    169 {
    170 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    171 	struct vmw_private *dev_priv = res->dev_priv;
    172 	struct ttm_buffer_object *bo = &res->backup->base;
    173 	struct {
    174 		SVGA3dCmdHeader header;
    175 		SVGA3dCmdDXSetCOTable body;
    176 	} *cmd;
    177 
    178 	WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
    179 	dma_resv_assert_held(bo->base.resv);
    180 
    181 	cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    182 	if (!cmd)
    183 		return -ENOMEM;
    184 
    185 	WARN_ON(vcotbl->ctx->id == SVGA3D_INVALID_ID);
    186 	WARN_ON(bo->mem.mem_type != VMW_PL_MOB);
    187 	cmd->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
    188 	cmd->header.size = sizeof(cmd->body);
    189 	cmd->body.cid = vcotbl->ctx->id;
    190 	cmd->body.type = vcotbl->type;
    191 	cmd->body.mobid = bo->mem.start;
    192 	cmd->body.validSizeInBytes = vcotbl->size_read_back;
    193 
    194 	vmw_fifo_commit_flush(dev_priv, sizeof(*cmd));
    195 	vcotbl->scrubbed = false;
    196 
    197 	return 0;
    198 }
    199 
    200 /**
    201  * vmw_cotable_bind - Undo a cotable unscrub operation
    202  *
    203  * @res: Pointer to the cotable resource
    204  * @val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
    205  * for convenience / fencing.
    206  *
    207  * This function issues commands to (re)bind the cotable to
    208  * its backing mob, which needs to be validated and reserved at this point.
    209  */
    210 static int vmw_cotable_bind(struct vmw_resource *res,
    211 			    struct ttm_validate_buffer *val_buf)
    212 {
    213 	/*
    214 	 * The create() callback may have changed @res->backup without
    215 	 * the caller noticing, and with val_buf->bo still pointing to
    216 	 * the old backup buffer. Although hackish, and not used currently,
    217 	 * take the opportunity to correct the value here so that it's not
    218 	 * misused in the future.
    219 	 */
    220 	val_buf->bo = &res->backup->base;
    221 
    222 	return vmw_cotable_unscrub(res);
    223 }
    224 
    225 /**
    226  * vmw_cotable_scrub - Scrub the cotable from the device.
    227  *
    228  * @res: Pointer to the cotable resource.
    229  * @readback: Whether initiate a readback of the cotable data to the backup
    230  * buffer.
    231  *
    232  * In some situations (context swapouts) it might be desirable to make the
    233  * device forget about the cotable without performing a full unbind. A full
    234  * unbind requires reserved backup buffers and it might not be possible to
    235  * reserve them due to locking order violation issues. The vmw_cotable_scrub
    236  * function implements a partial unbind() without that requirement but with the
    237  * following restrictions.
    238  * 1) Before the cotable is again used by the GPU, vmw_cotable_unscrub() must
    239  *    be called.
    240  * 2) Before the cotable backing buffer is used by the CPU, or during the
    241  *    resource destruction, vmw_cotable_unbind() must be called.
    242  */
    243 int vmw_cotable_scrub(struct vmw_resource *res, bool readback)
    244 {
    245 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    246 	struct vmw_private *dev_priv = res->dev_priv;
    247 	size_t submit_size;
    248 
    249 	struct {
    250 		SVGA3dCmdHeader header;
    251 		SVGA3dCmdDXReadbackCOTable body;
    252 	} *cmd0;
    253 	struct {
    254 		SVGA3dCmdHeader header;
    255 		SVGA3dCmdDXSetCOTable body;
    256 	} *cmd1;
    257 
    258 	if (vcotbl->scrubbed)
    259 		return 0;
    260 
    261 	if (co_info[vcotbl->type].unbind_func)
    262 		co_info[vcotbl->type].unbind_func(dev_priv,
    263 						  &vcotbl->resource_list,
    264 						  readback);
    265 	submit_size = sizeof(*cmd1);
    266 	if (readback)
    267 		submit_size += sizeof(*cmd0);
    268 
    269 	cmd1 = VMW_FIFO_RESERVE(dev_priv, submit_size);
    270 	if (!cmd1)
    271 		return -ENOMEM;
    272 
    273 	vcotbl->size_read_back = 0;
    274 	if (readback) {
    275 		cmd0 = (void *) cmd1;
    276 		cmd0->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
    277 		cmd0->header.size = sizeof(cmd0->body);
    278 		cmd0->body.cid = vcotbl->ctx->id;
    279 		cmd0->body.type = vcotbl->type;
    280 		cmd1 = (void *) &cmd0[1];
    281 		vcotbl->size_read_back = res->backup_size;
    282 	}
    283 	cmd1->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
    284 	cmd1->header.size = sizeof(cmd1->body);
    285 	cmd1->body.cid = vcotbl->ctx->id;
    286 	cmd1->body.type = vcotbl->type;
    287 	cmd1->body.mobid = SVGA3D_INVALID_ID;
    288 	cmd1->body.validSizeInBytes = 0;
    289 	vmw_fifo_commit_flush(dev_priv, submit_size);
    290 	vcotbl->scrubbed = true;
    291 
    292 	/* Trigger a create() on next validate. */
    293 	res->id = -1;
    294 
    295 	return 0;
    296 }
    297 
    298 /**
    299  * vmw_cotable_unbind - Cotable resource unbind callback
    300  *
    301  * @res: Pointer to the cotable resource.
    302  * @readback: Whether to read back cotable data to the backup buffer.
    303  * val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
    304  * for convenience / fencing.
    305  *
    306  * Unbinds the cotable from the device and fences the backup buffer.
    307  */
    308 static int vmw_cotable_unbind(struct vmw_resource *res,
    309 			      bool readback,
    310 			      struct ttm_validate_buffer *val_buf)
    311 {
    312 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    313 	struct vmw_private *dev_priv = res->dev_priv;
    314 	struct ttm_buffer_object *bo = val_buf->bo;
    315 	struct vmw_fence_obj *fence;
    316 
    317 	if (!vmw_resource_mob_attached(res))
    318 		return 0;
    319 
    320 	WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
    321 	dma_resv_assert_held(bo->base.resv);
    322 
    323 	mutex_lock(&dev_priv->binding_mutex);
    324 	if (!vcotbl->scrubbed)
    325 		vmw_dx_context_scrub_cotables(vcotbl->ctx, readback);
    326 	mutex_unlock(&dev_priv->binding_mutex);
    327 	(void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
    328 	vmw_bo_fence_single(bo, fence);
    329 	if (likely(fence != NULL))
    330 		vmw_fence_obj_unreference(&fence);
    331 
    332 	return 0;
    333 }
    334 
    335 /**
    336  * vmw_cotable_readback - Read back a cotable without unbinding.
    337  *
    338  * @res: The cotable resource.
    339  *
    340  * Reads back a cotable to its backing mob without scrubbing the MOB from
    341  * the cotable. The MOB is fenced for subsequent CPU access.
    342  */
    343 static int vmw_cotable_readback(struct vmw_resource *res)
    344 {
    345 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    346 	struct vmw_private *dev_priv = res->dev_priv;
    347 
    348 	struct {
    349 		SVGA3dCmdHeader header;
    350 		SVGA3dCmdDXReadbackCOTable body;
    351 	} *cmd;
    352 	struct vmw_fence_obj *fence;
    353 
    354 	if (!vcotbl->scrubbed) {
    355 		cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd));
    356 		if (!cmd)
    357 			return -ENOMEM;
    358 
    359 		cmd->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
    360 		cmd->header.size = sizeof(cmd->body);
    361 		cmd->body.cid = vcotbl->ctx->id;
    362 		cmd->body.type = vcotbl->type;
    363 		vcotbl->size_read_back = res->backup_size;
    364 		vmw_fifo_commit(dev_priv, sizeof(*cmd));
    365 	}
    366 
    367 	(void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
    368 	vmw_bo_fence_single(&res->backup->base, fence);
    369 	vmw_fence_obj_unreference(&fence);
    370 
    371 	return 0;
    372 }
    373 
    374 /**
    375  * vmw_cotable_resize - Resize a cotable.
    376  *
    377  * @res: The cotable resource.
    378  * @new_size: The new size.
    379  *
    380  * Resizes a cotable and binds the new backup buffer.
    381  * On failure the cotable is left intact.
    382  * Important! This function may not fail once the MOB switch has been
    383  * committed to hardware. That would put the device context in an
    384  * invalid state which we can't currently recover from.
    385  */
    386 static int vmw_cotable_resize(struct vmw_resource *res, size_t new_size)
    387 {
    388 	struct ttm_operation_ctx ctx = { false, false };
    389 	struct vmw_private *dev_priv = res->dev_priv;
    390 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    391 	struct vmw_buffer_object *buf, *old_buf = res->backup;
    392 	struct ttm_buffer_object *bo, *old_bo = &res->backup->base;
    393 	size_t old_size = res->backup_size;
    394 	size_t old_size_read_back = vcotbl->size_read_back;
    395 	size_t cur_size_read_back;
    396 	struct ttm_bo_kmap_obj old_map, new_map;
    397 	int ret;
    398 	size_t i;
    399 
    400 	ret = vmw_cotable_readback(res);
    401 	if (ret)
    402 		return ret;
    403 
    404 	cur_size_read_back = vcotbl->size_read_back;
    405 	vcotbl->size_read_back = old_size_read_back;
    406 
    407 	/*
    408 	 * While device is processing, Allocate and reserve a buffer object
    409 	 * for the new COTable. Initially pin the buffer object to make sure
    410 	 * we can use tryreserve without failure.
    411 	 */
    412 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
    413 	if (!buf)
    414 		return -ENOMEM;
    415 
    416 	ret = vmw_bo_init(dev_priv, buf, new_size, &vmw_mob_ne_placement,
    417 			  true, vmw_bo_bo_free);
    418 	if (ret) {
    419 		DRM_ERROR("Failed initializing new cotable MOB.\n");
    420 		return ret;
    421 	}
    422 
    423 	bo = &buf->base;
    424 	WARN_ON_ONCE(ttm_bo_reserve(bo, false, true, NULL));
    425 
    426 	ret = ttm_bo_wait(old_bo, false, false);
    427 	if (unlikely(ret != 0)) {
    428 		DRM_ERROR("Failed waiting for cotable unbind.\n");
    429 		goto out_wait;
    430 	}
    431 
    432 	/*
    433 	 * Do a page by page copy of COTables. This eliminates slow vmap()s.
    434 	 * This should really be a TTM utility.
    435 	 */
    436 	for (i = 0; i < old_bo->num_pages; ++i) {
    437 		bool dummy;
    438 
    439 		ret = ttm_bo_kmap(old_bo, i, 1, &old_map);
    440 		if (unlikely(ret != 0)) {
    441 			DRM_ERROR("Failed mapping old COTable on resize.\n");
    442 			goto out_wait;
    443 		}
    444 		ret = ttm_bo_kmap(bo, i, 1, &new_map);
    445 		if (unlikely(ret != 0)) {
    446 			DRM_ERROR("Failed mapping new COTable on resize.\n");
    447 			goto out_map_new;
    448 		}
    449 		memcpy(ttm_kmap_obj_virtual(&new_map, &dummy),
    450 		       ttm_kmap_obj_virtual(&old_map, &dummy),
    451 		       PAGE_SIZE);
    452 		ttm_bo_kunmap(&new_map);
    453 		ttm_bo_kunmap(&old_map);
    454 	}
    455 
    456 	/* Unpin new buffer, and switch backup buffers. */
    457 	ret = ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
    458 	if (unlikely(ret != 0)) {
    459 		DRM_ERROR("Failed validating new COTable backup buffer.\n");
    460 		goto out_wait;
    461 	}
    462 
    463 	vmw_resource_mob_detach(res);
    464 	res->backup = buf;
    465 	res->backup_size = new_size;
    466 	vcotbl->size_read_back = cur_size_read_back;
    467 
    468 	/*
    469 	 * Now tell the device to switch. If this fails, then we need to
    470 	 * revert the full resize.
    471 	 */
    472 	ret = vmw_cotable_unscrub(res);
    473 	if (ret) {
    474 		DRM_ERROR("Failed switching COTable backup buffer.\n");
    475 		res->backup = old_buf;
    476 		res->backup_size = old_size;
    477 		vcotbl->size_read_back = old_size_read_back;
    478 		vmw_resource_mob_attach(res);
    479 		goto out_wait;
    480 	}
    481 
    482 	vmw_resource_mob_attach(res);
    483 	/* Let go of the old mob. */
    484 	vmw_bo_unreference(&old_buf);
    485 	res->id = vcotbl->type;
    486 
    487 	return 0;
    488 
    489 out_map_new:
    490 	ttm_bo_kunmap(&old_map);
    491 out_wait:
    492 	ttm_bo_unreserve(bo);
    493 	vmw_bo_unreference(&buf);
    494 
    495 	return ret;
    496 }
    497 
    498 /**
    499  * vmw_cotable_create - Cotable resource create callback
    500  *
    501  * @res: Pointer to a cotable resource.
    502  *
    503  * There is no separate create command for cotables, so this callback, which
    504  * is called before bind() in the validation sequence is instead used for two
    505  * things.
    506  * 1) Unscrub the cotable if it is scrubbed and still attached to a backup
    507  *    buffer.
    508  * 2) Resize the cotable if needed.
    509  */
    510 static int vmw_cotable_create(struct vmw_resource *res)
    511 {
    512 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    513 	size_t new_size = res->backup_size;
    514 	size_t needed_size;
    515 	int ret;
    516 
    517 	/* Check whether we need to resize the cotable */
    518 	needed_size = (vcotbl->seen_entries + 1) * co_info[vcotbl->type].size;
    519 	while (needed_size > new_size)
    520 		new_size *= 2;
    521 
    522 	if (likely(new_size <= res->backup_size)) {
    523 		if (vcotbl->scrubbed && vmw_resource_mob_attached(res)) {
    524 			ret = vmw_cotable_unscrub(res);
    525 			if (ret)
    526 				return ret;
    527 		}
    528 		res->id = vcotbl->type;
    529 		return 0;
    530 	}
    531 
    532 	return vmw_cotable_resize(res, new_size);
    533 }
    534 
    535 /**
    536  * vmw_hw_cotable_destroy - Cotable hw_destroy callback
    537  *
    538  * @res: Pointer to a cotable resource.
    539  *
    540  * The final (part of resource destruction) destroy callback.
    541  */
    542 static void vmw_hw_cotable_destroy(struct vmw_resource *res)
    543 {
    544 	(void) vmw_cotable_destroy(res);
    545 }
    546 
    547 static size_t cotable_acc_size;
    548 
    549 /**
    550  * vmw_cotable_free - Cotable resource destructor
    551  *
    552  * @res: Pointer to a cotable resource.
    553  */
    554 static void vmw_cotable_free(struct vmw_resource *res)
    555 {
    556 	struct vmw_private *dev_priv = res->dev_priv;
    557 
    558 	kfree(res);
    559 	ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
    560 }
    561 
    562 /**
    563  * vmw_cotable_alloc - Create a cotable resource
    564  *
    565  * @dev_priv: Pointer to a device private struct.
    566  * @ctx: Pointer to the context resource.
    567  * The cotable resource will not add a refcount.
    568  * @type: The cotable type.
    569  */
    570 struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
    571 				       struct vmw_resource *ctx,
    572 				       u32 type)
    573 {
    574 	struct vmw_cotable *vcotbl;
    575 	struct ttm_operation_ctx ttm_opt_ctx = {
    576 		.interruptible = true,
    577 		.no_wait_gpu = false
    578 	};
    579 	int ret;
    580 	u32 num_entries;
    581 
    582 	if (unlikely(cotable_acc_size == 0))
    583 		cotable_acc_size = ttm_round_pot(sizeof(struct vmw_cotable));
    584 
    585 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
    586 				   cotable_acc_size, &ttm_opt_ctx);
    587 	if (unlikely(ret))
    588 		return ERR_PTR(ret);
    589 
    590 	vcotbl = kzalloc(sizeof(*vcotbl), GFP_KERNEL);
    591 	if (unlikely(!vcotbl)) {
    592 		ret = -ENOMEM;
    593 		goto out_no_alloc;
    594 	}
    595 
    596 	ret = vmw_resource_init(dev_priv, &vcotbl->res, true,
    597 				vmw_cotable_free, &vmw_cotable_func);
    598 	if (unlikely(ret != 0))
    599 		goto out_no_init;
    600 
    601 	INIT_LIST_HEAD(&vcotbl->resource_list);
    602 	vcotbl->res.id = type;
    603 	vcotbl->res.backup_size = PAGE_SIZE;
    604 	num_entries = PAGE_SIZE / co_info[type].size;
    605 	if (num_entries < co_info[type].min_initial_entries) {
    606 		vcotbl->res.backup_size = co_info[type].min_initial_entries *
    607 			co_info[type].size;
    608 		vcotbl->res.backup_size =
    609 			(vcotbl->res.backup_size + PAGE_SIZE - 1) & PAGE_MASK;
    610 	}
    611 
    612 	vcotbl->scrubbed = true;
    613 	vcotbl->seen_entries = -1;
    614 	vcotbl->type = type;
    615 	vcotbl->ctx = ctx;
    616 
    617 	vcotbl->res.hw_destroy = vmw_hw_cotable_destroy;
    618 
    619 	return &vcotbl->res;
    620 
    621 out_no_init:
    622 	kfree(vcotbl);
    623 out_no_alloc:
    624 	ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
    625 	return ERR_PTR(ret);
    626 }
    627 
    628 /**
    629  * vmw_cotable_notify - Notify the cotable about an item creation
    630  *
    631  * @res: Pointer to a cotable resource.
    632  * @id: Item id.
    633  */
    634 int vmw_cotable_notify(struct vmw_resource *res, int id)
    635 {
    636 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    637 
    638 	if (id < 0 || id >= SVGA_COTABLE_MAX_IDS) {
    639 		DRM_ERROR("Illegal COTable id. Type is %u. Id is %d\n",
    640 			  (unsigned) vcotbl->type, id);
    641 		return -EINVAL;
    642 	}
    643 
    644 	if (vcotbl->seen_entries < id) {
    645 		/* Trigger a call to create() on next validate */
    646 		res->id = -1;
    647 		vcotbl->seen_entries = id;
    648 	}
    649 
    650 	return 0;
    651 }
    652 
    653 /**
    654  * vmw_cotable_add_view - add a view to the cotable's list of active views.
    655  *
    656  * @res: pointer struct vmw_resource representing the cotable.
    657  * @head: pointer to the struct list_head member of the resource, dedicated
    658  * to the cotable active resource list.
    659  */
    660 void vmw_cotable_add_resource(struct vmw_resource *res, struct list_head *head)
    661 {
    662 	struct vmw_cotable *vcotbl =
    663 		container_of(res, struct vmw_cotable, res);
    664 
    665 	list_add_tail(head, &vcotbl->resource_list);
    666 }
    667