Home | History | Annotate | Line # | Download | only in vmwgfx
vmwgfx_cotable.c revision 1.1
      1 /*	$NetBSD: vmwgfx_cotable.c,v 1.1 2018/08/27 01:34:59 riastradh Exp $	*/
      2 
      3 /**************************************************************************
      4  *
      5  * Copyright  2014-2015 VMware, Inc., Palo Alto, CA., USA
      6  * All Rights Reserved.
      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.1 2018/08/27 01:34:59 riastradh Exp $");
     37 
     38 #include "vmwgfx_drv.h"
     39 #include "vmwgfx_resource_priv.h"
     40 #include <ttm/ttm_placement.h>
     41 #include "vmwgfx_so.h"
     42 
     43 /**
     44  * struct vmw_cotable - Context Object Table resource
     45  *
     46  * @res: struct vmw_resource we are deriving from.
     47  * @ctx: non-refcounted pointer to the owning context.
     48  * @size_read_back: Size of data read back during eviction.
     49  * @seen_entries: Seen entries in command stream for this cotable.
     50  * @type: The cotable type.
     51  * @scrubbed: Whether the cotable has been scrubbed.
     52  * @resource_list: List of resources in the cotable.
     53  */
     54 struct vmw_cotable {
     55 	struct vmw_resource res;
     56 	struct vmw_resource *ctx;
     57 	size_t size_read_back;
     58 	int seen_entries;
     59 	u32 type;
     60 	bool scrubbed;
     61 	struct list_head resource_list;
     62 };
     63 
     64 /**
     65  * struct vmw_cotable_info - Static info about cotable types
     66  *
     67  * @min_initial_entries: Min number of initial intries at cotable allocation
     68  * for this cotable type.
     69  * @size: Size of each entry.
     70  */
     71 struct vmw_cotable_info {
     72 	u32 min_initial_entries;
     73 	u32 size;
     74 	void (*unbind_func)(struct vmw_private *, struct list_head *,
     75 			    bool);
     76 };
     77 
     78 static const struct vmw_cotable_info co_info[] = {
     79 	{1, sizeof(SVGACOTableDXRTViewEntry), &vmw_view_cotable_list_destroy},
     80 	{1, sizeof(SVGACOTableDXDSViewEntry), &vmw_view_cotable_list_destroy},
     81 	{1, sizeof(SVGACOTableDXSRViewEntry), &vmw_view_cotable_list_destroy},
     82 	{1, sizeof(SVGACOTableDXElementLayoutEntry), NULL},
     83 	{1, sizeof(SVGACOTableDXBlendStateEntry), NULL},
     84 	{1, sizeof(SVGACOTableDXDepthStencilEntry), NULL},
     85 	{1, sizeof(SVGACOTableDXRasterizerStateEntry), NULL},
     86 	{1, sizeof(SVGACOTableDXSamplerEntry), NULL},
     87 	{1, sizeof(SVGACOTableDXStreamOutputEntry), NULL},
     88 	{1, sizeof(SVGACOTableDXQueryEntry), NULL},
     89 	{1, sizeof(SVGACOTableDXShaderEntry), &vmw_dx_shader_cotable_list_scrub}
     90 };
     91 
     92 /*
     93  * Cotables with bindings that we remove must be scrubbed first,
     94  * otherwise, the device will swap in an invalid context when we remove
     95  * bindings before scrubbing a cotable...
     96  */
     97 const SVGACOTableType vmw_cotable_scrub_order[] = {
     98 	SVGA_COTABLE_RTVIEW,
     99 	SVGA_COTABLE_DSVIEW,
    100 	SVGA_COTABLE_SRVIEW,
    101 	SVGA_COTABLE_DXSHADER,
    102 	SVGA_COTABLE_ELEMENTLAYOUT,
    103 	SVGA_COTABLE_BLENDSTATE,
    104 	SVGA_COTABLE_DEPTHSTENCIL,
    105 	SVGA_COTABLE_RASTERIZERSTATE,
    106 	SVGA_COTABLE_SAMPLER,
    107 	SVGA_COTABLE_STREAMOUTPUT,
    108 	SVGA_COTABLE_DXQUERY,
    109 };
    110 
    111 static int vmw_cotable_bind(struct vmw_resource *res,
    112 			    struct ttm_validate_buffer *val_buf);
    113 static int vmw_cotable_unbind(struct vmw_resource *res,
    114 			      bool readback,
    115 			      struct ttm_validate_buffer *val_buf);
    116 static int vmw_cotable_create(struct vmw_resource *res);
    117 static int vmw_cotable_destroy(struct vmw_resource *res);
    118 
    119 static const struct vmw_res_func vmw_cotable_func = {
    120 	.res_type = vmw_res_cotable,
    121 	.needs_backup = true,
    122 	.may_evict = true,
    123 	.type_name = "context guest backed object tables",
    124 	.backup_placement = &vmw_mob_placement,
    125 	.create = vmw_cotable_create,
    126 	.destroy = vmw_cotable_destroy,
    127 	.bind = vmw_cotable_bind,
    128 	.unbind = vmw_cotable_unbind,
    129 };
    130 
    131 /**
    132  * vmw_cotable - Convert a struct vmw_resource pointer to a struct
    133  * vmw_cotable pointer
    134  *
    135  * @res: Pointer to the resource.
    136  */
    137 static struct vmw_cotable *vmw_cotable(struct vmw_resource *res)
    138 {
    139 	return container_of(res, struct vmw_cotable, res);
    140 }
    141 
    142 /**
    143  * vmw_cotable_destroy - Cotable resource destroy callback
    144  *
    145  * @res: Pointer to the cotable resource.
    146  *
    147  * There is no device cotable destroy command, so this function only
    148  * makes sure that the resource id is set to invalid.
    149  */
    150 static int vmw_cotable_destroy(struct vmw_resource *res)
    151 {
    152 	res->id = -1;
    153 	return 0;
    154 }
    155 
    156 /**
    157  * vmw_cotable_unscrub - Undo a cotable unscrub operation
    158  *
    159  * @res: Pointer to the cotable resource
    160  *
    161  * This function issues commands to (re)bind the cotable to
    162  * its backing mob, which needs to be validated and reserved at this point.
    163  * This is identical to bind() except the function interface looks different.
    164  */
    165 static int vmw_cotable_unscrub(struct vmw_resource *res)
    166 {
    167 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    168 	struct vmw_private *dev_priv = res->dev_priv;
    169 	struct ttm_buffer_object *bo = &res->backup->base;
    170 	struct {
    171 		SVGA3dCmdHeader header;
    172 		SVGA3dCmdDXSetCOTable body;
    173 	} *cmd;
    174 
    175 	WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
    176 	lockdep_assert_held(&bo->resv->lock.base);
    177 
    178 	cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), SVGA3D_INVALID_ID);
    179 	if (!cmd) {
    180 		DRM_ERROR("Failed reserving FIFO space for cotable "
    181 			  "binding.\n");
    182 		return -ENOMEM;
    183 	}
    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_dx(dev_priv, submit_size, SVGA3D_INVALID_ID);
    270 	if (!cmd1) {
    271 		DRM_ERROR("Failed reserving FIFO space for cotable "
    272 			  "unbinding.\n");
    273 		return -ENOMEM;
    274 	}
    275 
    276 	vcotbl->size_read_back = 0;
    277 	if (readback) {
    278 		cmd0 = (void *) cmd1;
    279 		cmd0->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
    280 		cmd0->header.size = sizeof(cmd0->body);
    281 		cmd0->body.cid = vcotbl->ctx->id;
    282 		cmd0->body.type = vcotbl->type;
    283 		cmd1 = (void *) &cmd0[1];
    284 		vcotbl->size_read_back = res->backup_size;
    285 	}
    286 	cmd1->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
    287 	cmd1->header.size = sizeof(cmd1->body);
    288 	cmd1->body.cid = vcotbl->ctx->id;
    289 	cmd1->body.type = vcotbl->type;
    290 	cmd1->body.mobid = SVGA3D_INVALID_ID;
    291 	cmd1->body.validSizeInBytes = 0;
    292 	vmw_fifo_commit_flush(dev_priv, submit_size);
    293 	vcotbl->scrubbed = true;
    294 
    295 	/* Trigger a create() on next validate. */
    296 	res->id = -1;
    297 
    298 	return 0;
    299 }
    300 
    301 /**
    302  * vmw_cotable_unbind - Cotable resource unbind callback
    303  *
    304  * @res: Pointer to the cotable resource.
    305  * @readback: Whether to read back cotable data to the backup buffer.
    306  * val_buf: Pointer to a struct ttm_validate_buffer prepared by the caller
    307  * for convenience / fencing.
    308  *
    309  * Unbinds the cotable from the device and fences the backup buffer.
    310  */
    311 static int vmw_cotable_unbind(struct vmw_resource *res,
    312 			      bool readback,
    313 			      struct ttm_validate_buffer *val_buf)
    314 {
    315 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    316 	struct vmw_private *dev_priv = res->dev_priv;
    317 	struct ttm_buffer_object *bo = val_buf->bo;
    318 	struct vmw_fence_obj *fence;
    319 
    320 	if (list_empty(&res->mob_head))
    321 		return 0;
    322 
    323 	WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
    324 	lockdep_assert_held(&bo->resv->lock.base);
    325 
    326 	mutex_lock(&dev_priv->binding_mutex);
    327 	if (!vcotbl->scrubbed)
    328 		vmw_dx_context_scrub_cotables(vcotbl->ctx, readback);
    329 	mutex_unlock(&dev_priv->binding_mutex);
    330 	(void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
    331 	vmw_fence_single_bo(bo, fence);
    332 	if (likely(fence != NULL))
    333 		vmw_fence_obj_unreference(&fence);
    334 
    335 	return 0;
    336 }
    337 
    338 /**
    339  * vmw_cotable_readback - Read back a cotable without unbinding.
    340  *
    341  * @res: The cotable resource.
    342  *
    343  * Reads back a cotable to its backing mob without scrubbing the MOB from
    344  * the cotable. The MOB is fenced for subsequent CPU access.
    345  */
    346 static int vmw_cotable_readback(struct vmw_resource *res)
    347 {
    348 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    349 	struct vmw_private *dev_priv = res->dev_priv;
    350 
    351 	struct {
    352 		SVGA3dCmdHeader header;
    353 		SVGA3dCmdDXReadbackCOTable body;
    354 	} *cmd;
    355 	struct vmw_fence_obj *fence;
    356 
    357 	if (!vcotbl->scrubbed) {
    358 		cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd),
    359 					  SVGA3D_INVALID_ID);
    360 		if (!cmd) {
    361 			DRM_ERROR("Failed reserving FIFO space for cotable "
    362 				  "readback.\n");
    363 			return -ENOMEM;
    364 		}
    365 		cmd->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
    366 		cmd->header.size = sizeof(cmd->body);
    367 		cmd->body.cid = vcotbl->ctx->id;
    368 		cmd->body.type = vcotbl->type;
    369 		vcotbl->size_read_back = res->backup_size;
    370 		vmw_fifo_commit(dev_priv, sizeof(*cmd));
    371 	}
    372 
    373 	(void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
    374 	vmw_fence_single_bo(&res->backup->base, fence);
    375 	vmw_fence_obj_unreference(&fence);
    376 
    377 	return 0;
    378 }
    379 
    380 /**
    381  * vmw_cotable_resize - Resize a cotable.
    382  *
    383  * @res: The cotable resource.
    384  * @new_size: The new size.
    385  *
    386  * Resizes a cotable and binds the new backup buffer.
    387  * On failure the cotable is left intact.
    388  * Important! This function may not fail once the MOB switch has been
    389  * committed to hardware. That would put the device context in an
    390  * invalid state which we can't currently recover from.
    391  */
    392 static int vmw_cotable_resize(struct vmw_resource *res, size_t new_size)
    393 {
    394 	struct vmw_private *dev_priv = res->dev_priv;
    395 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    396 	struct vmw_dma_buffer *buf, *old_buf = res->backup;
    397 	struct ttm_buffer_object *bo, *old_bo = &res->backup->base;
    398 	size_t old_size = res->backup_size;
    399 	size_t old_size_read_back = vcotbl->size_read_back;
    400 	size_t cur_size_read_back;
    401 	struct ttm_bo_kmap_obj old_map, new_map;
    402 	int ret;
    403 	size_t i;
    404 
    405 	ret = vmw_cotable_readback(res);
    406 	if (ret)
    407 		return ret;
    408 
    409 	cur_size_read_back = vcotbl->size_read_back;
    410 	vcotbl->size_read_back = old_size_read_back;
    411 
    412 	/*
    413 	 * While device is processing, Allocate and reserve a buffer object
    414 	 * for the new COTable. Initially pin the buffer object to make sure
    415 	 * we can use tryreserve without failure.
    416 	 */
    417 	buf = kzalloc(sizeof(*buf), GFP_KERNEL);
    418 	if (!buf)
    419 		return -ENOMEM;
    420 
    421 	ret = vmw_dmabuf_init(dev_priv, buf, new_size, &vmw_mob_ne_placement,
    422 			      true, vmw_dmabuf_bo_free);
    423 	if (ret) {
    424 		DRM_ERROR("Failed initializing new cotable MOB.\n");
    425 		return ret;
    426 	}
    427 
    428 	bo = &buf->base;
    429 	WARN_ON_ONCE(ttm_bo_reserve(bo, false, true, false, NULL));
    430 
    431 	ret = ttm_bo_wait(old_bo, false, false, false);
    432 	if (unlikely(ret != 0)) {
    433 		DRM_ERROR("Failed waiting for cotable unbind.\n");
    434 		goto out_wait;
    435 	}
    436 
    437 	/*
    438 	 * Do a page by page copy of COTables. This eliminates slow vmap()s.
    439 	 * This should really be a TTM utility.
    440 	 */
    441 	for (i = 0; i < old_bo->num_pages; ++i) {
    442 		bool dummy;
    443 
    444 		ret = ttm_bo_kmap(old_bo, i, 1, &old_map);
    445 		if (unlikely(ret != 0)) {
    446 			DRM_ERROR("Failed mapping old COTable on resize.\n");
    447 			goto out_wait;
    448 		}
    449 		ret = ttm_bo_kmap(bo, i, 1, &new_map);
    450 		if (unlikely(ret != 0)) {
    451 			DRM_ERROR("Failed mapping new COTable on resize.\n");
    452 			goto out_map_new;
    453 		}
    454 		memcpy(ttm_kmap_obj_virtual(&new_map, &dummy),
    455 		       ttm_kmap_obj_virtual(&old_map, &dummy),
    456 		       PAGE_SIZE);
    457 		ttm_bo_kunmap(&new_map);
    458 		ttm_bo_kunmap(&old_map);
    459 	}
    460 
    461 	/* Unpin new buffer, and switch backup buffers. */
    462 	ret = ttm_bo_validate(bo, &vmw_mob_placement, false, false);
    463 	if (unlikely(ret != 0)) {
    464 		DRM_ERROR("Failed validating new COTable backup buffer.\n");
    465 		goto out_wait;
    466 	}
    467 
    468 	res->backup = buf;
    469 	res->backup_size = new_size;
    470 	vcotbl->size_read_back = cur_size_read_back;
    471 
    472 	/*
    473 	 * Now tell the device to switch. If this fails, then we need to
    474 	 * revert the full resize.
    475 	 */
    476 	ret = vmw_cotable_unscrub(res);
    477 	if (ret) {
    478 		DRM_ERROR("Failed switching COTable backup buffer.\n");
    479 		res->backup = old_buf;
    480 		res->backup_size = old_size;
    481 		vcotbl->size_read_back = old_size_read_back;
    482 		goto out_wait;
    483 	}
    484 
    485 	/* Let go of the old mob. */
    486 	list_del(&res->mob_head);
    487 	list_add_tail(&res->mob_head, &buf->res_list);
    488 	vmw_dmabuf_unreference(&old_buf);
    489 	res->id = vcotbl->type;
    490 
    491 	return 0;
    492 
    493 out_map_new:
    494 	ttm_bo_kunmap(&old_map);
    495 out_wait:
    496 	ttm_bo_unreserve(bo);
    497 	vmw_dmabuf_unreference(&buf);
    498 
    499 	return ret;
    500 }
    501 
    502 /**
    503  * vmw_cotable_create - Cotable resource create callback
    504  *
    505  * @res: Pointer to a cotable resource.
    506  *
    507  * There is no separate create command for cotables, so this callback, which
    508  * is called before bind() in the validation sequence is instead used for two
    509  * things.
    510  * 1) Unscrub the cotable if it is scrubbed and still attached to a backup
    511  *    buffer, that is, if @res->mob_head is non-empty.
    512  * 2) Resize the cotable if needed.
    513  */
    514 static int vmw_cotable_create(struct vmw_resource *res)
    515 {
    516 	struct vmw_cotable *vcotbl = vmw_cotable(res);
    517 	size_t new_size = res->backup_size;
    518 	size_t needed_size;
    519 	int ret;
    520 
    521 	/* Check whether we need to resize the cotable */
    522 	needed_size = (vcotbl->seen_entries + 1) * co_info[vcotbl->type].size;
    523 	while (needed_size > new_size)
    524 		new_size *= 2;
    525 
    526 	if (likely(new_size <= res->backup_size)) {
    527 		if (vcotbl->scrubbed && !list_empty(&res->mob_head)) {
    528 			ret = vmw_cotable_unscrub(res);
    529 			if (ret)
    530 				return ret;
    531 		}
    532 		res->id = vcotbl->type;
    533 		return 0;
    534 	}
    535 
    536 	return vmw_cotable_resize(res, new_size);
    537 }
    538 
    539 /**
    540  * vmw_hw_cotable_destroy - Cotable hw_destroy callback
    541  *
    542  * @res: Pointer to a cotable resource.
    543  *
    544  * The final (part of resource destruction) destroy callback.
    545  */
    546 static void vmw_hw_cotable_destroy(struct vmw_resource *res)
    547 {
    548 	(void) vmw_cotable_destroy(res);
    549 }
    550 
    551 static size_t cotable_acc_size;
    552 
    553 /**
    554  * vmw_cotable_free - Cotable resource destructor
    555  *
    556  * @res: Pointer to a cotable resource.
    557  */
    558 static void vmw_cotable_free(struct vmw_resource *res)
    559 {
    560 	struct vmw_private *dev_priv = res->dev_priv;
    561 
    562 	kfree(res);
    563 	ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
    564 }
    565 
    566 /**
    567  * vmw_cotable_alloc - Create a cotable resource
    568  *
    569  * @dev_priv: Pointer to a device private struct.
    570  * @ctx: Pointer to the context resource.
    571  * The cotable resource will not add a refcount.
    572  * @type: The cotable type.
    573  */
    574 struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
    575 				       struct vmw_resource *ctx,
    576 				       u32 type)
    577 {
    578 	struct vmw_cotable *vcotbl;
    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, false, true);
    587 	if (unlikely(ret))
    588 		return ERR_PTR(ret);
    589 
    590 	vcotbl = kzalloc(sizeof(*vcotbl), GFP_KERNEL);
    591 	if (unlikely(vcotbl == NULL)) {
    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 	vmw_resource_activate(&vcotbl->res, 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