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