Home | History | Annotate | Line # | Download | only in vmwgfx
      1  1.1  riastrad /*	$NetBSD: vmwgfx_so.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.3  riastrad // SPDX-License-Identifier: GPL-2.0 OR MIT
      4  1.1  riastrad /**************************************************************************
      5  1.3  riastrad  * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
      6  1.1  riastrad  *
      7  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      8  1.1  riastrad  * copy of this software and associated documentation files (the
      9  1.1  riastrad  * "Software"), to deal in the Software without restriction, including
     10  1.1  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     11  1.1  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     12  1.1  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     13  1.1  riastrad  * the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice (including the
     16  1.1  riastrad  * next paragraph) shall be included in all copies or substantial portions
     17  1.1  riastrad  * of the Software.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     22  1.1  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     23  1.1  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     24  1.1  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     25  1.1  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  1.1  riastrad  *
     27  1.1  riastrad  **************************************************************************/
     28  1.1  riastrad 
     29  1.1  riastrad #include <sys/cdefs.h>
     30  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: vmwgfx_so.c,v 1.3 2021/12/18 23:45:45 riastradh Exp $");
     31  1.1  riastrad 
     32  1.1  riastrad #include "vmwgfx_drv.h"
     33  1.1  riastrad #include "vmwgfx_resource_priv.h"
     34  1.1  riastrad #include "vmwgfx_so.h"
     35  1.1  riastrad #include "vmwgfx_binding.h"
     36  1.1  riastrad 
     37  1.1  riastrad /*
     38  1.1  riastrad  * The currently only reason we need to keep track of views is that if we
     39  1.1  riastrad  * destroy a hardware surface, all views pointing to it must also be destroyed,
     40  1.1  riastrad  * otherwise the device will error.
     41  1.1  riastrad  * So in particuar if a surface is evicted, we must destroy all views pointing
     42  1.1  riastrad  * to it, and all context bindings of that view. Similarly we must restore
     43  1.1  riastrad  * the view bindings, views and surfaces pointed to by the views when a
     44  1.1  riastrad  * context is referenced in the command stream.
     45  1.1  riastrad  */
     46  1.1  riastrad 
     47  1.1  riastrad /**
     48  1.1  riastrad  * struct vmw_view - view metadata
     49  1.1  riastrad  *
     50  1.1  riastrad  * @res: The struct vmw_resource we derive from
     51  1.1  riastrad  * @ctx: Non-refcounted pointer to the context this view belongs to.
     52  1.1  riastrad  * @srf: Refcounted pointer to the surface pointed to by this view.
     53  1.1  riastrad  * @cotable: Refcounted pointer to the cotable holding this view.
     54  1.1  riastrad  * @srf_head: List head for the surface-to-view list.
     55  1.1  riastrad  * @cotable_head: List head for the cotable-to_view list.
     56  1.1  riastrad  * @view_type: View type.
     57  1.1  riastrad  * @view_id: User-space per context view id. Currently used also as per
     58  1.1  riastrad  * context device view id.
     59  1.1  riastrad  * @cmd_size: Size of the SVGA3D define view command that we've copied from the
     60  1.1  riastrad  * command stream.
     61  1.1  riastrad  * @committed: Whether the view is actually created or pending creation at the
     62  1.1  riastrad  * device level.
     63  1.1  riastrad  * @cmd: The SVGA3D define view command copied from the command stream.
     64  1.1  riastrad  */
     65  1.1  riastrad struct vmw_view {
     66  1.1  riastrad 	struct rcu_head rcu;
     67  1.1  riastrad 	struct vmw_resource res;
     68  1.1  riastrad 	struct vmw_resource *ctx;      /* Immutable */
     69  1.1  riastrad 	struct vmw_resource *srf;      /* Immutable */
     70  1.1  riastrad 	struct vmw_resource *cotable;  /* Immutable */
     71  1.1  riastrad 	struct list_head srf_head;     /* Protected by binding_mutex */
     72  1.1  riastrad 	struct list_head cotable_head; /* Protected by binding_mutex */
     73  1.1  riastrad 	unsigned view_type;            /* Immutable */
     74  1.1  riastrad 	unsigned view_id;              /* Immutable */
     75  1.1  riastrad 	u32 cmd_size;                  /* Immutable */
     76  1.1  riastrad 	bool committed;                /* Protected by binding_mutex */
     77  1.1  riastrad 	u32 cmd[1];                    /* Immutable */
     78  1.1  riastrad };
     79  1.1  riastrad 
     80  1.1  riastrad static int vmw_view_create(struct vmw_resource *res);
     81  1.1  riastrad static int vmw_view_destroy(struct vmw_resource *res);
     82  1.1  riastrad static void vmw_hw_view_destroy(struct vmw_resource *res);
     83  1.1  riastrad static void vmw_view_commit_notify(struct vmw_resource *res,
     84  1.1  riastrad 				   enum vmw_cmdbuf_res_state state);
     85  1.1  riastrad 
     86  1.1  riastrad static const struct vmw_res_func vmw_view_func = {
     87  1.1  riastrad 	.res_type = vmw_res_view,
     88  1.1  riastrad 	.needs_backup = false,
     89  1.1  riastrad 	.may_evict = false,
     90  1.1  riastrad 	.type_name = "DX view",
     91  1.1  riastrad 	.backup_placement = NULL,
     92  1.1  riastrad 	.create = vmw_view_create,
     93  1.1  riastrad 	.commit_notify = vmw_view_commit_notify,
     94  1.1  riastrad };
     95  1.1  riastrad 
     96  1.1  riastrad /**
     97  1.1  riastrad  * struct vmw_view - view define command body stub
     98  1.1  riastrad  *
     99  1.1  riastrad  * @view_id: The device id of the view being defined
    100  1.1  riastrad  * @sid: The surface id of the view being defined
    101  1.1  riastrad  *
    102  1.1  riastrad  * This generic struct is used by the code to change @view_id and @sid of a
    103  1.1  riastrad  * saved view define command.
    104  1.1  riastrad  */
    105  1.1  riastrad struct vmw_view_define {
    106  1.1  riastrad 	uint32 view_id;
    107  1.1  riastrad 	uint32 sid;
    108  1.1  riastrad };
    109  1.1  riastrad 
    110  1.1  riastrad /**
    111  1.1  riastrad  * vmw_view - Convert a struct vmw_resource to a struct vmw_view
    112  1.1  riastrad  *
    113  1.1  riastrad  * @res: Pointer to the resource to convert.
    114  1.1  riastrad  *
    115  1.1  riastrad  * Returns a pointer to a struct vmw_view.
    116  1.1  riastrad  */
    117  1.1  riastrad static struct vmw_view *vmw_view(struct vmw_resource *res)
    118  1.1  riastrad {
    119  1.1  riastrad 	return container_of(res, struct vmw_view, res);
    120  1.1  riastrad }
    121  1.1  riastrad 
    122  1.1  riastrad /**
    123  1.1  riastrad  * vmw_view_commit_notify - Notify that a view operation has been committed to
    124  1.1  riastrad  * hardware from a user-supplied command stream.
    125  1.1  riastrad  *
    126  1.1  riastrad  * @res: Pointer to the view resource.
    127  1.1  riastrad  * @state: Indicating whether a creation or removal has been committed.
    128  1.1  riastrad  *
    129  1.1  riastrad  */
    130  1.1  riastrad static void vmw_view_commit_notify(struct vmw_resource *res,
    131  1.1  riastrad 				   enum vmw_cmdbuf_res_state state)
    132  1.1  riastrad {
    133  1.1  riastrad 	struct vmw_view *view = vmw_view(res);
    134  1.1  riastrad 	struct vmw_private *dev_priv = res->dev_priv;
    135  1.1  riastrad 
    136  1.1  riastrad 	mutex_lock(&dev_priv->binding_mutex);
    137  1.1  riastrad 	if (state == VMW_CMDBUF_RES_ADD) {
    138  1.1  riastrad 		struct vmw_surface *srf = vmw_res_to_srf(view->srf);
    139  1.1  riastrad 
    140  1.1  riastrad 		list_add_tail(&view->srf_head, &srf->view_list);
    141  1.1  riastrad 		vmw_cotable_add_resource(view->cotable, &view->cotable_head);
    142  1.1  riastrad 		view->committed = true;
    143  1.1  riastrad 		res->id = view->view_id;
    144  1.1  riastrad 
    145  1.1  riastrad 	} else {
    146  1.1  riastrad 		list_del_init(&view->cotable_head);
    147  1.1  riastrad 		list_del_init(&view->srf_head);
    148  1.1  riastrad 		view->committed = false;
    149  1.1  riastrad 		res->id = -1;
    150  1.1  riastrad 	}
    151  1.1  riastrad 	mutex_unlock(&dev_priv->binding_mutex);
    152  1.1  riastrad }
    153  1.1  riastrad 
    154  1.1  riastrad /**
    155  1.1  riastrad  * vmw_view_create - Create a hardware view.
    156  1.1  riastrad  *
    157  1.1  riastrad  * @res: Pointer to the view resource.
    158  1.1  riastrad  *
    159  1.1  riastrad  * Create a hardware view. Typically used if that view has previously been
    160  1.1  riastrad  * destroyed by an eviction operation.
    161  1.1  riastrad  */
    162  1.1  riastrad static int vmw_view_create(struct vmw_resource *res)
    163  1.1  riastrad {
    164  1.1  riastrad 	struct vmw_view *view = vmw_view(res);
    165  1.1  riastrad 	struct vmw_surface *srf = vmw_res_to_srf(view->srf);
    166  1.1  riastrad 	struct vmw_private *dev_priv = res->dev_priv;
    167  1.1  riastrad 	struct {
    168  1.1  riastrad 		SVGA3dCmdHeader header;
    169  1.1  riastrad 		struct vmw_view_define body;
    170  1.1  riastrad 	} *cmd;
    171  1.1  riastrad 
    172  1.1  riastrad 	mutex_lock(&dev_priv->binding_mutex);
    173  1.1  riastrad 	if (!view->committed) {
    174  1.1  riastrad 		mutex_unlock(&dev_priv->binding_mutex);
    175  1.1  riastrad 		return 0;
    176  1.1  riastrad 	}
    177  1.1  riastrad 
    178  1.3  riastrad 	cmd = VMW_FIFO_RESERVE_DX(res->dev_priv, view->cmd_size, view->ctx->id);
    179  1.1  riastrad 	if (!cmd) {
    180  1.1  riastrad 		mutex_unlock(&dev_priv->binding_mutex);
    181  1.1  riastrad 		return -ENOMEM;
    182  1.1  riastrad 	}
    183  1.3  riastrad 
    184  1.1  riastrad 	memcpy(cmd, &view->cmd, view->cmd_size);
    185  1.1  riastrad 	WARN_ON(cmd->body.view_id != view->view_id);
    186  1.1  riastrad 	/* Sid may have changed due to surface eviction. */
    187  1.1  riastrad 	WARN_ON(view->srf->id == SVGA3D_INVALID_ID);
    188  1.1  riastrad 	cmd->body.sid = view->srf->id;
    189  1.1  riastrad 	vmw_fifo_commit(res->dev_priv, view->cmd_size);
    190  1.1  riastrad 	res->id = view->view_id;
    191  1.1  riastrad 	list_add_tail(&view->srf_head, &srf->view_list);
    192  1.1  riastrad 	vmw_cotable_add_resource(view->cotable, &view->cotable_head);
    193  1.1  riastrad 	mutex_unlock(&dev_priv->binding_mutex);
    194  1.1  riastrad 
    195  1.1  riastrad 	return 0;
    196  1.1  riastrad }
    197  1.1  riastrad 
    198  1.1  riastrad /**
    199  1.1  riastrad  * vmw_view_destroy - Destroy a hardware view.
    200  1.1  riastrad  *
    201  1.1  riastrad  * @res: Pointer to the view resource.
    202  1.1  riastrad  *
    203  1.1  riastrad  * Destroy a hardware view. Typically used on unexpected termination of the
    204  1.1  riastrad  * owning process or if the surface the view is pointing to is destroyed.
    205  1.1  riastrad  */
    206  1.1  riastrad static int vmw_view_destroy(struct vmw_resource *res)
    207  1.1  riastrad {
    208  1.1  riastrad 	struct vmw_private *dev_priv = res->dev_priv;
    209  1.1  riastrad 	struct vmw_view *view = vmw_view(res);
    210  1.1  riastrad 	struct {
    211  1.1  riastrad 		SVGA3dCmdHeader header;
    212  1.1  riastrad 		union vmw_view_destroy body;
    213  1.1  riastrad 	} *cmd;
    214  1.1  riastrad 
    215  1.3  riastrad 	lockdep_assert_held_once(&dev_priv->binding_mutex);
    216  1.1  riastrad 	vmw_binding_res_list_scrub(&res->binding_head);
    217  1.1  riastrad 
    218  1.1  riastrad 	if (!view->committed || res->id == -1)
    219  1.1  riastrad 		return 0;
    220  1.1  riastrad 
    221  1.3  riastrad 	cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), view->ctx->id);
    222  1.3  riastrad 	if (!cmd)
    223  1.1  riastrad 		return -ENOMEM;
    224  1.1  riastrad 
    225  1.1  riastrad 	cmd->header.id = vmw_view_destroy_cmds[view->view_type];
    226  1.1  riastrad 	cmd->header.size = sizeof(cmd->body);
    227  1.1  riastrad 	cmd->body.view_id = view->view_id;
    228  1.1  riastrad 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
    229  1.1  riastrad 	res->id = -1;
    230  1.1  riastrad 	list_del_init(&view->cotable_head);
    231  1.1  riastrad 	list_del_init(&view->srf_head);
    232  1.1  riastrad 
    233  1.1  riastrad 	return 0;
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad /**
    237  1.1  riastrad  * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup.
    238  1.1  riastrad  *
    239  1.1  riastrad  * @res: Pointer to the view resource.
    240  1.1  riastrad  *
    241  1.1  riastrad  * Destroy a hardware view if it's still present.
    242  1.1  riastrad  */
    243  1.1  riastrad static void vmw_hw_view_destroy(struct vmw_resource *res)
    244  1.1  riastrad {
    245  1.1  riastrad 	struct vmw_private *dev_priv = res->dev_priv;
    246  1.1  riastrad 
    247  1.1  riastrad 	mutex_lock(&dev_priv->binding_mutex);
    248  1.1  riastrad 	WARN_ON(vmw_view_destroy(res));
    249  1.1  riastrad 	res->id = -1;
    250  1.1  riastrad 	mutex_unlock(&dev_priv->binding_mutex);
    251  1.1  riastrad }
    252  1.1  riastrad 
    253  1.1  riastrad /**
    254  1.1  riastrad  * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager
    255  1.1  riastrad  *
    256  1.1  riastrad  * @user_key: The user-space id used for the view.
    257  1.1  riastrad  * @view_type: The view type.
    258  1.1  riastrad  *
    259  1.1  riastrad  * Destroy a hardware view if it's still present.
    260  1.1  riastrad  */
    261  1.1  riastrad static u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type)
    262  1.1  riastrad {
    263  1.1  riastrad 	return user_key | (view_type << 20);
    264  1.1  riastrad }
    265  1.1  riastrad 
    266  1.1  riastrad /**
    267  1.1  riastrad  * vmw_view_id_ok - Basic view id and type range checks.
    268  1.1  riastrad  *
    269  1.1  riastrad  * @user_key: The user-space id used for the view.
    270  1.1  riastrad  * @view_type: The view type.
    271  1.1  riastrad  *
    272  1.1  riastrad  * Checks that the view id and type (typically provided by user-space) is
    273  1.1  riastrad  * valid.
    274  1.1  riastrad  */
    275  1.1  riastrad static bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type)
    276  1.1  riastrad {
    277  1.1  riastrad 	return (user_key < SVGA_COTABLE_MAX_IDS &&
    278  1.1  riastrad 		view_type < vmw_view_max);
    279  1.1  riastrad }
    280  1.1  riastrad 
    281  1.1  riastrad /**
    282  1.1  riastrad  * vmw_view_res_free - resource res_free callback for view resources
    283  1.1  riastrad  *
    284  1.1  riastrad  * @res: Pointer to a struct vmw_resource
    285  1.1  riastrad  *
    286  1.1  riastrad  * Frees memory and memory accounting held by a struct vmw_view.
    287  1.1  riastrad  */
    288  1.1  riastrad static void vmw_view_res_free(struct vmw_resource *res)
    289  1.1  riastrad {
    290  1.1  riastrad 	struct vmw_view *view = vmw_view(res);
    291  1.1  riastrad 	size_t size = offsetof(struct vmw_view, cmd) + view->cmd_size;
    292  1.1  riastrad 	struct vmw_private *dev_priv = res->dev_priv;
    293  1.1  riastrad 
    294  1.1  riastrad 	vmw_resource_unreference(&view->cotable);
    295  1.1  riastrad 	vmw_resource_unreference(&view->srf);
    296  1.1  riastrad 	kfree_rcu(view, rcu);
    297  1.1  riastrad 	ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
    298  1.1  riastrad }
    299  1.1  riastrad 
    300  1.1  riastrad /**
    301  1.1  riastrad  * vmw_view_add - Create a view resource and stage it for addition
    302  1.1  riastrad  * as a command buffer managed resource.
    303  1.1  riastrad  *
    304  1.1  riastrad  * @man: Pointer to the compat shader manager identifying the shader namespace.
    305  1.1  riastrad  * @ctx: Pointer to a struct vmw_resource identifying the active context.
    306  1.1  riastrad  * @srf: Pointer to a struct vmw_resource identifying the surface the view
    307  1.1  riastrad  * points to.
    308  1.1  riastrad  * @view_type: The view type deduced from the view create command.
    309  1.1  riastrad  * @user_key: The key that is used to identify the shader. The key is
    310  1.1  riastrad  * unique to the view type and to the context.
    311  1.1  riastrad  * @cmd: Pointer to the view create command in the command stream.
    312  1.1  riastrad  * @cmd_size: Size of the view create command in the command stream.
    313  1.1  riastrad  * @list: Caller's list of staged command buffer resource actions.
    314  1.1  riastrad  */
    315  1.1  riastrad int vmw_view_add(struct vmw_cmdbuf_res_manager *man,
    316  1.1  riastrad 		 struct vmw_resource *ctx,
    317  1.1  riastrad 		 struct vmw_resource *srf,
    318  1.1  riastrad 		 enum vmw_view_type view_type,
    319  1.1  riastrad 		 u32 user_key,
    320  1.1  riastrad 		 const void *cmd,
    321  1.1  riastrad 		 size_t cmd_size,
    322  1.1  riastrad 		 struct list_head *list)
    323  1.1  riastrad {
    324  1.1  riastrad 	static const size_t vmw_view_define_sizes[] = {
    325  1.1  riastrad 		[vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView),
    326  1.1  riastrad 		[vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView),
    327  1.1  riastrad 		[vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView)
    328  1.1  riastrad 	};
    329  1.1  riastrad 
    330  1.1  riastrad 	struct vmw_private *dev_priv = ctx->dev_priv;
    331  1.1  riastrad 	struct vmw_resource *res;
    332  1.1  riastrad 	struct vmw_view *view;
    333  1.3  riastrad 	struct ttm_operation_ctx ttm_opt_ctx = {
    334  1.3  riastrad 		.interruptible = true,
    335  1.3  riastrad 		.no_wait_gpu = false
    336  1.3  riastrad 	};
    337  1.1  riastrad 	size_t size;
    338  1.1  riastrad 	int ret;
    339  1.1  riastrad 
    340  1.1  riastrad 	if (cmd_size != vmw_view_define_sizes[view_type] +
    341  1.1  riastrad 	    sizeof(SVGA3dCmdHeader)) {
    342  1.3  riastrad 		VMW_DEBUG_USER("Illegal view create command size.\n");
    343  1.1  riastrad 		return -EINVAL;
    344  1.1  riastrad 	}
    345  1.1  riastrad 
    346  1.1  riastrad 	if (!vmw_view_id_ok(user_key, view_type)) {
    347  1.3  riastrad 		VMW_DEBUG_USER("Illegal view add view id.\n");
    348  1.1  riastrad 		return -EINVAL;
    349  1.1  riastrad 	}
    350  1.1  riastrad 
    351  1.1  riastrad 	size = offsetof(struct vmw_view, cmd) + cmd_size;
    352  1.1  riastrad 
    353  1.3  riastrad 	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, &ttm_opt_ctx);
    354  1.1  riastrad 	if (ret) {
    355  1.1  riastrad 		if (ret != -ERESTARTSYS)
    356  1.3  riastrad 			DRM_ERROR("Out of graphics memory for view creation\n");
    357  1.1  riastrad 		return ret;
    358  1.1  riastrad 	}
    359  1.1  riastrad 
    360  1.1  riastrad 	view = kmalloc(size, GFP_KERNEL);
    361  1.1  riastrad 	if (!view) {
    362  1.1  riastrad 		ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
    363  1.1  riastrad 		return -ENOMEM;
    364  1.1  riastrad 	}
    365  1.1  riastrad 
    366  1.1  riastrad 	res = &view->res;
    367  1.1  riastrad 	view->ctx = ctx;
    368  1.1  riastrad 	view->srf = vmw_resource_reference(srf);
    369  1.3  riastrad 	view->cotable = vmw_resource_reference
    370  1.3  riastrad 		(vmw_context_cotable(ctx, vmw_view_cotables[view_type]));
    371  1.1  riastrad 	view->view_type = view_type;
    372  1.1  riastrad 	view->view_id = user_key;
    373  1.1  riastrad 	view->cmd_size = cmd_size;
    374  1.1  riastrad 	view->committed = false;
    375  1.1  riastrad 	INIT_LIST_HEAD(&view->srf_head);
    376  1.1  riastrad 	INIT_LIST_HEAD(&view->cotable_head);
    377  1.1  riastrad 	memcpy(&view->cmd, cmd, cmd_size);
    378  1.1  riastrad 	ret = vmw_resource_init(dev_priv, res, true,
    379  1.1  riastrad 				vmw_view_res_free, &vmw_view_func);
    380  1.1  riastrad 	if (ret)
    381  1.1  riastrad 		goto out_resource_init;
    382  1.1  riastrad 
    383  1.1  riastrad 	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
    384  1.1  riastrad 				 vmw_view_key(user_key, view_type),
    385  1.1  riastrad 				 res, list);
    386  1.1  riastrad 	if (ret)
    387  1.1  riastrad 		goto out_resource_init;
    388  1.1  riastrad 
    389  1.1  riastrad 	res->id = view->view_id;
    390  1.3  riastrad 	res->hw_destroy = vmw_hw_view_destroy;
    391  1.1  riastrad 
    392  1.1  riastrad out_resource_init:
    393  1.1  riastrad 	vmw_resource_unreference(&res);
    394  1.1  riastrad 
    395  1.1  riastrad 	return ret;
    396  1.1  riastrad }
    397  1.1  riastrad 
    398  1.1  riastrad /**
    399  1.1  riastrad  * vmw_view_remove - Stage a view for removal.
    400  1.1  riastrad  *
    401  1.1  riastrad  * @man: Pointer to the view manager identifying the shader namespace.
    402  1.1  riastrad  * @user_key: The key that is used to identify the view. The key is
    403  1.1  riastrad  * unique to the view type.
    404  1.1  riastrad  * @view_type: View type
    405  1.1  riastrad  * @list: Caller's list of staged command buffer resource actions.
    406  1.1  riastrad  * @res_p: If the resource is in an already committed state, points to the
    407  1.1  riastrad  * struct vmw_resource on successful return. The pointer will be
    408  1.1  riastrad  * non ref-counted.
    409  1.1  riastrad  */
    410  1.1  riastrad int vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
    411  1.1  riastrad 		    u32 user_key, enum vmw_view_type view_type,
    412  1.1  riastrad 		    struct list_head *list,
    413  1.1  riastrad 		    struct vmw_resource **res_p)
    414  1.1  riastrad {
    415  1.1  riastrad 	if (!vmw_view_id_ok(user_key, view_type)) {
    416  1.3  riastrad 		VMW_DEBUG_USER("Illegal view remove view id.\n");
    417  1.1  riastrad 		return -EINVAL;
    418  1.1  riastrad 	}
    419  1.1  riastrad 
    420  1.1  riastrad 	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
    421  1.1  riastrad 				     vmw_view_key(user_key, view_type),
    422  1.1  riastrad 				     list, res_p);
    423  1.1  riastrad }
    424  1.1  riastrad 
    425  1.1  riastrad /**
    426  1.1  riastrad  * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable.
    427  1.1  riastrad  *
    428  1.1  riastrad  * @dev_priv: Pointer to a device private struct.
    429  1.1  riastrad  * @list: List of views belonging to a cotable.
    430  1.1  riastrad  * @readback: Unused. Needed for function interface only.
    431  1.1  riastrad  *
    432  1.1  riastrad  * This function evicts all views belonging to a cotable.
    433  1.1  riastrad  * It must be called with the binding_mutex held, and the caller must hold
    434  1.1  riastrad  * a reference to the view resource. This is typically called before the
    435  1.1  riastrad  * cotable is paged out.
    436  1.1  riastrad  */
    437  1.1  riastrad void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
    438  1.1  riastrad 				   struct list_head *list,
    439  1.1  riastrad 				   bool readback)
    440  1.1  riastrad {
    441  1.1  riastrad 	struct vmw_view *entry, *next;
    442  1.1  riastrad 
    443  1.3  riastrad 	lockdep_assert_held_once(&dev_priv->binding_mutex);
    444  1.1  riastrad 
    445  1.1  riastrad 	list_for_each_entry_safe(entry, next, list, cotable_head)
    446  1.1  riastrad 		WARN_ON(vmw_view_destroy(&entry->res));
    447  1.1  riastrad }
    448  1.1  riastrad 
    449  1.1  riastrad /**
    450  1.1  riastrad  * vmw_view_surface_list_destroy - Evict all views pointing to a surface
    451  1.1  riastrad  *
    452  1.1  riastrad  * @dev_priv: Pointer to a device private struct.
    453  1.1  riastrad  * @list: List of views pointing to a surface.
    454  1.1  riastrad  *
    455  1.1  riastrad  * This function evicts all views pointing to a surface. This is typically
    456  1.1  riastrad  * called before the surface is evicted.
    457  1.1  riastrad  */
    458  1.1  riastrad void vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
    459  1.1  riastrad 				   struct list_head *list)
    460  1.1  riastrad {
    461  1.1  riastrad 	struct vmw_view *entry, *next;
    462  1.1  riastrad 
    463  1.3  riastrad 	lockdep_assert_held_once(&dev_priv->binding_mutex);
    464  1.1  riastrad 
    465  1.1  riastrad 	list_for_each_entry_safe(entry, next, list, srf_head)
    466  1.1  riastrad 		WARN_ON(vmw_view_destroy(&entry->res));
    467  1.1  riastrad }
    468  1.1  riastrad 
    469  1.1  riastrad /**
    470  1.1  riastrad  * vmw_view_srf - Return a non-refcounted pointer to the surface a view is
    471  1.1  riastrad  * pointing to.
    472  1.1  riastrad  *
    473  1.1  riastrad  * @res: pointer to a view resource.
    474  1.1  riastrad  *
    475  1.1  riastrad  * Note that the view itself is holding a reference, so as long
    476  1.1  riastrad  * the view resource is alive, the surface resource will be.
    477  1.1  riastrad  */
    478  1.1  riastrad struct vmw_resource *vmw_view_srf(struct vmw_resource *res)
    479  1.1  riastrad {
    480  1.1  riastrad 	return vmw_view(res)->srf;
    481  1.1  riastrad }
    482  1.1  riastrad 
    483  1.1  riastrad /**
    484  1.1  riastrad  * vmw_view_lookup - Look up a view.
    485  1.1  riastrad  *
    486  1.1  riastrad  * @man: The context's cmdbuf ref manager.
    487  1.1  riastrad  * @view_type: The view type.
    488  1.1  riastrad  * @user_key: The view user id.
    489  1.1  riastrad  *
    490  1.1  riastrad  * returns a refcounted pointer to a view or an error pointer if not found.
    491  1.1  riastrad  */
    492  1.1  riastrad struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
    493  1.1  riastrad 				     enum vmw_view_type view_type,
    494  1.1  riastrad 				     u32 user_key)
    495  1.1  riastrad {
    496  1.1  riastrad 	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
    497  1.1  riastrad 				     vmw_view_key(user_key, view_type));
    498  1.1  riastrad }
    499  1.1  riastrad 
    500  1.3  riastrad /**
    501  1.3  riastrad  * vmw_view_dirtying - Return whether a view type is dirtying its resource
    502  1.3  riastrad  * @res: Pointer to the view
    503  1.3  riastrad  *
    504  1.3  riastrad  * Each time a resource is put on the validation list as the result of a
    505  1.3  riastrad  * view pointing to it, we need to determine whether that resource will
    506  1.3  riastrad  * be dirtied (written to by the GPU) as a result of the corresponding
    507  1.3  riastrad  * GPU operation. Currently only rendertarget- and depth-stencil views are
    508  1.3  riastrad  * capable of dirtying its resource.
    509  1.3  riastrad  *
    510  1.3  riastrad  * Return: Whether the view type of @res dirties the resource it points to.
    511  1.3  riastrad  */
    512  1.3  riastrad u32 vmw_view_dirtying(struct vmw_resource *res)
    513  1.3  riastrad {
    514  1.3  riastrad 	static u32 view_is_dirtying[vmw_view_max] = {
    515  1.3  riastrad 		[vmw_view_rt] = VMW_RES_DIRTY_SET,
    516  1.3  riastrad 		[vmw_view_ds] = VMW_RES_DIRTY_SET,
    517  1.3  riastrad 	};
    518  1.3  riastrad 
    519  1.3  riastrad 	/* Update this function as we add more view types */
    520  1.3  riastrad 	BUILD_BUG_ON(vmw_view_max != 3);
    521  1.3  riastrad 	return view_is_dirtying[vmw_view(res)->view_type];
    522  1.3  riastrad }
    523  1.3  riastrad 
    524  1.1  riastrad const u32 vmw_view_destroy_cmds[] = {
    525  1.1  riastrad 	[vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
    526  1.1  riastrad 	[vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
    527  1.1  riastrad 	[vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
    528  1.1  riastrad };
    529  1.1  riastrad 
    530  1.1  riastrad const SVGACOTableType vmw_view_cotables[] = {
    531  1.1  riastrad 	[vmw_view_sr] = SVGA_COTABLE_SRVIEW,
    532  1.1  riastrad 	[vmw_view_rt] = SVGA_COTABLE_RTVIEW,
    533  1.1  riastrad 	[vmw_view_ds] = SVGA_COTABLE_DSVIEW,
    534  1.1  riastrad };
    535  1.1  riastrad 
    536  1.1  riastrad const SVGACOTableType vmw_so_cotables[] = {
    537  1.1  riastrad 	[vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
    538  1.1  riastrad 	[vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
    539  1.1  riastrad 	[vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
    540  1.1  riastrad 	[vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
    541  1.1  riastrad 	[vmw_so_ss] = SVGA_COTABLE_SAMPLER,
    542  1.1  riastrad 	[vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT
    543  1.1  riastrad };
    544  1.1  riastrad 
    545  1.1  riastrad 
    546  1.1  riastrad /* To remove unused function warning */
    547  1.1  riastrad static void vmw_so_build_asserts(void) __attribute__((used));
    548  1.1  riastrad 
    549  1.1  riastrad 
    550  1.1  riastrad /*
    551  1.1  riastrad  * This function is unused at run-time, and only used to dump various build
    552  1.1  riastrad  * asserts important for code optimization assumptions.
    553  1.1  riastrad  */
    554  1.1  riastrad static void vmw_so_build_asserts(void)
    555  1.1  riastrad {
    556  1.1  riastrad 	/* Assert that our vmw_view_cmd_to_type() function is correct. */
    557  1.1  riastrad 	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
    558  1.1  riastrad 		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
    559  1.1  riastrad 	BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
    560  1.1  riastrad 		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
    561  1.1  riastrad 	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
    562  1.1  riastrad 		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
    563  1.1  riastrad 	BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
    564  1.1  riastrad 		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
    565  1.1  riastrad 	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
    566  1.1  riastrad 		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);
    567  1.1  riastrad 
    568  1.1  riastrad 	/* Assert that our "one body fits all" assumption is valid */
    569  1.1  riastrad 	BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));
    570  1.1  riastrad 
    571  1.1  riastrad 	/* Assert that the view key space can hold all view ids. */
    572  1.1  riastrad 	BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));
    573  1.1  riastrad 
    574  1.1  riastrad 	/*
    575  1.1  riastrad 	 * Assert that the offset of sid in all view define commands
    576  1.1  riastrad 	 * is what we assume it to be.
    577  1.1  riastrad 	 */
    578  1.1  riastrad 	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
    579  1.1  riastrad 		     offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
    580  1.1  riastrad 	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
    581  1.1  riastrad 		     offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
    582  1.1  riastrad 	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
    583  1.1  riastrad 		     offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));
    584  1.1  riastrad }
    585