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#include "ttm/ttm_bo_api.h"
30
31
32
33
34
35#define VMW_CMDBUF_INLINE_ALIGN 64
36#define VMW_CMDBUF_INLINE_SIZE \
37 (1024 - ALIGN(sizeof(SVGACBHeader), VMW_CMDBUF_INLINE_ALIGN))
38
39
40
41
42
43
44
45
46
47
48struct vmw_cmdbuf_context {
49 struct list_head submitted;
50 struct list_head hw_submitted;
51 struct list_head preempted;
52 unsigned num_hw_submitted;
53};
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101struct vmw_cmdbuf_man {
102 struct mutex cur_mutex;
103 struct mutex space_mutex;
104 struct work_struct work;
105 struct vmw_private *dev_priv;
106 struct vmw_cmdbuf_context ctx[SVGA_CB_CONTEXT_MAX];
107 struct list_head error;
108 struct drm_mm mm;
109 struct ttm_buffer_object *cmd_space;
110 struct ttm_bo_kmap_obj map_obj;
111 u8 *map;
112 struct vmw_cmdbuf_header *cur;
113 size_t cur_pos;
114 size_t default_size;
115 unsigned max_hw_submitted;
116 spinlock_t lock;
117 struct dma_pool *headers;
118 struct dma_pool *dheaders;
119 struct tasklet_struct tasklet;
120 wait_queue_head_t alloc_queue;
121 wait_queue_head_t idle_queue;
122 bool irq_on;
123 bool using_mob;
124 bool has_pool;
125 dma_addr_t handle;
126 size_t size;
127};
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144struct vmw_cmdbuf_header {
145 struct vmw_cmdbuf_man *man;
146 SVGACBHeader *cb_header;
147 SVGACBContext cb_context;
148 struct list_head list;
149 struct drm_mm_node node;
150 dma_addr_t handle;
151 u8 *cmd;
152 size_t size;
153 size_t reserved;
154 bool inline_space;
155};
156
157
158
159
160
161
162
163
164struct vmw_cmdbuf_dheader {
165 SVGACBHeader cb_header;
166 u8 cmd[VMW_CMDBUF_INLINE_SIZE] __aligned(VMW_CMDBUF_INLINE_ALIGN);
167};
168
169
170
171
172
173
174
175
176struct vmw_cmdbuf_alloc_info {
177 size_t page_size;
178 struct drm_mm_node *node;
179 bool done;
180};
181
182
183#define for_each_cmdbuf_ctx(_man, _i, _ctx) \
184 for (_i = 0, _ctx = &(_man)->ctx[0]; (_i) < SVGA_CB_CONTEXT_MAX; \
185 ++(_i), ++(_ctx))
186
187static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man, bool enable);
188
189
190
191
192
193
194
195
196static int vmw_cmdbuf_cur_lock(struct vmw_cmdbuf_man *man, bool interruptible)
197{
198 if (interruptible) {
199 if (mutex_lock_interruptible(&man->cur_mutex))
200 return -ERESTARTSYS;
201 } else {
202 mutex_lock(&man->cur_mutex);
203 }
204
205 return 0;
206}
207
208
209
210
211
212
213static void vmw_cmdbuf_cur_unlock(struct vmw_cmdbuf_man *man)
214{
215 mutex_unlock(&man->cur_mutex);
216}
217
218
219
220
221
222
223
224
225static void vmw_cmdbuf_header_inline_free(struct vmw_cmdbuf_header *header)
226{
227 struct vmw_cmdbuf_dheader *dheader;
228
229 if (WARN_ON_ONCE(!header->inline_space))
230 return;
231
232 dheader = container_of(header->cb_header, struct vmw_cmdbuf_dheader,
233 cb_header);
234 dma_pool_free(header->man->dheaders, dheader, header->handle);
235 kfree(header);
236}
237
238
239
240
241
242
243
244
245
246static void __vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
247{
248 struct vmw_cmdbuf_man *man = header->man;
249
250 lockdep_assert_held_once(&man->lock);
251
252 if (header->inline_space) {
253 vmw_cmdbuf_header_inline_free(header);
254 return;
255 }
256
257 drm_mm_remove_node(&header->node);
258 wake_up_all(&man->alloc_queue);
259 if (header->cb_header)
260 dma_pool_free(man->headers, header->cb_header,
261 header->handle);
262 kfree(header);
263}
264
265
266
267
268
269
270
271void vmw_cmdbuf_header_free(struct vmw_cmdbuf_header *header)
272{
273 struct vmw_cmdbuf_man *man = header->man;
274
275
276 if (header->inline_space) {
277 vmw_cmdbuf_header_inline_free(header);
278 return;
279 }
280 spin_lock_bh(&man->lock);
281 __vmw_cmdbuf_header_free(header);
282 spin_unlock_bh(&man->lock);
283}
284
285
286
287
288
289
290
291static int vmw_cmdbuf_header_submit(struct vmw_cmdbuf_header *header)
292{
293 struct vmw_cmdbuf_man *man = header->man;
294 u32 val;
295
296 val = upper_32_bits(header->handle);
297 vmw_write(man->dev_priv, SVGA_REG_COMMAND_HIGH, val);
298
299 val = lower_32_bits(header->handle);
300 val |= header->cb_context & SVGA_CB_CONTEXT_MASK;
301 vmw_write(man->dev_priv, SVGA_REG_COMMAND_LOW, val);
302
303 return header->cb_header->status;
304}
305
306
307
308
309
310
311static void vmw_cmdbuf_ctx_init(struct vmw_cmdbuf_context *ctx)
312{
313 INIT_LIST_HEAD(&ctx->hw_submitted);
314 INIT_LIST_HEAD(&ctx->submitted);
315 INIT_LIST_HEAD(&ctx->preempted);
316 ctx->num_hw_submitted = 0;
317}
318
319
320
321
322
323
324
325
326
327
328
329static void vmw_cmdbuf_ctx_submit(struct vmw_cmdbuf_man *man,
330 struct vmw_cmdbuf_context *ctx)
331{
332 while (ctx->num_hw_submitted < man->max_hw_submitted &&
333 !list_empty(&ctx->submitted)) {
334 struct vmw_cmdbuf_header *entry;
335 SVGACBStatus status;
336
337 entry = list_first_entry(&ctx->submitted,
338 struct vmw_cmdbuf_header,
339 list);
340
341 status = vmw_cmdbuf_header_submit(entry);
342
343
344 if (WARN_ON_ONCE(status == SVGA_CB_STATUS_QUEUE_FULL)) {
345 entry->cb_header->status = SVGA_CB_STATUS_NONE;
346 break;
347 }
348
349 list_del(&entry->list);
350 list_add_tail(&entry->list, &ctx->hw_submitted);
351 ctx->num_hw_submitted++;
352 }
353
354}
355
356
357
358
359
360
361
362
363
364
365
366static void vmw_cmdbuf_ctx_process(struct vmw_cmdbuf_man *man,
367 struct vmw_cmdbuf_context *ctx,
368 int *notempty)
369{
370 struct vmw_cmdbuf_header *entry, *next;
371
372 vmw_cmdbuf_ctx_submit(man, ctx);
373
374 list_for_each_entry_safe(entry, next, &ctx->hw_submitted, list) {
375 SVGACBStatus status = entry->cb_header->status;
376
377 if (status == SVGA_CB_STATUS_NONE)
378 break;
379
380 list_del(&entry->list);
381 wake_up_all(&man->idle_queue);
382 ctx->num_hw_submitted--;
383 switch (status) {
384 case SVGA_CB_STATUS_COMPLETED:
385 __vmw_cmdbuf_header_free(entry);
386 break;
387 case SVGA_CB_STATUS_COMMAND_ERROR:
388 case SVGA_CB_STATUS_CB_HEADER_ERROR:
389 list_add_tail(&entry->list, &man->error);
390 schedule_work(&man->work);
391 break;
392 case SVGA_CB_STATUS_PREEMPTED:
393 list_add(&entry->list, &ctx->preempted);
394 break;
395 default:
396 WARN_ONCE(true, "Undefined command buffer status.\n");
397 __vmw_cmdbuf_header_free(entry);
398 break;
399 }
400 }
401
402 vmw_cmdbuf_ctx_submit(man, ctx);
403 if (!list_empty(&ctx->submitted))
404 (*notempty)++;
405}
406
407
408
409
410
411
412
413
414
415
416
417static void vmw_cmdbuf_man_process(struct vmw_cmdbuf_man *man)
418{
419 int notempty;
420 struct vmw_cmdbuf_context *ctx;
421 int i;
422
423retry:
424 notempty = 0;
425 for_each_cmdbuf_ctx(man, i, ctx)
426 vmw_cmdbuf_ctx_process(man, ctx, ¬empty);
427
428 if (man->irq_on && !notempty) {
429 vmw_generic_waiter_remove(man->dev_priv,
430 SVGA_IRQFLAG_COMMAND_BUFFER,
431 &man->dev_priv->cmdbuf_waiters);
432 man->irq_on = false;
433 } else if (!man->irq_on && notempty) {
434 vmw_generic_waiter_add(man->dev_priv,
435 SVGA_IRQFLAG_COMMAND_BUFFER,
436 &man->dev_priv->cmdbuf_waiters);
437 man->irq_on = true;
438
439
440 goto retry;
441 }
442}
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457static void vmw_cmdbuf_ctx_add(struct vmw_cmdbuf_man *man,
458 struct vmw_cmdbuf_header *header,
459 SVGACBContext cb_context)
460{
461 if (!(header->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT))
462 header->cb_header->dxContext = 0;
463 header->cb_context = cb_context;
464 list_add_tail(&header->list, &man->ctx[cb_context].submitted);
465
466 vmw_cmdbuf_man_process(man);
467}
468
469
470
471
472
473
474
475
476
477
478
479
480static void vmw_cmdbuf_man_tasklet(unsigned long data)
481{
482 struct vmw_cmdbuf_man *man = (struct vmw_cmdbuf_man *) data;
483
484 spin_lock(&man->lock);
485 vmw_cmdbuf_man_process(man);
486 spin_unlock(&man->lock);
487}
488
489
490
491
492
493
494
495
496
497
498static void vmw_cmdbuf_work_func(struct work_struct *work)
499{
500 struct vmw_cmdbuf_man *man =
501 container_of(work, struct vmw_cmdbuf_man, work);
502 struct vmw_cmdbuf_header *entry, *next;
503 uint32_t dummy;
504 bool restart = false;
505
506 spin_lock_bh(&man->lock);
507 list_for_each_entry_safe(entry, next, &man->error, list) {
508 restart = true;
509 DRM_ERROR("Command buffer error.\n");
510
511 list_del(&entry->list);
512 __vmw_cmdbuf_header_free(entry);
513 wake_up_all(&man->idle_queue);
514 }
515 spin_unlock_bh(&man->lock);
516
517 if (restart && vmw_cmdbuf_startstop(man, true))
518 DRM_ERROR("Failed restarting command buffer context 0.\n");
519
520
521 vmw_fifo_send_fence(man->dev_priv, &dummy);
522}
523
524
525
526
527
528
529
530
531static bool vmw_cmdbuf_man_idle(struct vmw_cmdbuf_man *man,
532 bool check_preempted)
533{
534 struct vmw_cmdbuf_context *ctx;
535 bool idle = false;
536 int i;
537
538 spin_lock_bh(&man->lock);
539 vmw_cmdbuf_man_process(man);
540 for_each_cmdbuf_ctx(man, i, ctx) {
541 if (!list_empty(&ctx->submitted) ||
542 !list_empty(&ctx->hw_submitted) ||
543 (check_preempted && !list_empty(&ctx->preempted)))
544 goto out_unlock;
545 }
546
547 idle = list_empty(&man->error);
548
549out_unlock:
550 spin_unlock_bh(&man->lock);
551
552 return idle;
553}
554
555
556
557
558
559
560
561
562
563
564static void __vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man)
565{
566 struct vmw_cmdbuf_header *cur = man->cur;
567
568 WARN_ON(!mutex_is_locked(&man->cur_mutex));
569
570 if (!cur)
571 return;
572
573 spin_lock_bh(&man->lock);
574 if (man->cur_pos == 0) {
575 __vmw_cmdbuf_header_free(cur);
576 goto out_unlock;
577 }
578
579 man->cur->cb_header->length = man->cur_pos;
580 vmw_cmdbuf_ctx_add(man, man->cur, SVGA_CB_CONTEXT_0);
581out_unlock:
582 spin_unlock_bh(&man->lock);
583 man->cur = NULL;
584 man->cur_pos = 0;
585}
586
587
588
589
590
591
592
593
594
595
596
597int vmw_cmdbuf_cur_flush(struct vmw_cmdbuf_man *man,
598 bool interruptible)
599{
600 int ret = vmw_cmdbuf_cur_lock(man, interruptible);
601
602 if (ret)
603 return ret;
604
605 __vmw_cmdbuf_cur_flush(man);
606 vmw_cmdbuf_cur_unlock(man);
607
608 return 0;
609}
610
611
612
613
614
615
616
617
618
619
620
621
622int vmw_cmdbuf_idle(struct vmw_cmdbuf_man *man, bool interruptible,
623 unsigned long timeout)
624{
625 int ret;
626
627 ret = vmw_cmdbuf_cur_flush(man, interruptible);
628 vmw_generic_waiter_add(man->dev_priv,
629 SVGA_IRQFLAG_COMMAND_BUFFER,
630 &man->dev_priv->cmdbuf_waiters);
631
632 if (interruptible) {
633 ret = wait_event_interruptible_timeout
634 (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
635 timeout);
636 } else {
637 ret = wait_event_timeout
638 (man->idle_queue, vmw_cmdbuf_man_idle(man, true),
639 timeout);
640 }
641 vmw_generic_waiter_remove(man->dev_priv,
642 SVGA_IRQFLAG_COMMAND_BUFFER,
643 &man->dev_priv->cmdbuf_waiters);
644 if (ret == 0) {
645 if (!vmw_cmdbuf_man_idle(man, true))
646 ret = -EBUSY;
647 else
648 ret = 0;
649 }
650 if (ret > 0)
651 ret = 0;
652
653 return ret;
654}
655
656
657
658
659
660
661
662
663
664
665
666static bool vmw_cmdbuf_try_alloc(struct vmw_cmdbuf_man *man,
667 struct vmw_cmdbuf_alloc_info *info)
668{
669 int ret;
670
671 if (info->done)
672 return true;
673
674 memset(info->node, 0, sizeof(*info->node));
675 spin_lock_bh(&man->lock);
676 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
677 if (ret) {
678 vmw_cmdbuf_man_process(man);
679 ret = drm_mm_insert_node(&man->mm, info->node, info->page_size);
680 }
681
682 spin_unlock_bh(&man->lock);
683 info->done = !ret;
684
685 return info->done;
686}
687
688
689
690
691
692
693
694
695
696
697
698
699
700static int vmw_cmdbuf_alloc_space(struct vmw_cmdbuf_man *man,
701 struct drm_mm_node *node,
702 size_t size,
703 bool interruptible)
704{
705 struct vmw_cmdbuf_alloc_info info;
706
707 info.page_size = PAGE_ALIGN(size) >> PAGE_SHIFT;
708 info.node = node;
709 info.done = false;
710
711
712
713
714
715 if (interruptible) {
716 if (mutex_lock_interruptible(&man->space_mutex))
717 return -ERESTARTSYS;
718 } else {
719 mutex_lock(&man->space_mutex);
720 }
721
722
723 if (vmw_cmdbuf_try_alloc(man, &info))
724 goto out_unlock;
725
726 vmw_generic_waiter_add(man->dev_priv,
727 SVGA_IRQFLAG_COMMAND_BUFFER,
728 &man->dev_priv->cmdbuf_waiters);
729
730 if (interruptible) {
731 int ret;
732
733 ret = wait_event_interruptible
734 (man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
735 if (ret) {
736 vmw_generic_waiter_remove
737 (man->dev_priv, SVGA_IRQFLAG_COMMAND_BUFFER,
738 &man->dev_priv->cmdbuf_waiters);
739 mutex_unlock(&man->space_mutex);
740 return ret;
741 }
742 } else {
743 wait_event(man->alloc_queue, vmw_cmdbuf_try_alloc(man, &info));
744 }
745 vmw_generic_waiter_remove(man->dev_priv,
746 SVGA_IRQFLAG_COMMAND_BUFFER,
747 &man->dev_priv->cmdbuf_waiters);
748
749out_unlock:
750 mutex_unlock(&man->space_mutex);
751
752 return 0;
753}
754
755
756
757
758
759
760
761
762
763
764static int vmw_cmdbuf_space_pool(struct vmw_cmdbuf_man *man,
765 struct vmw_cmdbuf_header *header,
766 size_t size,
767 bool interruptible)
768{
769 SVGACBHeader *cb_hdr;
770 size_t offset;
771 int ret;
772
773 if (!man->has_pool)
774 return -ENOMEM;
775
776 ret = vmw_cmdbuf_alloc_space(man, &header->node, size, interruptible);
777
778 if (ret)
779 return ret;
780
781 header->cb_header = dma_pool_alloc(man->headers, GFP_KERNEL,
782 &header->handle);
783 if (!header->cb_header) {
784 ret = -ENOMEM;
785 goto out_no_cb_header;
786 }
787
788 header->size = header->node.size << PAGE_SHIFT;
789 cb_hdr = header->cb_header;
790 offset = header->node.start << PAGE_SHIFT;
791 header->cmd = man->map + offset;
792 memset(cb_hdr, 0, sizeof(*cb_hdr));
793 if (man->using_mob) {
794 cb_hdr->flags = SVGA_CB_FLAG_MOB;
795 cb_hdr->ptr.mob.mobid = man->cmd_space->mem.start;
796 cb_hdr->ptr.mob.mobOffset = offset;
797 } else {
798 cb_hdr->ptr.pa = (u64)man->handle + (u64)offset;
799 }
800
801 return 0;
802
803out_no_cb_header:
804 spin_lock_bh(&man->lock);
805 drm_mm_remove_node(&header->node);
806 spin_unlock_bh(&man->lock);
807
808 return ret;
809}
810
811
812
813
814
815
816
817
818
819static int vmw_cmdbuf_space_inline(struct vmw_cmdbuf_man *man,
820 struct vmw_cmdbuf_header *header,
821 int size)
822{
823 struct vmw_cmdbuf_dheader *dheader;
824 SVGACBHeader *cb_hdr;
825
826 if (WARN_ON_ONCE(size > VMW_CMDBUF_INLINE_SIZE))
827 return -ENOMEM;
828
829 dheader = dma_pool_alloc(man->dheaders, GFP_KERNEL,
830 &header->handle);
831 if (!dheader)
832 return -ENOMEM;
833
834 header->inline_space = true;
835 header->size = VMW_CMDBUF_INLINE_SIZE;
836 cb_hdr = &dheader->cb_header;
837 header->cb_header = cb_hdr;
838 header->cmd = dheader->cmd;
839 memset(dheader, 0, sizeof(*dheader));
840 cb_hdr->status = SVGA_CB_STATUS_NONE;
841 cb_hdr->flags = SVGA_CB_FLAG_NONE;
842 cb_hdr->ptr.pa = (u64)header->handle +
843 (u64)offsetof(struct vmw_cmdbuf_dheader, cmd);
844
845 return 0;
846}
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861void *vmw_cmdbuf_alloc(struct vmw_cmdbuf_man *man,
862 size_t size, bool interruptible,
863 struct vmw_cmdbuf_header **p_header)
864{
865 struct vmw_cmdbuf_header *header;
866 int ret = 0;
867
868 *p_header = NULL;
869
870 header = kzalloc(sizeof(*header), GFP_KERNEL);
871 if (!header)
872 return ERR_PTR(-ENOMEM);
873
874 if (size <= VMW_CMDBUF_INLINE_SIZE)
875 ret = vmw_cmdbuf_space_inline(man, header, size);
876 else
877 ret = vmw_cmdbuf_space_pool(man, header, size, interruptible);
878
879 if (ret) {
880 kfree(header);
881 return ERR_PTR(ret);
882 }
883
884 header->man = man;
885 INIT_LIST_HEAD(&header->list);
886 header->cb_header->status = SVGA_CB_STATUS_NONE;
887 *p_header = header;
888
889 return header->cmd;
890}
891
892
893
894
895
896
897
898
899
900
901
902
903
904static void *vmw_cmdbuf_reserve_cur(struct vmw_cmdbuf_man *man,
905 size_t size,
906 int ctx_id,
907 bool interruptible)
908{
909 struct vmw_cmdbuf_header *cur;
910 void *ret;
911
912 if (vmw_cmdbuf_cur_lock(man, interruptible))
913 return ERR_PTR(-ERESTARTSYS);
914
915 cur = man->cur;
916 if (cur && (size + man->cur_pos > cur->size ||
917 ((cur->cb_header->flags & SVGA_CB_FLAG_DX_CONTEXT) &&
918 ctx_id != cur->cb_header->dxContext)))
919 __vmw_cmdbuf_cur_flush(man);
920
921 if (!man->cur) {
922 ret = vmw_cmdbuf_alloc(man,
923 max_t(size_t, size, man->default_size),
924 interruptible, &man->cur);
925 if (IS_ERR(ret)) {
926 vmw_cmdbuf_cur_unlock(man);
927 return ret;
928 }
929
930 cur = man->cur;
931 }
932
933 if (ctx_id != SVGA3D_INVALID_ID) {
934 cur->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
935 cur->cb_header->dxContext = ctx_id;
936 }
937
938 cur->reserved = size;
939
940 return (void *) (man->cur->cmd + man->cur_pos);
941}
942
943
944
945
946
947
948
949
950static void vmw_cmdbuf_commit_cur(struct vmw_cmdbuf_man *man,
951 size_t size, bool flush)
952{
953 struct vmw_cmdbuf_header *cur = man->cur;
954
955 WARN_ON(!mutex_is_locked(&man->cur_mutex));
956
957 WARN_ON(size > cur->reserved);
958 man->cur_pos += size;
959 if (!size)
960 cur->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
961 if (flush)
962 __vmw_cmdbuf_cur_flush(man);
963 vmw_cmdbuf_cur_unlock(man);
964}
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979void *vmw_cmdbuf_reserve(struct vmw_cmdbuf_man *man, size_t size,
980 int ctx_id, bool interruptible,
981 struct vmw_cmdbuf_header *header)
982{
983 if (!header)
984 return vmw_cmdbuf_reserve_cur(man, size, ctx_id, interruptible);
985
986 if (size > header->size)
987 return ERR_PTR(-EINVAL);
988
989 if (ctx_id != SVGA3D_INVALID_ID) {
990 header->cb_header->flags |= SVGA_CB_FLAG_DX_CONTEXT;
991 header->cb_header->dxContext = ctx_id;
992 }
993
994 header->reserved = size;
995 return header->cmd;
996}
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007void vmw_cmdbuf_commit(struct vmw_cmdbuf_man *man, size_t size,
1008 struct vmw_cmdbuf_header *header, bool flush)
1009{
1010 if (!header) {
1011 vmw_cmdbuf_commit_cur(man, size, flush);
1012 return;
1013 }
1014
1015 (void) vmw_cmdbuf_cur_lock(man, false);
1016 __vmw_cmdbuf_cur_flush(man);
1017 WARN_ON(size > header->reserved);
1018 man->cur = header;
1019 man->cur_pos = size;
1020 if (!size)
1021 header->cb_header->flags &= ~SVGA_CB_FLAG_DX_CONTEXT;
1022 if (flush)
1023 __vmw_cmdbuf_cur_flush(man);
1024 vmw_cmdbuf_cur_unlock(man);
1025}
1026
1027
1028
1029
1030
1031
1032void vmw_cmdbuf_tasklet_schedule(struct vmw_cmdbuf_man *man)
1033{
1034 if (!man)
1035 return;
1036
1037 tasklet_schedule(&man->tasklet);
1038}
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049static int vmw_cmdbuf_send_device_command(struct vmw_cmdbuf_man *man,
1050 const void *command,
1051 size_t size)
1052{
1053 struct vmw_cmdbuf_header *header;
1054 int status;
1055 void *cmd = vmw_cmdbuf_alloc(man, size, false, &header);
1056
1057 if (IS_ERR(cmd))
1058 return PTR_ERR(cmd);
1059
1060 memcpy(cmd, command, size);
1061 header->cb_header->length = size;
1062 header->cb_context = SVGA_CB_CONTEXT_DEVICE;
1063 spin_lock_bh(&man->lock);
1064 status = vmw_cmdbuf_header_submit(header);
1065 spin_unlock_bh(&man->lock);
1066 vmw_cmdbuf_header_free(header);
1067
1068 if (status != SVGA_CB_STATUS_COMPLETED) {
1069 DRM_ERROR("Device context command failed with status %d\n",
1070 status);
1071 return -EINVAL;
1072 }
1073
1074 return 0;
1075}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086static int vmw_cmdbuf_startstop(struct vmw_cmdbuf_man *man,
1087 bool enable)
1088{
1089 struct {
1090 uint32 id;
1091 SVGADCCmdStartStop body;
1092 } __packed cmd;
1093
1094 cmd.id = SVGA_DC_CMD_START_STOP_CONTEXT;
1095 cmd.body.enable = (enable) ? 1 : 0;
1096 cmd.body.context = SVGA_CB_CONTEXT_0;
1097
1098 return vmw_cmdbuf_send_device_command(man, &cmd, sizeof(cmd));
1099}
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116int vmw_cmdbuf_set_pool_size(struct vmw_cmdbuf_man *man,
1117 size_t size, size_t default_size)
1118{
1119 struct vmw_private *dev_priv = man->dev_priv;
1120 bool dummy;
1121 int ret;
1122
1123 if (man->has_pool)
1124 return -EINVAL;
1125
1126
1127 size = PAGE_ALIGN(size);
1128 man->map = dma_alloc_coherent(&dev_priv->dev->pdev->dev, size,
1129 &man->handle, GFP_KERNEL);
1130 if (man->map) {
1131 man->using_mob = false;
1132 } else {
1133
1134
1135
1136
1137
1138
1139 if (!(dev_priv->capabilities & SVGA_CAP_DX))
1140 return -ENOMEM;
1141
1142 ret = ttm_bo_create(&dev_priv->bdev, size, ttm_bo_type_device,
1143 &vmw_mob_ne_placement, 0, false, NULL,
1144 &man->cmd_space);
1145 if (ret)
1146 return ret;
1147
1148 man->using_mob = true;
1149 ret = ttm_bo_kmap(man->cmd_space, 0, size >> PAGE_SHIFT,
1150 &man->map_obj);
1151 if (ret)
1152 goto out_no_map;
1153
1154 man->map = ttm_kmap_obj_virtual(&man->map_obj, &dummy);
1155 }
1156
1157 man->size = size;
1158 drm_mm_init(&man->mm, 0, size >> PAGE_SHIFT);
1159
1160 man->has_pool = true;
1161
1162
1163
1164
1165
1166
1167
1168 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1169 DRM_INFO("Using command buffers with %s pool.\n",
1170 (man->using_mob) ? "MOB" : "DMA");
1171
1172 return 0;
1173
1174out_no_map:
1175 if (man->using_mob)
1176 ttm_bo_unref(&man->cmd_space);
1177
1178 return ret;
1179}
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191struct vmw_cmdbuf_man *vmw_cmdbuf_man_create(struct vmw_private *dev_priv)
1192{
1193 struct vmw_cmdbuf_man *man;
1194 struct vmw_cmdbuf_context *ctx;
1195 int i;
1196 int ret;
1197
1198 if (!(dev_priv->capabilities & SVGA_CAP_COMMAND_BUFFERS))
1199 return ERR_PTR(-ENOSYS);
1200
1201 man = kzalloc(sizeof(*man), GFP_KERNEL);
1202 if (!man)
1203 return ERR_PTR(-ENOMEM);
1204
1205 man->headers = dma_pool_create("vmwgfx cmdbuf",
1206 &dev_priv->dev->pdev->dev,
1207 sizeof(SVGACBHeader),
1208 64, PAGE_SIZE);
1209 if (!man->headers) {
1210 ret = -ENOMEM;
1211 goto out_no_pool;
1212 }
1213
1214 man->dheaders = dma_pool_create("vmwgfx inline cmdbuf",
1215 &dev_priv->dev->pdev->dev,
1216 sizeof(struct vmw_cmdbuf_dheader),
1217 64, PAGE_SIZE);
1218 if (!man->dheaders) {
1219 ret = -ENOMEM;
1220 goto out_no_dpool;
1221 }
1222
1223 for_each_cmdbuf_ctx(man, i, ctx)
1224 vmw_cmdbuf_ctx_init(ctx);
1225
1226 INIT_LIST_HEAD(&man->error);
1227 spin_lock_init(&man->lock);
1228 mutex_init(&man->cur_mutex);
1229 mutex_init(&man->space_mutex);
1230 tasklet_init(&man->tasklet, vmw_cmdbuf_man_tasklet,
1231 (unsigned long) man);
1232 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1233 init_waitqueue_head(&man->alloc_queue);
1234 init_waitqueue_head(&man->idle_queue);
1235 man->dev_priv = dev_priv;
1236 man->max_hw_submitted = SVGA_CB_MAX_QUEUED_PER_CONTEXT - 1;
1237 INIT_WORK(&man->work, &vmw_cmdbuf_work_func);
1238 vmw_generic_waiter_add(dev_priv, SVGA_IRQFLAG_ERROR,
1239 &dev_priv->error_waiters);
1240 ret = vmw_cmdbuf_startstop(man, true);
1241 if (ret) {
1242 DRM_ERROR("Failed starting command buffer context 0.\n");
1243 vmw_cmdbuf_man_destroy(man);
1244 return ERR_PTR(ret);
1245 }
1246
1247 return man;
1248
1249out_no_dpool:
1250 dma_pool_destroy(man->headers);
1251out_no_pool:
1252 kfree(man);
1253
1254 return ERR_PTR(ret);
1255}
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268void vmw_cmdbuf_remove_pool(struct vmw_cmdbuf_man *man)
1269{
1270 if (!man->has_pool)
1271 return;
1272
1273 man->has_pool = false;
1274 man->default_size = VMW_CMDBUF_INLINE_SIZE;
1275 (void) vmw_cmdbuf_idle(man, false, 10*HZ);
1276 if (man->using_mob) {
1277 (void) ttm_bo_kunmap(&man->map_obj);
1278 ttm_bo_unref(&man->cmd_space);
1279 } else {
1280 dma_free_coherent(&man->dev_priv->dev->pdev->dev,
1281 man->size, man->map, man->handle);
1282 }
1283}
1284
1285
1286
1287
1288
1289
1290
1291
1292void vmw_cmdbuf_man_destroy(struct vmw_cmdbuf_man *man)
1293{
1294 WARN_ON_ONCE(man->has_pool);
1295 (void) vmw_cmdbuf_idle(man, false, 10*HZ);
1296 if (vmw_cmdbuf_startstop(man, false))
1297 DRM_ERROR("Failed stopping command buffer context 0.\n");
1298
1299 vmw_generic_waiter_remove(man->dev_priv, SVGA_IRQFLAG_ERROR,
1300 &man->dev_priv->error_waiters);
1301 tasklet_kill(&man->tasklet);
1302 (void) cancel_work_sync(&man->work);
1303 dma_pool_destroy(man->dheaders);
1304 dma_pool_destroy(man->headers);
1305 mutex_destroy(&man->cur_mutex);
1306 mutex_destroy(&man->space_mutex);
1307 kfree(man);
1308}
1309