Merge tag 'probes-fixes-v6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / drivers / gpu / drm / drm_vblank.c
CommitLineData
3ed4351a
DV
1/*
2 * drm_irq.c IRQ and vblank support
3 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
3ed4351a 27#include <linux/export.h>
5e6c2b4f 28#include <linux/kthread.h>
0500c04e
SR
29#include <linux/moduleparam.h>
30
31#include <drm/drm_crtc.h>
32#include <drm/drm_drv.h>
33#include <drm/drm_framebuffer.h>
c23d686f 34#include <drm/drm_managed.h>
f1e2b637 35#include <drm/drm_modeset_helper_vtables.h>
0500c04e 36#include <drm/drm_print.h>
0500c04e 37#include <drm/drm_vblank.h>
3ed4351a 38
3ed4351a 39#include "drm_internal.h"
0500c04e 40#include "drm_trace.h"
3ed4351a 41
57d30230
DV
42/**
43 * DOC: vblank handling
44 *
56b165fe
SR
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
8323c625 77 * | | ("scan out")
56b165fe
SR
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.
107 *
57d30230
DV
108 * Vertical blanking plays a major role in graphics rendering. To achieve
109 * tear-free display, users must synchronize page flips and/or rendering to
110 * vertical blanking. The DRM API offers ioctls to perform page flips
111 * synchronized to vertical blanking and wait for vertical blanking.
112 *
113 * The DRM core handles most of the vertical blanking management logic, which
114 * involves filtering out spurious interrupts, keeping race-free blanking
115 * counters, coping with counter wrap-around and resets and keeping use counts.
116 * It relies on the driver to generate vertical blanking interrupts and
117 * optionally provide a hardware vertical blanking counter.
118 *
119 * Drivers must initialize the vertical blanking handling core with a call to
120 * drm_vblank_init(). Minimally, a driver needs to implement
121 * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
1e55a53a 122 * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
57d30230
DV
123 * support.
124 *
125 * Vertical blanking interrupts can be enabled by the DRM core or by drivers
126 * themselves (for instance to handle page flipping operations). The DRM core
127 * maintains a vertical blanking use count to ensure that the interrupts are not
128 * disabled while a user still needs them. To increment the use count, drivers
129 * call drm_crtc_vblank_get() and release the vblank reference again with
130 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
131 * guaranteed to be enabled.
132 *
133 * On many hardware disabling the vblank interrupt cannot be done in a race-free
0d5040e4 134 * manner, see &drm_vblank_crtc_config.disable_immediate and
57d30230
DV
135 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
136 * vblanks after a timer has expired, which can be configured through the
137 * ``vblankoffdelay`` module parameter.
7beb691f
TZ
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.
57d30230
DV
144 */
145
3ed4351a
DV
146/* Retry timestamp calculation up to 3 times to satisfy
147 * drm_timestamp_precision before giving up.
148 */
149#define DRM_TIMESTAMP_MAXRETRIES 3
150
151/* Threshold in nanoseconds for detection of redundant
152 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
153 */
154#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
155
156static bool
157drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
67680d3c 158 ktime_t *tvblank, bool in_vblank_irq);
3ed4351a
DV
159
160static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
161
3ed4351a
DV
162static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
163
164module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
165module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
3ed4351a
DV
166MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
167MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
3ed4351a 168
d12e3649
VS
169static struct drm_vblank_crtc *
170drm_vblank_crtc(struct drm_device *dev, unsigned int pipe)
171{
172 return &dev->vblank[pipe];
173}
174
175struct drm_vblank_crtc *
176drm_crtc_vblank_crtc(struct drm_crtc *crtc)
177{
178 return drm_vblank_crtc(crtc->dev, drm_crtc_index(crtc));
179}
180EXPORT_SYMBOL(drm_crtc_vblank_crtc);
181
3ed4351a
DV
182static void store_vblank(struct drm_device *dev, unsigned int pipe,
183 u32 vblank_count_inc,
67680d3c 184 ktime_t t_vblank, u32 last)
3ed4351a 185{
d12e3649 186 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a
DV
187
188 assert_spin_locked(&dev->vblank_time_lock);
189
190 vblank->last = last;
191
192 write_seqlock(&vblank->seqlock);
67680d3c 193 vblank->time = t_vblank;
bd7e3f3b 194 atomic64_add(vblank_count_inc, &vblank->count);
3ed4351a
DV
195 write_sequnlock(&vblank->seqlock);
196}
197
ed20151a
VS
198static u32 drm_max_vblank_count(struct drm_device *dev, unsigned int pipe)
199{
d12e3649 200 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
ed20151a
VS
201
202 return vblank->max_vblank_count ?: dev->max_vblank_count;
203}
204
3ed4351a
DV
205/*
206 * "No hw counter" fallback implementation of .get_vblank_counter() hook,
0ae865ef 207 * if there is no usable hardware frame counter available.
3ed4351a
DV
208 */
209static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
210{
5a4784f4 211 drm_WARN_ON_ONCE(dev, drm_max_vblank_count(dev, pipe) != 0);
3ed4351a
DV
212 return 0;
213}
214
215static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
216{
217 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
218 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
219
5a4784f4 220 if (drm_WARN_ON(dev, !crtc))
4ffb8dee
JM
221 return 0;
222
3ed4351a
DV
223 if (crtc->funcs->get_vblank_counter)
224 return crtc->funcs->get_vblank_counter(crtc);
57bb1ee6 225 }
3ed4351a
DV
226
227 return drm_vblank_no_hw_counter(dev, pipe);
228}
229
230/*
231 * Reset the stored timestamp for the current vblank count to correspond
232 * to the last vblank occurred.
233 *
234 * Only to be called from drm_crtc_vblank_on().
235 *
236 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
237 * device vblank fields.
238 */
239static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
240{
241 u32 cur_vblank;
242 bool rc;
67680d3c 243 ktime_t t_vblank;
3ed4351a
DV
244 int count = DRM_TIMESTAMP_MAXRETRIES;
245
246 spin_lock(&dev->vblank_time_lock);
247
248 /*
249 * sample the current counter to avoid random jumps
250 * when drm_vblank_enable() applies the diff
251 */
252 do {
253 cur_vblank = __get_vblank_counter(dev, pipe);
254 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
255 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
256
257 /*
258 * Only reinitialize corresponding vblank timestamp if high-precision query
259 * available and didn't fail. Otherwise reinitialize delayed at next vblank
260 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
261 */
262 if (!rc)
67680d3c 263 t_vblank = 0;
3ed4351a
DV
264
265 /*
266 * +1 to make sure user will never see the same
267 * vblank counter value before and after a modeset
268 */
67680d3c 269 store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
3ed4351a
DV
270
271 spin_unlock(&dev->vblank_time_lock);
272}
273
274/*
275 * Call back into the driver to update the appropriate vblank counter
276 * (specified by @pipe). Deal with wraparound, if it occurred, and
277 * update the last read value so we can deal with wraparound on the next
278 * call if necessary.
279 *
280 * Only necessary when going from off->on, to account for frames we
281 * didn't get an interrupt for.
282 *
283 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
284 * device vblank fields.
285 */
286static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
287 bool in_vblank_irq)
288{
d12e3649 289 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a
DV
290 u32 cur_vblank, diff;
291 bool rc;
67680d3c 292 ktime_t t_vblank;
3ed4351a
DV
293 int count = DRM_TIMESTAMP_MAXRETRIES;
294 int framedur_ns = vblank->framedur_ns;
ed20151a 295 u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
3ed4351a
DV
296
297 /*
298 * Interrupts were disabled prior to this call, so deal with counter
299 * wrap if needed.
300 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
301 * here if the register is small or we had vblank interrupts off for
302 * a long time.
303 *
304 * We repeat the hardware vblank counter & timestamp query until
305 * we get consistent results. This to prevent races between gpu
306 * updating its hardware counter while we are retrieving the
307 * corresponding vblank timestamp.
308 */
309 do {
310 cur_vblank = __get_vblank_counter(dev, pipe);
311 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
312 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
313
ed20151a 314 if (max_vblank_count) {
3ed4351a 315 /* trust the hw counter when it's around */
ed20151a 316 diff = (cur_vblank - vblank->last) & max_vblank_count;
3ed4351a 317 } else if (rc && framedur_ns) {
67680d3c 318 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
3ed4351a
DV
319
320 /*
321 * Figure out how many vblanks we've missed based
322 * on the difference in the timestamps and the
323 * frame/field duration.
324 */
41ff5bd8 325
02149a76
SR
326 drm_dbg_vbl(dev, "crtc %u: Calculating number of vblanks."
327 " diff_ns = %lld, framedur_ns = %d)\n",
328 pipe, (long long)diff_ns, framedur_ns);
41ff5bd8 329
3ed4351a
DV
330 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
331
332 if (diff == 0 && in_vblank_irq)
02149a76
SR
333 drm_dbg_vbl(dev, "crtc %u: Redundant vblirq ignored\n",
334 pipe);
3ed4351a
DV
335 } else {
336 /* some kind of default for drivers w/o accurate vbl timestamping */
337 diff = in_vblank_irq ? 1 : 0;
338 }
339
340 /*
341 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
342 * interval? If so then vblank irqs keep running and it will likely
343 * happen that the hardware vblank counter is not trustworthy as it
344 * might reset at some point in that interval and vblank timestamps
345 * are not trustworthy either in that interval. Iow. this can result
346 * in a bogus diff >> 1 which must be avoided as it would cause
347 * random large forward jumps of the software vblank counter.
348 */
349 if (diff > 1 && (vblank->inmodeset & 0x2)) {
02149a76
SR
350 drm_dbg_vbl(dev,
351 "clamping vblank bump to 1 on crtc %u: diffr=%u"
352 " due to pre-modeset.\n", pipe, diff);
3ed4351a
DV
353 diff = 1;
354 }
355
02149a76
SR
356 drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
357 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
358 pipe, (unsigned long long)atomic64_read(&vblank->count),
359 diff, cur_vblank, vblank->last);
3ed4351a
DV
360
361 if (diff == 0) {
5a4784f4 362 drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
3ed4351a
DV
363 return;
364 }
365
366 /*
367 * Only reinitialize corresponding vblank timestamp if high-precision query
368 * available and didn't fail, or we were called from the vblank interrupt.
369 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
370 * for now, to mark the vblanktimestamp as invalid.
371 */
138b87fa 372 if (!rc && !in_vblank_irq)
67680d3c 373 t_vblank = 0;
3ed4351a 374
67680d3c 375 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
3ed4351a
DV
376}
377
5e6c2b4f 378u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
3ed4351a 379{
d12e3649 380 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
bd7e3f3b 381 u64 count;
3ed4351a 382
5a4784f4 383 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
384 return 0;
385
bd7e3f3b
DV
386 count = atomic64_read(&vblank->count);
387
388 /*
389 * This read barrier corresponds to the implicit write barrier of the
390 * write seqlock in store_vblank(). Note that this is the only place
391 * where we need an explicit barrier, since all other access goes
392 * through drm_vblank_count_and_time(), which already has the required
393 * read barrier curtesy of the read seqlock.
394 */
395 smp_rmb();
396
397 return count;
3ed4351a
DV
398}
399
400/**
ca814b25 401 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
3ed4351a
DV
402 * @crtc: which counter to retrieve
403 *
57d30230
DV
404 * This function is similar to drm_crtc_vblank_count() but this function
405 * interpolates to handle a race with vblank interrupts using the high precision
406 * timestamping support.
3ed4351a 407 *
57d30230
DV
408 * This is mostly useful for hardware that can obtain the scanout position, but
409 * doesn't have a hardware frame counter.
3ed4351a 410 */
3b765c0b 411u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
3ed4351a
DV
412{
413 struct drm_device *dev = crtc->dev;
414 unsigned int pipe = drm_crtc_index(crtc);
3b765c0b 415 u64 vblank;
3ed4351a
DV
416 unsigned long flags;
417
5a4784f4
SR
418 drm_WARN_ONCE(dev, drm_debug_enabled(DRM_UT_VBL) &&
419 !crtc->funcs->get_vblank_timestamp,
420 "This function requires support for accurate vblank timestamps.");
3ed4351a
DV
421
422 spin_lock_irqsave(&dev->vblank_time_lock, flags);
423
424 drm_update_vblank_count(dev, pipe, false);
425 vblank = drm_vblank_count(dev, pipe);
426
427 spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
428
429 return vblank;
430}
ca814b25 431EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
3ed4351a
DV
432
433static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
434{
435 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
436 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
437
5a4784f4 438 if (drm_WARN_ON(dev, !crtc))
4ffb8dee
JM
439 return;
440
f397d66b 441 if (crtc->funcs->disable_vblank)
3ed4351a 442 crtc->funcs->disable_vblank(crtc);
57bb1ee6 443 }
3ed4351a
DV
444}
445
446/*
447 * Disable vblank irq's on crtc, make sure that last vblank count
448 * of hardware and corresponding consistent software vblank counter
449 * are preserved, even if there are any spurious vblank irq's after
450 * disable.
451 */
452void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
453{
d12e3649 454 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a
DV
455 unsigned long irqflags;
456
457 assert_spin_locked(&dev->vbl_lock);
458
459 /* Prevent vblank irq processing while disabling vblank irqs,
460 * so no updates of timestamps or count can happen after we've
461 * disabled. Needed to prevent races in case of delayed irq's.
462 */
463 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
464
465 /*
68036b08
DP
466 * Update vblank count and disable vblank interrupts only if the
467 * interrupts were enabled. This avoids calling the ->disable_vblank()
468 * operation in atomic context with the hardware potentially runtime
469 * suspended.
3ed4351a 470 */
68036b08
DP
471 if (!vblank->enabled)
472 goto out;
3ed4351a
DV
473
474 /*
68036b08 475 * Update the count and timestamp to maintain the
3ed4351a
DV
476 * appearance that the counter has been ticking all along until
477 * this time. This makes the count account for the entire time
478 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
479 */
480 drm_update_vblank_count(dev, pipe, false);
68036b08
DP
481 __disable_vblank(dev, pipe);
482 vblank->enabled = false;
3ed4351a 483
68036b08 484out:
3ed4351a
DV
485 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
486}
487
e99e88a9 488static void vblank_disable_fn(struct timer_list *t)
3ed4351a 489{
41cb0855
IM
490 struct drm_vblank_crtc *vblank = timer_container_of(vblank, t,
491 disable_timer);
3ed4351a
DV
492 struct drm_device *dev = vblank->dev;
493 unsigned int pipe = vblank->pipe;
494 unsigned long irqflags;
495
496 spin_lock_irqsave(&dev->vbl_lock, irqflags);
497 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
02149a76 498 drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
3ed4351a
DV
499 drm_vblank_disable_and_save(dev, pipe);
500 }
501 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
502}
503
c23d686f 504static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
3ed4351a 505{
a7e5e06d 506 struct drm_vblank_crtc *vblank = ptr;
3ed4351a 507
a7e5e06d
LP
508 drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
509 drm_core_check_feature(dev, DRIVER_MODESET));
3ed4351a 510
5e6c2b4f 511 drm_vblank_destroy_worker(vblank);
8fa7292f 512 timer_delete_sync(&vblank->disable_timer);
3ed4351a 513}
3ed4351a
DV
514
515/**
516 * drm_vblank_init - initialize vblank support
517 * @dev: DRM device
518 * @num_crtcs: number of CRTCs supported by @dev
519 *
520 * This function initializes vblank support for @num_crtcs display pipelines.
c23d686f 521 * Cleanup is handled automatically through a cleanup function added with
a7e5e06d 522 * drmm_add_action_or_reset().
3ed4351a
DV
523 *
524 * Returns:
525 * Zero on success or a negative error code on failure.
526 */
527int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
528{
c23d686f 529 int ret;
3ed4351a
DV
530 unsigned int i;
531
532 spin_lock_init(&dev->vbl_lock);
533 spin_lock_init(&dev->vblank_time_lock);
534
c23d686f
DV
535 dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
536 if (!dev->vblank)
537 return -ENOMEM;
538
3ed4351a
DV
539 dev->num_crtcs = num_crtcs;
540
3ed4351a
DV
541 for (i = 0; i < num_crtcs; i++) {
542 struct drm_vblank_crtc *vblank = &dev->vblank[i];
543
544 vblank->dev = dev;
545 vblank->pipe = i;
546 init_waitqueue_head(&vblank->queue);
e99e88a9 547 timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
3ed4351a 548 seqlock_init(&vblank->seqlock);
a7e5e06d
LP
549
550 ret = drmm_add_action_or_reset(dev, drm_vblank_init_release,
551 vblank);
552 if (ret)
553 return ret;
5e6c2b4f
LP
554
555 ret = drm_vblank_worker_init(vblank);
556 if (ret)
557 return ret;
3ed4351a
DV
558 }
559
3ed4351a 560 return 0;
3ed4351a
DV
561}
562EXPORT_SYMBOL(drm_vblank_init);
563
7beb691f
TZ
564/**
565 * drm_dev_has_vblank - test if vblanking has been initialized for
566 * a device
567 * @dev: the device
568 *
569 * Drivers may call this function to test if vblank support is
570 * initialized for a device. For most hardware this means that vblanking
571 * can also be enabled.
572 *
573 * Atomic helpers use this function to initialize
574 * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
575 *
576 * Returns:
577 * True if vblanking has been initialized for the given device, false
578 * otherwise.
579 */
580bool drm_dev_has_vblank(const struct drm_device *dev)
581{
582 return dev->num_crtcs != 0;
583}
584EXPORT_SYMBOL(drm_dev_has_vblank);
585
3ed4351a
DV
586/**
587 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
588 * @crtc: which CRTC's vblank waitqueue to retrieve
589 *
590 * This function returns a pointer to the vblank waitqueue for the CRTC.
591 * Drivers can use this to implement vblank waits using wait_event() and related
592 * functions.
593 */
594wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
595{
596 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
597}
598EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
599
600
601/**
602 * drm_calc_timestamping_constants - calculate vblank timestamp constants
603 * @crtc: drm_crtc whose timestamp constants should be updated.
604 * @mode: display mode containing the scanout timings
605 *
57d30230
DV
606 * Calculate and store various constants which are later needed by vblank and
607 * swap-completion timestamping, e.g, by
7fe3f0d1
TZ
608 * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
609 * CRTC's true scanout timing, so they take things like panel scaling or
610 * other adjustments into account.
3ed4351a
DV
611 */
612void drm_calc_timestamping_constants(struct drm_crtc *crtc,
613 const struct drm_display_mode *mode)
614{
615 struct drm_device *dev = crtc->dev;
616 unsigned int pipe = drm_crtc_index(crtc);
d12e3649 617 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
3ed4351a
DV
618 int linedur_ns = 0, framedur_ns = 0;
619 int dotclock = mode->crtc_clock;
620
6015002e 621 if (!drm_dev_has_vblank(dev))
3ed4351a
DV
622 return;
623
5a4784f4 624 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
625 return;
626
627 /* Valid dotclock? */
628 if (dotclock > 0) {
629 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
630
631 /*
632 * Convert scanline length in pixels and video
633 * dot clock to line duration and frame duration
634 * in nanoseconds:
635 */
636 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
637 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
638
639 /*
640 * Fields of interlaced scanout modes are only half a frame duration.
641 */
642 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
643 framedur_ns /= 2;
02149a76
SR
644 } else {
645 drm_err(dev, "crtc %u: Can't calculate constants, dotclock = 0!\n",
646 crtc->base.id);
647 }
3ed4351a
DV
648
649 vblank->linedur_ns = linedur_ns;
650 vblank->framedur_ns = framedur_ns;
a3342f4d 651 drm_mode_copy(&vblank->hwmode, mode);
3ed4351a 652
02149a76
SR
653 drm_dbg_core(dev,
654 "crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
655 crtc->base.id, mode->crtc_htotal,
656 mode->crtc_vtotal, mode->crtc_vdisplay);
657 drm_dbg_core(dev, "crtc %u: clock %d kHz framedur %d linedur %d\n",
658 crtc->base.id, dotclock, framedur_ns, linedur_ns);
3ed4351a
DV
659}
660EXPORT_SYMBOL(drm_calc_timestamping_constants);
661
f1e2b637
TZ
662/**
663 * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
664 * timestamp helper
099d705d 665 * @crtc: CRTC whose vblank timestamp to retrieve
f1e2b637
TZ
666 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
667 * On return contains true maximum error of timestamp
668 * @vblank_time: Pointer to time which should receive the timestamp
669 * @in_vblank_irq:
670 * True when called from drm_crtc_handle_vblank(). Some drivers
671 * need to apply some workarounds for gpu-specific vblank irq quirks
672 * if flag is set.
673 * @get_scanout_position:
674 * Callback function to retrieve the scanout position. See
675 * @struct drm_crtc_helper_funcs.get_scanout_position.
f1e2b637
TZ
676 *
677 * Implements calculation of exact vblank timestamps from given drm_display_mode
678 * timings and current video scanout position of a CRTC.
679 *
680 * The current implementation only handles standard video modes. For double scan
681 * and interlaced modes the driver is supposed to adjust the hardware mode
682 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
683 * match the scanout position reported.
684 *
685 * Note that atomic drivers must call drm_calc_timestamping_constants() before
686 * enabling a CRTC. The atomic helpers already take care of that in
441959eb 687 * drm_atomic_helper_calc_timestamping_constants().
f1e2b637
TZ
688 *
689 * Returns:
f1e2b637
TZ
690 * Returns true on success, and false on failure, i.e. when no accurate
691 * timestamp could be acquired.
692 */
693bool
694drm_crtc_vblank_helper_get_vblank_timestamp_internal(
695 struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
696 bool in_vblank_irq,
48e67807 697 drm_vblank_get_scanout_position_func get_scanout_position)
f1e2b637
TZ
698{
699 struct drm_device *dev = crtc->dev;
700 unsigned int pipe = crtc->index;
701 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
702 struct timespec64 ts_etime, ts_vblank_time;
703 ktime_t stime, etime;
704 bool vbl_status;
705 const struct drm_display_mode *mode;
706 int vpos, hpos, i;
707 int delta_ns, duration_ns;
708
709 if (pipe >= dev->num_crtcs) {
02149a76 710 drm_err(dev, "Invalid crtc %u\n", pipe);
3ed4351a
DV
711 return false;
712 }
713
714 /* Scanout position query not supported? Should not happen. */
48e67807 715 if (!get_scanout_position) {
02149a76 716 drm_err(dev, "Called from CRTC w/o get_scanout_position()!?\n");
3ed4351a
DV
717 return false;
718 }
719
720 if (drm_drv_uses_atomic_modeset(dev))
721 mode = &vblank->hwmode;
722 else
723 mode = &crtc->hwmode;
724
725 /* If mode timing undefined, just return as no-op:
726 * Happens during initial modesetting of a crtc.
727 */
728 if (mode->crtc_clock == 0) {
02149a76
SR
729 drm_dbg_core(dev, "crtc %u: Noop due to uninitialized mode.\n",
730 pipe);
5a4784f4 731 drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
3ed4351a
DV
732 return false;
733 }
734
735 /* Get current scanout position with system timestamp.
736 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
737 * if single query takes longer than max_error nanoseconds.
738 *
739 * This guarantees a tight bound on maximum error if
740 * code gets preempted or delayed for some reason.
741 */
742 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
743 /*
744 * Get vertical and horizontal scanout position vpos, hpos,
745 * and bounding timestamps stime, etime, pre/post query.
746 */
48e67807
TZ
747 vbl_status = get_scanout_position(crtc, in_vblank_irq,
748 &vpos, &hpos,
749 &stime, &etime,
750 mode);
3ed4351a
DV
751
752 /* Return as no-op if scanout query unsupported or failed. */
753 if (!vbl_status) {
02149a76
SR
754 drm_dbg_core(dev,
755 "crtc %u : scanoutpos query failed.\n",
756 pipe);
3ed4351a
DV
757 return false;
758 }
759
760 /* Compute uncertainty in timestamp of scanout position query. */
761 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
762
763 /* Accept result with < max_error nsecs timing uncertainty. */
764 if (duration_ns <= *max_error)
765 break;
766 }
767
768 /* Noisy system timing? */
769 if (i == DRM_TIMESTAMP_MAXRETRIES) {
02149a76
SR
770 drm_dbg_core(dev,
771 "crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
772 pipe, duration_ns / 1000, *max_error / 1000, i);
3ed4351a
DV
773 }
774
775 /* Return upper bound of timestamp precision error. */
776 *max_error = duration_ns;
777
778 /* Convert scanout position into elapsed time at raw_time query
779 * since start of scanout at first display scanline. delta_ns
780 * can be negative if start of scanout hasn't happened yet.
781 */
782 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
783 mode->crtc_clock);
784
3ed4351a
DV
785 /* Subtract time delta from raw timestamp to get final
786 * vblank_time timestamp for end of vblank.
787 */
bcbec31c
VS
788 *vblank_time = ktime_sub_ns(etime, delta_ns);
789
f0a8f533 790 if (!drm_debug_enabled(DRM_UT_VBL))
bcbec31c
VS
791 return true;
792
793 ts_etime = ktime_to_timespec64(etime);
794 ts_vblank_time = ktime_to_timespec64(*vblank_time);
3ed4351a 795
02149a76
SR
796 drm_dbg_vbl(dev,
797 "crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
798 pipe, hpos, vpos,
799 (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
800 (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
801 duration_ns / 1000, i);
3ed4351a
DV
802
803 return true;
804}
f1e2b637 805EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
3ed4351a 806
7fe3f0d1
TZ
807/**
808 * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
809 * helper
810 * @crtc: CRTC whose vblank timestamp to retrieve
811 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
812 * On return contains true maximum error of timestamp
813 * @vblank_time: Pointer to time which should receive the timestamp
814 * @in_vblank_irq:
815 * True when called from drm_crtc_handle_vblank(). Some drivers
816 * need to apply some workarounds for gpu-specific vblank irq quirks
817 * if flag is set.
818 *
819 * Implements calculation of exact vblank timestamps from given drm_display_mode
820 * timings and current video scanout position of a CRTC. This can be directly
821 * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
822 * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
823 *
824 * The current implementation only handles standard video modes. For double scan
825 * and interlaced modes the driver is supposed to adjust the hardware mode
826 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
827 * match the scanout position reported.
828 *
829 * Note that atomic drivers must call drm_calc_timestamping_constants() before
830 * enabling a CRTC. The atomic helpers already take care of that in
441959eb 831 * drm_atomic_helper_calc_timestamping_constants().
7fe3f0d1
TZ
832 *
833 * Returns:
7fe3f0d1
TZ
834 * Returns true on success, and false on failure, i.e. when no accurate
835 * timestamp could be acquired.
836 */
837bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
838 int *max_error,
839 ktime_t *vblank_time,
840 bool in_vblank_irq)
841{
842 return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
843 crtc, max_error, vblank_time, in_vblank_irq,
48e67807 844 crtc->helper_private->get_scanout_position);
7fe3f0d1
TZ
845}
846EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
847
3ed4351a 848/**
b2c077d0
RC
849 * drm_crtc_get_last_vbltimestamp - retrieve raw timestamp for the most
850 * recent vblank interval
851 * @crtc: CRTC whose vblank timestamp to retrieve
67680d3c 852 * @tvblank: Pointer to target time which should receive the timestamp
3ed4351a
DV
853 * @in_vblank_irq:
854 * True when called from drm_crtc_handle_vblank(). Some drivers
855 * need to apply some workarounds for gpu-specific vblank irq quirks
856 * if flag is set.
857 *
858 * Fetches the system timestamp corresponding to the time of the most recent
859 * vblank interval on specified CRTC. May call into kms-driver to
860 * compute the timestamp with a high-precision GPU specific method.
861 *
862 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
863 * call, i.e., it isn't very precisely locked to the true vblank.
864 *
865 * Returns:
866 * True if timestamp is considered to be very precise, false otherwise.
867 */
868static bool
b2c077d0
RC
869drm_crtc_get_last_vbltimestamp(struct drm_crtc *crtc, ktime_t *tvblank,
870 bool in_vblank_irq)
3ed4351a
DV
871{
872 bool ret = false;
873
874 /* Define requested maximum error on timestamps (nanoseconds). */
875 int max_error = (int) drm_timestamp_precision * 1000;
876
877 /* Query driver if possible and precision timestamping enabled. */
7fe3f0d1 878 if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
7fe3f0d1
TZ
879 ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
880 tvblank, in_vblank_irq);
7fe3f0d1 881 }
3ed4351a
DV
882
883 /* GPU high precision timestamp query unsupported or failed.
884 * Return current monotonic/gettimeofday timestamp as best estimate.
885 */
886 if (!ret)
25e1a798 887 *tvblank = ktime_get();
3ed4351a
DV
888
889 return ret;
890}
891
b2c077d0
RC
892static bool
893drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
894 ktime_t *tvblank, bool in_vblank_irq)
895{
896 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
897
898 return drm_crtc_get_last_vbltimestamp(crtc, tvblank, in_vblank_irq);
899}
900
3ed4351a
DV
901/**
902 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
903 * @crtc: which counter to retrieve
904 *
905 * Fetches the "cooked" vblank count value that represents the number of
906 * vblank events since the system was booted, including lost events due to
57d30230
DV
907 * modesetting activity. Note that this timer isn't correct against a racing
908 * vblank interrupt (since it only reports the software vblank counter), see
ca814b25 909 * drm_crtc_accurate_vblank_count() for such use-cases.
3ed4351a 910 *
bd7e3f3b
DV
911 * Note that for a given vblank counter value drm_crtc_handle_vblank()
912 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
913 * provide a barrier: Any writes done before calling
914 * drm_crtc_handle_vblank() will be visible to callers of the later
0ae865ef 915 * functions, if the vblank count is the same or a later one.
bd7e3f3b
DV
916 *
917 * See also &drm_vblank_crtc.count.
918 *
3ed4351a
DV
919 * Returns:
920 * The software vblank counter.
921 */
570e8696 922u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
3ed4351a
DV
923{
924 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
925}
926EXPORT_SYMBOL(drm_crtc_vblank_count);
927
570e8696
KP
928/**
929 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
930 * system timestamp corresponding to that vblank counter value.
931 * @dev: DRM device
932 * @pipe: index of CRTC whose counter to retrieve
933 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
934 *
935 * Fetches the "cooked" vblank count value that represents the number of
936 * vblank events since the system was booted, including lost events due to
937 * modesetting activity. Returns corresponding system timestamp of the time
938 * of the vblank interval that corresponds to the current vblank counter value.
939 *
940 * This is the legacy version of drm_crtc_vblank_count_and_time().
941 */
942static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
67680d3c 943 ktime_t *vblanktime)
3ed4351a 944{
d12e3649 945 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
570e8696 946 u64 vblank_count;
3ed4351a
DV
947 unsigned int seq;
948
5a4784f4 949 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs)) {
67680d3c 950 *vblanktime = 0;
3ed4351a
DV
951 return 0;
952 }
953
954 do {
955 seq = read_seqbegin(&vblank->seqlock);
bd7e3f3b 956 vblank_count = atomic64_read(&vblank->count);
3ed4351a
DV
957 *vblanktime = vblank->time;
958 } while (read_seqretry(&vblank->seqlock, seq));
959
960 return vblank_count;
961}
962
963/**
964 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
965 * and the system timestamp corresponding to that vblank counter value
966 * @crtc: which counter to retrieve
67680d3c 967 * @vblanktime: Pointer to time to receive the vblank timestamp.
3ed4351a
DV
968 *
969 * Fetches the "cooked" vblank count value that represents the number of
970 * vblank events since the system was booted, including lost events due to
971 * modesetting activity. Returns corresponding system timestamp of the time
972 * of the vblank interval that corresponds to the current vblank counter value.
bd7e3f3b
DV
973 *
974 * Note that for a given vblank counter value drm_crtc_handle_vblank()
975 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
976 * provide a barrier: Any writes done before calling
977 * drm_crtc_handle_vblank() will be visible to callers of the later
0ae865ef 978 * functions, if the vblank count is the same or a later one.
bd7e3f3b
DV
979 *
980 * See also &drm_vblank_crtc.count.
3ed4351a 981 */
570e8696 982u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
67680d3c 983 ktime_t *vblanktime)
3ed4351a
DV
984{
985 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
986 vblanktime);
987}
988EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
989
b2c077d0
RC
990/**
991 * drm_crtc_next_vblank_start - calculate the time of the next vblank
992 * @crtc: the crtc for which to calculate next vblank time
993 * @vblanktime: pointer to time to receive the next vblank timestamp.
994 *
995 * Calculate the expected time of the start of the next vblank period,
996 * based on time of previous vblank and frame duration
997 */
998int drm_crtc_next_vblank_start(struct drm_crtc *crtc, ktime_t *vblanktime)
999{
6f1ccbf0
RC
1000 struct drm_vblank_crtc *vblank;
1001 struct drm_display_mode *mode;
b2c077d0
RC
1002 u64 vblank_start;
1003
6f1ccbf0
RC
1004 if (!drm_dev_has_vblank(crtc->dev))
1005 return -EINVAL;
1006
d12e3649 1007 vblank = drm_crtc_vblank_crtc(crtc);
6f1ccbf0
RC
1008 mode = &vblank->hwmode;
1009
b2c077d0
RC
1010 if (!vblank->framedur_ns || !vblank->linedur_ns)
1011 return -EINVAL;
1012
1013 if (!drm_crtc_get_last_vbltimestamp(crtc, vblanktime, false))
1014 return -EINVAL;
1015
1016 vblank_start = DIV_ROUND_DOWN_ULL(
1017 (u64)vblank->framedur_ns * mode->crtc_vblank_start,
1018 mode->crtc_vtotal);
1019 *vblanktime = ktime_add(*vblanktime, ns_to_ktime(vblank_start));
1020
1021 return 0;
1022}
1023EXPORT_SYMBOL(drm_crtc_next_vblank_start);
1024
3ed4351a
DV
1025static void send_vblank_event(struct drm_device *dev,
1026 struct drm_pending_vblank_event *e,
570e8696 1027 u64 seq, ktime_t now)
3ed4351a 1028{
bd386e51 1029 struct timespec64 tv;
3ed4351a 1030
bd386e51
KP
1031 switch (e->event.base.type) {
1032 case DRM_EVENT_VBLANK:
1033 case DRM_EVENT_FLIP_COMPLETE:
1034 tv = ktime_to_timespec64(now);
1035 e->event.vbl.sequence = seq;
1036 /*
1037 * e->event is a user space structure, with hardcoded unsigned
1038 * 32-bit seconds/microseconds. This is safe as we always use
1039 * monotonic timestamps since linux-4.15
1040 */
1041 e->event.vbl.tv_sec = tv.tv_sec;
1042 e->event.vbl.tv_usec = tv.tv_nsec / 1000;
1043 break;
3064abfa
KP
1044 case DRM_EVENT_CRTC_SEQUENCE:
1045 if (seq)
1046 e->event.seq.sequence = seq;
1047 e->event.seq.time_ns = ktime_to_ns(now);
1048 break;
bd386e51
KP
1049 }
1050 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
a78e7a51
VSS
1051 /*
1052 * Use the same timestamp for any associated fence signal to avoid
1053 * mismatch in timestamps for vsync & fence events triggered by the
1054 * same HW event. Frameworks like SurfaceFlinger in Android expects the
1055 * retire-fence timestamp to match exactly with HW vsync as it uses it
1056 * for its software vsync modeling.
1057 */
1058 drm_send_event_timestamp_locked(dev, &e->base, now);
3ed4351a
DV
1059}
1060
1061/**
1062 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1063 * @crtc: the source CRTC of the vblank event
1064 * @e: the event to send
1065 *
1066 * A lot of drivers need to generate vblank events for the very next vblank
1067 * interrupt. For example when the page flip interrupt happens when the page
1068 * flip gets armed, but not when it actually executes within the next vblank
1069 * period. This helper function implements exactly the required vblank arming
1070 * behaviour.
1071 *
1072 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
1073 * atomic commit must ensure that the next vblank happens at exactly the same
1074 * time as the atomic commit is committed to the hardware. This function itself
e13a0583 1075 * does **not** protect against the next vblank interrupt racing with either this
3ed4351a
DV
1076 * function call or the atomic commit operation. A possible sequence could be:
1077 *
1078 * 1. Driver commits new hardware state into vblank-synchronized registers.
1079 * 2. A vblank happens, committing the hardware state. Also the corresponding
1080 * vblank interrupt is fired off and fully processed by the interrupt
1081 * handler.
1082 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
1083 * 4. The event is only send out for the next vblank, which is wrong.
1084 *
1085 * An equivalent race can happen when the driver calls
1086 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
1087 *
1088 * The only way to make this work safely is to prevent the vblank from firing
1089 * (and the hardware from committing anything else) until the entire atomic
1090 * commit sequence has run to completion. If the hardware does not have such a
1091 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
1092 * Instead drivers need to manually send out the event from their interrupt
1093 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
1094 * possible race with the hardware committing the atomic update.
1095 *
e750218d
SA
1096 * Caller must hold a vblank reference for the event @e acquired by a
1097 * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
3ed4351a
DV
1098 */
1099void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1100 struct drm_pending_vblank_event *e)
1101{
1102 struct drm_device *dev = crtc->dev;
1103 unsigned int pipe = drm_crtc_index(crtc);
1104
1105 assert_spin_locked(&dev->event_lock);
1106
1107 e->pipe = pipe;
570e8696 1108 e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
3ed4351a
DV
1109 list_add_tail(&e->base.link, &dev->vblank_event_list);
1110}
1111EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1112
1113/**
1114 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1115 * @crtc: the source CRTC of the vblank event
1116 * @e: the event to send
1117 *
1118 * Updates sequence # and timestamp on event for the most recently processed
1119 * vblank, and sends it to userspace. Caller must hold event lock.
1120 *
1121 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
1122 * situation, especially to send out events for atomic commit operations.
1123 */
1124void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1125 struct drm_pending_vblank_event *e)
1126{
1127 struct drm_device *dev = crtc->dev;
570e8696
KP
1128 u64 seq;
1129 unsigned int pipe = drm_crtc_index(crtc);
67680d3c 1130 ktime_t now;
3ed4351a 1131
6015002e 1132 if (drm_dev_has_vblank(dev)) {
3ed4351a
DV
1133 seq = drm_vblank_count_and_time(dev, pipe, &now);
1134 } else {
1135 seq = 0;
1136
25e1a798 1137 now = ktime_get();
3ed4351a
DV
1138 }
1139 e->pipe = pipe;
67680d3c 1140 send_vblank_event(dev, e, seq, now);
3ed4351a
DV
1141}
1142EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1143
1144static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
1145{
1146 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1147 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1148
5a4784f4 1149 if (drm_WARN_ON(dev, !crtc))
4ffb8dee
JM
1150 return 0;
1151
3ed4351a
DV
1152 if (crtc->funcs->enable_vblank)
1153 return crtc->funcs->enable_vblank(crtc);
57bb1ee6 1154 }
3ed4351a 1155
f397d66b 1156 return -EINVAL;
3ed4351a
DV
1157}
1158
3ed4351a
DV
1159static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1160{
d12e3649 1161 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a
DV
1162 int ret = 0;
1163
1164 assert_spin_locked(&dev->vbl_lock);
1165
1166 spin_lock(&dev->vblank_time_lock);
1167
1168 if (!vblank->enabled) {
1169 /*
1170 * Enable vblank irqs under vblank_time_lock protection.
1171 * All vblank count & timestamp updates are held off
1172 * until we are done reinitializing master counter and
1173 * timestamps. Filtercode in drm_handle_vblank() will
1174 * prevent double-accounting of same vblank interval.
1175 */
1176 ret = __enable_vblank(dev, pipe);
02149a76
SR
1177 drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
1178 pipe, ret);
3ed4351a
DV
1179 if (ret) {
1180 atomic_dec(&vblank->refcount);
1181 } else {
1182 drm_update_vblank_count(dev, pipe, 0);
1183 /* drm_update_vblank_count() includes a wmb so we just
1184 * need to ensure that the compiler emits the write
1185 * to mark the vblank as enabled after the call
1186 * to drm_update_vblank_count().
1187 */
1188 WRITE_ONCE(vblank->enabled, true);
1189 }
1190 }
1191
1192 spin_unlock(&dev->vblank_time_lock);
1193
1194 return ret;
1195}
1196
5e6c2b4f 1197int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
3ed4351a 1198{
d12e3649 1199 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a
DV
1200 unsigned long irqflags;
1201 int ret = 0;
1202
6015002e 1203 if (!drm_dev_has_vblank(dev))
3ed4351a
DV
1204 return -EINVAL;
1205
5a4784f4 1206 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
1207 return -EINVAL;
1208
1209 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1210 /* Going from 0->1 means we have to enable interrupts again */
1211 if (atomic_add_return(1, &vblank->refcount) == 1) {
1212 ret = drm_vblank_enable(dev, pipe);
1213 } else {
1214 if (!vblank->enabled) {
1215 atomic_dec(&vblank->refcount);
1216 ret = -EINVAL;
1217 }
1218 }
1219 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1220
1221 return ret;
1222}
1223
1224/**
1225 * drm_crtc_vblank_get - get a reference count on vblank events
1226 * @crtc: which CRTC to own
1227 *
1228 * Acquire a reference count on vblank events to avoid having them disabled
1229 * while in use.
1230 *
1231 * Returns:
1232 * Zero on success or a negative error code on failure.
1233 */
1234int drm_crtc_vblank_get(struct drm_crtc *crtc)
1235{
1236 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1237}
1238EXPORT_SYMBOL(drm_crtc_vblank_get);
1239
5e6c2b4f 1240void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
3ed4351a 1241{
d12e3649 1242 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
0d5040e4 1243 int vblank_offdelay = vblank->config.offdelay_ms;
3ed4351a 1244
5a4784f4 1245 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
1246 return;
1247
5a4784f4 1248 if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
3ed4351a
DV
1249 return;
1250
1251 /* Last user schedules interrupt disable */
1252 if (atomic_dec_and_test(&vblank->refcount)) {
0d5040e4 1253 if (!vblank_offdelay)
3ed4351a 1254 return;
0d5040e4 1255 else if (vblank_offdelay < 0)
e99e88a9 1256 vblank_disable_fn(&vblank->disable_timer);
0d5040e4 1257 else if (!vblank->config.disable_immediate)
3ed4351a 1258 mod_timer(&vblank->disable_timer,
0d5040e4 1259 jiffies + ((vblank_offdelay * HZ) / 1000));
3ed4351a
DV
1260 }
1261}
1262
1263/**
1264 * drm_crtc_vblank_put - give up ownership of vblank events
1265 * @crtc: which counter to give up
1266 *
1267 * Release ownership of a given vblank counter, turning off interrupts
0d5040e4
HM
1268 * if possible. Disable interrupts after &drm_vblank_crtc_config.offdelay_ms
1269 * milliseconds.
3ed4351a
DV
1270 */
1271void drm_crtc_vblank_put(struct drm_crtc *crtc)
1272{
1273 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1274}
1275EXPORT_SYMBOL(drm_crtc_vblank_put);
1276
1277/**
1278 * drm_wait_one_vblank - wait for one vblank
1279 * @dev: DRM device
1280 * @pipe: CRTC index
1281 *
1282 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1283 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1284 * due to lack of driver support or because the crtc is off.
57d30230
DV
1285 *
1286 * This is the legacy version of drm_crtc_wait_one_vblank().
3ed4351a
DV
1287 */
1288void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1289{
d12e3649 1290 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a 1291 int ret;
3b765c0b 1292 u64 last;
3ed4351a 1293
5a4784f4 1294 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
1295 return;
1296
1297 ret = drm_vblank_get(dev, pipe);
5a4784f4
SR
1298 if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
1299 pipe, ret))
3ed4351a
DV
1300 return;
1301
1302 last = drm_vblank_count(dev, pipe);
1303
1304 ret = wait_event_timeout(vblank->queue,
1305 last != drm_vblank_count(dev, pipe),
1306 msecs_to_jiffies(100));
1307
5a4784f4 1308 drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
3ed4351a
DV
1309
1310 drm_vblank_put(dev, pipe);
1311}
1312EXPORT_SYMBOL(drm_wait_one_vblank);
1313
1314/**
1315 * drm_crtc_wait_one_vblank - wait for one vblank
1316 * @crtc: DRM crtc
1317 *
1318 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1319 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1320 * due to lack of driver support or because the crtc is off.
1321 */
1322void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1323{
1324 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1325}
1326EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1327
1328/**
1329 * drm_crtc_vblank_off - disable vblank events on a CRTC
1330 * @crtc: CRTC in question
1331 *
1332 * Drivers can use this function to shut down the vblank interrupt handling when
1333 * disabling a crtc. This function ensures that the latest vblank frame count is
1334 * stored so that drm_vblank_on can restore it again.
1335 *
1336 * Drivers must use this function when the hardware vblank counter can get
57d30230 1337 * reset, e.g. when suspending or disabling the @crtc in general.
3ed4351a
DV
1338 */
1339void drm_crtc_vblank_off(struct drm_crtc *crtc)
1340{
1341 struct drm_device *dev = crtc->dev;
1342 unsigned int pipe = drm_crtc_index(crtc);
d12e3649 1343 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
3ed4351a 1344 struct drm_pending_vblank_event *e, *t;
67680d3c 1345 ktime_t now;
570e8696 1346 u64 seq;
3ed4351a 1347
5a4784f4 1348 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
1349 return;
1350
5e6c2b4f
LP
1351 /*
1352 * Grab event_lock early to prevent vblank work from being scheduled
1353 * while we're in the middle of shutting down vblank interrupts
1354 */
a7e3ad5f 1355 spin_lock_irq(&dev->event_lock);
3ed4351a
DV
1356
1357 spin_lock(&dev->vbl_lock);
02149a76
SR
1358 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1359 pipe, vblank->enabled, vblank->inmodeset);
3ed4351a
DV
1360
1361 /* Avoid redundant vblank disables without previous
1362 * drm_crtc_vblank_on(). */
1363 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1364 drm_vblank_disable_and_save(dev, pipe);
1365
1366 wake_up(&vblank->queue);
1367
1368 /*
1369 * Prevent subsequent drm_vblank_get() from re-enabling
1370 * the vblank interrupt by bumping the refcount.
1371 */
1372 if (!vblank->inmodeset) {
1373 atomic_inc(&vblank->refcount);
1374 vblank->inmodeset = 1;
1375 }
1376 spin_unlock(&dev->vbl_lock);
1377
1378 /* Send any queued vblank events, lest the natives grow disquiet */
1379 seq = drm_vblank_count_and_time(dev, pipe, &now);
1380
1381 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1382 if (e->pipe != pipe)
1383 continue;
02149a76
SR
1384 drm_dbg_core(dev, "Sending premature vblank event on disable: "
1385 "wanted %llu, current %llu\n",
1386 e->sequence, seq);
3ed4351a
DV
1387 list_del(&e->base.link);
1388 drm_vblank_put(dev, pipe);
67680d3c 1389 send_vblank_event(dev, e, seq, now);
3ed4351a 1390 }
5e6c2b4f
LP
1391
1392 /* Cancel any leftover pending vblank work */
1393 drm_vblank_cancel_pending_works(vblank);
1394
a7e3ad5f 1395 spin_unlock_irq(&dev->event_lock);
3ed4351a
DV
1396
1397 /* Will be reset by the modeset helpers when re-enabling the crtc by
1398 * calling drm_calc_timestamping_constants(). */
1399 vblank->hwmode.crtc_clock = 0;
5e6c2b4f
LP
1400
1401 /* Wait for any vblank work that's still executing to finish */
1402 drm_vblank_flush_worker(vblank);
3ed4351a
DV
1403}
1404EXPORT_SYMBOL(drm_crtc_vblank_off);
1405
1406/**
1407 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1408 * @crtc: CRTC in question
1409 *
1410 * Drivers can use this function to reset the vblank state to off at load time.
1411 * Drivers should use this together with the drm_crtc_vblank_off() and
1412 * drm_crtc_vblank_on() functions. The difference compared to
1413 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1414 * and hence doesn't need to call any driver hooks.
57d30230
DV
1415 *
1416 * This is useful for recovering driver state e.g. on driver load, or on resume.
3ed4351a
DV
1417 */
1418void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1419{
1420 struct drm_device *dev = crtc->dev;
d12e3649 1421 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
3ed4351a 1422
fbc678e8 1423 spin_lock_irq(&dev->vbl_lock);
3ed4351a
DV
1424 /*
1425 * Prevent subsequent drm_vblank_get() from enabling the vblank
1426 * interrupt by bumping the refcount.
1427 */
1428 if (!vblank->inmodeset) {
1429 atomic_inc(&vblank->refcount);
1430 vblank->inmodeset = 1;
1431 }
fbc678e8 1432 spin_unlock_irq(&dev->vbl_lock);
3ed4351a 1433
5a4784f4 1434 drm_WARN_ON(dev, !list_empty(&dev->vblank_event_list));
5e6c2b4f 1435 drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
3ed4351a
DV
1436}
1437EXPORT_SYMBOL(drm_crtc_vblank_reset);
1438
ed20151a
VS
1439/**
1440 * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
1441 * @crtc: CRTC in question
1442 * @max_vblank_count: max hardware vblank counter value
1443 *
1444 * Update the maximum hardware vblank counter value for @crtc
1445 * at runtime. Useful for hardware where the operation of the
1446 * hardware vblank counter depends on the currently active
1447 * display configuration.
1448 *
1449 * For example, if the hardware vblank counter does not work
1450 * when a specific connector is active the maximum can be set
1451 * to zero. And when that specific connector isn't active the
1452 * maximum can again be set to the appropriate non-zero value.
1453 *
1454 * If used, must be called before drm_vblank_on().
1455 */
1456void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
1457 u32 max_vblank_count)
1458{
1459 struct drm_device *dev = crtc->dev;
d12e3649 1460 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
ed20151a 1461
5a4784f4
SR
1462 drm_WARN_ON(dev, dev->max_vblank_count);
1463 drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
ed20151a
VS
1464
1465 vblank->max_vblank_count = max_vblank_count;
1466}
1467EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
1468
3ed4351a 1469/**
0d5040e4
HM
1470 * drm_crtc_vblank_on_config - enable vblank events on a CRTC with custom
1471 * configuration options
3ed4351a 1472 * @crtc: CRTC in question
0d5040e4 1473 * @config: Vblank configuration value
3ed4351a 1474 *
0d5040e4
HM
1475 * See drm_crtc_vblank_on(). In addition, this function allows you to provide a
1476 * custom vblank configuration for a given CRTC.
1477 *
1478 * Note that @config is copied, the pointer does not need to stay valid beyond
1479 * this function call. For details of the parameters see
1480 * struct drm_vblank_crtc_config.
3ed4351a 1481 */
0d5040e4
HM
1482void drm_crtc_vblank_on_config(struct drm_crtc *crtc,
1483 const struct drm_vblank_crtc_config *config)
3ed4351a
DV
1484{
1485 struct drm_device *dev = crtc->dev;
1486 unsigned int pipe = drm_crtc_index(crtc);
d12e3649 1487 struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
3ed4351a 1488
5a4784f4 1489 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
1490 return;
1491
92cc68e3 1492 spin_lock_irq(&dev->vbl_lock);
02149a76
SR
1493 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1494 pipe, vblank->enabled, vblank->inmodeset);
3ed4351a 1495
0d5040e4
HM
1496 vblank->config = *config;
1497
3ed4351a
DV
1498 /* Drop our private "prevent drm_vblank_get" refcount */
1499 if (vblank->inmodeset) {
1500 atomic_dec(&vblank->refcount);
1501 vblank->inmodeset = 0;
1502 }
1503
1504 drm_reset_vblank_timestamp(dev, pipe);
1505
1506 /*
1507 * re-enable interrupts if there are users left, or the
1508 * user wishes vblank interrupts to be enabled all the time.
1509 */
0d5040e4 1510 if (atomic_read(&vblank->refcount) != 0 || !vblank->config.offdelay_ms)
5a4784f4 1511 drm_WARN_ON(dev, drm_vblank_enable(dev, pipe));
92cc68e3 1512 spin_unlock_irq(&dev->vbl_lock);
3ed4351a 1513}
0d5040e4
HM
1514EXPORT_SYMBOL(drm_crtc_vblank_on_config);
1515
1516/**
1517 * drm_crtc_vblank_on - enable vblank events on a CRTC
1518 * @crtc: CRTC in question
1519 *
1520 * This functions restores the vblank interrupt state captured with
1521 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1522 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1523 * unbalanced and so can also be unconditionally called in driver load code to
1524 * reflect the current hardware state of the crtc.
1525 *
1526 * Note that unlike in drm_crtc_vblank_on_config(), default values are used.
1527 */
1528void drm_crtc_vblank_on(struct drm_crtc *crtc)
1529{
1530 const struct drm_vblank_crtc_config config = {
1531 .offdelay_ms = drm_vblank_offdelay,
1532 .disable_immediate = crtc->dev->vblank_disable_immediate
1533 };
1534
1535 drm_crtc_vblank_on_config(crtc, &config);
1536}
3ed4351a
DV
1537EXPORT_SYMBOL(drm_crtc_vblank_on);
1538
f66aaab8 1539static void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
d0bb96b4
DP
1540{
1541 ktime_t t_vblank;
1542 struct drm_vblank_crtc *vblank;
1543 int framedur_ns;
1544 u64 diff_ns;
1545 u32 cur_vblank, diff = 1;
1546 int count = DRM_TIMESTAMP_MAXRETRIES;
167b4002 1547 u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
d0bb96b4 1548
5a4784f4 1549 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
d0bb96b4
DP
1550 return;
1551
1552 assert_spin_locked(&dev->vbl_lock);
1553 assert_spin_locked(&dev->vblank_time_lock);
1554
d12e3649 1555 vblank = drm_vblank_crtc(dev, pipe);
5a4784f4
SR
1556 drm_WARN_ONCE(dev,
1557 drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
1558 "Cannot compute missed vblanks without frame duration\n");
d0bb96b4
DP
1559 framedur_ns = vblank->framedur_ns;
1560
1561 do {
1562 cur_vblank = __get_vblank_counter(dev, pipe);
1563 drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
1564 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
1565
1566 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
1567 if (framedur_ns)
1568 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
1569
1570
02149a76
SR
1571 drm_dbg_vbl(dev,
1572 "missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1573 diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
167b4002 1574 vblank->last = (cur_vblank - diff) & max_vblank_count;
d0bb96b4 1575}
d0bb96b4
DP
1576
1577/**
ea3f0ef3
DP
1578 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1579 * @crtc: CRTC in question
1580 *
d0bb96b4 1581 * Power manamement features can cause frame counter resets between vblank
ea3f0ef3
DP
1582 * disable and enable. Drivers can use this function in their
1583 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1584 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1585 * vblank counter.
f66aaab8
DV
1586 *
1587 * Note that drivers must have race-free high-precision timestamping support,
1588 * i.e. &drm_crtc_funcs.get_vblank_timestamp must be hooked up and
0d5040e4 1589 * &drm_vblank_crtc_config.disable_immediate must be set to indicate the
f66aaab8
DV
1590 * time-stamping functions are race-free against vblank hardware counter
1591 * increments.
d0bb96b4
DP
1592 */
1593void drm_crtc_vblank_restore(struct drm_crtc *crtc)
1594{
0d5040e4
HM
1595 struct drm_device *dev = crtc->dev;
1596 unsigned int pipe = drm_crtc_index(crtc);
1597 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
1598
1599 drm_WARN_ON_ONCE(dev, !crtc->funcs->get_vblank_timestamp);
1600 drm_WARN_ON_ONCE(dev, vblank->inmodeset);
1601 drm_WARN_ON_ONCE(dev, !vblank->config.disable_immediate);
f66aaab8 1602
0d5040e4 1603 drm_vblank_restore(dev, pipe);
d0bb96b4
DP
1604}
1605EXPORT_SYMBOL(drm_crtc_vblank_restore);
1606
3ed4351a 1607static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
570e8696 1608 u64 req_seq,
3ed4351a
DV
1609 union drm_wait_vblank *vblwait,
1610 struct drm_file *file_priv)
1611{
d12e3649 1612 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a 1613 struct drm_pending_vblank_event *e;
67680d3c 1614 ktime_t now;
570e8696 1615 u64 seq;
3ed4351a
DV
1616 int ret;
1617
1618 e = kzalloc(sizeof(*e), GFP_KERNEL);
1619 if (e == NULL) {
1620 ret = -ENOMEM;
1621 goto err_put;
1622 }
1623
1624 e->pipe = pipe;
1625 e->event.base.type = DRM_EVENT_VBLANK;
bd386e51
KP
1626 e->event.base.length = sizeof(e->event.vbl);
1627 e->event.vbl.user_data = vblwait->request.signal;
1628 e->event.vbl.crtc_id = 0;
1629 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1630 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
948de842 1631
bd386e51
KP
1632 if (crtc)
1633 e->event.vbl.crtc_id = crtc->base.id;
1634 }
3ed4351a 1635
7f07c35f 1636 spin_lock_irq(&dev->event_lock);
3ed4351a
DV
1637
1638 /*
1639 * drm_crtc_vblank_off() might have been called after we called
1640 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1641 * vblank disable, so no need for further locking. The reference from
1642 * drm_vblank_get() protects against vblank disable from another source.
1643 */
1644 if (!READ_ONCE(vblank->enabled)) {
1645 ret = -EINVAL;
1646 goto err_unlock;
1647 }
1648
1649 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1650 &e->event.base);
1651
1652 if (ret)
1653 goto err_unlock;
1654
1655 seq = drm_vblank_count_and_time(dev, pipe, &now);
1656
02149a76
SR
1657 drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
1658 req_seq, seq, pipe);
3ed4351a 1659
570e8696 1660 trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
3ed4351a 1661
570e8696 1662 e->sequence = req_seq;
5e6c2b4f 1663 if (drm_vblank_passed(seq, req_seq)) {
3ed4351a 1664 drm_vblank_put(dev, pipe);
67680d3c 1665 send_vblank_event(dev, e, seq, now);
3ed4351a
DV
1666 vblwait->reply.sequence = seq;
1667 } else {
1668 /* drm_handle_vblank_events will call drm_vblank_put */
1669 list_add_tail(&e->base.link, &dev->vblank_event_list);
570e8696 1670 vblwait->reply.sequence = req_seq;
3ed4351a
DV
1671 }
1672
7f07c35f 1673 spin_unlock_irq(&dev->event_lock);
3ed4351a
DV
1674
1675 return 0;
1676
1677err_unlock:
7f07c35f 1678 spin_unlock_irq(&dev->event_lock);
3ed4351a
DV
1679 kfree(e);
1680err_put:
1681 drm_vblank_put(dev, pipe);
1682 return ret;
1683}
1684
1685static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
1686{
1687 if (vblwait->request.sequence)
1688 return false;
1689
1690 return _DRM_VBLANK_RELATIVE ==
1691 (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
1692 _DRM_VBLANK_EVENT |
1693 _DRM_VBLANK_NEXTONMISS));
1694}
1695
570e8696
KP
1696/*
1697 * Widen a 32-bit param to 64-bits.
1698 *
1699 * \param narrow 32-bit value (missing upper 32 bits)
1700 * \param near 64-bit value that should be 'close' to near
1701 *
1702 * This function returns a 64-bit value using the lower 32-bits from
1703 * 'narrow' and constructing the upper 32-bits so that the result is
1704 * as close as possible to 'near'.
1705 */
1706
1707static u64 widen_32_to_64(u32 narrow, u64 near)
1708{
1709 return near + (s32) (narrow - near);
1710}
1711
67680d3c
AB
1712static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
1713 struct drm_wait_vblank_reply *reply)
1714{
1715 ktime_t now;
1716 struct timespec64 ts;
1717
1718 /*
1719 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
25e1a798
AB
1720 * to store the seconds. This is safe as we always use monotonic
1721 * timestamps since linux-4.15.
67680d3c
AB
1722 */
1723 reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1724 ts = ktime_to_timespec64(now);
1725 reply->tval_sec = (u32)ts.tv_sec;
1726 reply->tval_usec = ts.tv_nsec / 1000;
1727}
1728
1e4cd78e
TZ
1729static bool drm_wait_vblank_supported(struct drm_device *dev)
1730{
1e4cd78e
TZ
1731 return drm_dev_has_vblank(dev);
1732}
1733
b6dcaaac
DV
1734int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
1735 struct drm_file *file_priv)
3ed4351a 1736{
7de440db 1737 struct drm_crtc *crtc;
3ed4351a
DV
1738 struct drm_vblank_crtc *vblank;
1739 union drm_wait_vblank *vblwait = data;
1740 int ret;
570e8696 1741 u64 req_seq, seq;
7de440db 1742 unsigned int pipe_index;
570e8696 1743 unsigned int flags, pipe, high_pipe;
3ed4351a 1744
1e4cd78e 1745 if (!drm_wait_vblank_supported(dev))
aed6105b 1746 return -EOPNOTSUPP;
3ed4351a
DV
1747
1748 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1749 return -EINVAL;
1750
1751 if (vblwait->request.type &
1752 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1753 _DRM_VBLANK_HIGH_CRTC_MASK)) {
02149a76
SR
1754 drm_dbg_core(dev,
1755 "Unsupported type value 0x%x, supported mask 0x%x\n",
1756 vblwait->request.type,
1757 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1758 _DRM_VBLANK_HIGH_CRTC_MASK));
3ed4351a
DV
1759 return -EINVAL;
1760 }
1761
1762 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1763 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1764 if (high_pipe)
7de440db 1765 pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
3ed4351a 1766 else
7de440db
KP
1767 pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1768
1769 /* Convert lease-relative crtc index into global crtc index */
1770 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1771 pipe = 0;
1772 drm_for_each_crtc(crtc, dev) {
1773 if (drm_lease_held(file_priv, crtc->base.id)) {
1774 if (pipe_index == 0)
1775 break;
1776 pipe_index--;
1777 }
1778 pipe++;
1779 }
1780 } else {
1781 pipe = pipe_index;
1782 }
1783
3ed4351a
DV
1784 if (pipe >= dev->num_crtcs)
1785 return -EINVAL;
1786
1787 vblank = &dev->vblank[pipe];
1788
1789 /* If the counter is currently enabled and accurate, short-circuit
1790 * queries to return the cached timestamp of the last vblank.
1791 */
0d5040e4 1792 if (vblank->config.disable_immediate &&
3ed4351a
DV
1793 drm_wait_vblank_is_query(vblwait) &&
1794 READ_ONCE(vblank->enabled)) {
67680d3c 1795 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
3ed4351a
DV
1796 return 0;
1797 }
1798
1799 ret = drm_vblank_get(dev, pipe);
1800 if (ret) {
02149a76
SR
1801 drm_dbg_core(dev,
1802 "crtc %d failed to acquire vblank counter, %d\n",
1803 pipe, ret);
3ed4351a
DV
1804 return ret;
1805 }
1806 seq = drm_vblank_count(dev, pipe);
1807
1808 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1809 case _DRM_VBLANK_RELATIVE:
570e8696
KP
1810 req_seq = seq + vblwait->request.sequence;
1811 vblwait->request.sequence = req_seq;
3ed4351a 1812 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
570e8696 1813 break;
3ed4351a 1814 case _DRM_VBLANK_ABSOLUTE:
570e8696 1815 req_seq = widen_32_to_64(vblwait->request.sequence, seq);
3ed4351a
DV
1816 break;
1817 default:
1818 ret = -EINVAL;
1819 goto done;
1820 }
1821
1822 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
5e6c2b4f 1823 drm_vblank_passed(seq, req_seq)) {
570e8696
KP
1824 req_seq = seq + 1;
1825 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
1826 vblwait->request.sequence = req_seq;
1827 }
3ed4351a
DV
1828
1829 if (flags & _DRM_VBLANK_EVENT) {
1830 /* must hold on to the vblank ref until the event fires
1831 * drm_vblank_put will be called asynchronously
1832 */
570e8696 1833 return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
3ed4351a
DV
1834 }
1835
570e8696 1836 if (req_seq != seq) {
05103ea9
SR
1837 int wait;
1838
02149a76
SR
1839 drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
1840 req_seq, pipe);
05103ea9 1841 wait = wait_event_interruptible_timeout(vblank->queue,
5e6c2b4f 1842 drm_vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
05103ea9
SR
1843 !READ_ONCE(vblank->enabled),
1844 msecs_to_jiffies(3000));
1845
1846 switch (wait) {
1847 case 0:
1848 /* timeout */
1849 ret = -EBUSY;
1850 break;
1851 case -ERESTARTSYS:
1852 /* interrupted by signal */
1853 ret = -EINTR;
1854 break;
1855 default:
1856 ret = 0;
1857 break;
1858 }
3ed4351a
DV
1859 }
1860
1861 if (ret != -EINTR) {
67680d3c 1862 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
3ed4351a 1863
02149a76
SR
1864 drm_dbg_core(dev, "crtc %d returning %u to client\n",
1865 pipe, vblwait->reply.sequence);
3ed4351a 1866 } else {
02149a76
SR
1867 drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
1868 pipe);
3ed4351a
DV
1869 }
1870
1871done:
1872 drm_vblank_put(dev, pipe);
1873 return ret;
1874}
1875
1876static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1877{
7fe3f0d1
TZ
1878 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1879 bool high_prec = false;
3ed4351a 1880 struct drm_pending_vblank_event *e, *t;
67680d3c 1881 ktime_t now;
570e8696 1882 u64 seq;
3ed4351a
DV
1883
1884 assert_spin_locked(&dev->event_lock);
1885
1886 seq = drm_vblank_count_and_time(dev, pipe, &now);
1887
1888 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1889 if (e->pipe != pipe)
1890 continue;
5e6c2b4f 1891 if (!drm_vblank_passed(seq, e->sequence))
3ed4351a
DV
1892 continue;
1893
02149a76
SR
1894 drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
1895 e->sequence, seq);
3ed4351a
DV
1896
1897 list_del(&e->base.link);
1898 drm_vblank_put(dev, pipe);
67680d3c 1899 send_vblank_event(dev, e, seq, now);
3ed4351a
DV
1900 }
1901
7fe3f0d1
TZ
1902 if (crtc && crtc->funcs->get_vblank_timestamp)
1903 high_prec = true;
7fe3f0d1
TZ
1904
1905 trace_drm_vblank_event(pipe, seq, now, high_prec);
3ed4351a
DV
1906}
1907
1908/**
1909 * drm_handle_vblank - handle a vblank event
1910 * @dev: DRM device
1911 * @pipe: index of CRTC where this event occurred
1912 *
1913 * Drivers should call this routine in their vblank interrupt handlers to
1914 * update the vblank counter and send any signals that may be pending.
1915 *
1916 * This is the legacy version of drm_crtc_handle_vblank().
1917 */
1918bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1919{
d12e3649 1920 struct drm_vblank_crtc *vblank = drm_vblank_crtc(dev, pipe);
3ed4351a
DV
1921 unsigned long irqflags;
1922 bool disable_irq;
1923
5a4784f4 1924 if (drm_WARN_ON_ONCE(dev, !drm_dev_has_vblank(dev)))
3ed4351a
DV
1925 return false;
1926
5a4784f4 1927 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
3ed4351a
DV
1928 return false;
1929
1930 spin_lock_irqsave(&dev->event_lock, irqflags);
1931
1932 /* Need timestamp lock to prevent concurrent execution with
1933 * vblank enable/disable, as this would cause inconsistent
1934 * or corrupted timestamps and vblank counts.
1935 */
1936 spin_lock(&dev->vblank_time_lock);
1937
1938 /* Vblank irq handling disabled. Nothing to do. */
1939 if (!vblank->enabled) {
1940 spin_unlock(&dev->vblank_time_lock);
1941 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1942 return false;
1943 }
1944
1945 drm_update_vblank_count(dev, pipe, true);
1946
1947 spin_unlock(&dev->vblank_time_lock);
1948
1949 wake_up(&vblank->queue);
1950
1951 /* With instant-off, we defer disabling the interrupt until after
1952 * we finish processing the following vblank after all events have
1953 * been signaled. The disable has to be last (after
1954 * drm_handle_vblank_events) so that the timestamp is always accurate.
1955 */
0d5040e4
HM
1956 disable_irq = (vblank->config.disable_immediate &&
1957 vblank->config.offdelay_ms > 0 &&
3ed4351a
DV
1958 !atomic_read(&vblank->refcount));
1959
1960 drm_handle_vblank_events(dev, pipe);
5e6c2b4f 1961 drm_handle_vblank_works(vblank);
3ed4351a
DV
1962
1963 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1964
1965 if (disable_irq)
e99e88a9 1966 vblank_disable_fn(&vblank->disable_timer);
3ed4351a
DV
1967
1968 return true;
1969}
1970EXPORT_SYMBOL(drm_handle_vblank);
1971
1972/**
1973 * drm_crtc_handle_vblank - handle a vblank event
1974 * @crtc: where this event occurred
1975 *
1976 * Drivers should call this routine in their vblank interrupt handlers to
1977 * update the vblank counter and send any signals that may be pending.
1978 *
1979 * This is the native KMS version of drm_handle_vblank().
1980 *
bd7e3f3b
DV
1981 * Note that for a given vblank counter value drm_crtc_handle_vblank()
1982 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
1983 * provide a barrier: Any writes done before calling
1984 * drm_crtc_handle_vblank() will be visible to callers of the later
0ae865ef 1985 * functions, if the vblank count is the same or a later one.
bd7e3f3b
DV
1986 *
1987 * See also &drm_vblank_crtc.count.
1988 *
3ed4351a
DV
1989 * Returns:
1990 * True if the event was successfully handled, false on failure.
1991 */
1992bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1993{
1994 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1995}
1996EXPORT_SYMBOL(drm_crtc_handle_vblank);
3064abfa
KP
1997
1998/*
1999 * Get crtc VBLANK count.
2000 *
2001 * \param dev DRM device
0ae865ef 2002 * \param data user argument, pointing to a drm_crtc_get_sequence structure.
3064abfa
KP
2003 * \param file_priv drm file private for the user's open file descriptor
2004 */
2005
2006int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
2007 struct drm_file *file_priv)
2008{
2009 struct drm_crtc *crtc;
2010 struct drm_vblank_crtc *vblank;
2011 int pipe;
2012 struct drm_crtc_get_sequence *get_seq = data;
2013 ktime_t now;
2014 bool vblank_enabled;
2015 int ret;
2016
2017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
69fdf420 2018 return -EOPNOTSUPP;
3064abfa 2019
1e4cd78e 2020 if (!drm_dev_has_vblank(dev))
aed6105b 2021 return -EOPNOTSUPP;
3064abfa
KP
2022
2023 crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
2024 if (!crtc)
2025 return -ENOENT;
2026
2027 pipe = drm_crtc_index(crtc);
2028
d12e3649 2029 vblank = drm_crtc_vblank_crtc(crtc);
0d5040e4
HM
2030 vblank_enabled = READ_ONCE(vblank->config.disable_immediate) &&
2031 READ_ONCE(vblank->enabled);
3064abfa
KP
2032
2033 if (!vblank_enabled) {
2034 ret = drm_crtc_vblank_get(crtc);
2035 if (ret) {
02149a76
SR
2036 drm_dbg_core(dev,
2037 "crtc %d failed to acquire vblank counter, %d\n",
2038 pipe, ret);
3064abfa
KP
2039 return ret;
2040 }
2041 }
2042 drm_modeset_lock(&crtc->mutex, NULL);
2043 if (crtc->state)
2044 get_seq->active = crtc->state->enable;
2045 else
2046 get_seq->active = crtc->enabled;
2047 drm_modeset_unlock(&crtc->mutex);
2048 get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
2049 get_seq->sequence_ns = ktime_to_ns(now);
2050 if (!vblank_enabled)
2051 drm_crtc_vblank_put(crtc);
2052 return 0;
2053}
2054
2055/*
2056 * Queue a event for VBLANK sequence
2057 *
2058 * \param dev DRM device
0ae865ef 2059 * \param data user argument, pointing to a drm_crtc_queue_sequence structure.
3064abfa
KP
2060 * \param file_priv drm file private for the user's open file descriptor
2061 */
2062
2063int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
2064 struct drm_file *file_priv)
2065{
2066 struct drm_crtc *crtc;
2067 struct drm_vblank_crtc *vblank;
2068 int pipe;
2069 struct drm_crtc_queue_sequence *queue_seq = data;
2070 ktime_t now;
2071 struct drm_pending_vblank_event *e;
2072 u32 flags;
2073 u64 seq;
2074 u64 req_seq;
2075 int ret;
3064abfa
KP
2076
2077 if (!drm_core_check_feature(dev, DRIVER_MODESET))
69fdf420 2078 return -EOPNOTSUPP;
3064abfa 2079
1e4cd78e 2080 if (!drm_dev_has_vblank(dev))
aed6105b 2081 return -EOPNOTSUPP;
3064abfa
KP
2082
2083 crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
2084 if (!crtc)
2085 return -ENOENT;
2086
2087 flags = queue_seq->flags;
2088 /* Check valid flag bits */
2089 if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
2090 DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
2091 return -EINVAL;
2092
2093 pipe = drm_crtc_index(crtc);
2094
d12e3649 2095 vblank = drm_crtc_vblank_crtc(crtc);
3064abfa
KP
2096
2097 e = kzalloc(sizeof(*e), GFP_KERNEL);
2098 if (e == NULL)
2099 return -ENOMEM;
2100
2101 ret = drm_crtc_vblank_get(crtc);
2102 if (ret) {
02149a76
SR
2103 drm_dbg_core(dev,
2104 "crtc %d failed to acquire vblank counter, %d\n",
2105 pipe, ret);
3064abfa
KP
2106 goto err_free;
2107 }
2108
2109 seq = drm_vblank_count_and_time(dev, pipe, &now);
2110 req_seq = queue_seq->sequence;
2111
2112 if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
2113 req_seq += seq;
2114
5e6c2b4f 2115 if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && drm_vblank_passed(seq, req_seq))
3064abfa
KP
2116 req_seq = seq + 1;
2117
2118 e->pipe = pipe;
2119 e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
2120 e->event.base.length = sizeof(e->event.seq);
2121 e->event.seq.user_data = queue_seq->user_data;
2122
5887a2ad 2123 spin_lock_irq(&dev->event_lock);
3064abfa
KP
2124
2125 /*
2126 * drm_crtc_vblank_off() might have been called after we called
2127 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
2128 * vblank disable, so no need for further locking. The reference from
2129 * drm_crtc_vblank_get() protects against vblank disable from another source.
2130 */
2131 if (!READ_ONCE(vblank->enabled)) {
2132 ret = -EINVAL;
2133 goto err_unlock;
2134 }
2135
2136 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
2137 &e->event.base);
2138
2139 if (ret)
2140 goto err_unlock;
2141
2142 e->sequence = req_seq;
2143
5e6c2b4f 2144 if (drm_vblank_passed(seq, req_seq)) {
3064abfa
KP
2145 drm_crtc_vblank_put(crtc);
2146 send_vblank_event(dev, e, seq, now);
2147 queue_seq->sequence = seq;
2148 } else {
2149 /* drm_handle_vblank_events will call drm_vblank_put */
2150 list_add_tail(&e->base.link, &dev->vblank_event_list);
2151 queue_seq->sequence = req_seq;
2152 }
2153
5887a2ad 2154 spin_unlock_irq(&dev->event_lock);
3064abfa
KP
2155 return 0;
2156
2157err_unlock:
5887a2ad 2158 spin_unlock_irq(&dev->event_lock);
3064abfa
KP
2159 drm_crtc_vblank_put(crtc);
2160err_free:
2161 kfree(e);
2162 return ret;
2163}
5e6c2b4f 2164