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#include "vmwgfx_drv.h"
29
30
31
32
33
34#define VMW_OTABLE_SETUP_SUB ((VMWGFX_ENABLE_SCREEN_TARGET_OTABLE ? 0 : 1))
35
36#ifdef CONFIG_64BIT
37#define VMW_PPN_SIZE 8
38#define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH64_0
39#define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH64_1
40#define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH64_2
41#else
42#define VMW_PPN_SIZE 4
43#define VMW_MOBFMT_PTDEPTH_0 SVGA3D_MOBFMT_PTDEPTH_0
44#define VMW_MOBFMT_PTDEPTH_1 SVGA3D_MOBFMT_PTDEPTH_1
45#define VMW_MOBFMT_PTDEPTH_2 SVGA3D_MOBFMT_PTDEPTH_2
46#endif
47
48
49
50
51
52
53
54
55
56struct vmw_mob {
57 struct ttm_buffer_object *pt_bo;
58 unsigned long num_pages;
59 unsigned pt_level;
60 dma_addr_t pt_root_page;
61 uint32_t id;
62};
63
64
65
66
67
68
69
70static const struct vmw_otable pre_dx_tables[] = {
71 {VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE, NULL, true},
72 {VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, NULL, true},
73 {VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE, NULL, true},
74 {VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE, NULL, true},
75 {VMWGFX_NUM_GB_SCREEN_TARGET * SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE,
76 NULL, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE}
77};
78
79static const struct vmw_otable dx_tables[] = {
80 {VMWGFX_NUM_MOB * SVGA3D_OTABLE_MOB_ENTRY_SIZE, NULL, true},
81 {VMWGFX_NUM_GB_SURFACE * SVGA3D_OTABLE_SURFACE_ENTRY_SIZE, NULL, true},
82 {VMWGFX_NUM_GB_CONTEXT * SVGA3D_OTABLE_CONTEXT_ENTRY_SIZE, NULL, true},
83 {VMWGFX_NUM_GB_SHADER * SVGA3D_OTABLE_SHADER_ENTRY_SIZE, NULL, true},
84 {VMWGFX_NUM_GB_SCREEN_TARGET * SVGA3D_OTABLE_SCREEN_TARGET_ENTRY_SIZE,
85 NULL, VMWGFX_ENABLE_SCREEN_TARGET_OTABLE},
86 {VMWGFX_NUM_DXCONTEXT * sizeof(SVGAOTableDXContextEntry), NULL, true},
87};
88
89static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
90 struct vmw_mob *mob);
91static void vmw_mob_pt_setup(struct vmw_mob *mob,
92 struct vmw_piter data_iter,
93 unsigned long num_data_pages);
94
95
96
97
98
99
100
101
102
103
104
105
106
107static int vmw_setup_otable_base(struct vmw_private *dev_priv,
108 SVGAOTableType type,
109 struct ttm_buffer_object *otable_bo,
110 unsigned long offset,
111 struct vmw_otable *otable)
112{
113 struct {
114 SVGA3dCmdHeader header;
115 SVGA3dCmdSetOTableBase64 body;
116 } *cmd;
117 struct vmw_mob *mob;
118 const struct vmw_sg_table *vsgt;
119 struct vmw_piter iter;
120 int ret;
121
122 BUG_ON(otable->page_table != NULL);
123
124 vsgt = vmw_bo_sg_table(otable_bo);
125 vmw_piter_start(&iter, vsgt, offset >> PAGE_SHIFT);
126 WARN_ON(!vmw_piter_next(&iter));
127
128 mob = vmw_mob_create(otable->size >> PAGE_SHIFT);
129 if (unlikely(mob == NULL)) {
130 DRM_ERROR("Failed creating OTable page table.\n");
131 return -ENOMEM;
132 }
133
134 if (otable->size <= PAGE_SIZE) {
135 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
136 mob->pt_root_page = vmw_piter_dma_addr(&iter);
137 } else if (vsgt->num_regions == 1) {
138 mob->pt_level = SVGA3D_MOBFMT_RANGE;
139 mob->pt_root_page = vmw_piter_dma_addr(&iter);
140 } else {
141 ret = vmw_mob_pt_populate(dev_priv, mob);
142 if (unlikely(ret != 0))
143 goto out_no_populate;
144
145 vmw_mob_pt_setup(mob, iter, otable->size >> PAGE_SHIFT);
146 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
147 }
148
149 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
150 if (unlikely(cmd == NULL)) {
151 DRM_ERROR("Failed reserving FIFO space for OTable setup.\n");
152 ret = -ENOMEM;
153 goto out_no_fifo;
154 }
155
156 memset(cmd, 0, sizeof(*cmd));
157 cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE64;
158 cmd->header.size = sizeof(cmd->body);
159 cmd->body.type = type;
160 cmd->body.baseAddress = mob->pt_root_page >> PAGE_SHIFT;
161 cmd->body.sizeInBytes = otable->size;
162 cmd->body.validSizeInBytes = 0;
163 cmd->body.ptDepth = mob->pt_level;
164
165
166
167
168
169
170 BUG_ON(mob->pt_level == VMW_MOBFMT_PTDEPTH_2);
171
172 vmw_fifo_commit(dev_priv, sizeof(*cmd));
173 otable->page_table = mob;
174
175 return 0;
176
177out_no_fifo:
178out_no_populate:
179 vmw_mob_destroy(mob);
180 return ret;
181}
182
183
184
185
186
187
188
189
190
191static void vmw_takedown_otable_base(struct vmw_private *dev_priv,
192 SVGAOTableType type,
193 struct vmw_otable *otable)
194{
195 struct {
196 SVGA3dCmdHeader header;
197 SVGA3dCmdSetOTableBase body;
198 } *cmd;
199 struct ttm_buffer_object *bo;
200
201 if (otable->page_table == NULL)
202 return;
203
204 bo = otable->page_table->pt_bo;
205 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
206 if (unlikely(cmd == NULL)) {
207 DRM_ERROR("Failed reserving FIFO space for OTable "
208 "takedown.\n");
209 return;
210 }
211
212 memset(cmd, 0, sizeof(*cmd));
213 cmd->header.id = SVGA_3D_CMD_SET_OTABLE_BASE;
214 cmd->header.size = sizeof(cmd->body);
215 cmd->body.type = type;
216 cmd->body.baseAddress = 0;
217 cmd->body.sizeInBytes = 0;
218 cmd->body.validSizeInBytes = 0;
219 cmd->body.ptDepth = SVGA3D_MOBFMT_INVALID;
220 vmw_fifo_commit(dev_priv, sizeof(*cmd));
221
222 if (bo) {
223 int ret;
224
225 ret = ttm_bo_reserve(bo, false, true, NULL);
226 BUG_ON(ret != 0);
227
228 vmw_bo_fence_single(bo, NULL);
229 ttm_bo_unreserve(bo);
230 }
231
232 vmw_mob_destroy(otable->page_table);
233 otable->page_table = NULL;
234}
235
236
237static int vmw_otable_batch_setup(struct vmw_private *dev_priv,
238 struct vmw_otable_batch *batch)
239{
240 unsigned long offset;
241 unsigned long bo_size;
242 struct vmw_otable *otables = batch->otables;
243 struct ttm_operation_ctx ctx = {
244 .interruptible = false,
245 .no_wait_gpu = false
246 };
247 SVGAOTableType i;
248 int ret;
249
250 bo_size = 0;
251 for (i = 0; i < batch->num_otables; ++i) {
252 if (!otables[i].enabled)
253 continue;
254
255 otables[i].size =
256 (otables[i].size + PAGE_SIZE - 1) & PAGE_MASK;
257 bo_size += otables[i].size;
258 }
259
260 ret = ttm_bo_create(&dev_priv->bdev, bo_size,
261 ttm_bo_type_device,
262 &vmw_sys_ne_placement,
263 0, false, &batch->otable_bo);
264
265 if (unlikely(ret != 0))
266 goto out_no_bo;
267
268 ret = ttm_bo_reserve(batch->otable_bo, false, true, NULL);
269 BUG_ON(ret != 0);
270 ret = vmw_bo_driver.ttm_tt_populate(batch->otable_bo->ttm, &ctx);
271 if (unlikely(ret != 0))
272 goto out_unreserve;
273 ret = vmw_bo_map_dma(batch->otable_bo);
274 if (unlikely(ret != 0))
275 goto out_unreserve;
276
277 ttm_bo_unreserve(batch->otable_bo);
278
279 offset = 0;
280 for (i = 0; i < batch->num_otables; ++i) {
281 if (!batch->otables[i].enabled)
282 continue;
283
284 ret = vmw_setup_otable_base(dev_priv, i, batch->otable_bo,
285 offset,
286 &otables[i]);
287 if (unlikely(ret != 0))
288 goto out_no_setup;
289 offset += otables[i].size;
290 }
291
292 return 0;
293
294out_unreserve:
295 ttm_bo_unreserve(batch->otable_bo);
296out_no_setup:
297 for (i = 0; i < batch->num_otables; ++i) {
298 if (batch->otables[i].enabled)
299 vmw_takedown_otable_base(dev_priv, i,
300 &batch->otables[i]);
301 }
302
303 ttm_bo_unref(&batch->otable_bo);
304out_no_bo:
305 return ret;
306}
307
308
309
310
311
312
313
314
315
316
317
318
319int vmw_otables_setup(struct vmw_private *dev_priv)
320{
321 struct vmw_otable **otables = &dev_priv->otable_batch.otables;
322 int ret;
323
324 if (dev_priv->has_dx) {
325 *otables = kmemdup(dx_tables, sizeof(dx_tables), GFP_KERNEL);
326 if (!(*otables))
327 return -ENOMEM;
328
329 dev_priv->otable_batch.num_otables = ARRAY_SIZE(dx_tables);
330 } else {
331 *otables = kmemdup(pre_dx_tables, sizeof(pre_dx_tables),
332 GFP_KERNEL);
333 if (!(*otables))
334 return -ENOMEM;
335
336 dev_priv->otable_batch.num_otables = ARRAY_SIZE(pre_dx_tables);
337 }
338
339 ret = vmw_otable_batch_setup(dev_priv, &dev_priv->otable_batch);
340 if (unlikely(ret != 0))
341 goto out_setup;
342
343 return 0;
344
345out_setup:
346 kfree(*otables);
347 return ret;
348}
349
350static void vmw_otable_batch_takedown(struct vmw_private *dev_priv,
351 struct vmw_otable_batch *batch)
352{
353 SVGAOTableType i;
354 struct ttm_buffer_object *bo = batch->otable_bo;
355 int ret;
356
357 for (i = 0; i < batch->num_otables; ++i)
358 if (batch->otables[i].enabled)
359 vmw_takedown_otable_base(dev_priv, i,
360 &batch->otables[i]);
361
362 ret = ttm_bo_reserve(bo, false, true, NULL);
363 BUG_ON(ret != 0);
364
365 vmw_bo_fence_single(bo, NULL);
366 ttm_bo_unreserve(bo);
367
368 ttm_bo_unref(&batch->otable_bo);
369}
370
371
372
373
374
375
376
377
378void vmw_otables_takedown(struct vmw_private *dev_priv)
379{
380 vmw_otable_batch_takedown(dev_priv, &dev_priv->otable_batch);
381 kfree(dev_priv->otable_batch.otables);
382}
383
384
385
386
387
388
389
390static unsigned long vmw_mob_calculate_pt_pages(unsigned long data_pages)
391{
392 unsigned long data_size = data_pages * PAGE_SIZE;
393 unsigned long tot_size = 0;
394
395 while (likely(data_size > PAGE_SIZE)) {
396 data_size = DIV_ROUND_UP(data_size, PAGE_SIZE);
397 data_size *= VMW_PPN_SIZE;
398 tot_size += (data_size + PAGE_SIZE - 1) & PAGE_MASK;
399 }
400
401 return tot_size >> PAGE_SHIFT;
402}
403
404
405
406
407
408
409struct vmw_mob *vmw_mob_create(unsigned long data_pages)
410{
411 struct vmw_mob *mob = kzalloc(sizeof(*mob), GFP_KERNEL);
412
413 if (unlikely(!mob))
414 return NULL;
415
416 mob->num_pages = vmw_mob_calculate_pt_pages(data_pages);
417
418 return mob;
419}
420
421
422
423
424
425
426
427
428
429
430
431
432static int vmw_mob_pt_populate(struct vmw_private *dev_priv,
433 struct vmw_mob *mob)
434{
435 int ret;
436 struct ttm_operation_ctx ctx = {
437 .interruptible = false,
438 .no_wait_gpu = false
439 };
440
441 BUG_ON(mob->pt_bo != NULL);
442
443 ret = ttm_bo_create(&dev_priv->bdev, mob->num_pages * PAGE_SIZE,
444 ttm_bo_type_device,
445 &vmw_sys_ne_placement,
446 0, false, &mob->pt_bo);
447 if (unlikely(ret != 0))
448 return ret;
449
450 ret = ttm_bo_reserve(mob->pt_bo, false, true, NULL);
451
452 BUG_ON(ret != 0);
453 ret = vmw_bo_driver.ttm_tt_populate(mob->pt_bo->ttm, &ctx);
454 if (unlikely(ret != 0))
455 goto out_unreserve;
456 ret = vmw_bo_map_dma(mob->pt_bo);
457 if (unlikely(ret != 0))
458 goto out_unreserve;
459
460 ttm_bo_unreserve(mob->pt_bo);
461
462 return 0;
463
464out_unreserve:
465 ttm_bo_unreserve(mob->pt_bo);
466 ttm_bo_unref(&mob->pt_bo);
467
468 return ret;
469}
470
471
472
473
474
475
476
477
478
479
480#if (VMW_PPN_SIZE == 8)
481static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
482{
483 *((u64 *) *addr) = val >> PAGE_SHIFT;
484 *addr += 2;
485}
486#else
487static void vmw_mob_assign_ppn(u32 **addr, dma_addr_t val)
488{
489 *(*addr)++ = val >> PAGE_SHIFT;
490}
491#endif
492
493
494
495
496
497
498
499
500
501
502
503
504static unsigned long vmw_mob_build_pt(struct vmw_piter *data_iter,
505 unsigned long num_data_pages,
506 struct vmw_piter *pt_iter)
507{
508 unsigned long pt_size = num_data_pages * VMW_PPN_SIZE;
509 unsigned long num_pt_pages = DIV_ROUND_UP(pt_size, PAGE_SIZE);
510 unsigned long pt_page;
511 u32 *addr, *save_addr;
512 unsigned long i;
513 struct page *page;
514
515 for (pt_page = 0; pt_page < num_pt_pages; ++pt_page) {
516 page = vmw_piter_page(pt_iter);
517
518 save_addr = addr = kmap_atomic(page);
519
520 for (i = 0; i < PAGE_SIZE / VMW_PPN_SIZE; ++i) {
521 vmw_mob_assign_ppn(&addr,
522 vmw_piter_dma_addr(data_iter));
523 if (unlikely(--num_data_pages == 0))
524 break;
525 WARN_ON(!vmw_piter_next(data_iter));
526 }
527 kunmap_atomic(save_addr);
528 vmw_piter_next(pt_iter);
529 }
530
531 return num_pt_pages;
532}
533
534
535
536
537
538
539
540
541
542
543
544static void vmw_mob_pt_setup(struct vmw_mob *mob,
545 struct vmw_piter data_iter,
546 unsigned long num_data_pages)
547{
548 unsigned long num_pt_pages = 0;
549 struct ttm_buffer_object *bo = mob->pt_bo;
550 struct vmw_piter save_pt_iter;
551 struct vmw_piter pt_iter;
552 const struct vmw_sg_table *vsgt;
553 int ret;
554
555 ret = ttm_bo_reserve(bo, false, true, NULL);
556 BUG_ON(ret != 0);
557
558 vsgt = vmw_bo_sg_table(bo);
559 vmw_piter_start(&pt_iter, vsgt, 0);
560 BUG_ON(!vmw_piter_next(&pt_iter));
561 mob->pt_level = 0;
562 while (likely(num_data_pages > 1)) {
563 ++mob->pt_level;
564 BUG_ON(mob->pt_level > 2);
565 save_pt_iter = pt_iter;
566 num_pt_pages = vmw_mob_build_pt(&data_iter, num_data_pages,
567 &pt_iter);
568 data_iter = save_pt_iter;
569 num_data_pages = num_pt_pages;
570 }
571
572 mob->pt_root_page = vmw_piter_dma_addr(&save_pt_iter);
573 ttm_bo_unreserve(bo);
574}
575
576
577
578
579
580
581void vmw_mob_destroy(struct vmw_mob *mob)
582{
583 if (mob->pt_bo)
584 ttm_bo_unref(&mob->pt_bo);
585 kfree(mob);
586}
587
588
589
590
591
592
593
594void vmw_mob_unbind(struct vmw_private *dev_priv,
595 struct vmw_mob *mob)
596{
597 struct {
598 SVGA3dCmdHeader header;
599 SVGA3dCmdDestroyGBMob body;
600 } *cmd;
601 int ret;
602 struct ttm_buffer_object *bo = mob->pt_bo;
603
604 if (bo) {
605 ret = ttm_bo_reserve(bo, false, true, NULL);
606
607
608
609 BUG_ON(ret != 0);
610 }
611
612 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
613 if (unlikely(cmd == NULL)) {
614 DRM_ERROR("Failed reserving FIFO space for Memory "
615 "Object unbinding.\n");
616 } else {
617 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_MOB;
618 cmd->header.size = sizeof(cmd->body);
619 cmd->body.mobid = mob->id;
620 vmw_fifo_commit(dev_priv, sizeof(*cmd));
621 }
622 if (bo) {
623 vmw_bo_fence_single(bo, NULL);
624 ttm_bo_unreserve(bo);
625 }
626 vmw_fifo_resource_dec(dev_priv);
627}
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644int vmw_mob_bind(struct vmw_private *dev_priv,
645 struct vmw_mob *mob,
646 const struct vmw_sg_table *vsgt,
647 unsigned long num_data_pages,
648 int32_t mob_id)
649{
650 int ret;
651 bool pt_set_up = false;
652 struct vmw_piter data_iter;
653 struct {
654 SVGA3dCmdHeader header;
655 SVGA3dCmdDefineGBMob64 body;
656 } *cmd;
657
658 mob->id = mob_id;
659 vmw_piter_start(&data_iter, vsgt, 0);
660 if (unlikely(!vmw_piter_next(&data_iter)))
661 return 0;
662
663 if (likely(num_data_pages == 1)) {
664 mob->pt_level = VMW_MOBFMT_PTDEPTH_0;
665 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
666 } else if (vsgt->num_regions == 1) {
667 mob->pt_level = SVGA3D_MOBFMT_RANGE;
668 mob->pt_root_page = vmw_piter_dma_addr(&data_iter);
669 } else if (unlikely(mob->pt_bo == NULL)) {
670 ret = vmw_mob_pt_populate(dev_priv, mob);
671 if (unlikely(ret != 0))
672 return ret;
673
674 vmw_mob_pt_setup(mob, data_iter, num_data_pages);
675 pt_set_up = true;
676 mob->pt_level += VMW_MOBFMT_PTDEPTH_1 - SVGA3D_MOBFMT_PTDEPTH_1;
677 }
678
679 vmw_fifo_resource_inc(dev_priv);
680
681 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
682 if (unlikely(cmd == NULL)) {
683 DRM_ERROR("Failed reserving FIFO space for Memory "
684 "Object binding.\n");
685 goto out_no_cmd_space;
686 }
687
688 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_MOB64;
689 cmd->header.size = sizeof(cmd->body);
690 cmd->body.mobid = mob_id;
691 cmd->body.ptDepth = mob->pt_level;
692 cmd->body.base = mob->pt_root_page >> PAGE_SHIFT;
693 cmd->body.sizeInBytes = num_data_pages * PAGE_SIZE;
694
695 vmw_fifo_commit(dev_priv, sizeof(*cmd));
696
697 return 0;
698
699out_no_cmd_space:
700 vmw_fifo_resource_dec(dev_priv);
701 if (pt_set_up)
702 ttm_bo_unref(&mob->pt_bo);
703
704 return -ENOMEM;
705}
706