162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 OR MIT
262306a36Sopenharmony_ci/**************************************************************************
362306a36Sopenharmony_ci * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
662306a36Sopenharmony_ci * copy of this software and associated documentation files (the
762306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
862306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
962306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
1062306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
1162306a36Sopenharmony_ci * the following conditions:
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the
1462306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
1562306a36Sopenharmony_ci * of the Software.
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1862306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1962306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
2062306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
2162306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
2262306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2362306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci **************************************************************************/
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#include "vmwgfx_bo.h"
2862306a36Sopenharmony_ci#include "vmwgfx_drv.h"
2962306a36Sopenharmony_ci#include "vmwgfx_resource_priv.h"
3062306a36Sopenharmony_ci#include "vmwgfx_so.h"
3162306a36Sopenharmony_ci#include "vmwgfx_binding.h"
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/*
3462306a36Sopenharmony_ci * The currently only reason we need to keep track of views is that if we
3562306a36Sopenharmony_ci * destroy a hardware surface, all views pointing to it must also be destroyed,
3662306a36Sopenharmony_ci * otherwise the device will error.
3762306a36Sopenharmony_ci * So in particular if a surface is evicted, we must destroy all views pointing
3862306a36Sopenharmony_ci * to it, and all context bindings of that view. Similarly we must restore
3962306a36Sopenharmony_ci * the view bindings, views and surfaces pointed to by the views when a
4062306a36Sopenharmony_ci * context is referenced in the command stream.
4162306a36Sopenharmony_ci */
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/**
4462306a36Sopenharmony_ci * struct vmw_view - view metadata
4562306a36Sopenharmony_ci *
4662306a36Sopenharmony_ci * @rcu: RCU callback head
4762306a36Sopenharmony_ci * @res: The struct vmw_resource we derive from
4862306a36Sopenharmony_ci * @ctx: Non-refcounted pointer to the context this view belongs to.
4962306a36Sopenharmony_ci * @srf: Refcounted pointer to the surface pointed to by this view.
5062306a36Sopenharmony_ci * @cotable: Refcounted pointer to the cotable holding this view.
5162306a36Sopenharmony_ci * @srf_head: List head for the surface-to-view list.
5262306a36Sopenharmony_ci * @cotable_head: List head for the cotable-to_view list.
5362306a36Sopenharmony_ci * @view_type: View type.
5462306a36Sopenharmony_ci * @view_id: User-space per context view id. Currently used also as per
5562306a36Sopenharmony_ci * context device view id.
5662306a36Sopenharmony_ci * @cmd_size: Size of the SVGA3D define view command that we've copied from the
5762306a36Sopenharmony_ci * command stream.
5862306a36Sopenharmony_ci * @committed: Whether the view is actually created or pending creation at the
5962306a36Sopenharmony_ci * device level.
6062306a36Sopenharmony_ci * @cmd: The SVGA3D define view command copied from the command stream.
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_cistruct vmw_view {
6362306a36Sopenharmony_ci	struct rcu_head rcu;
6462306a36Sopenharmony_ci	struct vmw_resource res;
6562306a36Sopenharmony_ci	struct vmw_resource *ctx;      /* Immutable */
6662306a36Sopenharmony_ci	struct vmw_resource *srf;      /* Immutable */
6762306a36Sopenharmony_ci	struct vmw_resource *cotable;  /* Immutable */
6862306a36Sopenharmony_ci	struct list_head srf_head;     /* Protected by binding_mutex */
6962306a36Sopenharmony_ci	struct list_head cotable_head; /* Protected by binding_mutex */
7062306a36Sopenharmony_ci	unsigned view_type;            /* Immutable */
7162306a36Sopenharmony_ci	unsigned view_id;              /* Immutable */
7262306a36Sopenharmony_ci	u32 cmd_size;                  /* Immutable */
7362306a36Sopenharmony_ci	bool committed;                /* Protected by binding_mutex */
7462306a36Sopenharmony_ci	u32 cmd[];		       /* Immutable */
7562306a36Sopenharmony_ci};
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistatic int vmw_view_create(struct vmw_resource *res);
7862306a36Sopenharmony_cistatic int vmw_view_destroy(struct vmw_resource *res);
7962306a36Sopenharmony_cistatic void vmw_hw_view_destroy(struct vmw_resource *res);
8062306a36Sopenharmony_cistatic void vmw_view_commit_notify(struct vmw_resource *res,
8162306a36Sopenharmony_ci				   enum vmw_cmdbuf_res_state state);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_cistatic const struct vmw_res_func vmw_view_func = {
8462306a36Sopenharmony_ci	.res_type = vmw_res_view,
8562306a36Sopenharmony_ci	.needs_guest_memory = false,
8662306a36Sopenharmony_ci	.may_evict = false,
8762306a36Sopenharmony_ci	.type_name = "DX view",
8862306a36Sopenharmony_ci	.domain = VMW_BO_DOMAIN_SYS,
8962306a36Sopenharmony_ci	.busy_domain = VMW_BO_DOMAIN_SYS,
9062306a36Sopenharmony_ci	.create = vmw_view_create,
9162306a36Sopenharmony_ci	.commit_notify = vmw_view_commit_notify,
9262306a36Sopenharmony_ci};
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/**
9562306a36Sopenharmony_ci * struct vmw_view_define - view define command body stub
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * @view_id: The device id of the view being defined
9862306a36Sopenharmony_ci * @sid: The surface id of the view being defined
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * This generic struct is used by the code to change @view_id and @sid of a
10162306a36Sopenharmony_ci * saved view define command.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_cistruct vmw_view_define {
10462306a36Sopenharmony_ci	uint32 view_id;
10562306a36Sopenharmony_ci	uint32 sid;
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/**
10962306a36Sopenharmony_ci * vmw_view - Convert a struct vmw_resource to a struct vmw_view
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * @res: Pointer to the resource to convert.
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci * Returns a pointer to a struct vmw_view.
11462306a36Sopenharmony_ci */
11562306a36Sopenharmony_cistatic struct vmw_view *vmw_view(struct vmw_resource *res)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	return container_of(res, struct vmw_view, res);
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/**
12162306a36Sopenharmony_ci * vmw_view_commit_notify - Notify that a view operation has been committed to
12262306a36Sopenharmony_ci * hardware from a user-supplied command stream.
12362306a36Sopenharmony_ci *
12462306a36Sopenharmony_ci * @res: Pointer to the view resource.
12562306a36Sopenharmony_ci * @state: Indicating whether a creation or removal has been committed.
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci */
12862306a36Sopenharmony_cistatic void vmw_view_commit_notify(struct vmw_resource *res,
12962306a36Sopenharmony_ci				   enum vmw_cmdbuf_res_state state)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	struct vmw_view *view = vmw_view(res);
13262306a36Sopenharmony_ci	struct vmw_private *dev_priv = res->dev_priv;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	mutex_lock(&dev_priv->binding_mutex);
13562306a36Sopenharmony_ci	if (state == VMW_CMDBUF_RES_ADD) {
13662306a36Sopenharmony_ci		struct vmw_surface *srf = vmw_res_to_srf(view->srf);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci		list_add_tail(&view->srf_head, &srf->view_list);
13962306a36Sopenharmony_ci		vmw_cotable_add_resource(view->cotable, &view->cotable_head);
14062306a36Sopenharmony_ci		view->committed = true;
14162306a36Sopenharmony_ci		res->id = view->view_id;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	} else {
14462306a36Sopenharmony_ci		list_del_init(&view->cotable_head);
14562306a36Sopenharmony_ci		list_del_init(&view->srf_head);
14662306a36Sopenharmony_ci		view->committed = false;
14762306a36Sopenharmony_ci		res->id = -1;
14862306a36Sopenharmony_ci	}
14962306a36Sopenharmony_ci	mutex_unlock(&dev_priv->binding_mutex);
15062306a36Sopenharmony_ci}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci/**
15362306a36Sopenharmony_ci * vmw_view_create - Create a hardware view.
15462306a36Sopenharmony_ci *
15562306a36Sopenharmony_ci * @res: Pointer to the view resource.
15662306a36Sopenharmony_ci *
15762306a36Sopenharmony_ci * Create a hardware view. Typically used if that view has previously been
15862306a36Sopenharmony_ci * destroyed by an eviction operation.
15962306a36Sopenharmony_ci */
16062306a36Sopenharmony_cistatic int vmw_view_create(struct vmw_resource *res)
16162306a36Sopenharmony_ci{
16262306a36Sopenharmony_ci	struct vmw_view *view = vmw_view(res);
16362306a36Sopenharmony_ci	struct vmw_surface *srf = vmw_res_to_srf(view->srf);
16462306a36Sopenharmony_ci	struct vmw_private *dev_priv = res->dev_priv;
16562306a36Sopenharmony_ci	struct {
16662306a36Sopenharmony_ci		SVGA3dCmdHeader header;
16762306a36Sopenharmony_ci		struct vmw_view_define body;
16862306a36Sopenharmony_ci	} *cmd;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	mutex_lock(&dev_priv->binding_mutex);
17162306a36Sopenharmony_ci	if (!view->committed) {
17262306a36Sopenharmony_ci		mutex_unlock(&dev_priv->binding_mutex);
17362306a36Sopenharmony_ci		return 0;
17462306a36Sopenharmony_ci	}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	cmd = VMW_CMD_CTX_RESERVE(res->dev_priv, view->cmd_size, view->ctx->id);
17762306a36Sopenharmony_ci	if (!cmd) {
17862306a36Sopenharmony_ci		mutex_unlock(&dev_priv->binding_mutex);
17962306a36Sopenharmony_ci		return -ENOMEM;
18062306a36Sopenharmony_ci	}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	memcpy(cmd, &view->cmd, view->cmd_size);
18362306a36Sopenharmony_ci	WARN_ON(cmd->body.view_id != view->view_id);
18462306a36Sopenharmony_ci	/* Sid may have changed due to surface eviction. */
18562306a36Sopenharmony_ci	WARN_ON(view->srf->id == SVGA3D_INVALID_ID);
18662306a36Sopenharmony_ci	cmd->body.sid = view->srf->id;
18762306a36Sopenharmony_ci	vmw_cmd_commit(res->dev_priv, view->cmd_size);
18862306a36Sopenharmony_ci	res->id = view->view_id;
18962306a36Sopenharmony_ci	list_add_tail(&view->srf_head, &srf->view_list);
19062306a36Sopenharmony_ci	vmw_cotable_add_resource(view->cotable, &view->cotable_head);
19162306a36Sopenharmony_ci	mutex_unlock(&dev_priv->binding_mutex);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	return 0;
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/**
19762306a36Sopenharmony_ci * vmw_view_destroy - Destroy a hardware view.
19862306a36Sopenharmony_ci *
19962306a36Sopenharmony_ci * @res: Pointer to the view resource.
20062306a36Sopenharmony_ci *
20162306a36Sopenharmony_ci * Destroy a hardware view. Typically used on unexpected termination of the
20262306a36Sopenharmony_ci * owning process or if the surface the view is pointing to is destroyed.
20362306a36Sopenharmony_ci */
20462306a36Sopenharmony_cistatic int vmw_view_destroy(struct vmw_resource *res)
20562306a36Sopenharmony_ci{
20662306a36Sopenharmony_ci	struct vmw_private *dev_priv = res->dev_priv;
20762306a36Sopenharmony_ci	struct vmw_view *view = vmw_view(res);
20862306a36Sopenharmony_ci	struct {
20962306a36Sopenharmony_ci		SVGA3dCmdHeader header;
21062306a36Sopenharmony_ci		union vmw_view_destroy body;
21162306a36Sopenharmony_ci	} *cmd;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	lockdep_assert_held_once(&dev_priv->binding_mutex);
21462306a36Sopenharmony_ci	vmw_binding_res_list_scrub(&res->binding_head);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	if (!view->committed || res->id == -1)
21762306a36Sopenharmony_ci		return 0;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	cmd = VMW_CMD_CTX_RESERVE(dev_priv, sizeof(*cmd), view->ctx->id);
22062306a36Sopenharmony_ci	if (!cmd)
22162306a36Sopenharmony_ci		return -ENOMEM;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	cmd->header.id = vmw_view_destroy_cmds[view->view_type];
22462306a36Sopenharmony_ci	cmd->header.size = sizeof(cmd->body);
22562306a36Sopenharmony_ci	cmd->body.view_id = view->view_id;
22662306a36Sopenharmony_ci	vmw_cmd_commit(dev_priv, sizeof(*cmd));
22762306a36Sopenharmony_ci	res->id = -1;
22862306a36Sopenharmony_ci	list_del_init(&view->cotable_head);
22962306a36Sopenharmony_ci	list_del_init(&view->srf_head);
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	return 0;
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci/**
23562306a36Sopenharmony_ci * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup.
23662306a36Sopenharmony_ci *
23762306a36Sopenharmony_ci * @res: Pointer to the view resource.
23862306a36Sopenharmony_ci *
23962306a36Sopenharmony_ci * Destroy a hardware view if it's still present.
24062306a36Sopenharmony_ci */
24162306a36Sopenharmony_cistatic void vmw_hw_view_destroy(struct vmw_resource *res)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	struct vmw_private *dev_priv = res->dev_priv;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	mutex_lock(&dev_priv->binding_mutex);
24662306a36Sopenharmony_ci	WARN_ON(vmw_view_destroy(res));
24762306a36Sopenharmony_ci	res->id = -1;
24862306a36Sopenharmony_ci	mutex_unlock(&dev_priv->binding_mutex);
24962306a36Sopenharmony_ci}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci/**
25262306a36Sopenharmony_ci * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager
25362306a36Sopenharmony_ci *
25462306a36Sopenharmony_ci * @user_key: The user-space id used for the view.
25562306a36Sopenharmony_ci * @view_type: The view type.
25662306a36Sopenharmony_ci *
25762306a36Sopenharmony_ci * Destroy a hardware view if it's still present.
25862306a36Sopenharmony_ci */
25962306a36Sopenharmony_cistatic u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type)
26062306a36Sopenharmony_ci{
26162306a36Sopenharmony_ci	return user_key | (view_type << 20);
26262306a36Sopenharmony_ci}
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci/**
26562306a36Sopenharmony_ci * vmw_view_id_ok - Basic view id and type range checks.
26662306a36Sopenharmony_ci *
26762306a36Sopenharmony_ci * @user_key: The user-space id used for the view.
26862306a36Sopenharmony_ci * @view_type: The view type.
26962306a36Sopenharmony_ci *
27062306a36Sopenharmony_ci * Checks that the view id and type (typically provided by user-space) is
27162306a36Sopenharmony_ci * valid.
27262306a36Sopenharmony_ci */
27362306a36Sopenharmony_cistatic bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type)
27462306a36Sopenharmony_ci{
27562306a36Sopenharmony_ci	return (user_key < SVGA_COTABLE_MAX_IDS &&
27662306a36Sopenharmony_ci		view_type < vmw_view_max);
27762306a36Sopenharmony_ci}
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci/**
28062306a36Sopenharmony_ci * vmw_view_res_free - resource res_free callback for view resources
28162306a36Sopenharmony_ci *
28262306a36Sopenharmony_ci * @res: Pointer to a struct vmw_resource
28362306a36Sopenharmony_ci *
28462306a36Sopenharmony_ci * Frees memory held by the struct vmw_view.
28562306a36Sopenharmony_ci */
28662306a36Sopenharmony_cistatic void vmw_view_res_free(struct vmw_resource *res)
28762306a36Sopenharmony_ci{
28862306a36Sopenharmony_ci	struct vmw_view *view = vmw_view(res);
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	vmw_resource_unreference(&view->cotable);
29162306a36Sopenharmony_ci	vmw_resource_unreference(&view->srf);
29262306a36Sopenharmony_ci	kfree_rcu(view, rcu);
29362306a36Sopenharmony_ci}
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci/**
29662306a36Sopenharmony_ci * vmw_view_add - Create a view resource and stage it for addition
29762306a36Sopenharmony_ci * as a command buffer managed resource.
29862306a36Sopenharmony_ci *
29962306a36Sopenharmony_ci * @man: Pointer to the compat shader manager identifying the shader namespace.
30062306a36Sopenharmony_ci * @ctx: Pointer to a struct vmw_resource identifying the active context.
30162306a36Sopenharmony_ci * @srf: Pointer to a struct vmw_resource identifying the surface the view
30262306a36Sopenharmony_ci * points to.
30362306a36Sopenharmony_ci * @view_type: The view type deduced from the view create command.
30462306a36Sopenharmony_ci * @user_key: The key that is used to identify the shader. The key is
30562306a36Sopenharmony_ci * unique to the view type and to the context.
30662306a36Sopenharmony_ci * @cmd: Pointer to the view create command in the command stream.
30762306a36Sopenharmony_ci * @cmd_size: Size of the view create command in the command stream.
30862306a36Sopenharmony_ci * @list: Caller's list of staged command buffer resource actions.
30962306a36Sopenharmony_ci */
31062306a36Sopenharmony_ciint vmw_view_add(struct vmw_cmdbuf_res_manager *man,
31162306a36Sopenharmony_ci		 struct vmw_resource *ctx,
31262306a36Sopenharmony_ci		 struct vmw_resource *srf,
31362306a36Sopenharmony_ci		 enum vmw_view_type view_type,
31462306a36Sopenharmony_ci		 u32 user_key,
31562306a36Sopenharmony_ci		 const void *cmd,
31662306a36Sopenharmony_ci		 size_t cmd_size,
31762306a36Sopenharmony_ci		 struct list_head *list)
31862306a36Sopenharmony_ci{
31962306a36Sopenharmony_ci	static const size_t vmw_view_define_sizes[] = {
32062306a36Sopenharmony_ci		[vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView),
32162306a36Sopenharmony_ci		[vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView),
32262306a36Sopenharmony_ci		[vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView),
32362306a36Sopenharmony_ci		[vmw_view_ua] = sizeof(SVGA3dCmdDXDefineUAView)
32462306a36Sopenharmony_ci	};
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	struct vmw_private *dev_priv = ctx->dev_priv;
32762306a36Sopenharmony_ci	struct vmw_resource *res;
32862306a36Sopenharmony_ci	struct vmw_view *view;
32962306a36Sopenharmony_ci	size_t size;
33062306a36Sopenharmony_ci	int ret;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	if (cmd_size != vmw_view_define_sizes[view_type] +
33362306a36Sopenharmony_ci	    sizeof(SVGA3dCmdHeader)) {
33462306a36Sopenharmony_ci		VMW_DEBUG_USER("Illegal view create command size.\n");
33562306a36Sopenharmony_ci		return -EINVAL;
33662306a36Sopenharmony_ci	}
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci	if (!vmw_view_id_ok(user_key, view_type)) {
33962306a36Sopenharmony_ci		VMW_DEBUG_USER("Illegal view add view id.\n");
34062306a36Sopenharmony_ci		return -EINVAL;
34162306a36Sopenharmony_ci	}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	size = offsetof(struct vmw_view, cmd) + cmd_size;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	view = kmalloc(size, GFP_KERNEL);
34662306a36Sopenharmony_ci	if (!view) {
34762306a36Sopenharmony_ci		return -ENOMEM;
34862306a36Sopenharmony_ci	}
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	res = &view->res;
35162306a36Sopenharmony_ci	view->ctx = ctx;
35262306a36Sopenharmony_ci	view->srf = vmw_resource_reference(srf);
35362306a36Sopenharmony_ci	view->cotable = vmw_resource_reference
35462306a36Sopenharmony_ci		(vmw_context_cotable(ctx, vmw_view_cotables[view_type]));
35562306a36Sopenharmony_ci	view->view_type = view_type;
35662306a36Sopenharmony_ci	view->view_id = user_key;
35762306a36Sopenharmony_ci	view->cmd_size = cmd_size;
35862306a36Sopenharmony_ci	view->committed = false;
35962306a36Sopenharmony_ci	INIT_LIST_HEAD(&view->srf_head);
36062306a36Sopenharmony_ci	INIT_LIST_HEAD(&view->cotable_head);
36162306a36Sopenharmony_ci	memcpy(&view->cmd, cmd, cmd_size);
36262306a36Sopenharmony_ci	ret = vmw_resource_init(dev_priv, res, true,
36362306a36Sopenharmony_ci				vmw_view_res_free, &vmw_view_func);
36462306a36Sopenharmony_ci	if (ret)
36562306a36Sopenharmony_ci		goto out_resource_init;
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci	ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
36862306a36Sopenharmony_ci				 vmw_view_key(user_key, view_type),
36962306a36Sopenharmony_ci				 res, list);
37062306a36Sopenharmony_ci	if (ret)
37162306a36Sopenharmony_ci		goto out_resource_init;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	res->id = view->view_id;
37462306a36Sopenharmony_ci	res->hw_destroy = vmw_hw_view_destroy;
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ciout_resource_init:
37762306a36Sopenharmony_ci	vmw_resource_unreference(&res);
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	return ret;
38062306a36Sopenharmony_ci}
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci/**
38362306a36Sopenharmony_ci * vmw_view_remove - Stage a view for removal.
38462306a36Sopenharmony_ci *
38562306a36Sopenharmony_ci * @man: Pointer to the view manager identifying the shader namespace.
38662306a36Sopenharmony_ci * @user_key: The key that is used to identify the view. The key is
38762306a36Sopenharmony_ci * unique to the view type.
38862306a36Sopenharmony_ci * @view_type: View type
38962306a36Sopenharmony_ci * @list: Caller's list of staged command buffer resource actions.
39062306a36Sopenharmony_ci * @res_p: If the resource is in an already committed state, points to the
39162306a36Sopenharmony_ci * struct vmw_resource on successful return. The pointer will be
39262306a36Sopenharmony_ci * non ref-counted.
39362306a36Sopenharmony_ci */
39462306a36Sopenharmony_ciint vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
39562306a36Sopenharmony_ci		    u32 user_key, enum vmw_view_type view_type,
39662306a36Sopenharmony_ci		    struct list_head *list,
39762306a36Sopenharmony_ci		    struct vmw_resource **res_p)
39862306a36Sopenharmony_ci{
39962306a36Sopenharmony_ci	if (!vmw_view_id_ok(user_key, view_type)) {
40062306a36Sopenharmony_ci		VMW_DEBUG_USER("Illegal view remove view id.\n");
40162306a36Sopenharmony_ci		return -EINVAL;
40262306a36Sopenharmony_ci	}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
40562306a36Sopenharmony_ci				     vmw_view_key(user_key, view_type),
40662306a36Sopenharmony_ci				     list, res_p);
40762306a36Sopenharmony_ci}
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci/**
41062306a36Sopenharmony_ci * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable.
41162306a36Sopenharmony_ci *
41262306a36Sopenharmony_ci * @dev_priv: Pointer to a device private struct.
41362306a36Sopenharmony_ci * @list: List of views belonging to a cotable.
41462306a36Sopenharmony_ci * @readback: Unused. Needed for function interface only.
41562306a36Sopenharmony_ci *
41662306a36Sopenharmony_ci * This function evicts all views belonging to a cotable.
41762306a36Sopenharmony_ci * It must be called with the binding_mutex held, and the caller must hold
41862306a36Sopenharmony_ci * a reference to the view resource. This is typically called before the
41962306a36Sopenharmony_ci * cotable is paged out.
42062306a36Sopenharmony_ci */
42162306a36Sopenharmony_civoid vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
42262306a36Sopenharmony_ci				   struct list_head *list,
42362306a36Sopenharmony_ci				   bool readback)
42462306a36Sopenharmony_ci{
42562306a36Sopenharmony_ci	struct vmw_view *entry, *next;
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	lockdep_assert_held_once(&dev_priv->binding_mutex);
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci	list_for_each_entry_safe(entry, next, list, cotable_head)
43062306a36Sopenharmony_ci		WARN_ON(vmw_view_destroy(&entry->res));
43162306a36Sopenharmony_ci}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci/**
43462306a36Sopenharmony_ci * vmw_view_surface_list_destroy - Evict all views pointing to a surface
43562306a36Sopenharmony_ci *
43662306a36Sopenharmony_ci * @dev_priv: Pointer to a device private struct.
43762306a36Sopenharmony_ci * @list: List of views pointing to a surface.
43862306a36Sopenharmony_ci *
43962306a36Sopenharmony_ci * This function evicts all views pointing to a surface. This is typically
44062306a36Sopenharmony_ci * called before the surface is evicted.
44162306a36Sopenharmony_ci */
44262306a36Sopenharmony_civoid vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
44362306a36Sopenharmony_ci				   struct list_head *list)
44462306a36Sopenharmony_ci{
44562306a36Sopenharmony_ci	struct vmw_view *entry, *next;
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci	lockdep_assert_held_once(&dev_priv->binding_mutex);
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	list_for_each_entry_safe(entry, next, list, srf_head)
45062306a36Sopenharmony_ci		WARN_ON(vmw_view_destroy(&entry->res));
45162306a36Sopenharmony_ci}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci/**
45462306a36Sopenharmony_ci * vmw_view_srf - Return a non-refcounted pointer to the surface a view is
45562306a36Sopenharmony_ci * pointing to.
45662306a36Sopenharmony_ci *
45762306a36Sopenharmony_ci * @res: pointer to a view resource.
45862306a36Sopenharmony_ci *
45962306a36Sopenharmony_ci * Note that the view itself is holding a reference, so as long
46062306a36Sopenharmony_ci * the view resource is alive, the surface resource will be.
46162306a36Sopenharmony_ci */
46262306a36Sopenharmony_cistruct vmw_resource *vmw_view_srf(struct vmw_resource *res)
46362306a36Sopenharmony_ci{
46462306a36Sopenharmony_ci	return vmw_view(res)->srf;
46562306a36Sopenharmony_ci}
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci/**
46862306a36Sopenharmony_ci * vmw_view_lookup - Look up a view.
46962306a36Sopenharmony_ci *
47062306a36Sopenharmony_ci * @man: The context's cmdbuf ref manager.
47162306a36Sopenharmony_ci * @view_type: The view type.
47262306a36Sopenharmony_ci * @user_key: The view user id.
47362306a36Sopenharmony_ci *
47462306a36Sopenharmony_ci * returns a refcounted pointer to a view or an error pointer if not found.
47562306a36Sopenharmony_ci */
47662306a36Sopenharmony_cistruct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
47762306a36Sopenharmony_ci				     enum vmw_view_type view_type,
47862306a36Sopenharmony_ci				     u32 user_key)
47962306a36Sopenharmony_ci{
48062306a36Sopenharmony_ci	return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
48162306a36Sopenharmony_ci				     vmw_view_key(user_key, view_type));
48262306a36Sopenharmony_ci}
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci/**
48562306a36Sopenharmony_ci * vmw_view_dirtying - Return whether a view type is dirtying its resource
48662306a36Sopenharmony_ci * @res: Pointer to the view
48762306a36Sopenharmony_ci *
48862306a36Sopenharmony_ci * Each time a resource is put on the validation list as the result of a
48962306a36Sopenharmony_ci * view pointing to it, we need to determine whether that resource will
49062306a36Sopenharmony_ci * be dirtied (written to by the GPU) as a result of the corresponding
49162306a36Sopenharmony_ci * GPU operation. Currently only rendertarget-, depth-stencil and unordered
49262306a36Sopenharmony_ci * access views are capable of dirtying its resource.
49362306a36Sopenharmony_ci *
49462306a36Sopenharmony_ci * Return: Whether the view type of @res dirties the resource it points to.
49562306a36Sopenharmony_ci */
49662306a36Sopenharmony_ciu32 vmw_view_dirtying(struct vmw_resource *res)
49762306a36Sopenharmony_ci{
49862306a36Sopenharmony_ci	static u32 view_is_dirtying[vmw_view_max] = {
49962306a36Sopenharmony_ci		[vmw_view_rt] = VMW_RES_DIRTY_SET,
50062306a36Sopenharmony_ci		[vmw_view_ds] = VMW_RES_DIRTY_SET,
50162306a36Sopenharmony_ci		[vmw_view_ua] = VMW_RES_DIRTY_SET,
50262306a36Sopenharmony_ci	};
50362306a36Sopenharmony_ci
50462306a36Sopenharmony_ci	/* Update this function as we add more view types */
50562306a36Sopenharmony_ci	BUILD_BUG_ON(vmw_view_max != 4);
50662306a36Sopenharmony_ci	return view_is_dirtying[vmw_view(res)->view_type];
50762306a36Sopenharmony_ci}
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ciconst u32 vmw_view_destroy_cmds[] = {
51062306a36Sopenharmony_ci	[vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
51162306a36Sopenharmony_ci	[vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
51262306a36Sopenharmony_ci	[vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
51362306a36Sopenharmony_ci	[vmw_view_ua] = SVGA_3D_CMD_DX_DESTROY_UA_VIEW,
51462306a36Sopenharmony_ci};
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ciconst SVGACOTableType vmw_view_cotables[] = {
51762306a36Sopenharmony_ci	[vmw_view_sr] = SVGA_COTABLE_SRVIEW,
51862306a36Sopenharmony_ci	[vmw_view_rt] = SVGA_COTABLE_RTVIEW,
51962306a36Sopenharmony_ci	[vmw_view_ds] = SVGA_COTABLE_DSVIEW,
52062306a36Sopenharmony_ci	[vmw_view_ua] = SVGA_COTABLE_UAVIEW,
52162306a36Sopenharmony_ci};
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ciconst SVGACOTableType vmw_so_cotables[] = {
52462306a36Sopenharmony_ci	[vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
52562306a36Sopenharmony_ci	[vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
52662306a36Sopenharmony_ci	[vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
52762306a36Sopenharmony_ci	[vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
52862306a36Sopenharmony_ci	[vmw_so_ss] = SVGA_COTABLE_SAMPLER,
52962306a36Sopenharmony_ci	[vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT,
53062306a36Sopenharmony_ci	[vmw_so_max]= SVGA_COTABLE_MAX
53162306a36Sopenharmony_ci};
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci/* To remove unused function warning */
53562306a36Sopenharmony_cistatic void vmw_so_build_asserts(void) __attribute__((used));
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci/*
53962306a36Sopenharmony_ci * This function is unused at run-time, and only used to dump various build
54062306a36Sopenharmony_ci * asserts important for code optimization assumptions.
54162306a36Sopenharmony_ci */
54262306a36Sopenharmony_cistatic void vmw_so_build_asserts(void)
54362306a36Sopenharmony_ci{
54462306a36Sopenharmony_ci	/* Assert that our vmw_view_cmd_to_type() function is correct. */
54562306a36Sopenharmony_ci	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
54662306a36Sopenharmony_ci		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
54762306a36Sopenharmony_ci	BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
54862306a36Sopenharmony_ci		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
54962306a36Sopenharmony_ci	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
55062306a36Sopenharmony_ci		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
55162306a36Sopenharmony_ci	BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
55262306a36Sopenharmony_ci		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
55362306a36Sopenharmony_ci	BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
55462306a36Sopenharmony_ci		     SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	/* Assert that our "one body fits all" assumption is valid */
55762306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	/* Assert that the view key space can hold all view ids. */
56062306a36Sopenharmony_ci	BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	/*
56362306a36Sopenharmony_ci	 * Assert that the offset of sid in all view define commands
56462306a36Sopenharmony_ci	 * is what we assume it to be.
56562306a36Sopenharmony_ci	 */
56662306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
56762306a36Sopenharmony_ci		     offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
56862306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
56962306a36Sopenharmony_ci		     offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
57062306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
57162306a36Sopenharmony_ci		     offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));
57262306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
57362306a36Sopenharmony_ci		     offsetof(SVGA3dCmdDXDefineUAView, sid));
57462306a36Sopenharmony_ci	BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
57562306a36Sopenharmony_ci		     offsetof(SVGA3dCmdDXDefineDepthStencilView_v2, sid));
57662306a36Sopenharmony_ci}
577