forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/gpu/drm/drm_vblank.c
....@@ -24,15 +24,86 @@
2424 * OTHER DEALINGS IN THE SOFTWARE.
2525 */
2626
27
-#include <drm/drm_vblank.h>
28
-#include <drm/drmP.h>
2927 #include <linux/export.h>
28
+#include <linux/kthread.h>
29
+#include <linux/moduleparam.h>
3030
31
-#include "drm_trace.h"
31
+#include <drm/drm_crtc.h>
32
+#include <drm/drm_drv.h>
33
+#include <drm/drm_framebuffer.h>
34
+#include <drm/drm_managed.h>
35
+#include <drm/drm_modeset_helper_vtables.h>
36
+#include <drm/drm_print.h>
37
+#include <drm/drm_vblank.h>
38
+
3239 #include "drm_internal.h"
40
+#include "drm_trace.h"
3341
3442 /**
3543 * DOC: vblank handling
44
+ *
45
+ * From the computer's perspective, every time the monitor displays
46
+ * a new frame the scanout engine has "scanned out" the display image
47
+ * from top to bottom, one row of pixels at a time. The current row
48
+ * of pixels is referred to as the current scanline.
49
+ *
50
+ * In addition to the display's visible area, there's usually a couple of
51
+ * extra scanlines which aren't actually displayed on the screen.
52
+ * These extra scanlines don't contain image data and are occasionally used
53
+ * for features like audio and infoframes. The region made up of these
54
+ * scanlines is referred to as the vertical blanking region, or vblank for
55
+ * short.
56
+ *
57
+ * For historical reference, the vertical blanking period was designed to
58
+ * give the electron gun (on CRTs) enough time to move back to the top of
59
+ * the screen to start scanning out the next frame. Similar for horizontal
60
+ * blanking periods. They were designed to give the electron gun enough
61
+ * time to move back to the other side of the screen to start scanning the
62
+ * next scanline.
63
+ *
64
+ * ::
65
+ *
66
+ *
67
+ * physical → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
68
+ * top of | |
69
+ * display | |
70
+ * | New frame |
71
+ * | |
72
+ * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓|
73
+ * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| ← Scanline,
74
+ * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓| updates the
75
+ * | | frame as it
76
+ * | | travels down
77
+ * | | ("sacn out")
78
+ * | Old frame |
79
+ * | |
80
+ * | |
81
+ * | |
82
+ * | | physical
83
+ * | | bottom of
84
+ * vertical |⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽| ← display
85
+ * blanking ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
86
+ * region → ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
87
+ * ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
88
+ * start of → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
89
+ * new frame
90
+ *
91
+ * "Physical top of display" is the reference point for the high-precision/
92
+ * corrected timestamp.
93
+ *
94
+ * On a lot of display hardware, programming needs to take effect during the
95
+ * vertical blanking period so that settings like gamma, the image buffer
96
+ * buffer to be scanned out, etc. can safely be changed without showing
97
+ * any visual artifacts on the screen. In some unforgiving hardware, some of
98
+ * this programming has to both start and end in the same vblank. To help
99
+ * with the timing of the hardware programming, an interrupt is usually
100
+ * available to notify the driver when it can start the updating of registers.
101
+ * The interrupt is in this context named the vblank interrupt.
102
+ *
103
+ * The vblank interrupt may be fired at different points depending on the
104
+ * hardware. Some hardware implementations will fire the interrupt when the
105
+ * new frame start, other implementations will fire the interrupt at different
106
+ * points in time.
36107 *
37108 * Vertical blanking plays a major role in graphics rendering. To achieve
38109 * tear-free display, users must synchronize page flips and/or rendering to
....@@ -48,7 +119,7 @@
48119 * Drivers must initialize the vertical blanking handling core with a call to
49120 * drm_vblank_init(). Minimally, a driver needs to implement
50121 * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
51
- * drm_crtc_handle_vblank() in it's vblank interrupt handler for working vblank
122
+ * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
52123 * support.
53124 *
54125 * Vertical blanking interrupts can be enabled by the DRM core or by drivers
....@@ -64,6 +135,12 @@
64135 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
65136 * vblanks after a timer has expired, which can be configured through the
66137 * ``vblankoffdelay`` module parameter.
138
+ *
139
+ * Drivers for hardware without support for vertical-blanking interrupts
140
+ * must not call drm_vblank_init(). For such drivers, atomic helpers will
141
+ * automatically generate fake vblank events as part of the display update.
142
+ * This functionality also can be controlled by the driver by enabling and
143
+ * disabling struct drm_crtc_state.no_vblank.
67144 */
68145
69146 /* Retry timestamp calculation up to 3 times to satisfy
....@@ -101,7 +178,7 @@
101178
102179 write_seqlock(&vblank->seqlock);
103180 vblank->time = t_vblank;
104
- vblank->count += vblank_count_inc;
181
+ atomic64_add(vblank_count_inc, &vblank->count);
105182 write_sequnlock(&vblank->seqlock);
106183 }
107184
....@@ -118,7 +195,7 @@
118195 */
119196 static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
120197 {
121
- WARN_ON_ONCE(drm_max_vblank_count(dev, pipe) != 0);
198
+ drm_WARN_ON_ONCE(dev, drm_max_vblank_count(dev, pipe) != 0);
122199 return 0;
123200 }
124201
....@@ -127,15 +204,14 @@
127204 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
128205 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
129206
130
- if (WARN_ON(!crtc))
207
+ if (drm_WARN_ON(dev, !crtc))
131208 return 0;
132209
133210 if (crtc->funcs->get_vblank_counter)
134211 return crtc->funcs->get_vblank_counter(crtc);
135
- }
136
-
137
- if (dev->driver->get_vblank_counter)
212
+ } else if (dev->driver->get_vblank_counter) {
138213 return dev->driver->get_vblank_counter(dev, pipe);
214
+ }
139215
140216 return drm_vblank_no_hw_counter(dev, pipe);
141217 }
....@@ -235,12 +311,16 @@
235311 * on the difference in the timestamps and the
236312 * frame/field duration.
237313 */
314
+
315
+ drm_dbg_vbl(dev, "crtc %u: Calculating number of vblanks."
316
+ " diff_ns = %lld, framedur_ns = %d)\n",
317
+ pipe, (long long)diff_ns, framedur_ns);
318
+
238319 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
239320
240321 if (diff == 0 && in_vblank_irq)
241
- DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
242
- " diff_ns = %lld, framedur_ns = %d)\n",
243
- pipe, (long long) diff_ns, framedur_ns);
322
+ drm_dbg_vbl(dev, "crtc %u: Redundant vblirq ignored\n",
323
+ pipe);
244324 } else {
245325 /* some kind of default for drivers w/o accurate vbl timestamping */
246326 diff = in_vblank_irq ? 1 : 0;
....@@ -256,17 +336,19 @@
256336 * random large forward jumps of the software vblank counter.
257337 */
258338 if (diff > 1 && (vblank->inmodeset & 0x2)) {
259
- DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
260
- " due to pre-modeset.\n", pipe, diff);
339
+ drm_dbg_vbl(dev,
340
+ "clamping vblank bump to 1 on crtc %u: diffr=%u"
341
+ " due to pre-modeset.\n", pipe, diff);
261342 diff = 1;
262343 }
263344
264
- DRM_DEBUG_VBL("updating vblank count on crtc %u:"
265
- " current=%llu, diff=%u, hw=%u hw_last=%u\n",
266
- pipe, vblank->count, diff, cur_vblank, vblank->last);
345
+ drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
346
+ " current=%llu, diff=%u, hw=%u hw_last=%u\n",
347
+ pipe, (unsigned long long)atomic64_read(&vblank->count),
348
+ diff, cur_vblank, vblank->last);
267349
268350 if (diff == 0) {
269
- WARN_ON_ONCE(cur_vblank != vblank->last);
351
+ drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
270352 return;
271353 }
272354
....@@ -282,14 +364,26 @@
282364 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
283365 }
284366
285
-static u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
367
+u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
286368 {
287369 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
370
+ u64 count;
288371
289
- if (WARN_ON(pipe >= dev->num_crtcs))
372
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
290373 return 0;
291374
292
- return vblank->count;
375
+ count = atomic64_read(&vblank->count);
376
+
377
+ /*
378
+ * This read barrier corresponds to the implicit write barrier of the
379
+ * write seqlock in store_vblank(). Note that this is the only place
380
+ * where we need an explicit barrier, since all other access goes
381
+ * through drm_vblank_count_and_time(), which already has the required
382
+ * read barrier curtesy of the read seqlock.
383
+ */
384
+ smp_rmb();
385
+
386
+ return count;
293387 }
294388
295389 /**
....@@ -310,8 +404,9 @@
310404 u64 vblank;
311405 unsigned long flags;
312406
313
- WARN_ONCE(drm_debug & DRM_UT_VBL && !dev->driver->get_vblank_timestamp,
314
- "This function requires support for accurate vblank timestamps.");
407
+ drm_WARN_ONCE(dev, drm_debug_enabled(DRM_UT_VBL) &&
408
+ !crtc->funcs->get_vblank_timestamp,
409
+ "This function requires support for accurate vblank timestamps.");
315410
316411 spin_lock_irqsave(&dev->vblank_time_lock, flags);
317412
....@@ -329,16 +424,14 @@
329424 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
330425 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
331426
332
- if (WARN_ON(!crtc))
427
+ if (drm_WARN_ON(dev, !crtc))
333428 return;
334429
335
- if (crtc->funcs->disable_vblank) {
430
+ if (crtc->funcs->disable_vblank)
336431 crtc->funcs->disable_vblank(crtc);
337
- return;
338
- }
432
+ } else {
433
+ dev->driver->disable_vblank(dev, pipe);
339434 }
340
-
341
- dev->driver->disable_vblank(dev, pipe);
342435 }
343436
344437 /*
....@@ -392,32 +485,21 @@
392485
393486 spin_lock_irqsave(&dev->vbl_lock, irqflags);
394487 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
395
- DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
488
+ drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
396489 drm_vblank_disable_and_save(dev, pipe);
397490 }
398491 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
399492 }
400493
401
-void drm_vblank_cleanup(struct drm_device *dev)
494
+static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
402495 {
403
- unsigned int pipe;
496
+ struct drm_vblank_crtc *vblank = ptr;
404497
405
- /* Bail if the driver didn't call drm_vblank_init() */
406
- if (dev->num_crtcs == 0)
407
- return;
498
+ drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
499
+ drm_core_check_feature(dev, DRIVER_MODESET));
408500
409
- for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
410
- struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
411
-
412
- WARN_ON(READ_ONCE(vblank->enabled) &&
413
- drm_core_check_feature(dev, DRIVER_MODESET));
414
-
415
- del_timer_sync(&vblank->disable_timer);
416
- }
417
-
418
- kfree(dev->vblank);
419
-
420
- dev->num_crtcs = 0;
501
+ drm_vblank_destroy_worker(vblank);
502
+ del_timer_sync(&vblank->disable_timer);
421503 }
422504
423505 /**
....@@ -426,25 +508,25 @@
426508 * @num_crtcs: number of CRTCs supported by @dev
427509 *
428510 * This function initializes vblank support for @num_crtcs display pipelines.
429
- * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
430
- * drivers with a &drm_driver.release callback.
511
+ * Cleanup is handled automatically through a cleanup function added with
512
+ * drmm_add_action_or_reset().
431513 *
432514 * Returns:
433515 * Zero on success or a negative error code on failure.
434516 */
435517 int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
436518 {
437
- int ret = -ENOMEM;
519
+ int ret;
438520 unsigned int i;
439521
440522 spin_lock_init(&dev->vbl_lock);
441523 spin_lock_init(&dev->vblank_time_lock);
442524
443
- dev->num_crtcs = num_crtcs;
444
-
445
- dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
525
+ dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
446526 if (!dev->vblank)
447
- goto err;
527
+ return -ENOMEM;
528
+
529
+ dev->num_crtcs = num_crtcs;
448530
449531 for (i = 0; i < num_crtcs; i++) {
450532 struct drm_vblank_crtc *vblank = &dev->vblank[i];
....@@ -454,30 +536,42 @@
454536 init_waitqueue_head(&vblank->queue);
455537 timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
456538 seqlock_init(&vblank->seqlock);
457
- }
458539
459
- DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
540
+ ret = drmm_add_action_or_reset(dev, drm_vblank_init_release,
541
+ vblank);
542
+ if (ret)
543
+ return ret;
460544
461
- /* Driver specific high-precision vblank timestamping supported? */
462
- if (dev->driver->get_vblank_timestamp)
463
- DRM_INFO("Driver supports precise vblank timestamp query.\n");
464
- else
465
- DRM_INFO("No driver support for vblank timestamp query.\n");
466
-
467
- /* Must have precise timestamping for reliable vblank instant disable */
468
- if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
469
- dev->vblank_disable_immediate = false;
470
- DRM_INFO("Setting vblank_disable_immediate to false because "
471
- "get_vblank_timestamp == NULL\n");
545
+ ret = drm_vblank_worker_init(vblank);
546
+ if (ret)
547
+ return ret;
472548 }
473549
474550 return 0;
475
-
476
-err:
477
- dev->num_crtcs = 0;
478
- return ret;
479551 }
480552 EXPORT_SYMBOL(drm_vblank_init);
553
+
554
+/**
555
+ * drm_dev_has_vblank - test if vblanking has been initialized for
556
+ * a device
557
+ * @dev: the device
558
+ *
559
+ * Drivers may call this function to test if vblank support is
560
+ * initialized for a device. For most hardware this means that vblanking
561
+ * can also be enabled.
562
+ *
563
+ * Atomic helpers use this function to initialize
564
+ * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
565
+ *
566
+ * Returns:
567
+ * True if vblanking has been initialized for the given device, false
568
+ * otherwise.
569
+ */
570
+bool drm_dev_has_vblank(const struct drm_device *dev)
571
+{
572
+ return dev->num_crtcs != 0;
573
+}
574
+EXPORT_SYMBOL(drm_dev_has_vblank);
481575
482576 /**
483577 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
....@@ -501,9 +595,9 @@
501595 *
502596 * Calculate and store various constants which are later needed by vblank and
503597 * swap-completion timestamping, e.g, by
504
- * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
505
- * scanout timing, so they take things like panel scaling or other adjustments
506
- * into account.
598
+ * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
599
+ * CRTC's true scanout timing, so they take things like panel scaling or
600
+ * other adjustments into account.
507601 */
508602 void drm_calc_timestamping_constants(struct drm_crtc *crtc,
509603 const struct drm_display_mode *mode)
....@@ -514,10 +608,10 @@
514608 int linedur_ns = 0, framedur_ns = 0;
515609 int dotclock = mode->crtc_clock;
516610
517
- if (!dev->num_crtcs)
611
+ if (!drm_dev_has_vblank(dev))
518612 return;
519613
520
- if (WARN_ON(pipe >= dev->num_crtcs))
614
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
521615 return;
522616
523617 /* Valid dotclock? */
....@@ -537,26 +631,28 @@
537631 */
538632 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
539633 framedur_ns /= 2;
540
- } else
541
- DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
542
- crtc->base.id);
634
+ } else {
635
+ drm_err(dev, "crtc %u: Can't calculate constants, dotclock = 0!\n",
636
+ crtc->base.id);
637
+ }
543638
544639 vblank->linedur_ns = linedur_ns;
545640 vblank->framedur_ns = framedur_ns;
546641 vblank->hwmode = *mode;
547642
548
- DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
549
- crtc->base.id, mode->crtc_htotal,
550
- mode->crtc_vtotal, mode->crtc_vdisplay);
551
- DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
552
- crtc->base.id, dotclock, framedur_ns, linedur_ns);
643
+ drm_dbg_core(dev,
644
+ "crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
645
+ crtc->base.id, mode->crtc_htotal,
646
+ mode->crtc_vtotal, mode->crtc_vdisplay);
647
+ drm_dbg_core(dev, "crtc %u: clock %d kHz framedur %d linedur %d\n",
648
+ crtc->base.id, dotclock, framedur_ns, linedur_ns);
553649 }
554650 EXPORT_SYMBOL(drm_calc_timestamping_constants);
555651
556652 /**
557
- * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
558
- * @dev: DRM device
559
- * @pipe: index of CRTC whose vblank timestamp to retrieve
653
+ * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
654
+ * timestamp helper
655
+ * @crtc: CRTC whose vblank timestamp to retrieve
560656 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
561657 * On return contains true maximum error of timestamp
562658 * @vblank_time: Pointer to time which should receive the timestamp
....@@ -564,11 +660,12 @@
564660 * True when called from drm_crtc_handle_vblank(). Some drivers
565661 * need to apply some workarounds for gpu-specific vblank irq quirks
566662 * if flag is set.
663
+ * @get_scanout_position:
664
+ * Callback function to retrieve the scanout position. See
665
+ * @struct drm_crtc_helper_funcs.get_scanout_position.
567666 *
568667 * Implements calculation of exact vblank timestamps from given drm_display_mode
569
- * timings and current video scanout position of a CRTC. This can be directly
570
- * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
571
- * if &drm_driver.get_scanout_position is implemented.
668
+ * timings and current video scanout position of a CRTC.
572669 *
573670 * The current implementation only handles standard video modes. For double scan
574671 * and interlaced modes the driver is supposed to adjust the hardware mode
....@@ -577,41 +674,37 @@
577674 *
578675 * Note that atomic drivers must call drm_calc_timestamping_constants() before
579676 * enabling a CRTC. The atomic helpers already take care of that in
580
- * drm_atomic_helper_update_legacy_modeset_state().
677
+ * drm_atomic_helper_calc_timestamping_constants().
581678 *
582679 * Returns:
583680 *
584681 * Returns true on success, and false on failure, i.e. when no accurate
585682 * timestamp could be acquired.
586683 */
587
-bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
588
- unsigned int pipe,
589
- int *max_error,
590
- ktime_t *vblank_time,
591
- bool in_vblank_irq)
684
+bool
685
+drm_crtc_vblank_helper_get_vblank_timestamp_internal(
686
+ struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
687
+ bool in_vblank_irq,
688
+ drm_vblank_get_scanout_position_func get_scanout_position)
592689 {
690
+ struct drm_device *dev = crtc->dev;
691
+ unsigned int pipe = crtc->index;
692
+ struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
593693 struct timespec64 ts_etime, ts_vblank_time;
594694 ktime_t stime, etime;
595695 bool vbl_status;
596
- struct drm_crtc *crtc;
597696 const struct drm_display_mode *mode;
598
- struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
599697 int vpos, hpos, i;
600698 int delta_ns, duration_ns;
601699
602
- if (!drm_core_check_feature(dev, DRIVER_MODESET))
603
- return false;
604
-
605
- crtc = drm_crtc_from_index(dev, pipe);
606
-
607
- if (pipe >= dev->num_crtcs || !crtc) {
608
- DRM_ERROR("Invalid crtc %u\n", pipe);
700
+ if (pipe >= dev->num_crtcs) {
701
+ drm_err(dev, "Invalid crtc %u\n", pipe);
609702 return false;
610703 }
611704
612705 /* Scanout position query not supported? Should not happen. */
613
- if (!dev->driver->get_scanout_position) {
614
- DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
706
+ if (!get_scanout_position) {
707
+ drm_err(dev, "Called from CRTC w/o get_scanout_position()!?\n");
615708 return false;
616709 }
617710
....@@ -624,9 +717,9 @@
624717 * Happens during initial modesetting of a crtc.
625718 */
626719 if (mode->crtc_clock == 0) {
627
- DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
628
- WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
629
-
720
+ drm_dbg_core(dev, "crtc %u: Noop due to uninitialized mode.\n",
721
+ pipe);
722
+ drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
630723 return false;
631724 }
632725
....@@ -642,16 +735,16 @@
642735 * Get vertical and horizontal scanout position vpos, hpos,
643736 * and bounding timestamps stime, etime, pre/post query.
644737 */
645
- vbl_status = dev->driver->get_scanout_position(dev, pipe,
646
- in_vblank_irq,
647
- &vpos, &hpos,
648
- &stime, &etime,
649
- mode);
738
+ vbl_status = get_scanout_position(crtc, in_vblank_irq,
739
+ &vpos, &hpos,
740
+ &stime, &etime,
741
+ mode);
650742
651743 /* Return as no-op if scanout query unsupported or failed. */
652744 if (!vbl_status) {
653
- DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
654
- pipe);
745
+ drm_dbg_core(dev,
746
+ "crtc %u : scanoutpos query failed.\n",
747
+ pipe);
655748 return false;
656749 }
657750
....@@ -665,8 +758,9 @@
665758
666759 /* Noisy system timing? */
667760 if (i == DRM_TIMESTAMP_MAXRETRIES) {
668
- DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
669
- pipe, duration_ns/1000, *max_error/1000, i);
761
+ drm_dbg_core(dev,
762
+ "crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
763
+ pipe, duration_ns / 1000, *max_error / 1000, i);
670764 }
671765
672766 /* Return upper bound of timestamp precision error. */
....@@ -684,21 +778,64 @@
684778 */
685779 *vblank_time = ktime_sub_ns(etime, delta_ns);
686780
687
- if ((drm_debug & DRM_UT_VBL) == 0)
781
+ if (!drm_debug_enabled(DRM_UT_VBL))
688782 return true;
689783
690784 ts_etime = ktime_to_timespec64(etime);
691785 ts_vblank_time = ktime_to_timespec64(*vblank_time);
692786
693
- DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
694
- pipe, hpos, vpos,
695
- (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
696
- (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
697
- duration_ns / 1000, i);
787
+ drm_dbg_vbl(dev,
788
+ "crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
789
+ pipe, hpos, vpos,
790
+ (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
791
+ (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
792
+ duration_ns / 1000, i);
698793
699794 return true;
700795 }
701
-EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
796
+EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
797
+
798
+/**
799
+ * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
800
+ * helper
801
+ * @crtc: CRTC whose vblank timestamp to retrieve
802
+ * @max_error: Desired maximum allowable error in timestamps (nanosecs)
803
+ * On return contains true maximum error of timestamp
804
+ * @vblank_time: Pointer to time which should receive the timestamp
805
+ * @in_vblank_irq:
806
+ * True when called from drm_crtc_handle_vblank(). Some drivers
807
+ * need to apply some workarounds for gpu-specific vblank irq quirks
808
+ * if flag is set.
809
+ *
810
+ * Implements calculation of exact vblank timestamps from given drm_display_mode
811
+ * timings and current video scanout position of a CRTC. This can be directly
812
+ * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
813
+ * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
814
+ *
815
+ * The current implementation only handles standard video modes. For double scan
816
+ * and interlaced modes the driver is supposed to adjust the hardware mode
817
+ * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
818
+ * match the scanout position reported.
819
+ *
820
+ * Note that atomic drivers must call drm_calc_timestamping_constants() before
821
+ * enabling a CRTC. The atomic helpers already take care of that in
822
+ * drm_atomic_helper_calc_timestamping_constants().
823
+ *
824
+ * Returns:
825
+ *
826
+ * Returns true on success, and false on failure, i.e. when no accurate
827
+ * timestamp could be acquired.
828
+ */
829
+bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
830
+ int *max_error,
831
+ ktime_t *vblank_time,
832
+ bool in_vblank_irq)
833
+{
834
+ return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
835
+ crtc, max_error, vblank_time, in_vblank_irq,
836
+ crtc->helper_private->get_scanout_position);
837
+}
838
+EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
702839
703840 /**
704841 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
....@@ -725,15 +862,19 @@
725862 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
726863 ktime_t *tvblank, bool in_vblank_irq)
727864 {
865
+ struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
728866 bool ret = false;
729867
730868 /* Define requested maximum error on timestamps (nanoseconds). */
731869 int max_error = (int) drm_timestamp_precision * 1000;
732870
733871 /* Query driver if possible and precision timestamping enabled. */
734
- if (dev->driver->get_vblank_timestamp && (max_error > 0))
735
- ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
872
+ if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
873
+ struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
874
+
875
+ ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
736876 tvblank, in_vblank_irq);
877
+ }
737878
738879 /* GPU high precision timestamp query unsupported or failed.
739880 * Return current monotonic/gettimeofday timestamp as best estimate.
....@@ -753,6 +894,14 @@
753894 * modesetting activity. Note that this timer isn't correct against a racing
754895 * vblank interrupt (since it only reports the software vblank counter), see
755896 * drm_crtc_accurate_vblank_count() for such use-cases.
897
+ *
898
+ * Note that for a given vblank counter value drm_crtc_handle_vblank()
899
+ * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
900
+ * provide a barrier: Any writes done before calling
901
+ * drm_crtc_handle_vblank() will be visible to callers of the later
902
+ * functions, iff the vblank count is the same or a later one.
903
+ *
904
+ * See also &drm_vblank_crtc.count.
756905 *
757906 * Returns:
758907 * The software vblank counter.
....@@ -784,14 +933,14 @@
784933 u64 vblank_count;
785934 unsigned int seq;
786935
787
- if (WARN_ON(pipe >= dev->num_crtcs)) {
936
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs)) {
788937 *vblanktime = 0;
789938 return 0;
790939 }
791940
792941 do {
793942 seq = read_seqbegin(&vblank->seqlock);
794
- vblank_count = vblank->count;
943
+ vblank_count = atomic64_read(&vblank->count);
795944 *vblanktime = vblank->time;
796945 } while (read_seqretry(&vblank->seqlock, seq));
797946
....@@ -808,6 +957,14 @@
808957 * vblank events since the system was booted, including lost events due to
809958 * modesetting activity. Returns corresponding system timestamp of the time
810959 * of the vblank interval that corresponds to the current vblank counter value.
960
+ *
961
+ * Note that for a given vblank counter value drm_crtc_handle_vblank()
962
+ * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
963
+ * provide a barrier: Any writes done before calling
964
+ * drm_crtc_handle_vblank() will be visible to callers of the later
965
+ * functions, iff the vblank count is the same or a later one.
966
+ *
967
+ * See also &drm_vblank_crtc.count.
811968 */
812969 u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
813970 ktime_t *vblanktime)
....@@ -843,7 +1000,14 @@
8431000 break;
8441001 }
8451002 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
846
- drm_send_event_locked(dev, &e->base);
1003
+ /*
1004
+ * Use the same timestamp for any associated fence signal to avoid
1005
+ * mismatch in timestamps for vsync & fence events triggered by the
1006
+ * same HW event. Frameworks like SurfaceFlinger in Android expects the
1007
+ * retire-fence timestamp to match exactly with HW vsync as it uses it
1008
+ * for its software vsync modeling.
1009
+ */
1010
+ drm_send_event_timestamp_locked(dev, &e->base, now);
8471011 }
8481012
8491013 /**
....@@ -881,8 +1045,8 @@
8811045 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
8821046 * possible race with the hardware committing the atomic update.
8831047 *
884
- * Caller must hold a vblank reference for the event @e, which will be dropped
885
- * when the next vblank arrives.
1048
+ * Caller must hold a vblank reference for the event @e acquired by a
1049
+ * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
8861050 */
8871051 void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
8881052 struct drm_pending_vblank_event *e)
....@@ -917,7 +1081,7 @@
9171081 unsigned int pipe = drm_crtc_index(crtc);
9181082 ktime_t now;
9191083
920
- if (dev->num_crtcs > 0) {
1084
+ if (drm_dev_has_vblank(dev)) {
9211085 seq = drm_vblank_count_and_time(dev, pipe, &now);
9221086 } else {
9231087 seq = 0;
....@@ -934,14 +1098,16 @@
9341098 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
9351099 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
9361100
937
- if (WARN_ON(!crtc))
1101
+ if (drm_WARN_ON(dev, !crtc))
9381102 return 0;
9391103
9401104 if (crtc->funcs->enable_vblank)
9411105 return crtc->funcs->enable_vblank(crtc);
1106
+ } else if (dev->driver->enable_vblank) {
1107
+ return dev->driver->enable_vblank(dev, pipe);
9421108 }
9431109
944
- return dev->driver->enable_vblank(dev, pipe);
1110
+ return -EINVAL;
9451111 }
9461112
9471113 static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
....@@ -962,7 +1128,8 @@
9621128 * prevent double-accounting of same vblank interval.
9631129 */
9641130 ret = __enable_vblank(dev, pipe);
965
- DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
1131
+ drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
1132
+ pipe, ret);
9661133 if (ret) {
9671134 atomic_dec(&vblank->refcount);
9681135 } else {
....@@ -981,16 +1148,16 @@
9811148 return ret;
9821149 }
9831150
984
-static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
1151
+int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
9851152 {
9861153 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
9871154 unsigned long irqflags;
9881155 int ret = 0;
9891156
990
- if (!dev->num_crtcs)
1157
+ if (!drm_dev_has_vblank(dev))
9911158 return -EINVAL;
9921159
993
- if (WARN_ON(pipe >= dev->num_crtcs))
1160
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
9941161 return -EINVAL;
9951162
9961163 spin_lock_irqsave(&dev->vbl_lock, irqflags);
....@@ -1024,14 +1191,14 @@
10241191 }
10251192 EXPORT_SYMBOL(drm_crtc_vblank_get);
10261193
1027
-static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1194
+void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
10281195 {
10291196 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
10301197
1031
- if (WARN_ON(pipe >= dev->num_crtcs))
1198
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
10321199 return;
10331200
1034
- if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1201
+ if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
10351202 return;
10361203
10371204 /* Last user schedules interrupt disable */
....@@ -1076,11 +1243,12 @@
10761243 int ret;
10771244 u64 last;
10781245
1079
- if (WARN_ON(pipe >= dev->num_crtcs))
1246
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
10801247 return;
10811248
10821249 ret = drm_vblank_get(dev, pipe);
1083
- if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1250
+ if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
1251
+ pipe, ret))
10841252 return;
10851253
10861254 last = drm_vblank_count(dev, pipe);
....@@ -1089,7 +1257,7 @@
10891257 last != drm_vblank_count(dev, pipe),
10901258 msecs_to_jiffies(100));
10911259
1092
- WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1260
+ drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
10931261
10941262 drm_vblank_put(dev, pipe);
10951263 }
....@@ -1126,19 +1294,21 @@
11261294 unsigned int pipe = drm_crtc_index(crtc);
11271295 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
11281296 struct drm_pending_vblank_event *e, *t;
1129
-
11301297 ktime_t now;
1131
- unsigned long irqflags;
11321298 u64 seq;
11331299
1134
- if (WARN_ON(pipe >= dev->num_crtcs))
1300
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
11351301 return;
11361302
1137
- spin_lock_irqsave(&dev->event_lock, irqflags);
1303
+ /*
1304
+ * Grab event_lock early to prevent vblank work from being scheduled
1305
+ * while we're in the middle of shutting down vblank interrupts
1306
+ */
1307
+ spin_lock_irq(&dev->event_lock);
11381308
11391309 spin_lock(&dev->vbl_lock);
1140
- DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1141
- pipe, vblank->enabled, vblank->inmodeset);
1310
+ drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1311
+ pipe, vblank->enabled, vblank->inmodeset);
11421312
11431313 /* Avoid redundant vblank disables without previous
11441314 * drm_crtc_vblank_on(). */
....@@ -1163,18 +1333,25 @@
11631333 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
11641334 if (e->pipe != pipe)
11651335 continue;
1166
- DRM_DEBUG("Sending premature vblank event on disable: "
1167
- "wanted %llu, current %llu\n",
1168
- e->sequence, seq);
1336
+ drm_dbg_core(dev, "Sending premature vblank event on disable: "
1337
+ "wanted %llu, current %llu\n",
1338
+ e->sequence, seq);
11691339 list_del(&e->base.link);
11701340 drm_vblank_put(dev, pipe);
11711341 send_vblank_event(dev, e, seq, now);
11721342 }
1173
- spin_unlock_irqrestore(&dev->event_lock, irqflags);
1343
+
1344
+ /* Cancel any leftover pending vblank work */
1345
+ drm_vblank_cancel_pending_works(vblank);
1346
+
1347
+ spin_unlock_irq(&dev->event_lock);
11741348
11751349 /* Will be reset by the modeset helpers when re-enabling the crtc by
11761350 * calling drm_calc_timestamping_constants(). */
11771351 vblank->hwmode.crtc_clock = 0;
1352
+
1353
+ /* Wait for any vblank work that's still executing to finish */
1354
+ drm_vblank_flush_worker(vblank);
11781355 }
11791356 EXPORT_SYMBOL(drm_crtc_vblank_off);
11801357
....@@ -1193,11 +1370,10 @@
11931370 void drm_crtc_vblank_reset(struct drm_crtc *crtc)
11941371 {
11951372 struct drm_device *dev = crtc->dev;
1196
- unsigned long irqflags;
11971373 unsigned int pipe = drm_crtc_index(crtc);
11981374 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
11991375
1200
- spin_lock_irqsave(&dev->vbl_lock, irqflags);
1376
+ spin_lock_irq(&dev->vbl_lock);
12011377 /*
12021378 * Prevent subsequent drm_vblank_get() from enabling the vblank
12031379 * interrupt by bumping the refcount.
....@@ -1206,9 +1382,10 @@
12061382 atomic_inc(&vblank->refcount);
12071383 vblank->inmodeset = 1;
12081384 }
1209
- spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1385
+ spin_unlock_irq(&dev->vbl_lock);
12101386
1211
- WARN_ON(!list_empty(&dev->vblank_event_list));
1387
+ drm_WARN_ON(dev, !list_empty(&dev->vblank_event_list));
1388
+ drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
12121389 }
12131390 EXPORT_SYMBOL(drm_crtc_vblank_reset);
12141391
....@@ -1236,8 +1413,8 @@
12361413 unsigned int pipe = drm_crtc_index(crtc);
12371414 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
12381415
1239
- WARN_ON(dev->max_vblank_count);
1240
- WARN_ON(!READ_ONCE(vblank->inmodeset));
1416
+ drm_WARN_ON(dev, dev->max_vblank_count);
1417
+ drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
12411418
12421419 vblank->max_vblank_count = max_vblank_count;
12431420 }
....@@ -1258,14 +1435,13 @@
12581435 struct drm_device *dev = crtc->dev;
12591436 unsigned int pipe = drm_crtc_index(crtc);
12601437 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1261
- unsigned long irqflags;
12621438
1263
- if (WARN_ON(pipe >= dev->num_crtcs))
1439
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
12641440 return;
12651441
1266
- spin_lock_irqsave(&dev->vbl_lock, irqflags);
1267
- DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1268
- pipe, vblank->enabled, vblank->inmodeset);
1442
+ spin_lock_irq(&dev->vbl_lock);
1443
+ drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1444
+ pipe, vblank->enabled, vblank->inmodeset);
12691445
12701446 /* Drop our private "prevent drm_vblank_get" refcount */
12711447 if (vblank->inmodeset) {
....@@ -1280,8 +1456,8 @@
12801456 * user wishes vblank interrupts to be enabled all the time.
12811457 */
12821458 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1283
- WARN_ON(drm_vblank_enable(dev, pipe));
1284
- spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1459
+ drm_WARN_ON(dev, drm_vblank_enable(dev, pipe));
1460
+ spin_unlock_irq(&dev->vbl_lock);
12851461 }
12861462 EXPORT_SYMBOL(drm_crtc_vblank_on);
12871463
....@@ -1307,15 +1483,16 @@
13071483 u32 cur_vblank, diff = 1;
13081484 int count = DRM_TIMESTAMP_MAXRETRIES;
13091485
1310
- if (WARN_ON(pipe >= dev->num_crtcs))
1486
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
13111487 return;
13121488
13131489 assert_spin_locked(&dev->vbl_lock);
13141490 assert_spin_locked(&dev->vblank_time_lock);
13151491
13161492 vblank = &dev->vblank[pipe];
1317
- WARN_ONCE((drm_debug & DRM_UT_VBL) && !vblank->framedur_ns,
1318
- "Cannot compute missed vblanks without frame duration\n");
1493
+ drm_WARN_ONCE(dev,
1494
+ drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
1495
+ "Cannot compute missed vblanks without frame duration\n");
13191496 framedur_ns = vblank->framedur_ns;
13201497
13211498 do {
....@@ -1328,8 +1505,9 @@
13281505 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
13291506
13301507
1331
- DRM_DEBUG_VBL("missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1332
- diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
1508
+ drm_dbg_vbl(dev,
1509
+ "missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1510
+ diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
13331511 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
13341512 }
13351513 EXPORT_SYMBOL(drm_vblank_restore);
....@@ -1356,10 +1534,10 @@
13561534 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
13571535
13581536 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1359
- if (!dev->num_crtcs)
1537
+ if (!drm_dev_has_vblank(dev))
13601538 return;
13611539
1362
- if (WARN_ON(pipe >= dev->num_crtcs))
1540
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
13631541 return;
13641542
13651543 /*
....@@ -1380,19 +1558,18 @@
13801558 unsigned int pipe)
13811559 {
13821560 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1383
- unsigned long irqflags;
13841561
13851562 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1386
- if (!dev->num_crtcs)
1563
+ if (!drm_dev_has_vblank(dev))
13871564 return;
13881565
1389
- if (WARN_ON(pipe >= dev->num_crtcs))
1566
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
13901567 return;
13911568
13921569 if (vblank->inmodeset) {
1393
- spin_lock_irqsave(&dev->vbl_lock, irqflags);
1570
+ spin_lock_irq(&dev->vbl_lock);
13941571 drm_reset_vblank_timestamp(dev, pipe);
1395
- spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1572
+ spin_unlock_irq(&dev->vbl_lock);
13961573
13971574 if (vblank->inmodeset & 0x2)
13981575 drm_vblank_put(dev, pipe);
....@@ -1408,7 +1585,7 @@
14081585 unsigned int pipe;
14091586
14101587 /* If drm_vblank_init() hasn't been called yet, just no-op */
1411
- if (!dev->num_crtcs)
1588
+ if (!drm_dev_has_vblank(dev))
14121589 return 0;
14131590
14141591 /* KMS drivers handle this internally */
....@@ -1433,11 +1610,6 @@
14331610 return 0;
14341611 }
14351612
1436
-static inline bool vblank_passed(u64 seq, u64 ref)
1437
-{
1438
- return (seq - ref) <= (1 << 23);
1439
-}
1440
-
14411613 static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
14421614 u64 req_seq,
14431615 union drm_wait_vblank *vblwait,
....@@ -1446,7 +1618,6 @@
14461618 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
14471619 struct drm_pending_vblank_event *e;
14481620 ktime_t now;
1449
- unsigned long flags;
14501621 u64 seq;
14511622 int ret;
14521623
....@@ -1463,11 +1634,12 @@
14631634 e->event.vbl.crtc_id = 0;
14641635 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
14651636 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1637
+
14661638 if (crtc)
14671639 e->event.vbl.crtc_id = crtc->base.id;
14681640 }
14691641
1470
- spin_lock_irqsave(&dev->event_lock, flags);
1642
+ spin_lock_irq(&dev->event_lock);
14711643
14721644 /*
14731645 * drm_crtc_vblank_off() might have been called after we called
....@@ -1488,13 +1660,13 @@
14881660
14891661 seq = drm_vblank_count_and_time(dev, pipe, &now);
14901662
1491
- DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
1492
- req_seq, seq, pipe);
1663
+ drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
1664
+ req_seq, seq, pipe);
14931665
14941666 trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
14951667
14961668 e->sequence = req_seq;
1497
- if (vblank_passed(seq, req_seq)) {
1669
+ if (drm_vblank_passed(seq, req_seq)) {
14981670 drm_vblank_put(dev, pipe);
14991671 send_vblank_event(dev, e, seq, now);
15001672 vblwait->reply.sequence = seq;
....@@ -1504,12 +1676,12 @@
15041676 vblwait->reply.sequence = req_seq;
15051677 }
15061678
1507
- spin_unlock_irqrestore(&dev->event_lock, flags);
1679
+ spin_unlock_irq(&dev->event_lock);
15081680
15091681 return 0;
15101682
15111683 err_unlock:
1512
- spin_unlock_irqrestore(&dev->event_lock, flags);
1684
+ spin_unlock_irq(&dev->event_lock);
15131685 kfree(e);
15141686 err_put:
15151687 drm_vblank_put(dev, pipe);
....@@ -1580,10 +1752,11 @@
15801752 if (vblwait->request.type &
15811753 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
15821754 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1583
- DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1584
- vblwait->request.type,
1585
- (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1586
- _DRM_VBLANK_HIGH_CRTC_MASK));
1755
+ drm_dbg_core(dev,
1756
+ "Unsupported type value 0x%x, supported mask 0x%x\n",
1757
+ vblwait->request.type,
1758
+ (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1759
+ _DRM_VBLANK_HIGH_CRTC_MASK));
15871760 return -EINVAL;
15881761 }
15891762
....@@ -1626,7 +1799,9 @@
16261799
16271800 ret = drm_vblank_get(dev, pipe);
16281801 if (ret) {
1629
- DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1802
+ drm_dbg_core(dev,
1803
+ "crtc %d failed to acquire vblank counter, %d\n",
1804
+ pipe, ret);
16301805 return ret;
16311806 }
16321807 seq = drm_vblank_count(dev, pipe);
....@@ -1646,7 +1821,7 @@
16461821 }
16471822
16481823 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1649
- vblank_passed(seq, req_seq)) {
1824
+ drm_vblank_passed(seq, req_seq)) {
16501825 req_seq = seq + 1;
16511826 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
16521827 vblwait->request.sequence = req_seq;
....@@ -1660,21 +1835,38 @@
16601835 }
16611836
16621837 if (req_seq != seq) {
1663
- DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
1664
- req_seq, pipe);
1665
- DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1666
- vblank_passed(drm_vblank_count(dev, pipe),
1667
- req_seq) ||
1668
- !READ_ONCE(vblank->enabled));
1838
+ int wait;
1839
+
1840
+ drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
1841
+ req_seq, pipe);
1842
+ wait = wait_event_interruptible_timeout(vblank->queue,
1843
+ drm_vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
1844
+ !READ_ONCE(vblank->enabled),
1845
+ msecs_to_jiffies(3000));
1846
+
1847
+ switch (wait) {
1848
+ case 0:
1849
+ /* timeout */
1850
+ ret = -EBUSY;
1851
+ break;
1852
+ case -ERESTARTSYS:
1853
+ /* interrupted by signal */
1854
+ ret = -EINTR;
1855
+ break;
1856
+ default:
1857
+ ret = 0;
1858
+ break;
1859
+ }
16691860 }
16701861
16711862 if (ret != -EINTR) {
16721863 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
16731864
1674
- DRM_DEBUG("crtc %d returning %u to client\n",
1675
- pipe, vblwait->reply.sequence);
1865
+ drm_dbg_core(dev, "crtc %d returning %u to client\n",
1866
+ pipe, vblwait->reply.sequence);
16761867 } else {
1677
- DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
1868
+ drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
1869
+ pipe);
16781870 }
16791871
16801872 done:
....@@ -1684,6 +1876,8 @@
16841876
16851877 static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
16861878 {
1879
+ struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1880
+ bool high_prec = false;
16871881 struct drm_pending_vblank_event *e, *t;
16881882 ktime_t now;
16891883 u64 seq;
....@@ -1695,18 +1889,21 @@
16951889 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
16961890 if (e->pipe != pipe)
16971891 continue;
1698
- if (!vblank_passed(seq, e->sequence))
1892
+ if (!drm_vblank_passed(seq, e->sequence))
16991893 continue;
17001894
1701
- DRM_DEBUG("vblank event on %llu, current %llu\n",
1702
- e->sequence, seq);
1895
+ drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
1896
+ e->sequence, seq);
17031897
17041898 list_del(&e->base.link);
17051899 drm_vblank_put(dev, pipe);
17061900 send_vblank_event(dev, e, seq, now);
17071901 }
17081902
1709
- trace_drm_vblank_event(pipe, seq);
1903
+ if (crtc && crtc->funcs->get_vblank_timestamp)
1904
+ high_prec = true;
1905
+
1906
+ trace_drm_vblank_event(pipe, seq, now, high_prec);
17101907 }
17111908
17121909 /**
....@@ -1725,10 +1922,10 @@
17251922 unsigned long irqflags;
17261923 bool disable_irq;
17271924
1728
- if (WARN_ON_ONCE(!dev->num_crtcs))
1925
+ if (drm_WARN_ON_ONCE(dev, !drm_dev_has_vblank(dev)))
17291926 return false;
17301927
1731
- if (WARN_ON(pipe >= dev->num_crtcs))
1928
+ if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
17321929 return false;
17331930
17341931 spin_lock_irqsave(&dev->event_lock, irqflags);
....@@ -1762,6 +1959,7 @@
17621959 !atomic_read(&vblank->refcount));
17631960
17641961 drm_handle_vblank_events(dev, pipe);
1962
+ drm_handle_vblank_works(vblank);
17651963
17661964 spin_unlock_irqrestore(&dev->event_lock, irqflags);
17671965
....@@ -1780,6 +1978,14 @@
17801978 * update the vblank counter and send any signals that may be pending.
17811979 *
17821980 * This is the native KMS version of drm_handle_vblank().
1981
+ *
1982
+ * Note that for a given vblank counter value drm_crtc_handle_vblank()
1983
+ * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
1984
+ * provide a barrier: Any writes done before calling
1985
+ * drm_crtc_handle_vblank() will be visible to callers of the later
1986
+ * functions, iff the vblank count is the same or a later one.
1987
+ *
1988
+ * See also &drm_vblank_crtc.count.
17831989 *
17841990 * Returns:
17851991 * True if the event was successfully handled, false on failure.
....@@ -1810,7 +2016,7 @@
18102016 int ret;
18112017
18122018 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1813
- return -EINVAL;
2019
+ return -EOPNOTSUPP;
18142020
18152021 if (!dev->irq_enabled)
18162022 return -EOPNOTSUPP;
....@@ -1827,7 +2033,9 @@
18272033 if (!vblank_enabled) {
18282034 ret = drm_crtc_vblank_get(crtc);
18292035 if (ret) {
1830
- DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
2036
+ drm_dbg_core(dev,
2037
+ "crtc %d failed to acquire vblank counter, %d\n",
2038
+ pipe, ret);
18312039 return ret;
18322040 }
18332041 }
....@@ -1865,10 +2073,9 @@
18652073 u64 seq;
18662074 u64 req_seq;
18672075 int ret;
1868
- unsigned long spin_flags;
18692076
18702077 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1871
- return -EINVAL;
2078
+ return -EOPNOTSUPP;
18722079
18732080 if (!dev->irq_enabled)
18742081 return -EOPNOTSUPP;
....@@ -1893,7 +2100,9 @@
18932100
18942101 ret = drm_crtc_vblank_get(crtc);
18952102 if (ret) {
1896
- DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
2103
+ drm_dbg_core(dev,
2104
+ "crtc %d failed to acquire vblank counter, %d\n",
2105
+ pipe, ret);
18972106 goto err_free;
18982107 }
18992108
....@@ -1903,7 +2112,7 @@
19032112 if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
19042113 req_seq += seq;
19052114
1906
- if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && vblank_passed(seq, req_seq))
2115
+ if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && drm_vblank_passed(seq, req_seq))
19072116 req_seq = seq + 1;
19082117
19092118 e->pipe = pipe;
....@@ -1911,7 +2120,7 @@
19112120 e->event.base.length = sizeof(e->event.seq);
19122121 e->event.seq.user_data = queue_seq->user_data;
19132122
1914
- spin_lock_irqsave(&dev->event_lock, spin_flags);
2123
+ spin_lock_irq(&dev->event_lock);
19152124
19162125 /*
19172126 * drm_crtc_vblank_off() might have been called after we called
....@@ -1932,7 +2141,7 @@
19322141
19332142 e->sequence = req_seq;
19342143
1935
- if (vblank_passed(seq, req_seq)) {
2144
+ if (drm_vblank_passed(seq, req_seq)) {
19362145 drm_crtc_vblank_put(crtc);
19372146 send_vblank_event(dev, e, seq, now);
19382147 queue_seq->sequence = seq;
....@@ -1942,13 +2151,14 @@
19422151 queue_seq->sequence = req_seq;
19432152 }
19442153
1945
- spin_unlock_irqrestore(&dev->event_lock, spin_flags);
2154
+ spin_unlock_irq(&dev->event_lock);
19462155 return 0;
19472156
19482157 err_unlock:
1949
- spin_unlock_irqrestore(&dev->event_lock, spin_flags);
2158
+ spin_unlock_irq(&dev->event_lock);
19502159 drm_crtc_vblank_put(crtc);
19512160 err_free:
19522161 kfree(e);
19532162 return ret;
19542163 }
2164
+