1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60struct vmw_view {
61 struct rcu_head rcu;
62 struct vmw_resource res;
63 struct vmw_resource *ctx;
64 struct vmw_resource *srf;
65 struct vmw_resource *cotable;
66 struct list_head srf_head;
67 struct list_head cotable_head;
68 unsigned view_type;
69 unsigned view_id;
70 u32 cmd_size;
71 bool committed;
72 u32 cmd[1];
73};
74
75static int vmw_view_create(struct vmw_resource *res);
76static int vmw_view_destroy(struct vmw_resource *res);
77static void vmw_hw_view_destroy(struct vmw_resource *res);
78static void vmw_view_commit_notify(struct vmw_resource *res,
79 enum vmw_cmdbuf_res_state state);
80
81static 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
93
94
95
96
97
98
99
100struct vmw_view_define {
101 uint32 view_id;
102 uint32 sid;
103};
104
105
106
107
108
109
110
111
112static struct vmw_view *vmw_view(struct vmw_resource *res)
113{
114 return container_of(res, struct vmw_view, res);
115}
116
117
118
119
120
121
122
123
124
125static 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
151
152
153
154
155
156
157static 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
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
196
197
198
199
200
201
202static 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 WARN_ON_ONCE(!mutex_is_locked(&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
237
238
239
240
241
242static 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
254
255
256
257
258
259
260static 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
267
268
269
270
271
272
273
274static 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
282
283
284
285
286
287static 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314int 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_context_cotable(ctx, vmw_view_cotables[view_type]);
370 view->view_type = view_type;
371 view->view_id = user_key;
372 view->cmd_size = cmd_size;
373 view->committed = false;
374 INIT_LIST_HEAD(&view->srf_head);
375 INIT_LIST_HEAD(&view->cotable_head);
376 memcpy(&view->cmd, cmd, cmd_size);
377 ret = vmw_resource_init(dev_priv, res, true,
378 vmw_view_res_free, &vmw_view_func);
379 if (ret)
380 goto out_resource_init;
381
382 ret = vmw_cmdbuf_res_add(man, vmw_cmdbuf_res_view,
383 vmw_view_key(user_key, view_type),
384 res, list);
385 if (ret)
386 goto out_resource_init;
387
388 res->id = view->view_id;
389 vmw_resource_activate(res, vmw_hw_view_destroy);
390
391out_resource_init:
392 vmw_resource_unreference(&res);
393
394 return ret;
395}
396
397
398
399
400
401
402
403
404
405
406
407
408
409int vmw_view_remove(struct vmw_cmdbuf_res_manager *man,
410 u32 user_key, enum vmw_view_type view_type,
411 struct list_head *list,
412 struct vmw_resource **res_p)
413{
414 if (!vmw_view_id_ok(user_key, view_type)) {
415 DRM_ERROR("Illegal view remove view id.\n");
416 return -EINVAL;
417 }
418
419 return vmw_cmdbuf_res_remove(man, vmw_cmdbuf_res_view,
420 vmw_view_key(user_key, view_type),
421 list, res_p);
422}
423
424
425
426
427
428
429
430
431
432
433
434
435
436void vmw_view_cotable_list_destroy(struct vmw_private *dev_priv,
437 struct list_head *list,
438 bool readback)
439{
440 struct vmw_view *entry, *next;
441
442 WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
443
444 list_for_each_entry_safe(entry, next, list, cotable_head)
445 WARN_ON(vmw_view_destroy(&entry->res));
446}
447
448
449
450
451
452
453
454
455
456
457void vmw_view_surface_list_destroy(struct vmw_private *dev_priv,
458 struct list_head *list)
459{
460 struct vmw_view *entry, *next;
461
462 WARN_ON_ONCE(!mutex_is_locked(&dev_priv->binding_mutex));
463
464 list_for_each_entry_safe(entry, next, list, srf_head)
465 WARN_ON(vmw_view_destroy(&entry->res));
466}
467
468
469
470
471
472
473
474
475
476
477struct vmw_resource *vmw_view_srf(struct vmw_resource *res)
478{
479 return vmw_view(res)->srf;
480}
481
482
483
484
485
486
487
488
489
490
491struct vmw_resource *vmw_view_lookup(struct vmw_cmdbuf_res_manager *man,
492 enum vmw_view_type view_type,
493 u32 user_key)
494{
495 return vmw_cmdbuf_res_lookup(man, vmw_cmdbuf_res_view,
496 vmw_view_key(user_key, view_type));
497}
498
499const u32 vmw_view_destroy_cmds[] = {
500 [vmw_view_sr] = SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW,
501 [vmw_view_rt] = SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW,
502 [vmw_view_ds] = SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW,
503};
504
505const SVGACOTableType vmw_view_cotables[] = {
506 [vmw_view_sr] = SVGA_COTABLE_SRVIEW,
507 [vmw_view_rt] = SVGA_COTABLE_RTVIEW,
508 [vmw_view_ds] = SVGA_COTABLE_DSVIEW,
509};
510
511const SVGACOTableType vmw_so_cotables[] = {
512 [vmw_so_el] = SVGA_COTABLE_ELEMENTLAYOUT,
513 [vmw_so_bs] = SVGA_COTABLE_BLENDSTATE,
514 [vmw_so_ds] = SVGA_COTABLE_DEPTHSTENCIL,
515 [vmw_so_rs] = SVGA_COTABLE_RASTERIZERSTATE,
516 [vmw_so_ss] = SVGA_COTABLE_SAMPLER,
517 [vmw_so_so] = SVGA_COTABLE_STREAMOUTPUT
518};
519
520
521
522static void vmw_so_build_asserts(void) __attribute__((used));
523
524
525
526
527
528
529static void vmw_so_build_asserts(void)
530{
531
532 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_SHADERRESOURCE_VIEW !=
533 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 1);
534 BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_RENDERTARGET_VIEW !=
535 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 2);
536 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_RENDERTARGET_VIEW !=
537 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 3);
538 BUILD_BUG_ON(SVGA_3D_CMD_DX_DEFINE_DEPTHSTENCIL_VIEW !=
539 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 4);
540 BUILD_BUG_ON(SVGA_3D_CMD_DX_DESTROY_DEPTHSTENCIL_VIEW !=
541 SVGA_3D_CMD_DX_DEFINE_SHADERRESOURCE_VIEW + 5);
542
543
544 BUILD_BUG_ON(sizeof(union vmw_view_destroy) != sizeof(u32));
545
546
547 BUILD_BUG_ON(SVGA_COTABLE_MAX_IDS >= ((1 << 20) - 1));
548
549
550
551
552
553 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
554 offsetof(SVGA3dCmdDXDefineShaderResourceView, sid));
555 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
556 offsetof(SVGA3dCmdDXDefineRenderTargetView, sid));
557 BUILD_BUG_ON(offsetof(struct vmw_view_define, sid) !=
558 offsetof(SVGA3dCmdDXDefineDepthStencilView, sid));
559}
560