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