Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / vmwgfx / vmwgfx_so.c
blob3f97b61dd5d83bf87c437ccf28ec45e95c497728
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3 * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
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:
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.
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.
25 **************************************************************************/
27 #include "vmwgfx_drv.h"
28 #include "vmwgfx_resource_priv.h"
29 #include "vmwgfx_so.h"
30 #include "vmwgfx_binding.h"
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.
42 /**
43 * struct vmw_view - view metadata
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.
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 */
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);
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,
91 /**
92 * struct vmw_view - view define command body stub
94 * @view_id: The device id of the view being defined
95 * @sid: The surface id of the view being defined
97 * This generic struct is used by the code to change @view_id and @sid of a
98 * saved view define command.
100 struct vmw_view_define {
101 uint32 view_id;
102 uint32 sid;
106 * vmw_view - Convert a struct vmw_resource to a struct vmw_view
108 * @res: Pointer to the resource to convert.
110 * Returns a pointer to a struct vmw_view.
112 static struct vmw_view *vmw_view(struct vmw_resource *res)
114 return container_of(res, struct vmw_view, res);
118 * vmw_view_commit_notify - Notify that a view operation has been committed to
119 * hardware from a user-supplied command stream.
121 * @res: Pointer to the view resource.
122 * @state: Indicating whether a creation or removal has been committed.
125 static void vmw_view_commit_notify(struct vmw_resource *res,
126 enum vmw_cmdbuf_res_state state)
128 struct vmw_view *view = vmw_view(res);
129 struct vmw_private *dev_priv = res->dev_priv;
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);
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;
140 } else {
141 list_del_init(&view->cotable_head);
142 list_del_init(&view->srf_head);
143 view->committed = false;
144 res->id = -1;
146 mutex_unlock(&dev_priv->binding_mutex);
150 * vmw_view_create - Create a hardware view.
152 * @res: Pointer to the view resource.
154 * Create a hardware view. Typically used if that view has previously been
155 * destroyed by an eviction operation.
157 static int vmw_view_create(struct vmw_resource *res)
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;
167 mutex_lock(&dev_priv->binding_mutex);
168 if (!view->committed) {
169 mutex_unlock(&dev_priv->binding_mutex);
170 return 0;
173 cmd = VMW_FIFO_RESERVE_DX(res->dev_priv, view->cmd_size, view->ctx->id);
174 if (!cmd) {
175 mutex_unlock(&dev_priv->binding_mutex);
176 return -ENOMEM;
179 memcpy(cmd, &view->cmd, view->cmd_size);
180 WARN_ON(cmd->body.view_id != view->view_id);
181 /* Sid may have changed due to surface eviction. */
182 WARN_ON(view->srf->id == SVGA3D_INVALID_ID);
183 cmd->body.sid = view->srf->id;
184 vmw_fifo_commit(res->dev_priv, view->cmd_size);
185 res->id = view->view_id;
186 list_add_tail(&view->srf_head, &srf->view_list);
187 vmw_cotable_add_resource(view->cotable, &view->cotable_head);
188 mutex_unlock(&dev_priv->binding_mutex);
190 return 0;
194 * vmw_view_destroy - Destroy a hardware view.
196 * @res: Pointer to the view resource.
198 * Destroy a hardware view. Typically used on unexpected termination of the
199 * owning process or if the surface the view is pointing to is destroyed.
201 static int vmw_view_destroy(struct vmw_resource *res)
203 struct vmw_private *dev_priv = res->dev_priv;
204 struct vmw_view *view = vmw_view(res);
205 struct {
206 SVGA3dCmdHeader header;
207 union vmw_view_destroy body;
208 } *cmd;
210 lockdep_assert_held_once(&dev_priv->binding_mutex);
211 vmw_binding_res_list_scrub(&res->binding_head);
213 if (!view->committed || res->id == -1)
214 return 0;
216 cmd = VMW_FIFO_RESERVE_DX(dev_priv, sizeof(*cmd), view->ctx->id);
217 if (!cmd)
218 return -ENOMEM;
220 cmd->header.id = vmw_view_destroy_cmds[view->view_type];
221 cmd->header.size = sizeof(cmd->body);
222 cmd->body.view_id = view->view_id;
223 vmw_fifo_commit(dev_priv, sizeof(*cmd));
224 res->id = -1;
225 list_del_init(&view->cotable_head);
226 list_del_init(&view->srf_head);
228 return 0;
232 * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup.
234 * @res: Pointer to the view resource.
236 * Destroy a hardware view if it's still present.
238 static void vmw_hw_view_destroy(struct vmw_resource *res)
240 struct vmw_private *dev_priv = res->dev_priv;
242 mutex_lock(&dev_priv->binding_mutex);
243 WARN_ON(vmw_view_destroy(res));
244 res->id = -1;
245 mutex_unlock(&dev_priv->binding_mutex);
249 * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager
251 * @user_key: The user-space id used for the view.
252 * @view_type: The view type.
254 * Destroy a hardware view if it's still present.
256 static u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type)
258 return user_key | (view_type << 20);
262 * vmw_view_id_ok - Basic view id and type range checks.
264 * @user_key: The user-space id used for the view.
265 * @view_type: The view type.
267 * Checks that the view id and type (typically provided by user-space) is
268 * valid.
270 static bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type)
272 return (user_key < SVGA_COTABLE_MAX_IDS &&
273 view_type < vmw_view_max);
277 * vmw_view_res_free - resource res_free callback for view resources
279 * @res: Pointer to a struct vmw_resource
281 * Frees memory and memory accounting held by a struct vmw_view.
283 static void vmw_view_res_free(struct vmw_resource *res)
285 struct vmw_view *view = vmw_view(res);
286 size_t size = offsetof(struct vmw_view, cmd) + view->cmd_size;
287 struct vmw_private *dev_priv = res->dev_priv;
289 vmw_resource_unreference(&view->cotable);
290 vmw_resource_unreference(&view->srf);
291 kfree_rcu(view, rcu);
292 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
296 * vmw_view_add - Create a view resource and stage it for addition
297 * as a command buffer managed resource.
299 * @man: Pointer to the compat shader manager identifying the shader namespace.
300 * @ctx: Pointer to a struct vmw_resource identifying the active context.
301 * @srf: Pointer to a struct vmw_resource identifying the surface the view
302 * points to.
303 * @view_type: The view type deduced from the view create command.
304 * @user_key: The key that is used to identify the shader. The key is
305 * unique to the view type and to the context.
306 * @cmd: Pointer to the view create command in the command stream.
307 * @cmd_size: Size of the view create command in the command stream.
308 * @list: Caller's list of staged command buffer resource actions.
310 int vmw_view_add(struct vmw_cmdbuf_res_manager *man,
311 struct vmw_resource *ctx,
312 struct vmw_resource *srf,
313 enum vmw_view_type view_type,
314 u32 user_key,
315 const void *cmd,
316 size_t cmd_size,
317 struct list_head *list)
319 static const size_t vmw_view_define_sizes[] = {
320 [vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView),
321 [vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView),
322 [vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView),
323 [vmw_view_ua] = sizeof(SVGA3dCmdDXDefineUAView)
326 struct vmw_private *dev_priv = ctx->dev_priv;
327 struct vmw_resource *res;
328 struct vmw_view *view;
329 struct ttm_operation_ctx ttm_opt_ctx = {
330 .interruptible = true,
331 .no_wait_gpu = false
333 size_t size;
334 int ret;
336 if (cmd_size != vmw_view_define_sizes[view_type] +
337 sizeof(SVGA3dCmdHeader)) {
338 VMW_DEBUG_USER("Illegal view create command size.\n");
339 return -EINVAL;
342 if (!vmw_view_id_ok(user_key, view_type)) {
343 VMW_DEBUG_USER("Illegal view add view id.\n");
344 return -EINVAL;
347 size = offsetof(struct vmw_view, cmd) + cmd_size;
349 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, &ttm_opt_ctx);
350 if (ret) {
351 if (ret != -ERESTARTSYS)
352 DRM_ERROR("Out of graphics memory for view creation\n");
353 return ret;
356 view = kmalloc(size, GFP_KERNEL);
357 if (!view) {
358 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
359 return -ENOMEM;
362 res = &view->res;
363 view->ctx = ctx;
364 view->srf = vmw_resource_reference(srf);
365 view->cotable = vmw_resource_reference
366 (vmw_context_cotable(ctx, vmw_view_cotables[view_type]));
367 view->view_type = view_type;
368 view->view_id = user_key;
369 view->cmd_size = cmd_size;
370 view->committed = false;
371 INIT_LIST_HEAD(&view->srf_head);
372 INIT_LIST_HEAD(&view->cotable_head);
373 memcpy(&view->cmd, cmd, cmd_size);
374 ret = vmw_resource_init(dev_priv, res, true,
375 vmw_view_res_free, &vmw_view_func);
376 if (ret)
377 goto out_resource_init;
379 ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
380 vmw_view_key(user_key, view_type),
381 res, list);
382 if (ret)
383 goto out_resource_init;
385 res->id = view->view_id;
386 res->hw_destroy = vmw_hw_view_destroy;
388 out_resource_init:
389 vmw_resource_unreference(&res);
391 return ret;
395 * vmw_view_remove - Stage a view for removal.
397 * @man: Pointer to the view manager identifying the shader namespace.
398 * @user_key: The key that is used to identify the view. The key is
399 * unique to the view type.
400 * @view_type: View type
401 * @list: Caller's list of staged command buffer resource actions.
402 * @res_p: If the resource is in an already committed state, points to the
403 * struct vmw_resource on successful return. The pointer will be
404 * non ref-counted.
406 int vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
407 u32 user_key, enum vmw_view_type view_type,
408 struct list_head *list,
409 struct vmw_resource **res_p)
411 if (!vmw_view_id_ok(user_key, view_type)) {
412 VMW_DEBUG_USER("Illegal view remove view id.\n");
413 return -EINVAL;
416 return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
417 vmw_view_key(user_key, view_type),
418 list, res_p);
422 * vmw_view_cotable_list_destroy - Evict all views belonging to a cotable.
424 * @dev_priv: Pointer to a device private struct.
425 * @list: List of views belonging to a cotable.
426 * @readback: Unused. Needed for function interface only.
428 * This function evicts all views belonging to a cotable.
429 * It must be called with the binding_mutex held, and the caller must hold
430 * a reference to the view resource. This is typically called before the
431 * cotable is paged out.
433 void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
434 struct list_head *list,
435 bool readback)
437 struct vmw_view *entry, *next;
439 lockdep_assert_held_once(&dev_priv->binding_mutex);
441 list_for_each_entry_safe(entry, next, list, cotable_head)
442 WARN_ON(vmw_view_destroy(&entry->res));
446 * vmw_view_surface_list_destroy - Evict all views pointing to a surface
448 * @dev_priv: Pointer to a device private struct.
449 * @list: List of views pointing to a surface.
451 * This function evicts all views pointing to a surface. This is typically
452 * called before the surface is evicted.
454 void vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
455 struct list_head *list)
457 struct vmw_view *entry, *next;
459 lockdep_assert_held_once(&dev_priv->binding_mutex);
461 list_for_each_entry_safe(entry, next, list, srf_head)
462 WARN_ON(vmw_view_destroy(&entry->res));
466 * vmw_view_srf - Return a non-refcounted pointer to the surface a view is
467 * pointing to.
469 * @res: pointer to a view resource.
471 * Note that the view itself is holding a reference, so as long
472 * the view resource is alive, the surface resource will be.
474 struct vmw_resource *vmw_view_srf(struct vmw_resource *res)
476 return vmw_view(res)->srf;
480 * vmw_view_lookup - Look up a view.
482 * @man: The context's cmdbuf ref manager.
483 * @view_type: The view type.
484 * @user_key: The view user id.
486 * returns a refcounted pointer to a view or an error pointer if not found.
488 struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
489 enum vmw_view_type view_type,
490 u32 user_key)
492 return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
493 vmw_view_key(user_key, view_type));
497 * vmw_view_dirtying - Return whether a view type is dirtying its resource
498 * @res: Pointer to the view
500 * Each time a resource is put on the validation list as the result of a
501 * view pointing to it, we need to determine whether that resource will
502 * be dirtied (written to by the GPU) as a result of the corresponding
503 * GPU operation. Currently only rendertarget-, depth-stencil and unordered
504 * access views are capable of dirtying its resource.
506 * Return: Whether the view type of @res dirties the resource it points to.
508 u32 vmw_view_dirtying(struct vmw_resource *res)
510 static u32 view_is_dirtying[vmw_view_max] = {
511 [vmw_view_rt] = VMW_RES_DIRTY_SET,
512 [vmw_view_ds] = VMW_RES_DIRTY_SET,
513 [vmw_view_ua] = VMW_RES_DIRTY_SET,
516 /* Update this function as we add more view types */
517 BUILD_BUG_ON(vmw_view_max != 4);
518 return view_is_dirtying[vmw_view(res)->view_type];
521 const u32 vmw_view_destroy_cmds[] = {
522 [vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
523 [vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
524 [vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
525 [vmw_view_ua] = SVGA_3D_CMD_DX_DESTROY_UA_VIEW,
528 const SVGACOTableType vmw_view_cotables[] = {
529 [vmw_view_sr] = SVGA_COTABLE_SRVIEW,
530 [vmw_view_rt] = SVGA_COTABLE_RTVIEW,
531 [vmw_view_ds] = SVGA_COTABLE_DSVIEW,
532 [vmw_view_ua] = SVGA_COTABLE_UAVIEW,
535 const SVGACOTableType vmw_so_cotables[] = {
536 [vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
537 [vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
538 [vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
539 [vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
540 [vmw_so_ss] = SVGA_COTABLE_SAMPLER,
541 [vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT
545 /* To remove unused function warning */
546 static void vmw_so_build_asserts(void) __attribute__((used));
550 * This function is unused at run-time, and only used to dump various build
551 * asserts important for code optimization assumptions.
553 static void vmw_so_build_asserts(void)
555 /* Assert that our vmw_view_cmd_to_type() function is correct. */
556 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
557 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
558 BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
559 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
560 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
561 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
562 BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
563 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
564 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
565 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);
567 /* Assert that our "one body fits all" assumption is valid */
568 BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));
570 /* Assert that the view key space can hold all view ids. */
571 BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));
574 * Assert that the offset of sid in all view define commands
575 * is what we assume it to be.
577 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
578 offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
579 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
580 offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
581 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
582 offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));