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