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
28
29
30
31
32
33#include <drm/ttm/ttm_placement.h>
34
35#include "vmwgfx_drv.h"
36#include "vmwgfx_resource_priv.h"
37#include "vmwgfx_so.h"
38
39
40
41
42
43
44
45
46
47
48
49
50struct vmw_cotable {
51 struct vmw_resource res;
52 struct vmw_resource *ctx;
53 size_t size_read_back;
54 int seen_entries;
55 u32 type;
56 bool scrubbed;
57 struct list_head resource_list;
58};
59
60
61
62
63
64
65
66
67struct vmw_cotable_info {
68 u32 min_initial_entries;
69 u32 size;
70 void (*unbind_func)(struct vmw_private *, struct list_head *,
71 bool);
72};
73
74static const struct vmw_cotable_info co_info[] = {
75 {1, sizeof(SVGACOTableDXRTViewEntry), &vmw_view_cotable_list_destroy},
76 {1, sizeof(SVGACOTableDXDSViewEntry), &vmw_view_cotable_list_destroy},
77 {1, sizeof(SVGACOTableDXSRViewEntry), &vmw_view_cotable_list_destroy},
78 {1, sizeof(SVGACOTableDXElementLayoutEntry), NULL},
79 {1, sizeof(SVGACOTableDXBlendStateEntry), NULL},
80 {1, sizeof(SVGACOTableDXDepthStencilEntry), NULL},
81 {1, sizeof(SVGACOTableDXRasterizerStateEntry), NULL},
82 {1, sizeof(SVGACOTableDXSamplerEntry), NULL},
83 {1, sizeof(SVGACOTableDXStreamOutputEntry), NULL},
84 {1, sizeof(SVGACOTableDXQueryEntry), NULL},
85 {1, sizeof(SVGACOTableDXShaderEntry), &vmw_dx_shader_cotable_list_scrub}
86};
87
88
89
90
91
92
93const SVGACOTableType vmw_cotable_scrub_order[] = {
94 SVGA_COTABLE_RTVIEW,
95 SVGA_COTABLE_DSVIEW,
96 SVGA_COTABLE_SRVIEW,
97 SVGA_COTABLE_DXSHADER,
98 SVGA_COTABLE_ELEMENTLAYOUT,
99 SVGA_COTABLE_BLENDSTATE,
100 SVGA_COTABLE_DEPTHSTENCIL,
101 SVGA_COTABLE_RASTERIZERSTATE,
102 SVGA_COTABLE_SAMPLER,
103 SVGA_COTABLE_STREAMOUTPUT,
104 SVGA_COTABLE_DXQUERY,
105};
106
107static int vmw_cotable_bind(struct vmw_resource *res,
108 struct ttm_validate_buffer *val_buf);
109static int vmw_cotable_unbind(struct vmw_resource *res,
110 bool readback,
111 struct ttm_validate_buffer *val_buf);
112static int vmw_cotable_create(struct vmw_resource *res);
113static int vmw_cotable_destroy(struct vmw_resource *res);
114
115static const struct vmw_res_func vmw_cotable_func = {
116 .res_type = vmw_res_cotable,
117 .needs_backup = true,
118 .may_evict = true,
119 .type_name = "context guest backed object tables",
120 .backup_placement = &vmw_mob_placement,
121 .create = vmw_cotable_create,
122 .destroy = vmw_cotable_destroy,
123 .bind = vmw_cotable_bind,
124 .unbind = vmw_cotable_unbind,
125};
126
127
128
129
130
131
132
133static struct vmw_cotable *vmw_cotable(struct vmw_resource *res)
134{
135 return container_of(res, struct vmw_cotable, res);
136}
137
138
139
140
141
142
143
144
145
146static int vmw_cotable_destroy(struct vmw_resource *res)
147{
148 res->id = -1;
149 return 0;
150}
151
152
153
154
155
156
157
158
159
160
161static int vmw_cotable_unscrub(struct vmw_resource *res)
162{
163 struct vmw_cotable *vcotbl = vmw_cotable(res);
164 struct vmw_private *dev_priv = res->dev_priv;
165 struct ttm_buffer_object *bo = &res->backup->base;
166 struct {
167 SVGA3dCmdHeader header;
168 SVGA3dCmdDXSetCOTable body;
169 } *cmd;
170
171 WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
172 lockdep_assert_held(&bo->resv->lock.base);
173
174 cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), SVGA3D_INVALID_ID);
175 if (!cmd) {
176 DRM_ERROR("Failed reserving FIFO space for cotable "
177 "binding.\n");
178 return -ENOMEM;
179 }
180
181 WARN_ON(vcotbl->ctx->id == SVGA3D_INVALID_ID);
182 WARN_ON(bo->mem.mem_type != VMW_PL_MOB);
183 cmd->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
184 cmd->header.size = sizeof(cmd->body);
185 cmd->body.cid = vcotbl->ctx->id;
186 cmd->body.type = vcotbl->type;
187 cmd->body.mobid = bo->mem.start;
188 cmd->body.validSizeInBytes = vcotbl->size_read_back;
189
190 vmw_fifo_commit_flush(dev_priv, sizeof(*cmd));
191 vcotbl->scrubbed = false;
192
193 return 0;
194}
195
196
197
198
199
200
201
202
203
204
205
206static int vmw_cotable_bind(struct vmw_resource *res,
207 struct ttm_validate_buffer *val_buf)
208{
209
210
211
212
213
214
215
216 val_buf->bo = &res->backup->base;
217
218 return vmw_cotable_unscrub(res);
219}
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239int vmw_cotable_scrub(struct vmw_resource *res, bool readback)
240{
241 struct vmw_cotable *vcotbl = vmw_cotable(res);
242 struct vmw_private *dev_priv = res->dev_priv;
243 size_t submit_size;
244
245 struct {
246 SVGA3dCmdHeader header;
247 SVGA3dCmdDXReadbackCOTable body;
248 } *cmd0;
249 struct {
250 SVGA3dCmdHeader header;
251 SVGA3dCmdDXSetCOTable body;
252 } *cmd1;
253
254 if (vcotbl->scrubbed)
255 return 0;
256
257 if (co_info[vcotbl->type].unbind_func)
258 co_info[vcotbl->type].unbind_func(dev_priv,
259 &vcotbl->resource_list,
260 readback);
261 submit_size = sizeof(*cmd1);
262 if (readback)
263 submit_size += sizeof(*cmd0);
264
265 cmd1 = vmw_fifo_reserve_dx(dev_priv, submit_size, SVGA3D_INVALID_ID);
266 if (!cmd1) {
267 DRM_ERROR("Failed reserving FIFO space for cotable "
268 "unbinding.\n");
269 return -ENOMEM;
270 }
271
272 vcotbl->size_read_back = 0;
273 if (readback) {
274 cmd0 = (void *) cmd1;
275 cmd0->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
276 cmd0->header.size = sizeof(cmd0->body);
277 cmd0->body.cid = vcotbl->ctx->id;
278 cmd0->body.type = vcotbl->type;
279 cmd1 = (void *) &cmd0[1];
280 vcotbl->size_read_back = res->backup_size;
281 }
282 cmd1->header.id = SVGA_3D_CMD_DX_SET_COTABLE;
283 cmd1->header.size = sizeof(cmd1->body);
284 cmd1->body.cid = vcotbl->ctx->id;
285 cmd1->body.type = vcotbl->type;
286 cmd1->body.mobid = SVGA3D_INVALID_ID;
287 cmd1->body.validSizeInBytes = 0;
288 vmw_fifo_commit_flush(dev_priv, submit_size);
289 vcotbl->scrubbed = true;
290
291
292 res->id = -1;
293
294 return 0;
295}
296
297
298
299
300
301
302
303
304
305
306
307static int vmw_cotable_unbind(struct vmw_resource *res,
308 bool readback,
309 struct ttm_validate_buffer *val_buf)
310{
311 struct vmw_cotable *vcotbl = vmw_cotable(res);
312 struct vmw_private *dev_priv = res->dev_priv;
313 struct ttm_buffer_object *bo = val_buf->bo;
314 struct vmw_fence_obj *fence;
315
316 if (list_empty(&res->mob_head))
317 return 0;
318
319 WARN_ON_ONCE(bo->mem.mem_type != VMW_PL_MOB);
320 lockdep_assert_held(&bo->resv->lock.base);
321
322 mutex_lock(&dev_priv->binding_mutex);
323 if (!vcotbl->scrubbed)
324 vmw_dx_context_scrub_cotables(vcotbl->ctx, readback);
325 mutex_unlock(&dev_priv->binding_mutex);
326 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
327 vmw_bo_fence_single(bo, fence);
328 if (likely(fence != NULL))
329 vmw_fence_obj_unreference(&fence);
330
331 return 0;
332}
333
334
335
336
337
338
339
340
341
342static int vmw_cotable_readback(struct vmw_resource *res)
343{
344 struct vmw_cotable *vcotbl = vmw_cotable(res);
345 struct vmw_private *dev_priv = res->dev_priv;
346
347 struct {
348 SVGA3dCmdHeader header;
349 SVGA3dCmdDXReadbackCOTable body;
350 } *cmd;
351 struct vmw_fence_obj *fence;
352
353 if (!vcotbl->scrubbed) {
354 cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd),
355 SVGA3D_INVALID_ID);
356 if (!cmd) {
357 DRM_ERROR("Failed reserving FIFO space for cotable "
358 "readback.\n");
359 return -ENOMEM;
360 }
361 cmd->header.id = SVGA_3D_CMD_DX_READBACK_COTABLE;
362 cmd->header.size = sizeof(cmd->body);
363 cmd->body.cid = vcotbl->ctx->id;
364 cmd->body.type = vcotbl->type;
365 vcotbl->size_read_back = res->backup_size;
366 vmw_fifo_commit(dev_priv, sizeof(*cmd));
367 }
368
369 (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
370 vmw_bo_fence_single(&res->backup->base, fence);
371 vmw_fence_obj_unreference(&fence);
372
373 return 0;
374}
375
376
377
378
379
380
381
382
383
384
385
386
387
388static int vmw_cotable_resize(struct vmw_resource *res, size_t new_size)
389{
390 struct ttm_operation_ctx ctx = { false, false };
391 struct vmw_private *dev_priv = res->dev_priv;
392 struct vmw_cotable *vcotbl = vmw_cotable(res);
393 struct vmw_buffer_object *buf, *old_buf = res->backup;
394 struct ttm_buffer_object *bo, *old_bo = &res->backup->base;
395 size_t old_size = res->backup_size;
396 size_t old_size_read_back = vcotbl->size_read_back;
397 size_t cur_size_read_back;
398 struct ttm_bo_kmap_obj old_map, new_map;
399 int ret;
400 size_t i;
401
402 ret = vmw_cotable_readback(res);
403 if (ret)
404 return ret;
405
406 cur_size_read_back = vcotbl->size_read_back;
407 vcotbl->size_read_back = old_size_read_back;
408
409
410
411
412
413
414 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
415 if (!buf)
416 return -ENOMEM;
417
418 ret = vmw_bo_init(dev_priv, buf, new_size, &vmw_mob_ne_placement,
419 true, vmw_bo_bo_free);
420 if (ret) {
421 DRM_ERROR("Failed initializing new cotable MOB.\n");
422 return ret;
423 }
424
425 bo = &buf->base;
426 WARN_ON_ONCE(ttm_bo_reserve(bo, false, true, NULL));
427
428 ret = ttm_bo_wait(old_bo, false, false);
429 if (unlikely(ret != 0)) {
430 DRM_ERROR("Failed waiting for cotable unbind.\n");
431 goto out_wait;
432 }
433
434
435
436
437
438 for (i = 0; i < old_bo->num_pages; ++i) {
439 bool dummy;
440
441 ret = ttm_bo_kmap(old_bo, i, 1, &old_map);
442 if (unlikely(ret != 0)) {
443 DRM_ERROR("Failed mapping old COTable on resize.\n");
444 goto out_wait;
445 }
446 ret = ttm_bo_kmap(bo, i, 1, &new_map);
447 if (unlikely(ret != 0)) {
448 DRM_ERROR("Failed mapping new COTable on resize.\n");
449 goto out_map_new;
450 }
451 memcpy(ttm_kmap_obj_virtual(&new_map, &dummy),
452 ttm_kmap_obj_virtual(&old_map, &dummy),
453 PAGE_SIZE);
454 ttm_bo_kunmap(&new_map);
455 ttm_bo_kunmap(&old_map);
456 }
457
458
459 ret = ttm_bo_validate(bo, &vmw_mob_placement, &ctx);
460 if (unlikely(ret != 0)) {
461 DRM_ERROR("Failed validating new COTable backup buffer.\n");
462 goto out_wait;
463 }
464
465 res->backup = buf;
466 res->backup_size = new_size;
467 vcotbl->size_read_back = cur_size_read_back;
468
469
470
471
472
473 ret = vmw_cotable_unscrub(res);
474 if (ret) {
475 DRM_ERROR("Failed switching COTable backup buffer.\n");
476 res->backup = old_buf;
477 res->backup_size = old_size;
478 vcotbl->size_read_back = old_size_read_back;
479 goto out_wait;
480 }
481
482
483 list_del(&res->mob_head);
484 list_add_tail(&res->mob_head, &buf->res_list);
485 vmw_bo_unreference(&old_buf);
486 res->id = vcotbl->type;
487
488 return 0;
489
490out_map_new:
491 ttm_bo_kunmap(&old_map);
492out_wait:
493 ttm_bo_unreserve(bo);
494 vmw_bo_unreference(&buf);
495
496 return ret;
497}
498
499
500
501
502
503
504
505
506
507
508
509
510
511static int vmw_cotable_create(struct vmw_resource *res)
512{
513 struct vmw_cotable *vcotbl = vmw_cotable(res);
514 size_t new_size = res->backup_size;
515 size_t needed_size;
516 int ret;
517
518
519 needed_size = (vcotbl->seen_entries + 1) * co_info[vcotbl->type].size;
520 while (needed_size > new_size)
521 new_size *= 2;
522
523 if (likely(new_size <= res->backup_size)) {
524 if (vcotbl->scrubbed && !list_empty(&res->mob_head)) {
525 ret = vmw_cotable_unscrub(res);
526 if (ret)
527 return ret;
528 }
529 res->id = vcotbl->type;
530 return 0;
531 }
532
533 return vmw_cotable_resize(res, new_size);
534}
535
536
537
538
539
540
541
542
543static void vmw_hw_cotable_destroy(struct vmw_resource *res)
544{
545 (void) vmw_cotable_destroy(res);
546}
547
548static size_t cotable_acc_size;
549
550
551
552
553
554
555static void vmw_cotable_free(struct vmw_resource *res)
556{
557 struct vmw_private *dev_priv = res->dev_priv;
558
559 kfree(res);
560 ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
561}
562
563
564
565
566
567
568
569
570
571struct vmw_resource *vmw_cotable_alloc(struct vmw_private *dev_priv,
572 struct vmw_resource *ctx,
573 u32 type)
574{
575 struct vmw_cotable *vcotbl;
576 struct ttm_operation_ctx ttm_opt_ctx = {
577 .interruptible = true,
578 .no_wait_gpu = false
579 };
580 int ret;
581 u32 num_entries;
582
583 if (unlikely(cotable_acc_size == 0))
584 cotable_acc_size = ttm_round_pot(sizeof(struct vmw_cotable));
585
586 ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
587 cotable_acc_size, &ttm_opt_ctx);
588 if (unlikely(ret))
589 return ERR_PTR(ret);
590
591 vcotbl = kzalloc(sizeof(*vcotbl), GFP_KERNEL);
592 if (unlikely(!vcotbl)) {
593 ret = -ENOMEM;
594 goto out_no_alloc;
595 }
596
597 ret = vmw_resource_init(dev_priv, &vcotbl->res, true,
598 vmw_cotable_free, &vmw_cotable_func);
599 if (unlikely(ret != 0))
600 goto out_no_init;
601
602 INIT_LIST_HEAD(&vcotbl->resource_list);
603 vcotbl->res.id = type;
604 vcotbl->res.backup_size = PAGE_SIZE;
605 num_entries = PAGE_SIZE / co_info[type].size;
606 if (num_entries < co_info[type].min_initial_entries) {
607 vcotbl->res.backup_size = co_info[type].min_initial_entries *
608 co_info[type].size;
609 vcotbl->res.backup_size =
610 (vcotbl->res.backup_size + PAGE_SIZE - 1) & PAGE_MASK;
611 }
612
613 vcotbl->scrubbed = true;
614 vcotbl->seen_entries = -1;
615 vcotbl->type = type;
616 vcotbl->ctx = ctx;
617
618 vcotbl->res.hw_destroy = vmw_hw_cotable_destroy;
619
620 return &vcotbl->res;
621
622out_no_init:
623 kfree(vcotbl);
624out_no_alloc:
625 ttm_mem_global_free(vmw_mem_glob(dev_priv), cotable_acc_size);
626 return ERR_PTR(ret);
627}
628
629
630
631
632
633
634
635int vmw_cotable_notify(struct vmw_resource *res, int id)
636{
637 struct vmw_cotable *vcotbl = vmw_cotable(res);
638
639 if (id < 0 || id >= SVGA_COTABLE_MAX_IDS) {
640 DRM_ERROR("Illegal COTable id. Type is %u. Id is %d\n",
641 (unsigned) vcotbl->type, id);
642 return -EINVAL;
643 }
644
645 if (vcotbl->seen_entries < id) {
646
647 res->id = -1;
648 vcotbl->seen_entries = id;
649 }
650
651 return 0;
652}
653
654
655
656
657
658
659
660
661void vmw_cotable_add_resource(struct vmw_resource *res, struct list_head *head)
662{
663 struct vmw_cotable *vcotbl =
664 container_of(res, struct vmw_cotable, res);
665
666 list_add_tail(head, &vcotbl->resource_list);
667}
668