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