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