vmwgfx_so.c revision 1.1 1 /* $NetBSD: vmwgfx_so.c,v 1.1 2018/08/27 01:35:00 riastradh Exp $ */
2
3 /**************************************************************************
4 * Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: vmwgfx_so.c,v 1.1 2018/08/27 01:35:00 riastradh Exp $");
31
32 #include "vmwgfx_drv.h"
33 #include "vmwgfx_resource_priv.h"
34 #include "vmwgfx_so.h"
35 #include "vmwgfx_binding.h"
36
37 /*
38 * The currently only reason we need to keep track of views is that if we
39 * destroy a hardware surface, all views pointing to it must also be destroyed,
40 * otherwise the device will error.
41 * So in particuar if a surface is evicted, we must destroy all views pointing
42 * to it, and all context bindings of that view. Similarly we must restore
43 * the view bindings, views and surfaces pointed to by the views when a
44 * context is referenced in the command stream.
45 */
46
47 /**
48 * struct vmw_view - view metadata
49 *
50 * @res: The struct vmw_resource we derive from
51 * @ctx: Non-refcounted pointer to the context this view belongs to.
52 * @srf: Refcounted pointer to the surface pointed to by this view.
53 * @cotable: Refcounted pointer to the cotable holding this view.
54 * @srf_head: List head for the surface-to-view list.
55 * @cotable_head: List head for the cotable-to_view list.
56 * @view_type: View type.
57 * @view_id: User-space per context view id. Currently used also as per
58 * context device view id.
59 * @cmd_size: Size of the SVGA3D define view command that we've copied from the
60 * command stream.
61 * @committed: Whether the view is actually created or pending creation at the
62 * device level.
63 * @cmd: The SVGA3D define view command copied from the command stream.
64 */
65 struct vmw_view {
66 struct rcu_head rcu;
67 struct vmw_resource res;
68 struct vmw_resource *ctx; /* Immutable */
69 struct vmw_resource *srf; /* Immutable */
70 struct vmw_resource *cotable; /* Immutable */
71 struct list_head srf_head; /* Protected by binding_mutex */
72 struct list_head cotable_head; /* Protected by binding_mutex */
73 unsigned view_type; /* Immutable */
74 unsigned view_id; /* Immutable */
75 u32 cmd_size; /* Immutable */
76 bool committed; /* Protected by binding_mutex */
77 u32 cmd[1]; /* Immutable */
78 };
79
80 static int vmw_view_create(struct vmw_resource *res);
81 static int vmw_view_destroy(struct vmw_resource *res);
82 static void vmw_hw_view_destroy(struct vmw_resource *res);
83 static void vmw_view_commit_notify(struct vmw_resource *res,
84 enum vmw_cmdbuf_res_state state);
85
86 static const struct vmw_res_func vmw_view_func = {
87 .res_type = vmw_res_view,
88 .needs_backup = false,
89 .may_evict = false,
90 .type_name = "DX view",
91 .backup_placement = NULL,
92 .create = vmw_view_create,
93 .commit_notify = vmw_view_commit_notify,
94 };
95
96 /**
97 * struct vmw_view - view define command body stub
98 *
99 * @view_id: The device id of the view being defined
100 * @sid: The surface id of the view being defined
101 *
102 * This generic struct is used by the code to change @view_id and @sid of a
103 * saved view define command.
104 */
105 struct vmw_view_define {
106 uint32 view_id;
107 uint32 sid;
108 };
109
110 /**
111 * vmw_view - Convert a struct vmw_resource to a struct vmw_view
112 *
113 * @res: Pointer to the resource to convert.
114 *
115 * Returns a pointer to a struct vmw_view.
116 */
117 static struct vmw_view *vmw_view(struct vmw_resource *res)
118 {
119 return container_of(res, struct vmw_view, res);
120 }
121
122 /**
123 * vmw_view_commit_notify - Notify that a view operation has been committed to
124 * hardware from a user-supplied command stream.
125 *
126 * @res: Pointer to the view resource.
127 * @state: Indicating whether a creation or removal has been committed.
128 *
129 */
130 static void vmw_view_commit_notify(struct vmw_resource *res,
131 enum vmw_cmdbuf_res_state state)
132 {
133 struct vmw_view *view = vmw_view(res);
134 struct vmw_private *dev_priv = res->dev_priv;
135
136 mutex_lock(&dev_priv->binding_mutex);
137 if (state == VMW_CMDBUF_RES_ADD) {
138 struct vmw_surface *srf = vmw_res_to_srf(view->srf);
139
140 list_add_tail(&view->srf_head, &srf->view_list);
141 vmw_cotable_add_resource(view->cotable, &view->cotable_head);
142 view->committed = true;
143 res->id = view->view_id;
144
145 } else {
146 list_del_init(&view->cotable_head);
147 list_del_init(&view->srf_head);
148 view->committed = false;
149 res->id = -1;
150 }
151 mutex_unlock(&dev_priv->binding_mutex);
152 }
153
154 /**
155 * vmw_view_create - Create a hardware view.
156 *
157 * @res: Pointer to the view resource.
158 *
159 * Create a hardware view. Typically used if that view has previously been
160 * destroyed by an eviction operation.
161 */
162 static int vmw_view_create(struct vmw_resource *res)
163 {
164 struct vmw_view *view = vmw_view(res);
165 struct vmw_surface *srf = vmw_res_to_srf(view->srf);
166 struct vmw_private *dev_priv = res->dev_priv;
167 struct {
168 SVGA3dCmdHeader header;
169 struct vmw_view_define body;
170 } *cmd;
171
172 mutex_lock(&dev_priv->binding_mutex);
173 if (!view->committed) {
174 mutex_unlock(&dev_priv->binding_mutex);
175 return 0;
176 }
177
178 cmd = vmw_fifo_reserve_dx(res->dev_priv, view->cmd_size,
179 view->ctx->id);
180 if (!cmd) {
181 DRM_ERROR("Failed reserving FIFO space for view creation.\n");
182 mutex_unlock(&dev_priv->binding_mutex);
183 return -ENOMEM;
184 }
185 memcpy(cmd, &view->cmd, view->cmd_size);
186 WARN_ON(cmd->body.view_id != view->view_id);
187 /* Sid may have changed due to surface eviction. */
188 WARN_ON(view->srf->id == SVGA3D_INVALID_ID);
189 cmd->body.sid = view->srf->id;
190 vmw_fifo_commit(res->dev_priv, view->cmd_size);
191 res->id = view->view_id;
192 list_add_tail(&view->srf_head, &srf->view_list);
193 vmw_cotable_add_resource(view->cotable, &view->cotable_head);
194 mutex_unlock(&dev_priv->binding_mutex);
195
196 return 0;
197 }
198
199 /**
200 * vmw_view_destroy - Destroy a hardware view.
201 *
202 * @res: Pointer to the view resource.
203 *
204 * Destroy a hardware view. Typically used on unexpected termination of the
205 * owning process or if the surface the view is pointing to is destroyed.
206 */
207 static int vmw_view_destroy(struct vmw_resource *res)
208 {
209 struct vmw_private *dev_priv = res->dev_priv;
210 struct vmw_view *view = vmw_view(res);
211 struct {
212 SVGA3dCmdHeader header;
213 union vmw_view_destroy body;
214 } *cmd;
215
216 WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
217 vmw_binding_res_list_scrub(&res->binding_head);
218
219 if (!view->committed || res->id == -1)
220 return 0;
221
222 cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), view->ctx->id);
223 if (!cmd) {
224 DRM_ERROR("Failed reserving FIFO space for view "
225 "destruction.\n");
226 return -ENOMEM;
227 }
228
229 cmd->header.id = vmw_view_destroy_cmds[view->view_type];
230 cmd->header.size = sizeof(cmd->body);
231 cmd->body.view_id = view->view_id;
232 vmw_fifo_commit(dev_priv, sizeof(*cmd));
233 res->id = -1;
234 list_del_init(&view->cotable_head);
235 list_del_init(&view->srf_head);
236
237 return 0;
238 }
239
240 /**
241 * vmw_hw_view_destroy - Destroy a hardware view as part of resource cleanup.
242 *
243 * @res: Pointer to the view resource.
244 *
245 * Destroy a hardware view if it's still present.
246 */
247 static void vmw_hw_view_destroy(struct vmw_resource *res)
248 {
249 struct vmw_private *dev_priv = res->dev_priv;
250
251 mutex_lock(&dev_priv->binding_mutex);
252 WARN_ON(vmw_view_destroy(res));
253 res->id = -1;
254 mutex_unlock(&dev_priv->binding_mutex);
255 }
256
257 /**
258 * vmw_view_key - Compute a view key suitable for the cmdbuf resource manager
259 *
260 * @user_key: The user-space id used for the view.
261 * @view_type: The view type.
262 *
263 * Destroy a hardware view if it's still present.
264 */
265 static u32 vmw_view_key(u32 user_key, enum vmw_view_type view_type)
266 {
267 return user_key | (view_type << 20);
268 }
269
270 /**
271 * vmw_view_id_ok - Basic view id and type range checks.
272 *
273 * @user_key: The user-space id used for the view.
274 * @view_type: The view type.
275 *
276 * Checks that the view id and type (typically provided by user-space) is
277 * valid.
278 */
279 static bool vmw_view_id_ok(u32 user_key, enum vmw_view_type view_type)
280 {
281 return (user_key < SVGA_COTABLE_MAX_IDS &&
282 view_type < vmw_view_max);
283 }
284
285 /**
286 * vmw_view_res_free - resource res_free callback for view resources
287 *
288 * @res: Pointer to a struct vmw_resource
289 *
290 * Frees memory and memory accounting held by a struct vmw_view.
291 */
292 static void vmw_view_res_free(struct vmw_resource *res)
293 {
294 struct vmw_view *view = vmw_view(res);
295 size_t size = offsetof(struct vmw_view, cmd) + view->cmd_size;
296 struct vmw_private *dev_priv = res->dev_priv;
297
298 vmw_resource_unreference(&view->cotable);
299 vmw_resource_unreference(&view->srf);
300 kfree_rcu(view, rcu);
301 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
302 }
303
304 /**
305 * vmw_view_add - Create a view resource and stage it for addition
306 * as a command buffer managed resource.
307 *
308 * @man: Pointer to the compat shader manager identifying the shader namespace.
309 * @ctx: Pointer to a struct vmw_resource identifying the active context.
310 * @srf: Pointer to a struct vmw_resource identifying the surface the view
311 * points to.
312 * @view_type: The view type deduced from the view create command.
313 * @user_key: The key that is used to identify the shader. The key is
314 * unique to the view type and to the context.
315 * @cmd: Pointer to the view create command in the command stream.
316 * @cmd_size: Size of the view create command in the command stream.
317 * @list: Caller's list of staged command buffer resource actions.
318 */
319 int vmw_view_add(struct vmw_cmdbuf_res_manager *man,
320 struct vmw_resource *ctx,
321 struct vmw_resource *srf,
322 enum vmw_view_type view_type,
323 u32 user_key,
324 const void *cmd,
325 size_t cmd_size,
326 struct list_head *list)
327 {
328 static const size_t vmw_view_define_sizes[] = {
329 [vmw_view_sr] = sizeof(SVGA3dCmdDXDefineShaderResourceView),
330 [vmw_view_rt] = sizeof(SVGA3dCmdDXDefineRenderTargetView),
331 [vmw_view_ds] = sizeof(SVGA3dCmdDXDefineDepthStencilView)
332 };
333
334 struct vmw_private *dev_priv = ctx->dev_priv;
335 struct vmw_resource *res;
336 struct vmw_view *view;
337 size_t size;
338 int ret;
339
340 if (cmd_size != vmw_view_define_sizes[view_type] +
341 sizeof(SVGA3dCmdHeader)) {
342 DRM_ERROR("Illegal view create command size.\n");
343 return -EINVAL;
344 }
345
346 if (!vmw_view_id_ok(user_key, view_type)) {
347 DRM_ERROR("Illegal view add view id.\n");
348 return -EINVAL;
349 }
350
351 size = offsetof(struct vmw_view, cmd) + cmd_size;
352
353 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), size, false, true);
354 if (ret) {
355 if (ret != -ERESTARTSYS)
356 DRM_ERROR("Out of graphics memory for view"
357 " creation.\n");
358 return ret;
359 }
360
361 view = kmalloc(size, GFP_KERNEL);
362 if (!view) {
363 ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
364 return -ENOMEM;
365 }
366
367 res = &view->res;
368 view->ctx = ctx;
369 view->srf = vmw_resource_reference(srf);
370 view->cotable = 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 vmw_resource_activate(res, 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 WARN_ON_ONCE(!mutex_is_locked(&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 WARN_ON_ONCE(!mutex_is_locked(&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