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