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
34
35#include <drm/drmP.h>
36#include "drm_trace.h"
37#include "drm_internal.h"
38
39#include <linux/interrupt.h>
40#include <linux/slab.h>
41
42#include <linux/vgaarb.h>
43#include <linux/export.h>
44
45
46
47
48#define DRM_TIMESTAMP_MAXRETRIES 3
49
50
51
52
53#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
54
55static bool
56drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
57 struct timeval *tvblank, unsigned flags);
58
59static unsigned int drm_timestamp_precision = 20;
60
61
62
63
64
65unsigned int drm_timestamp_monotonic = 1;
66
67static int drm_vblank_offdelay = 5000;
68
69module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
70module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
71module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
72MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
73MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
74MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
75
76static void store_vblank(struct drm_device *dev, unsigned int pipe,
77 u32 vblank_count_inc,
78 struct timeval *t_vblank, u32 last)
79{
80 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
81
82 assert_spin_locked(&dev->vblank_time_lock);
83
84 vblank->last = last;
85
86 write_seqlock(&vblank->seqlock);
87 vblank->time = *t_vblank;
88 vblank->count += vblank_count_inc;
89 write_sequnlock(&vblank->seqlock);
90}
91
92
93
94
95
96
97
98
99
100
101static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
102{
103 u32 cur_vblank;
104 bool rc;
105 struct timeval t_vblank;
106 int count = DRM_TIMESTAMP_MAXRETRIES;
107
108 spin_lock(&dev->vblank_time_lock);
109
110
111
112
113
114 do {
115 cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
116 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
117 } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
118
119
120
121
122
123
124 if (!rc)
125 t_vblank = (struct timeval) {0, 0};
126
127
128
129
130
131 store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
132
133 spin_unlock(&dev->vblank_time_lock);
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
149 unsigned long flags)
150{
151 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
152 u32 cur_vblank, diff;
153 bool rc;
154 struct timeval t_vblank;
155 int count = DRM_TIMESTAMP_MAXRETRIES;
156 int framedur_ns = vblank->framedur_ns;
157
158
159
160
161
162
163
164
165
166
167
168
169
170 do {
171 cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
172 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
173 } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
174
175 if (dev->max_vblank_count != 0) {
176
177 diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
178 } else if (rc && framedur_ns) {
179 const struct timeval *t_old;
180 u64 diff_ns;
181
182 t_old = &vblank->time;
183 diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
184
185
186
187
188
189
190 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
191
192 if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
193 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
194 " diff_ns = %lld, framedur_ns = %d)\n",
195 pipe, (long long) diff_ns, framedur_ns);
196 } else {
197
198 diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
199 }
200
201
202
203
204
205
206
207
208
209
210 if (diff > 1 && (vblank->inmodeset & 0x2)) {
211 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
212 " due to pre-modeset.\n", pipe, diff);
213 diff = 1;
214 }
215
216 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
217 " current=%u, diff=%u, hw=%u hw_last=%u\n",
218 pipe, vblank->count, diff, cur_vblank, vblank->last);
219
220 if (diff == 0) {
221 WARN_ON_ONCE(cur_vblank != vblank->last);
222 return;
223 }
224
225
226
227
228
229
230
231 if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
232 t_vblank = (struct timeval) {0, 0};
233
234 store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
235}
236
237
238
239
240
241
242
243
244
245
246
247u32 drm_accurate_vblank_count(struct drm_crtc *crtc)
248{
249 struct drm_device *dev = crtc->dev;
250 unsigned int pipe = drm_crtc_index(crtc);
251 u32 vblank;
252 unsigned long flags;
253
254 WARN(!dev->driver->get_vblank_timestamp,
255 "This function requires support for accurate vblank timestamps.");
256
257 spin_lock_irqsave(&dev->vblank_time_lock, flags);
258
259 drm_update_vblank_count(dev, pipe, 0);
260 vblank = drm_vblank_count(dev, pipe);
261
262 spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
263
264 return vblank;
265}
266EXPORT_SYMBOL(drm_accurate_vblank_count);
267
268
269
270
271
272
273
274static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
275{
276 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
277 unsigned long irqflags;
278
279
280
281
282
283 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
284
285
286
287
288
289
290 if (vblank->enabled) {
291 dev->driver->disable_vblank(dev, pipe);
292 vblank->enabled = false;
293 }
294
295
296
297
298
299
300
301 drm_update_vblank_count(dev, pipe, 0);
302
303 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
304}
305
306static void vblank_disable_fn(unsigned long arg)
307{
308 struct drm_vblank_crtc *vblank = (void *)arg;
309 struct drm_device *dev = vblank->dev;
310 unsigned int pipe = vblank->pipe;
311 unsigned long irqflags;
312
313 spin_lock_irqsave(&dev->vbl_lock, irqflags);
314 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
315 DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
316 vblank_disable_and_save(dev, pipe);
317 }
318 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
319}
320
321
322
323
324
325
326
327void drm_vblank_cleanup(struct drm_device *dev)
328{
329 unsigned int pipe;
330
331
332 if (dev->num_crtcs == 0)
333 return;
334
335 for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
336 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
337
338 WARN_ON(vblank->enabled &&
339 drm_core_check_feature(dev, DRIVER_MODESET));
340
341 del_timer_sync(&vblank->disable_timer);
342 }
343
344 kfree(dev->vblank);
345
346 dev->num_crtcs = 0;
347}
348EXPORT_SYMBOL(drm_vblank_cleanup);
349
350
351
352
353
354
355
356
357
358
359
360int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
361{
362 int ret = -ENOMEM;
363 unsigned int i;
364
365 spin_lock_init(&dev->vbl_lock);
366 spin_lock_init(&dev->vblank_time_lock);
367
368 dev->num_crtcs = num_crtcs;
369
370 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
371 if (!dev->vblank)
372 goto err;
373
374 for (i = 0; i < num_crtcs; i++) {
375 struct drm_vblank_crtc *vblank = &dev->vblank[i];
376
377 vblank->dev = dev;
378 vblank->pipe = i;
379 init_waitqueue_head(&vblank->queue);
380 setup_timer(&vblank->disable_timer, vblank_disable_fn,
381 (unsigned long)vblank);
382 seqlock_init(&vblank->seqlock);
383 }
384
385 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
386
387
388 if (dev->driver->get_vblank_timestamp)
389 DRM_INFO("Driver supports precise vblank timestamp query.\n");
390 else
391 DRM_INFO("No driver support for vblank timestamp query.\n");
392
393
394 if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
395 dev->vblank_disable_immediate = false;
396 DRM_INFO("Setting vblank_disable_immediate to false because "
397 "get_vblank_timestamp == NULL\n");
398 }
399
400 return 0;
401
402err:
403 dev->num_crtcs = 0;
404 return ret;
405}
406EXPORT_SYMBOL(drm_vblank_init);
407
408static void drm_irq_vgaarb_nokms(void *cookie, bool state)
409{
410 struct drm_device *dev = cookie;
411
412 if (dev->driver->vgaarb_irq) {
413 dev->driver->vgaarb_irq(dev, state);
414 return;
415 }
416
417 if (!dev->irq_enabled)
418 return;
419
420 if (state) {
421 if (dev->driver->irq_uninstall)
422 dev->driver->irq_uninstall(dev);
423 } else {
424 if (dev->driver->irq_preinstall)
425 dev->driver->irq_preinstall(dev);
426 if (dev->driver->irq_postinstall)
427 dev->driver->irq_postinstall(dev);
428 }
429}
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448int drm_irq_install(struct drm_device *dev, int irq)
449{
450 int ret;
451 unsigned long sh_flags = 0;
452
453 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
454 return -EINVAL;
455
456 if (irq == 0)
457 return -EINVAL;
458
459
460 if (!dev->dev_private)
461 return -EINVAL;
462
463 if (dev->irq_enabled)
464 return -EBUSY;
465 dev->irq_enabled = true;
466
467 DRM_DEBUG("irq=%d\n", irq);
468
469
470 if (dev->driver->irq_preinstall)
471 dev->driver->irq_preinstall(dev);
472
473
474 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
475 sh_flags = IRQF_SHARED;
476
477 ret = request_irq(irq, dev->driver->irq_handler,
478 sh_flags, dev->driver->name, dev);
479
480 if (ret < 0) {
481 dev->irq_enabled = false;
482 return ret;
483 }
484
485 if (drm_core_check_feature(dev, DRIVER_LEGACY))
486 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
487
488
489 if (dev->driver->irq_postinstall)
490 ret = dev->driver->irq_postinstall(dev);
491
492 if (ret < 0) {
493 dev->irq_enabled = false;
494 if (drm_core_check_feature(dev, DRIVER_LEGACY))
495 vga_client_register(dev->pdev, NULL, NULL, NULL);
496 free_irq(irq, dev);
497 } else {
498 dev->irq = irq;
499 }
500
501 return ret;
502}
503EXPORT_SYMBOL(drm_irq_install);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521int drm_irq_uninstall(struct drm_device *dev)
522{
523 unsigned long irqflags;
524 bool irq_enabled;
525 int i;
526
527 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
528 return -EINVAL;
529
530 irq_enabled = dev->irq_enabled;
531 dev->irq_enabled = false;
532
533
534
535
536
537
538
539 if (dev->num_crtcs) {
540 spin_lock_irqsave(&dev->vbl_lock, irqflags);
541 for (i = 0; i < dev->num_crtcs; i++) {
542 struct drm_vblank_crtc *vblank = &dev->vblank[i];
543
544 if (!vblank->enabled)
545 continue;
546
547 WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
548
549 vblank_disable_and_save(dev, i);
550 wake_up(&vblank->queue);
551 }
552 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
553 }
554
555 if (!irq_enabled)
556 return -EINVAL;
557
558 DRM_DEBUG("irq=%d\n", dev->irq);
559
560 if (drm_core_check_feature(dev, DRIVER_LEGACY))
561 vga_client_register(dev->pdev, NULL, NULL, NULL);
562
563 if (dev->driver->irq_uninstall)
564 dev->driver->irq_uninstall(dev);
565
566 free_irq(dev->irq, dev);
567
568 return 0;
569}
570EXPORT_SYMBOL(drm_irq_uninstall);
571
572
573
574
575
576
577
578
579
580
581
582
583int drm_control(struct drm_device *dev, void *data,
584 struct drm_file *file_priv)
585{
586 struct drm_control *ctl = data;
587 int ret = 0, irq;
588
589
590
591
592
593 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
594 return 0;
595 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
596 return 0;
597
598 if (WARN_ON(!dev->pdev))
599 return -EINVAL;
600
601 switch (ctl->func) {
602 case DRM_INST_HANDLER:
603 irq = dev->pdev->irq;
604
605 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
606 ctl->irq != irq)
607 return -EINVAL;
608 mutex_lock(&dev->struct_mutex);
609 ret = drm_irq_install(dev, irq);
610 mutex_unlock(&dev->struct_mutex);
611
612 return ret;
613 case DRM_UNINST_HANDLER:
614 mutex_lock(&dev->struct_mutex);
615 ret = drm_irq_uninstall(dev);
616 mutex_unlock(&dev->struct_mutex);
617
618 return ret;
619 default:
620 return -EINVAL;
621 }
622}
623
624
625
626
627
628
629
630
631
632
633
634
635void drm_calc_timestamping_constants(struct drm_crtc *crtc,
636 const struct drm_display_mode *mode)
637{
638 struct drm_device *dev = crtc->dev;
639 unsigned int pipe = drm_crtc_index(crtc);
640 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
641 int linedur_ns = 0, framedur_ns = 0;
642 int dotclock = mode->crtc_clock;
643
644 if (!dev->num_crtcs)
645 return;
646
647 if (WARN_ON(pipe >= dev->num_crtcs))
648 return;
649
650
651 if (dotclock > 0) {
652 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
653
654
655
656
657
658
659 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
660 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
661
662
663
664
665 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
666 framedur_ns /= 2;
667 } else
668 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
669 crtc->base.id);
670
671 vblank->linedur_ns = linedur_ns;
672 vblank->framedur_ns = framedur_ns;
673
674 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
675 crtc->base.id, mode->crtc_htotal,
676 mode->crtc_vtotal, mode->crtc_vdisplay);
677 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
678 crtc->base.id, dotclock, framedur_ns, linedur_ns);
679}
680EXPORT_SYMBOL(drm_calc_timestamping_constants);
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
728 unsigned int pipe,
729 int *max_error,
730 struct timeval *vblank_time,
731 unsigned flags,
732 const struct drm_display_mode *mode)
733{
734 struct timeval tv_etime;
735 ktime_t stime, etime;
736 unsigned int vbl_status;
737 int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
738 int vpos, hpos, i;
739 int delta_ns, duration_ns;
740
741 if (pipe >= dev->num_crtcs) {
742 DRM_ERROR("Invalid crtc %u\n", pipe);
743 return -EINVAL;
744 }
745
746
747 if (!dev->driver->get_scanout_position) {
748 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
749 return -EIO;
750 }
751
752
753
754
755 if (mode->crtc_clock == 0) {
756 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
757 return -EAGAIN;
758 }
759
760
761
762
763
764
765
766
767 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
768
769
770
771
772 vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
773 &vpos, &hpos,
774 &stime, &etime,
775 mode);
776
777
778 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
779 DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
780 pipe, vbl_status);
781 return -EIO;
782 }
783
784
785 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
786
787
788 if (duration_ns <= *max_error)
789 break;
790 }
791
792
793 if (i == DRM_TIMESTAMP_MAXRETRIES) {
794 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
795 pipe, duration_ns/1000, *max_error/1000, i);
796 }
797
798
799 *max_error = duration_ns;
800
801
802
803
804
805
806 if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
807 ret |= DRM_VBLANKTIME_IN_VBLANK;
808
809
810
811
812
813 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
814 mode->crtc_clock);
815
816 if (!drm_timestamp_monotonic)
817 etime = ktime_mono_to_real(etime);
818
819
820 tv_etime = ktime_to_timeval(etime);
821
822
823
824 etime = ktime_sub_ns(etime, delta_ns);
825 *vblank_time = ktime_to_timeval(etime);
826
827 DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
828 pipe, vbl_status, hpos, vpos,
829 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
830 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
831 duration_ns/1000, i);
832
833 return ret;
834}
835EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
836
837static struct timeval get_drm_timestamp(void)
838{
839 ktime_t now;
840
841 now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
842 return ktime_to_timeval(now);
843}
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865static bool
866drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
867 struct timeval *tvblank, unsigned flags)
868{
869 int ret;
870
871
872 int max_error = (int) drm_timestamp_precision * 1000;
873
874
875 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
876 ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
877 tvblank, flags);
878 if (ret > 0)
879 return true;
880 }
881
882
883
884
885 *tvblank = get_drm_timestamp();
886
887 return false;
888}
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
905{
906 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
907
908 if (WARN_ON(pipe >= dev->num_crtcs))
909 return 0;
910
911 return vblank->count;
912}
913EXPORT_SYMBOL(drm_vblank_count);
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
929{
930 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
931}
932EXPORT_SYMBOL(drm_crtc_vblank_count);
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948static u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
949 struct timeval *vblanktime)
950{
951 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
952 u32 vblank_count;
953 unsigned int seq;
954
955 if (WARN_ON(pipe >= dev->num_crtcs))
956 return 0;
957
958 do {
959 seq = read_seqbegin(&vblank->seqlock);
960 vblank_count = vblank->count;
961 *vblanktime = vblank->time;
962 } while (read_seqretry(&vblank->seqlock, seq));
963
964 return vblank_count;
965}
966
967
968
969
970
971
972
973
974
975
976
977
978u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
979 struct timeval *vblanktime)
980{
981 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
982 vblanktime);
983}
984EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
985
986static void send_vblank_event(struct drm_device *dev,
987 struct drm_pending_vblank_event *e,
988 unsigned long seq, struct timeval *now)
989{
990 e->event.sequence = seq;
991 e->event.tv_sec = now->tv_sec;
992 e->event.tv_usec = now->tv_usec;
993
994 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
995 e->event.sequence);
996
997 drm_send_event_locked(dev, &e->base);
998}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1040 struct drm_pending_vblank_event *e)
1041{
1042 struct drm_device *dev = crtc->dev;
1043 unsigned int pipe = drm_crtc_index(crtc);
1044
1045 assert_spin_locked(&dev->event_lock);
1046
1047 e->pipe = pipe;
1048 e->event.sequence = drm_vblank_count(dev, pipe);
1049 list_add_tail(&e->base.link, &dev->vblank_event_list);
1050}
1051EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1065 struct drm_pending_vblank_event *e)
1066{
1067 struct drm_device *dev = crtc->dev;
1068 unsigned int seq, pipe = drm_crtc_index(crtc);
1069 struct timeval now;
1070
1071 if (dev->num_crtcs > 0) {
1072 seq = drm_vblank_count_and_time(dev, pipe, &now);
1073 } else {
1074 seq = 0;
1075
1076 now = get_drm_timestamp();
1077 }
1078 e->pipe = pipe;
1079 send_vblank_event(dev, e, seq, &now);
1080}
1081EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1092{
1093 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1094 int ret = 0;
1095
1096 assert_spin_locked(&dev->vbl_lock);
1097
1098 spin_lock(&dev->vblank_time_lock);
1099
1100 if (!vblank->enabled) {
1101
1102
1103
1104
1105
1106
1107
1108 ret = dev->driver->enable_vblank(dev, pipe);
1109 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
1110 if (ret)
1111 atomic_dec(&vblank->refcount);
1112 else {
1113 vblank->enabled = true;
1114 drm_update_vblank_count(dev, pipe, 0);
1115 }
1116 }
1117
1118 spin_unlock(&dev->vblank_time_lock);
1119
1120 return ret;
1121}
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
1137{
1138 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1139 unsigned long irqflags;
1140 int ret = 0;
1141
1142 if (!dev->num_crtcs)
1143 return -EINVAL;
1144
1145 if (WARN_ON(pipe >= dev->num_crtcs))
1146 return -EINVAL;
1147
1148 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1149
1150 if (atomic_add_return(1, &vblank->refcount) == 1) {
1151 ret = drm_vblank_enable(dev, pipe);
1152 } else {
1153 if (!vblank->enabled) {
1154 atomic_dec(&vblank->refcount);
1155 ret = -EINVAL;
1156 }
1157 }
1158 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1159
1160 return ret;
1161}
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173int drm_crtc_vblank_get(struct drm_crtc *crtc)
1174{
1175 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1176}
1177EXPORT_SYMBOL(drm_crtc_vblank_get);
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1190{
1191 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1192
1193 if (WARN_ON(pipe >= dev->num_crtcs))
1194 return;
1195
1196 if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1197 return;
1198
1199
1200 if (atomic_dec_and_test(&vblank->refcount)) {
1201 if (drm_vblank_offdelay == 0)
1202 return;
1203 else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
1204 vblank_disable_fn((unsigned long)vblank);
1205 else
1206 mod_timer(&vblank->disable_timer,
1207 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1208 }
1209}
1210
1211
1212
1213
1214
1215
1216
1217
1218void drm_crtc_vblank_put(struct drm_crtc *crtc)
1219{
1220 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1221}
1222EXPORT_SYMBOL(drm_crtc_vblank_put);
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1234{
1235 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1236 int ret;
1237 u32 last;
1238
1239 if (WARN_ON(pipe >= dev->num_crtcs))
1240 return;
1241
1242 ret = drm_vblank_get(dev, pipe);
1243 if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1244 return;
1245
1246 last = drm_vblank_count(dev, pipe);
1247
1248 ret = wait_event_timeout(vblank->queue,
1249 last != drm_vblank_count(dev, pipe),
1250 msecs_to_jiffies(100));
1251
1252 WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1253
1254 drm_vblank_put(dev, pipe);
1255}
1256EXPORT_SYMBOL(drm_wait_one_vblank);
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1267{
1268 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1269}
1270EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
1287{
1288 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1289 struct drm_pending_vblank_event *e, *t;
1290 struct timeval now;
1291 unsigned long irqflags;
1292 unsigned int seq;
1293
1294 if (WARN_ON(pipe >= dev->num_crtcs))
1295 return;
1296
1297 spin_lock_irqsave(&dev->event_lock, irqflags);
1298
1299 spin_lock(&dev->vbl_lock);
1300 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1301 pipe, vblank->enabled, vblank->inmodeset);
1302
1303
1304 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1305 vblank_disable_and_save(dev, pipe);
1306
1307 wake_up(&vblank->queue);
1308
1309
1310
1311
1312
1313 if (!vblank->inmodeset) {
1314 atomic_inc(&vblank->refcount);
1315 vblank->inmodeset = 1;
1316 }
1317 spin_unlock(&dev->vbl_lock);
1318
1319
1320 seq = drm_vblank_count_and_time(dev, pipe, &now);
1321
1322 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1323 if (e->pipe != pipe)
1324 continue;
1325 DRM_DEBUG("Sending premature vblank event on disable: "
1326 "wanted %u, current %u\n",
1327 e->event.sequence, seq);
1328 list_del(&e->base.link);
1329 drm_vblank_put(dev, pipe);
1330 send_vblank_event(dev, e, seq, &now);
1331 }
1332 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1333}
1334EXPORT_SYMBOL(drm_vblank_off);
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349void drm_crtc_vblank_off(struct drm_crtc *crtc)
1350{
1351 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1352}
1353EXPORT_SYMBOL(drm_crtc_vblank_off);
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1366{
1367 struct drm_device *dev = crtc->dev;
1368 unsigned long irqflags;
1369 unsigned int pipe = drm_crtc_index(crtc);
1370 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1371
1372 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1373
1374
1375
1376
1377 if (!vblank->inmodeset) {
1378 atomic_inc(&vblank->refcount);
1379 vblank->inmodeset = 1;
1380 }
1381 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1382
1383 WARN_ON(!list_empty(&dev->vblank_event_list));
1384}
1385EXPORT_SYMBOL(drm_crtc_vblank_reset);
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
1400{
1401 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1402 unsigned long irqflags;
1403
1404 if (WARN_ON(pipe >= dev->num_crtcs))
1405 return;
1406
1407 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1408 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1409 pipe, vblank->enabled, vblank->inmodeset);
1410
1411
1412 if (vblank->inmodeset) {
1413 atomic_dec(&vblank->refcount);
1414 vblank->inmodeset = 0;
1415 }
1416
1417 drm_reset_vblank_timestamp(dev, pipe);
1418
1419
1420
1421
1422
1423 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1424 WARN_ON(drm_vblank_enable(dev, pipe));
1425 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1426}
1427EXPORT_SYMBOL(drm_vblank_on);
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440void drm_crtc_vblank_on(struct drm_crtc *crtc)
1441{
1442 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1443}
1444EXPORT_SYMBOL(drm_crtc_vblank_on);
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
1470{
1471 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1472
1473
1474 if (!dev->num_crtcs)
1475 return;
1476
1477 if (WARN_ON(pipe >= dev->num_crtcs))
1478 return;
1479
1480
1481
1482
1483
1484
1485
1486
1487 if (!vblank->inmodeset) {
1488 vblank->inmodeset = 0x1;
1489 if (drm_vblank_get(dev, pipe) == 0)
1490 vblank->inmodeset |= 0x2;
1491 }
1492}
1493EXPORT_SYMBOL(drm_vblank_pre_modeset);
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
1504{
1505 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1506 unsigned long irqflags;
1507
1508
1509 if (!dev->num_crtcs)
1510 return;
1511
1512 if (WARN_ON(pipe >= dev->num_crtcs))
1513 return;
1514
1515 if (vblank->inmodeset) {
1516 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1517 drm_reset_vblank_timestamp(dev, pipe);
1518 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1519
1520 if (vblank->inmodeset & 0x2)
1521 drm_vblank_put(dev, pipe);
1522
1523 vblank->inmodeset = 0;
1524 }
1525}
1526EXPORT_SYMBOL(drm_vblank_post_modeset);
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539int drm_modeset_ctl(struct drm_device *dev, void *data,
1540 struct drm_file *file_priv)
1541{
1542 struct drm_modeset_ctl *modeset = data;
1543 unsigned int pipe;
1544
1545
1546 if (!dev->num_crtcs)
1547 return 0;
1548
1549
1550 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1551 return 0;
1552
1553 pipe = modeset->crtc;
1554 if (pipe >= dev->num_crtcs)
1555 return -EINVAL;
1556
1557 switch (modeset->cmd) {
1558 case _DRM_PRE_MODESET:
1559 drm_vblank_pre_modeset(dev, pipe);
1560 break;
1561 case _DRM_POST_MODESET:
1562 drm_vblank_post_modeset(dev, pipe);
1563 break;
1564 default:
1565 return -EINVAL;
1566 }
1567
1568 return 0;
1569}
1570
1571static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1572 union drm_wait_vblank *vblwait,
1573 struct drm_file *file_priv)
1574{
1575 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1576 struct drm_pending_vblank_event *e;
1577 struct timeval now;
1578 unsigned long flags;
1579 unsigned int seq;
1580 int ret;
1581
1582 e = kzalloc(sizeof(*e), GFP_KERNEL);
1583 if (e == NULL) {
1584 ret = -ENOMEM;
1585 goto err_put;
1586 }
1587
1588 e->pipe = pipe;
1589 e->base.pid = current->pid;
1590 e->event.base.type = DRM_EVENT_VBLANK;
1591 e->event.base.length = sizeof(e->event);
1592 e->event.user_data = vblwait->request.signal;
1593
1594 spin_lock_irqsave(&dev->event_lock, flags);
1595
1596
1597
1598
1599
1600
1601
1602
1603 if (!vblank->enabled) {
1604 ret = -EINVAL;
1605 goto err_unlock;
1606 }
1607
1608 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1609 &e->event.base);
1610
1611 if (ret)
1612 goto err_unlock;
1613
1614 seq = drm_vblank_count_and_time(dev, pipe, &now);
1615
1616 DRM_DEBUG("event on vblank count %u, current %u, crtc %u\n",
1617 vblwait->request.sequence, seq, pipe);
1618
1619 trace_drm_vblank_event_queued(current->pid, pipe,
1620 vblwait->request.sequence);
1621
1622 e->event.sequence = vblwait->request.sequence;
1623 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1624 drm_vblank_put(dev, pipe);
1625 send_vblank_event(dev, e, seq, &now);
1626 vblwait->reply.sequence = seq;
1627 } else {
1628
1629 list_add_tail(&e->base.link, &dev->vblank_event_list);
1630 vblwait->reply.sequence = vblwait->request.sequence;
1631 }
1632
1633 spin_unlock_irqrestore(&dev->event_lock, flags);
1634
1635 return 0;
1636
1637err_unlock:
1638 spin_unlock_irqrestore(&dev->event_lock, flags);
1639 kfree(e);
1640err_put:
1641 drm_vblank_put(dev, pipe);
1642 return ret;
1643}
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659int drm_wait_vblank(struct drm_device *dev, void *data,
1660 struct drm_file *file_priv)
1661{
1662 struct drm_vblank_crtc *vblank;
1663 union drm_wait_vblank *vblwait = data;
1664 int ret;
1665 unsigned int flags, seq, pipe, high_pipe;
1666
1667 if (!dev->irq_enabled)
1668 return -EINVAL;
1669
1670 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1671 return -EINVAL;
1672
1673 if (vblwait->request.type &
1674 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1675 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1676 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1677 vblwait->request.type,
1678 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1679 _DRM_VBLANK_HIGH_CRTC_MASK));
1680 return -EINVAL;
1681 }
1682
1683 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1684 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1685 if (high_pipe)
1686 pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1687 else
1688 pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1689 if (pipe >= dev->num_crtcs)
1690 return -EINVAL;
1691
1692 vblank = &dev->vblank[pipe];
1693
1694 ret = drm_vblank_get(dev, pipe);
1695 if (ret) {
1696 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1697 return ret;
1698 }
1699 seq = drm_vblank_count(dev, pipe);
1700
1701 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1702 case _DRM_VBLANK_RELATIVE:
1703 vblwait->request.sequence += seq;
1704 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1705 case _DRM_VBLANK_ABSOLUTE:
1706 break;
1707 default:
1708 ret = -EINVAL;
1709 goto done;
1710 }
1711
1712 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1713 (seq - vblwait->request.sequence) <= (1 << 23)) {
1714 vblwait->request.sequence = seq + 1;
1715 }
1716
1717 if (flags & _DRM_VBLANK_EVENT) {
1718
1719
1720
1721 return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
1722 }
1723
1724 DRM_DEBUG("waiting on vblank count %u, crtc %u\n",
1725 vblwait->request.sequence, pipe);
1726 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1727 (((drm_vblank_count(dev, pipe) -
1728 vblwait->request.sequence) <= (1 << 23)) ||
1729 !vblank->enabled ||
1730 !dev->irq_enabled));
1731
1732 if (ret != -EINTR) {
1733 struct timeval now;
1734
1735 vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
1736 vblwait->reply.tval_sec = now.tv_sec;
1737 vblwait->reply.tval_usec = now.tv_usec;
1738
1739 DRM_DEBUG("returning %u to client\n",
1740 vblwait->reply.sequence);
1741 } else {
1742 DRM_DEBUG("vblank wait interrupted by signal\n");
1743 }
1744
1745done:
1746 drm_vblank_put(dev, pipe);
1747 return ret;
1748}
1749
1750static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1751{
1752 struct drm_pending_vblank_event *e, *t;
1753 struct timeval now;
1754 unsigned int seq;
1755
1756 assert_spin_locked(&dev->event_lock);
1757
1758 seq = drm_vblank_count_and_time(dev, pipe, &now);
1759
1760 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1761 if (e->pipe != pipe)
1762 continue;
1763 if ((seq - e->event.sequence) > (1<<23))
1764 continue;
1765
1766 DRM_DEBUG("vblank event on %u, current %u\n",
1767 e->event.sequence, seq);
1768
1769 list_del(&e->base.link);
1770 drm_vblank_put(dev, pipe);
1771 send_vblank_event(dev, e, seq, &now);
1772 }
1773
1774 trace_drm_vblank_event(pipe, seq);
1775}
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1788{
1789 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1790 unsigned long irqflags;
1791
1792 if (WARN_ON_ONCE(!dev->num_crtcs))
1793 return false;
1794
1795 if (WARN_ON(pipe >= dev->num_crtcs))
1796 return false;
1797
1798 spin_lock_irqsave(&dev->event_lock, irqflags);
1799
1800
1801
1802
1803
1804 spin_lock(&dev->vblank_time_lock);
1805
1806
1807 if (!vblank->enabled) {
1808 spin_unlock(&dev->vblank_time_lock);
1809 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1810 return false;
1811 }
1812
1813 drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
1814
1815 spin_unlock(&dev->vblank_time_lock);
1816
1817 wake_up(&vblank->queue);
1818 drm_handle_vblank_events(dev, pipe);
1819
1820 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1821
1822 return true;
1823}
1824EXPORT_SYMBOL(drm_handle_vblank);
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1839{
1840 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1841}
1842EXPORT_SYMBOL(drm_crtc_handle_vblank);
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
1856{
1857 WARN_ON_ONCE(dev->max_vblank_count != 0);
1858 return 0;
1859}
1860EXPORT_SYMBOL(drm_vblank_no_hw_counter);
1861