blob: 27b6db073a5c2c043aa7e4578fe07cd730615c7e [file] [log] [blame]
Daniel Vetter3ed43512017-05-31 11:21:46 +02001/*
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
27#include <drm/drm_vblank.h>
28#include <drm/drmP.h>
29#include <linux/export.h>
30
31#include "drm_trace.h"
32#include "drm_internal.h"
33
Daniel Vetter57d30232017-05-24 16:51:45 +020034/**
35 * DOC: vblank handling
36 *
37 * Vertical blanking plays a major role in graphics rendering. To achieve
38 * tear-free display, users must synchronize page flips and/or rendering to
39 * vertical blanking. The DRM API offers ioctls to perform page flips
40 * synchronized to vertical blanking and wait for vertical blanking.
41 *
42 * The DRM core handles most of the vertical blanking management logic, which
43 * involves filtering out spurious interrupts, keeping race-free blanking
44 * counters, coping with counter wrap-around and resets and keeping use counts.
45 * It relies on the driver to generate vertical blanking interrupts and
46 * optionally provide a hardware vertical blanking counter.
47 *
48 * Drivers must initialize the vertical blanking handling core with a call to
49 * drm_vblank_init(). Minimally, a driver needs to implement
50 * &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
52 * support.
53 *
54 * Vertical blanking interrupts can be enabled by the DRM core or by drivers
55 * themselves (for instance to handle page flipping operations). The DRM core
56 * maintains a vertical blanking use count to ensure that the interrupts are not
57 * disabled while a user still needs them. To increment the use count, drivers
58 * call drm_crtc_vblank_get() and release the vblank reference again with
59 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
60 * guaranteed to be enabled.
61 *
62 * On many hardware disabling the vblank interrupt cannot be done in a race-free
63 * manner, see &drm_driver.vblank_disable_immediate and
64 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
65 * vblanks after a timer has expired, which can be configured through the
66 * ``vblankoffdelay`` module parameter.
67 */
68
Daniel Vetter3ed43512017-05-31 11:21:46 +020069/* Retry timestamp calculation up to 3 times to satisfy
70 * drm_timestamp_precision before giving up.
71 */
72#define DRM_TIMESTAMP_MAXRETRIES 3
73
74/* Threshold in nanoseconds for detection of redundant
75 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
76 */
77#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
78
79static bool
80drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +020081 ktime_t *tvblank, bool in_vblank_irq);
Daniel Vetter3ed43512017-05-31 11:21:46 +020082
83static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
84
Daniel Vetter3ed43512017-05-31 11:21:46 +020085static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
86
87module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
88module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
Daniel Vetter3ed43512017-05-31 11:21:46 +020089MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
90MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
Daniel Vetter3ed43512017-05-31 11:21:46 +020091
92static void store_vblank(struct drm_device *dev, unsigned int pipe,
93 u32 vblank_count_inc,
Arnd Bergmann67680d32017-10-11 17:20:12 +020094 ktime_t t_vblank, u32 last)
Daniel Vetter3ed43512017-05-31 11:21:46 +020095{
96 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
97
98 assert_spin_locked(&dev->vblank_time_lock);
99
100 vblank->last = last;
101
102 write_seqlock(&vblank->seqlock);
Arnd Bergmann67680d32017-10-11 17:20:12 +0200103 vblank->time = t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200104 vblank->count += vblank_count_inc;
105 write_sequnlock(&vblank->seqlock);
106}
107
108/*
109 * "No hw counter" fallback implementation of .get_vblank_counter() hook,
110 * if there is no useable hardware frame counter available.
111 */
112static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
113{
114 WARN_ON_ONCE(dev->max_vblank_count != 0);
115 return 0;
116}
117
118static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
119{
120 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
121 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
122
123 if (crtc->funcs->get_vblank_counter)
124 return crtc->funcs->get_vblank_counter(crtc);
125 }
126
127 if (dev->driver->get_vblank_counter)
128 return dev->driver->get_vblank_counter(dev, pipe);
129
130 return drm_vblank_no_hw_counter(dev, pipe);
131}
132
133/*
134 * Reset the stored timestamp for the current vblank count to correspond
135 * to the last vblank occurred.
136 *
137 * Only to be called from drm_crtc_vblank_on().
138 *
139 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
140 * device vblank fields.
141 */
142static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
143{
144 u32 cur_vblank;
145 bool rc;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200146 ktime_t t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200147 int count = DRM_TIMESTAMP_MAXRETRIES;
148
149 spin_lock(&dev->vblank_time_lock);
150
151 /*
152 * sample the current counter to avoid random jumps
153 * when drm_vblank_enable() applies the diff
154 */
155 do {
156 cur_vblank = __get_vblank_counter(dev, pipe);
157 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
158 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
159
160 /*
161 * Only reinitialize corresponding vblank timestamp if high-precision query
162 * available and didn't fail. Otherwise reinitialize delayed at next vblank
163 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
164 */
165 if (!rc)
Arnd Bergmann67680d32017-10-11 17:20:12 +0200166 t_vblank = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200167
168 /*
169 * +1 to make sure user will never see the same
170 * vblank counter value before and after a modeset
171 */
Arnd Bergmann67680d32017-10-11 17:20:12 +0200172 store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200173
174 spin_unlock(&dev->vblank_time_lock);
175}
176
177/*
178 * Call back into the driver to update the appropriate vblank counter
179 * (specified by @pipe). Deal with wraparound, if it occurred, and
180 * update the last read value so we can deal with wraparound on the next
181 * call if necessary.
182 *
183 * Only necessary when going from off->on, to account for frames we
184 * didn't get an interrupt for.
185 *
186 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
187 * device vblank fields.
188 */
189static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
190 bool in_vblank_irq)
191{
192 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
193 u32 cur_vblank, diff;
194 bool rc;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200195 ktime_t t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200196 int count = DRM_TIMESTAMP_MAXRETRIES;
197 int framedur_ns = vblank->framedur_ns;
198
199 /*
200 * Interrupts were disabled prior to this call, so deal with counter
201 * wrap if needed.
202 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
203 * here if the register is small or we had vblank interrupts off for
204 * a long time.
205 *
206 * We repeat the hardware vblank counter & timestamp query until
207 * we get consistent results. This to prevent races between gpu
208 * updating its hardware counter while we are retrieving the
209 * corresponding vblank timestamp.
210 */
211 do {
212 cur_vblank = __get_vblank_counter(dev, pipe);
213 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
214 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
215
216 if (dev->max_vblank_count != 0) {
217 /* trust the hw counter when it's around */
218 diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
219 } else if (rc && framedur_ns) {
Arnd Bergmann67680d32017-10-11 17:20:12 +0200220 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200221
222 /*
223 * Figure out how many vblanks we've missed based
224 * on the difference in the timestamps and the
225 * frame/field duration.
226 */
227 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
228
229 if (diff == 0 && in_vblank_irq)
230 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
231 " diff_ns = %lld, framedur_ns = %d)\n",
232 pipe, (long long) diff_ns, framedur_ns);
233 } else {
234 /* some kind of default for drivers w/o accurate vbl timestamping */
235 diff = in_vblank_irq ? 1 : 0;
236 }
237
238 /*
239 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
240 * interval? If so then vblank irqs keep running and it will likely
241 * happen that the hardware vblank counter is not trustworthy as it
242 * might reset at some point in that interval and vblank timestamps
243 * are not trustworthy either in that interval. Iow. this can result
244 * in a bogus diff >> 1 which must be avoided as it would cause
245 * random large forward jumps of the software vblank counter.
246 */
247 if (diff > 1 && (vblank->inmodeset & 0x2)) {
248 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
249 " due to pre-modeset.\n", pipe, diff);
250 diff = 1;
251 }
252
253 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
Keith Packard570e8692017-10-12 11:57:49 -0700254 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
Daniel Vetter3ed43512017-05-31 11:21:46 +0200255 pipe, vblank->count, diff, cur_vblank, vblank->last);
256
257 if (diff == 0) {
258 WARN_ON_ONCE(cur_vblank != vblank->last);
259 return;
260 }
261
262 /*
263 * Only reinitialize corresponding vblank timestamp if high-precision query
264 * available and didn't fail, or we were called from the vblank interrupt.
265 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
266 * for now, to mark the vblanktimestamp as invalid.
267 */
Laurent Pinchart138b87f2017-06-29 15:37:20 +0300268 if (!rc && !in_vblank_irq)
Arnd Bergmann67680d32017-10-11 17:20:12 +0200269 t_vblank = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200270
Arnd Bergmann67680d32017-10-11 17:20:12 +0200271 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200272}
273
274static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
275{
276 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
277
278 if (WARN_ON(pipe >= dev->num_crtcs))
279 return 0;
280
281 return vblank->count;
282}
283
284/**
Daniel Vetterca814b22017-05-24 16:51:47 +0200285 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
Daniel Vetter3ed43512017-05-31 11:21:46 +0200286 * @crtc: which counter to retrieve
287 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200288 * This function is similar to drm_crtc_vblank_count() but this function
289 * interpolates to handle a race with vblank interrupts using the high precision
290 * timestamping support.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200291 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200292 * This is mostly useful for hardware that can obtain the scanout position, but
293 * doesn't have a hardware frame counter.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200294 */
Daniel Vetterca814b22017-05-24 16:51:47 +0200295u32 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200296{
297 struct drm_device *dev = crtc->dev;
298 unsigned int pipe = drm_crtc_index(crtc);
299 u32 vblank;
300 unsigned long flags;
301
302 WARN(!dev->driver->get_vblank_timestamp,
303 "This function requires support for accurate vblank timestamps.");
304
305 spin_lock_irqsave(&dev->vblank_time_lock, flags);
306
307 drm_update_vblank_count(dev, pipe, false);
308 vblank = drm_vblank_count(dev, pipe);
309
310 spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
311
312 return vblank;
313}
Daniel Vetterca814b22017-05-24 16:51:47 +0200314EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200315
316static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
317{
318 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
319 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
320
321 if (crtc->funcs->disable_vblank) {
322 crtc->funcs->disable_vblank(crtc);
323 return;
324 }
325 }
326
327 dev->driver->disable_vblank(dev, pipe);
328}
329
330/*
331 * Disable vblank irq's on crtc, make sure that last vblank count
332 * of hardware and corresponding consistent software vblank counter
333 * are preserved, even if there are any spurious vblank irq's after
334 * disable.
335 */
336void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
337{
338 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
339 unsigned long irqflags;
340
341 assert_spin_locked(&dev->vbl_lock);
342
343 /* Prevent vblank irq processing while disabling vblank irqs,
344 * so no updates of timestamps or count can happen after we've
345 * disabled. Needed to prevent races in case of delayed irq's.
346 */
347 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
348
349 /*
350 * Only disable vblank interrupts if they're enabled. This avoids
351 * calling the ->disable_vblank() operation in atomic context with the
352 * hardware potentially runtime suspended.
353 */
354 if (vblank->enabled) {
355 __disable_vblank(dev, pipe);
356 vblank->enabled = false;
357 }
358
359 /*
360 * Always update the count and timestamp to maintain the
361 * appearance that the counter has been ticking all along until
362 * this time. This makes the count account for the entire time
363 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
364 */
365 drm_update_vblank_count(dev, pipe, false);
366
367 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
368}
369
370static void vblank_disable_fn(unsigned long arg)
371{
372 struct drm_vblank_crtc *vblank = (void *)arg;
373 struct drm_device *dev = vblank->dev;
374 unsigned int pipe = vblank->pipe;
375 unsigned long irqflags;
376
377 spin_lock_irqsave(&dev->vbl_lock, irqflags);
378 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
379 DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
380 drm_vblank_disable_and_save(dev, pipe);
381 }
382 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
383}
384
Daniel Vetter3ed43512017-05-31 11:21:46 +0200385void drm_vblank_cleanup(struct drm_device *dev)
386{
387 unsigned int pipe;
388
389 /* Bail if the driver didn't call drm_vblank_init() */
390 if (dev->num_crtcs == 0)
391 return;
392
393 for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
394 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
395
396 WARN_ON(READ_ONCE(vblank->enabled) &&
397 drm_core_check_feature(dev, DRIVER_MODESET));
398
399 del_timer_sync(&vblank->disable_timer);
400 }
401
402 kfree(dev->vblank);
403
404 dev->num_crtcs = 0;
405}
Daniel Vetter3ed43512017-05-31 11:21:46 +0200406
407/**
408 * drm_vblank_init - initialize vblank support
409 * @dev: DRM device
410 * @num_crtcs: number of CRTCs supported by @dev
411 *
412 * This function initializes vblank support for @num_crtcs display pipelines.
Daniel Vetterb4164d62017-06-26 18:19:49 +0200413 * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
414 * drivers with a &drm_driver.release callback.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200415 *
416 * Returns:
417 * Zero on success or a negative error code on failure.
418 */
419int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
420{
421 int ret = -ENOMEM;
422 unsigned int i;
423
424 spin_lock_init(&dev->vbl_lock);
425 spin_lock_init(&dev->vblank_time_lock);
426
427 dev->num_crtcs = num_crtcs;
428
429 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
430 if (!dev->vblank)
431 goto err;
432
433 for (i = 0; i < num_crtcs; i++) {
434 struct drm_vblank_crtc *vblank = &dev->vblank[i];
435
436 vblank->dev = dev;
437 vblank->pipe = i;
438 init_waitqueue_head(&vblank->queue);
439 setup_timer(&vblank->disable_timer, vblank_disable_fn,
440 (unsigned long)vblank);
441 seqlock_init(&vblank->seqlock);
442 }
443
444 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
445
446 /* Driver specific high-precision vblank timestamping supported? */
447 if (dev->driver->get_vblank_timestamp)
448 DRM_INFO("Driver supports precise vblank timestamp query.\n");
449 else
450 DRM_INFO("No driver support for vblank timestamp query.\n");
451
452 /* Must have precise timestamping for reliable vblank instant disable */
453 if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
454 dev->vblank_disable_immediate = false;
455 DRM_INFO("Setting vblank_disable_immediate to false because "
456 "get_vblank_timestamp == NULL\n");
457 }
458
459 return 0;
460
461err:
462 dev->num_crtcs = 0;
463 return ret;
464}
465EXPORT_SYMBOL(drm_vblank_init);
466
467/**
468 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
469 * @crtc: which CRTC's vblank waitqueue to retrieve
470 *
471 * This function returns a pointer to the vblank waitqueue for the CRTC.
472 * Drivers can use this to implement vblank waits using wait_event() and related
473 * functions.
474 */
475wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
476{
477 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
478}
479EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
480
481
482/**
483 * drm_calc_timestamping_constants - calculate vblank timestamp constants
484 * @crtc: drm_crtc whose timestamp constants should be updated.
485 * @mode: display mode containing the scanout timings
486 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200487 * Calculate and store various constants which are later needed by vblank and
488 * swap-completion timestamping, e.g, by
489 * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
490 * scanout timing, so they take things like panel scaling or other adjustments
491 * into account.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200492 */
493void drm_calc_timestamping_constants(struct drm_crtc *crtc,
494 const struct drm_display_mode *mode)
495{
496 struct drm_device *dev = crtc->dev;
497 unsigned int pipe = drm_crtc_index(crtc);
498 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
499 int linedur_ns = 0, framedur_ns = 0;
500 int dotclock = mode->crtc_clock;
501
502 if (!dev->num_crtcs)
503 return;
504
505 if (WARN_ON(pipe >= dev->num_crtcs))
506 return;
507
508 /* Valid dotclock? */
509 if (dotclock > 0) {
510 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
511
512 /*
513 * Convert scanline length in pixels and video
514 * dot clock to line duration and frame duration
515 * in nanoseconds:
516 */
517 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
518 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
519
520 /*
521 * Fields of interlaced scanout modes are only half a frame duration.
522 */
523 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
524 framedur_ns /= 2;
525 } else
526 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
527 crtc->base.id);
528
529 vblank->linedur_ns = linedur_ns;
530 vblank->framedur_ns = framedur_ns;
531 vblank->hwmode = *mode;
532
533 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
534 crtc->base.id, mode->crtc_htotal,
535 mode->crtc_vtotal, mode->crtc_vdisplay);
536 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
537 crtc->base.id, dotclock, framedur_ns, linedur_ns);
538}
539EXPORT_SYMBOL(drm_calc_timestamping_constants);
540
541/**
542 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
543 * @dev: DRM device
544 * @pipe: index of CRTC whose vblank timestamp to retrieve
545 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
546 * On return contains true maximum error of timestamp
Arnd Bergmann67680d32017-10-11 17:20:12 +0200547 * @vblank_time: Pointer to time which should receive the timestamp
Daniel Vetter3ed43512017-05-31 11:21:46 +0200548 * @in_vblank_irq:
549 * True when called from drm_crtc_handle_vblank(). Some drivers
550 * need to apply some workarounds for gpu-specific vblank irq quirks
551 * if flag is set.
552 *
553 * Implements calculation of exact vblank timestamps from given drm_display_mode
Daniel Vetter57d30232017-05-24 16:51:45 +0200554 * timings and current video scanout position of a CRTC. This can be directly
555 * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
556 * if &drm_driver.get_scanout_position is implemented.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200557 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200558 * The current implementation only handles standard video modes. For double scan
559 * and interlaced modes the driver is supposed to adjust the hardware mode
560 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
561 * match the scanout position reported.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200562 *
563 * Note that atomic drivers must call drm_calc_timestamping_constants() before
564 * enabling a CRTC. The atomic helpers already take care of that in
565 * drm_atomic_helper_update_legacy_modeset_state().
566 *
567 * Returns:
568 *
569 * Returns true on success, and false on failure, i.e. when no accurate
570 * timestamp could be acquired.
571 */
572bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
573 unsigned int pipe,
574 int *max_error,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200575 ktime_t *vblank_time,
Daniel Vetter3ed43512017-05-31 11:21:46 +0200576 bool in_vblank_irq)
577{
Arnd Bergmann67680d32017-10-11 17:20:12 +0200578 struct timespec64 ts_etime, ts_vblank_time;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200579 ktime_t stime, etime;
580 bool vbl_status;
581 struct drm_crtc *crtc;
582 const struct drm_display_mode *mode;
583 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
584 int vpos, hpos, i;
585 int delta_ns, duration_ns;
586
587 if (!drm_core_check_feature(dev, DRIVER_MODESET))
588 return false;
589
590 crtc = drm_crtc_from_index(dev, pipe);
591
592 if (pipe >= dev->num_crtcs || !crtc) {
593 DRM_ERROR("Invalid crtc %u\n", pipe);
594 return false;
595 }
596
597 /* Scanout position query not supported? Should not happen. */
598 if (!dev->driver->get_scanout_position) {
599 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
600 return false;
601 }
602
603 if (drm_drv_uses_atomic_modeset(dev))
604 mode = &vblank->hwmode;
605 else
606 mode = &crtc->hwmode;
607
608 /* If mode timing undefined, just return as no-op:
609 * Happens during initial modesetting of a crtc.
610 */
611 if (mode->crtc_clock == 0) {
612 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
613 WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
614
615 return false;
616 }
617
618 /* Get current scanout position with system timestamp.
619 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
620 * if single query takes longer than max_error nanoseconds.
621 *
622 * This guarantees a tight bound on maximum error if
623 * code gets preempted or delayed for some reason.
624 */
625 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
626 /*
627 * Get vertical and horizontal scanout position vpos, hpos,
628 * and bounding timestamps stime, etime, pre/post query.
629 */
630 vbl_status = dev->driver->get_scanout_position(dev, pipe,
631 in_vblank_irq,
632 &vpos, &hpos,
633 &stime, &etime,
634 mode);
635
636 /* Return as no-op if scanout query unsupported or failed. */
637 if (!vbl_status) {
638 DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
639 pipe);
640 return false;
641 }
642
643 /* Compute uncertainty in timestamp of scanout position query. */
644 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
645
646 /* Accept result with < max_error nsecs timing uncertainty. */
647 if (duration_ns <= *max_error)
648 break;
649 }
650
651 /* Noisy system timing? */
652 if (i == DRM_TIMESTAMP_MAXRETRIES) {
653 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
654 pipe, duration_ns/1000, *max_error/1000, i);
655 }
656
657 /* Return upper bound of timestamp precision error. */
658 *max_error = duration_ns;
659
660 /* Convert scanout position into elapsed time at raw_time query
661 * since start of scanout at first display scanline. delta_ns
662 * can be negative if start of scanout hasn't happened yet.
663 */
664 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
665 mode->crtc_clock);
666
Daniel Vetter3ed43512017-05-31 11:21:46 +0200667 /* save this only for debugging purposes */
Arnd Bergmann67680d32017-10-11 17:20:12 +0200668 ts_etime = ktime_to_timespec64(etime);
669 ts_vblank_time = ktime_to_timespec64(*vblank_time);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200670 /* Subtract time delta from raw timestamp to get final
671 * vblank_time timestamp for end of vblank.
672 */
673 etime = ktime_sub_ns(etime, delta_ns);
Arnd Bergmann67680d32017-10-11 17:20:12 +0200674 *vblank_time = etime;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200675
Arnd Bergmann67680d32017-10-11 17:20:12 +0200676 DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
Daniel Vetter3ed43512017-05-31 11:21:46 +0200677 pipe, hpos, vpos,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200678 (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
679 (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
680 duration_ns / 1000, i);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200681
682 return true;
683}
684EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
685
Daniel Vetter3ed43512017-05-31 11:21:46 +0200686/**
687 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
688 * vblank interval
689 * @dev: DRM device
690 * @pipe: index of CRTC whose vblank timestamp to retrieve
Arnd Bergmann67680d32017-10-11 17:20:12 +0200691 * @tvblank: Pointer to target time which should receive the timestamp
Daniel Vetter3ed43512017-05-31 11:21:46 +0200692 * @in_vblank_irq:
693 * True when called from drm_crtc_handle_vblank(). Some drivers
694 * need to apply some workarounds for gpu-specific vblank irq quirks
695 * if flag is set.
696 *
697 * Fetches the system timestamp corresponding to the time of the most recent
698 * vblank interval on specified CRTC. May call into kms-driver to
699 * compute the timestamp with a high-precision GPU specific method.
700 *
701 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
702 * call, i.e., it isn't very precisely locked to the true vblank.
703 *
704 * Returns:
705 * True if timestamp is considered to be very precise, false otherwise.
706 */
707static bool
708drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200709 ktime_t *tvblank, bool in_vblank_irq)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200710{
711 bool ret = false;
712
713 /* Define requested maximum error on timestamps (nanoseconds). */
714 int max_error = (int) drm_timestamp_precision * 1000;
715
716 /* Query driver if possible and precision timestamping enabled. */
717 if (dev->driver->get_vblank_timestamp && (max_error > 0))
718 ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
719 tvblank, in_vblank_irq);
720
721 /* GPU high precision timestamp query unsupported or failed.
722 * Return current monotonic/gettimeofday timestamp as best estimate.
723 */
724 if (!ret)
Arnd Bergmann25e1a792017-10-11 17:20:13 +0200725 *tvblank = ktime_get();
Daniel Vetter3ed43512017-05-31 11:21:46 +0200726
727 return ret;
728}
729
730/**
731 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
732 * @crtc: which counter to retrieve
733 *
734 * Fetches the "cooked" vblank count value that represents the number of
735 * vblank events since the system was booted, including lost events due to
Daniel Vetter57d30232017-05-24 16:51:45 +0200736 * modesetting activity. Note that this timer isn't correct against a racing
737 * vblank interrupt (since it only reports the software vblank counter), see
Daniel Vetterca814b22017-05-24 16:51:47 +0200738 * drm_crtc_accurate_vblank_count() for such use-cases.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200739 *
740 * Returns:
741 * The software vblank counter.
742 */
Keith Packard570e8692017-10-12 11:57:49 -0700743u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200744{
745 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
746}
747EXPORT_SYMBOL(drm_crtc_vblank_count);
748
Keith Packard570e8692017-10-12 11:57:49 -0700749/**
750 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
751 * system timestamp corresponding to that vblank counter value.
752 * @dev: DRM device
753 * @pipe: index of CRTC whose counter to retrieve
754 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
755 *
756 * Fetches the "cooked" vblank count value that represents the number of
757 * vblank events since the system was booted, including lost events due to
758 * modesetting activity. Returns corresponding system timestamp of the time
759 * of the vblank interval that corresponds to the current vblank counter value.
760 *
761 * This is the legacy version of drm_crtc_vblank_count_and_time().
762 */
763static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200764 ktime_t *vblanktime)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200765{
766 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Keith Packard570e8692017-10-12 11:57:49 -0700767 u64 vblank_count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200768 unsigned int seq;
769
770 if (WARN_ON(pipe >= dev->num_crtcs)) {
Arnd Bergmann67680d32017-10-11 17:20:12 +0200771 *vblanktime = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200772 return 0;
773 }
774
775 do {
776 seq = read_seqbegin(&vblank->seqlock);
777 vblank_count = vblank->count;
778 *vblanktime = vblank->time;
779 } while (read_seqretry(&vblank->seqlock, seq));
780
781 return vblank_count;
782}
783
784/**
785 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
786 * and the system timestamp corresponding to that vblank counter value
787 * @crtc: which counter to retrieve
Arnd Bergmann67680d32017-10-11 17:20:12 +0200788 * @vblanktime: Pointer to time to receive the vblank timestamp.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200789 *
790 * Fetches the "cooked" vblank count value that represents the number of
791 * vblank events since the system was booted, including lost events due to
792 * modesetting activity. Returns corresponding system timestamp of the time
793 * of the vblank interval that corresponds to the current vblank counter value.
794 */
Keith Packard570e8692017-10-12 11:57:49 -0700795u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200796 ktime_t *vblanktime)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200797{
798 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
799 vblanktime);
800}
801EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
802
803static void send_vblank_event(struct drm_device *dev,
804 struct drm_pending_vblank_event *e,
Keith Packard570e8692017-10-12 11:57:49 -0700805 u64 seq, ktime_t now)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200806{
Keith Packardbd386e52017-07-05 14:34:23 -0700807 struct timespec64 tv;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200808
Keith Packardbd386e52017-07-05 14:34:23 -0700809 switch (e->event.base.type) {
810 case DRM_EVENT_VBLANK:
811 case DRM_EVENT_FLIP_COMPLETE:
812 tv = ktime_to_timespec64(now);
813 e->event.vbl.sequence = seq;
814 /*
815 * e->event is a user space structure, with hardcoded unsigned
816 * 32-bit seconds/microseconds. This is safe as we always use
817 * monotonic timestamps since linux-4.15
818 */
819 e->event.vbl.tv_sec = tv.tv_sec;
820 e->event.vbl.tv_usec = tv.tv_nsec / 1000;
821 break;
822 }
823 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200824 drm_send_event_locked(dev, &e->base);
825}
826
827/**
828 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
829 * @crtc: the source CRTC of the vblank event
830 * @e: the event to send
831 *
832 * A lot of drivers need to generate vblank events for the very next vblank
833 * interrupt. For example when the page flip interrupt happens when the page
834 * flip gets armed, but not when it actually executes within the next vblank
835 * period. This helper function implements exactly the required vblank arming
836 * behaviour.
837 *
838 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
839 * atomic commit must ensure that the next vblank happens at exactly the same
840 * time as the atomic commit is committed to the hardware. This function itself
Daniel Vettere13a0582017-07-05 06:56:29 +0200841 * does **not** protect against the next vblank interrupt racing with either this
Daniel Vetter3ed43512017-05-31 11:21:46 +0200842 * function call or the atomic commit operation. A possible sequence could be:
843 *
844 * 1. Driver commits new hardware state into vblank-synchronized registers.
845 * 2. A vblank happens, committing the hardware state. Also the corresponding
846 * vblank interrupt is fired off and fully processed by the interrupt
847 * handler.
848 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
849 * 4. The event is only send out for the next vblank, which is wrong.
850 *
851 * An equivalent race can happen when the driver calls
852 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
853 *
854 * The only way to make this work safely is to prevent the vblank from firing
855 * (and the hardware from committing anything else) until the entire atomic
856 * commit sequence has run to completion. If the hardware does not have such a
857 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
858 * Instead drivers need to manually send out the event from their interrupt
859 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
860 * possible race with the hardware committing the atomic update.
861 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200862 * Caller must hold a vblank reference for the event @e, which will be dropped
863 * when the next vblank arrives.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200864 */
865void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
866 struct drm_pending_vblank_event *e)
867{
868 struct drm_device *dev = crtc->dev;
869 unsigned int pipe = drm_crtc_index(crtc);
870
871 assert_spin_locked(&dev->event_lock);
872
873 e->pipe = pipe;
Keith Packard570e8692017-10-12 11:57:49 -0700874 e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200875 list_add_tail(&e->base.link, &dev->vblank_event_list);
876}
877EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
878
879/**
880 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
881 * @crtc: the source CRTC of the vblank event
882 * @e: the event to send
883 *
884 * Updates sequence # and timestamp on event for the most recently processed
885 * vblank, and sends it to userspace. Caller must hold event lock.
886 *
887 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
888 * situation, especially to send out events for atomic commit operations.
889 */
890void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
891 struct drm_pending_vblank_event *e)
892{
893 struct drm_device *dev = crtc->dev;
Keith Packard570e8692017-10-12 11:57:49 -0700894 u64 seq;
895 unsigned int pipe = drm_crtc_index(crtc);
Arnd Bergmann67680d32017-10-11 17:20:12 +0200896 ktime_t now;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200897
898 if (dev->num_crtcs > 0) {
899 seq = drm_vblank_count_and_time(dev, pipe, &now);
900 } else {
901 seq = 0;
902
Arnd Bergmann25e1a792017-10-11 17:20:13 +0200903 now = ktime_get();
Daniel Vetter3ed43512017-05-31 11:21:46 +0200904 }
905 e->pipe = pipe;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200906 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200907}
908EXPORT_SYMBOL(drm_crtc_send_vblank_event);
909
910static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
911{
912 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
913 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
914
915 if (crtc->funcs->enable_vblank)
916 return crtc->funcs->enable_vblank(crtc);
917 }
918
919 return dev->driver->enable_vblank(dev, pipe);
920}
921
Daniel Vetter3ed43512017-05-31 11:21:46 +0200922static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
923{
924 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
925 int ret = 0;
926
927 assert_spin_locked(&dev->vbl_lock);
928
929 spin_lock(&dev->vblank_time_lock);
930
931 if (!vblank->enabled) {
932 /*
933 * Enable vblank irqs under vblank_time_lock protection.
934 * All vblank count & timestamp updates are held off
935 * until we are done reinitializing master counter and
936 * timestamps. Filtercode in drm_handle_vblank() will
937 * prevent double-accounting of same vblank interval.
938 */
939 ret = __enable_vblank(dev, pipe);
940 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
941 if (ret) {
942 atomic_dec(&vblank->refcount);
943 } else {
944 drm_update_vblank_count(dev, pipe, 0);
945 /* drm_update_vblank_count() includes a wmb so we just
946 * need to ensure that the compiler emits the write
947 * to mark the vblank as enabled after the call
948 * to drm_update_vblank_count().
949 */
950 WRITE_ONCE(vblank->enabled, true);
951 }
952 }
953
954 spin_unlock(&dev->vblank_time_lock);
955
956 return ret;
957}
958
Daniel Vetter3ed43512017-05-31 11:21:46 +0200959static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
960{
961 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
962 unsigned long irqflags;
963 int ret = 0;
964
965 if (!dev->num_crtcs)
966 return -EINVAL;
967
968 if (WARN_ON(pipe >= dev->num_crtcs))
969 return -EINVAL;
970
971 spin_lock_irqsave(&dev->vbl_lock, irqflags);
972 /* Going from 0->1 means we have to enable interrupts again */
973 if (atomic_add_return(1, &vblank->refcount) == 1) {
974 ret = drm_vblank_enable(dev, pipe);
975 } else {
976 if (!vblank->enabled) {
977 atomic_dec(&vblank->refcount);
978 ret = -EINVAL;
979 }
980 }
981 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
982
983 return ret;
984}
985
986/**
987 * drm_crtc_vblank_get - get a reference count on vblank events
988 * @crtc: which CRTC to own
989 *
990 * Acquire a reference count on vblank events to avoid having them disabled
991 * while in use.
992 *
993 * Returns:
994 * Zero on success or a negative error code on failure.
995 */
996int drm_crtc_vblank_get(struct drm_crtc *crtc)
997{
998 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
999}
1000EXPORT_SYMBOL(drm_crtc_vblank_get);
1001
Daniel Vetter3ed43512017-05-31 11:21:46 +02001002static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1003{
1004 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1005
1006 if (WARN_ON(pipe >= dev->num_crtcs))
1007 return;
1008
1009 if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1010 return;
1011
1012 /* Last user schedules interrupt disable */
1013 if (atomic_dec_and_test(&vblank->refcount)) {
1014 if (drm_vblank_offdelay == 0)
1015 return;
1016 else if (drm_vblank_offdelay < 0)
1017 vblank_disable_fn((unsigned long)vblank);
1018 else if (!dev->vblank_disable_immediate)
1019 mod_timer(&vblank->disable_timer,
1020 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1021 }
1022}
1023
1024/**
1025 * drm_crtc_vblank_put - give up ownership of vblank events
1026 * @crtc: which counter to give up
1027 *
1028 * Release ownership of a given vblank counter, turning off interrupts
1029 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1030 */
1031void drm_crtc_vblank_put(struct drm_crtc *crtc)
1032{
1033 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1034}
1035EXPORT_SYMBOL(drm_crtc_vblank_put);
1036
1037/**
1038 * drm_wait_one_vblank - wait for one vblank
1039 * @dev: DRM device
1040 * @pipe: CRTC index
1041 *
1042 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1043 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1044 * due to lack of driver support or because the crtc is off.
Daniel Vetter57d30232017-05-24 16:51:45 +02001045 *
1046 * This is the legacy version of drm_crtc_wait_one_vblank().
Daniel Vetter3ed43512017-05-31 11:21:46 +02001047 */
1048void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1049{
1050 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1051 int ret;
1052 u32 last;
1053
1054 if (WARN_ON(pipe >= dev->num_crtcs))
1055 return;
1056
1057 ret = drm_vblank_get(dev, pipe);
1058 if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1059 return;
1060
1061 last = drm_vblank_count(dev, pipe);
1062
1063 ret = wait_event_timeout(vblank->queue,
1064 last != drm_vblank_count(dev, pipe),
1065 msecs_to_jiffies(100));
1066
1067 WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1068
1069 drm_vblank_put(dev, pipe);
1070}
1071EXPORT_SYMBOL(drm_wait_one_vblank);
1072
1073/**
1074 * drm_crtc_wait_one_vblank - wait for one vblank
1075 * @crtc: DRM crtc
1076 *
1077 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1078 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1079 * due to lack of driver support or because the crtc is off.
1080 */
1081void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1082{
1083 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1084}
1085EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1086
1087/**
1088 * drm_crtc_vblank_off - disable vblank events on a CRTC
1089 * @crtc: CRTC in question
1090 *
1091 * Drivers can use this function to shut down the vblank interrupt handling when
1092 * disabling a crtc. This function ensures that the latest vblank frame count is
1093 * stored so that drm_vblank_on can restore it again.
1094 *
1095 * Drivers must use this function when the hardware vblank counter can get
Daniel Vetter57d30232017-05-24 16:51:45 +02001096 * reset, e.g. when suspending or disabling the @crtc in general.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001097 */
1098void drm_crtc_vblank_off(struct drm_crtc *crtc)
1099{
1100 struct drm_device *dev = crtc->dev;
1101 unsigned int pipe = drm_crtc_index(crtc);
1102 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1103 struct drm_pending_vblank_event *e, *t;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001104
1105 ktime_t now;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001106 unsigned long irqflags;
Keith Packard570e8692017-10-12 11:57:49 -07001107 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001108
1109 if (WARN_ON(pipe >= dev->num_crtcs))
1110 return;
1111
1112 spin_lock_irqsave(&dev->event_lock, irqflags);
1113
1114 spin_lock(&dev->vbl_lock);
1115 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1116 pipe, vblank->enabled, vblank->inmodeset);
1117
1118 /* Avoid redundant vblank disables without previous
1119 * drm_crtc_vblank_on(). */
1120 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1121 drm_vblank_disable_and_save(dev, pipe);
1122
1123 wake_up(&vblank->queue);
1124
1125 /*
1126 * Prevent subsequent drm_vblank_get() from re-enabling
1127 * the vblank interrupt by bumping the refcount.
1128 */
1129 if (!vblank->inmodeset) {
1130 atomic_inc(&vblank->refcount);
1131 vblank->inmodeset = 1;
1132 }
1133 spin_unlock(&dev->vbl_lock);
1134
1135 /* Send any queued vblank events, lest the natives grow disquiet */
1136 seq = drm_vblank_count_and_time(dev, pipe, &now);
1137
1138 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1139 if (e->pipe != pipe)
1140 continue;
1141 DRM_DEBUG("Sending premature vblank event on disable: "
Keith Packard570e8692017-10-12 11:57:49 -07001142 "wanted %llu, current %llu\n",
1143 e->sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001144 list_del(&e->base.link);
1145 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001146 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001147 }
1148 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1149
1150 /* Will be reset by the modeset helpers when re-enabling the crtc by
1151 * calling drm_calc_timestamping_constants(). */
1152 vblank->hwmode.crtc_clock = 0;
1153}
1154EXPORT_SYMBOL(drm_crtc_vblank_off);
1155
1156/**
1157 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1158 * @crtc: CRTC in question
1159 *
1160 * Drivers can use this function to reset the vblank state to off at load time.
1161 * Drivers should use this together with the drm_crtc_vblank_off() and
1162 * drm_crtc_vblank_on() functions. The difference compared to
1163 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1164 * and hence doesn't need to call any driver hooks.
Daniel Vetter57d30232017-05-24 16:51:45 +02001165 *
1166 * This is useful for recovering driver state e.g. on driver load, or on resume.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001167 */
1168void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1169{
1170 struct drm_device *dev = crtc->dev;
1171 unsigned long irqflags;
1172 unsigned int pipe = drm_crtc_index(crtc);
1173 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1174
1175 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1176 /*
1177 * Prevent subsequent drm_vblank_get() from enabling the vblank
1178 * interrupt by bumping the refcount.
1179 */
1180 if (!vblank->inmodeset) {
1181 atomic_inc(&vblank->refcount);
1182 vblank->inmodeset = 1;
1183 }
1184 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1185
1186 WARN_ON(!list_empty(&dev->vblank_event_list));
1187}
1188EXPORT_SYMBOL(drm_crtc_vblank_reset);
1189
1190/**
1191 * drm_crtc_vblank_on - enable vblank events on a CRTC
1192 * @crtc: CRTC in question
1193 *
1194 * This functions restores the vblank interrupt state captured with
Daniel Vetter57d30232017-05-24 16:51:45 +02001195 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1196 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1197 * unbalanced and so can also be unconditionally called in driver load code to
1198 * reflect the current hardware state of the crtc.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001199 */
1200void drm_crtc_vblank_on(struct drm_crtc *crtc)
1201{
1202 struct drm_device *dev = crtc->dev;
1203 unsigned int pipe = drm_crtc_index(crtc);
1204 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1205 unsigned long irqflags;
1206
1207 if (WARN_ON(pipe >= dev->num_crtcs))
1208 return;
1209
1210 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1211 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1212 pipe, vblank->enabled, vblank->inmodeset);
1213
1214 /* Drop our private "prevent drm_vblank_get" refcount */
1215 if (vblank->inmodeset) {
1216 atomic_dec(&vblank->refcount);
1217 vblank->inmodeset = 0;
1218 }
1219
1220 drm_reset_vblank_timestamp(dev, pipe);
1221
1222 /*
1223 * re-enable interrupts if there are users left, or the
1224 * user wishes vblank interrupts to be enabled all the time.
1225 */
1226 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1227 WARN_ON(drm_vblank_enable(dev, pipe));
1228 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1229}
1230EXPORT_SYMBOL(drm_crtc_vblank_on);
1231
1232static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
1233 unsigned int pipe)
1234{
1235 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1236
1237 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1238 if (!dev->num_crtcs)
1239 return;
1240
1241 if (WARN_ON(pipe >= dev->num_crtcs))
1242 return;
1243
1244 /*
1245 * To avoid all the problems that might happen if interrupts
1246 * were enabled/disabled around or between these calls, we just
1247 * have the kernel take a reference on the CRTC (just once though
1248 * to avoid corrupting the count if multiple, mismatch calls occur),
1249 * so that interrupts remain enabled in the interim.
1250 */
1251 if (!vblank->inmodeset) {
1252 vblank->inmodeset = 0x1;
1253 if (drm_vblank_get(dev, pipe) == 0)
1254 vblank->inmodeset |= 0x2;
1255 }
1256}
1257
1258static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
1259 unsigned int pipe)
1260{
1261 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1262 unsigned long irqflags;
1263
1264 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1265 if (!dev->num_crtcs)
1266 return;
1267
1268 if (WARN_ON(pipe >= dev->num_crtcs))
1269 return;
1270
1271 if (vblank->inmodeset) {
1272 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1273 drm_reset_vblank_timestamp(dev, pipe);
1274 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1275
1276 if (vblank->inmodeset & 0x2)
1277 drm_vblank_put(dev, pipe);
1278
1279 vblank->inmodeset = 0;
1280 }
1281}
1282
Daniel Vetterb6dcaaac2017-05-24 16:51:46 +02001283int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
1284 struct drm_file *file_priv)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001285{
1286 struct drm_modeset_ctl *modeset = data;
1287 unsigned int pipe;
1288
1289 /* If drm_vblank_init() hasn't been called yet, just no-op */
1290 if (!dev->num_crtcs)
1291 return 0;
1292
1293 /* KMS drivers handle this internally */
1294 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1295 return 0;
1296
1297 pipe = modeset->crtc;
1298 if (pipe >= dev->num_crtcs)
1299 return -EINVAL;
1300
1301 switch (modeset->cmd) {
1302 case _DRM_PRE_MODESET:
1303 drm_legacy_vblank_pre_modeset(dev, pipe);
1304 break;
1305 case _DRM_POST_MODESET:
1306 drm_legacy_vblank_post_modeset(dev, pipe);
1307 break;
1308 default:
1309 return -EINVAL;
1310 }
1311
1312 return 0;
1313}
1314
Keith Packard570e8692017-10-12 11:57:49 -07001315static inline bool vblank_passed(u64 seq, u64 ref)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001316{
1317 return (seq - ref) <= (1 << 23);
1318}
1319
1320static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
Keith Packard570e8692017-10-12 11:57:49 -07001321 u64 req_seq,
Daniel Vetter3ed43512017-05-31 11:21:46 +02001322 union drm_wait_vblank *vblwait,
1323 struct drm_file *file_priv)
1324{
1325 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1326 struct drm_pending_vblank_event *e;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001327 ktime_t now;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001328 unsigned long flags;
Keith Packard570e8692017-10-12 11:57:49 -07001329 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001330 int ret;
1331
1332 e = kzalloc(sizeof(*e), GFP_KERNEL);
1333 if (e == NULL) {
1334 ret = -ENOMEM;
1335 goto err_put;
1336 }
1337
1338 e->pipe = pipe;
1339 e->event.base.type = DRM_EVENT_VBLANK;
Keith Packardbd386e52017-07-05 14:34:23 -07001340 e->event.base.length = sizeof(e->event.vbl);
1341 e->event.vbl.user_data = vblwait->request.signal;
1342 e->event.vbl.crtc_id = 0;
1343 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1344 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1345 if (crtc)
1346 e->event.vbl.crtc_id = crtc->base.id;
1347 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001348
1349 spin_lock_irqsave(&dev->event_lock, flags);
1350
1351 /*
1352 * drm_crtc_vblank_off() might have been called after we called
1353 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1354 * vblank disable, so no need for further locking. The reference from
1355 * drm_vblank_get() protects against vblank disable from another source.
1356 */
1357 if (!READ_ONCE(vblank->enabled)) {
1358 ret = -EINVAL;
1359 goto err_unlock;
1360 }
1361
1362 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1363 &e->event.base);
1364
1365 if (ret)
1366 goto err_unlock;
1367
1368 seq = drm_vblank_count_and_time(dev, pipe, &now);
1369
Keith Packard570e8692017-10-12 11:57:49 -07001370 DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
1371 req_seq, seq, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001372
Keith Packard570e8692017-10-12 11:57:49 -07001373 trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001374
Keith Packard570e8692017-10-12 11:57:49 -07001375 e->sequence = req_seq;
1376 if (vblank_passed(seq, req_seq)) {
Daniel Vetter3ed43512017-05-31 11:21:46 +02001377 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001378 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001379 vblwait->reply.sequence = seq;
1380 } else {
1381 /* drm_handle_vblank_events will call drm_vblank_put */
1382 list_add_tail(&e->base.link, &dev->vblank_event_list);
Keith Packard570e8692017-10-12 11:57:49 -07001383 vblwait->reply.sequence = req_seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001384 }
1385
1386 spin_unlock_irqrestore(&dev->event_lock, flags);
1387
1388 return 0;
1389
1390err_unlock:
1391 spin_unlock_irqrestore(&dev->event_lock, flags);
1392 kfree(e);
1393err_put:
1394 drm_vblank_put(dev, pipe);
1395 return ret;
1396}
1397
1398static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
1399{
1400 if (vblwait->request.sequence)
1401 return false;
1402
1403 return _DRM_VBLANK_RELATIVE ==
1404 (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
1405 _DRM_VBLANK_EVENT |
1406 _DRM_VBLANK_NEXTONMISS));
1407}
1408
Keith Packard570e8692017-10-12 11:57:49 -07001409/*
1410 * Widen a 32-bit param to 64-bits.
1411 *
1412 * \param narrow 32-bit value (missing upper 32 bits)
1413 * \param near 64-bit value that should be 'close' to near
1414 *
1415 * This function returns a 64-bit value using the lower 32-bits from
1416 * 'narrow' and constructing the upper 32-bits so that the result is
1417 * as close as possible to 'near'.
1418 */
1419
1420static u64 widen_32_to_64(u32 narrow, u64 near)
1421{
1422 return near + (s32) (narrow - near);
1423}
1424
Arnd Bergmann67680d32017-10-11 17:20:12 +02001425static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
1426 struct drm_wait_vblank_reply *reply)
1427{
1428 ktime_t now;
1429 struct timespec64 ts;
1430
1431 /*
1432 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
Arnd Bergmann25e1a792017-10-11 17:20:13 +02001433 * to store the seconds. This is safe as we always use monotonic
1434 * timestamps since linux-4.15.
Arnd Bergmann67680d32017-10-11 17:20:12 +02001435 */
1436 reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1437 ts = ktime_to_timespec64(now);
1438 reply->tval_sec = (u32)ts.tv_sec;
1439 reply->tval_usec = ts.tv_nsec / 1000;
1440}
1441
Daniel Vetterb6dcaaac2017-05-24 16:51:46 +02001442int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
1443 struct drm_file *file_priv)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001444{
1445 struct drm_vblank_crtc *vblank;
1446 union drm_wait_vblank *vblwait = data;
1447 int ret;
Keith Packard570e8692017-10-12 11:57:49 -07001448 u64 req_seq, seq;
1449 unsigned int flags, pipe, high_pipe;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001450
1451 if (!dev->irq_enabled)
1452 return -EINVAL;
1453
1454 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1455 return -EINVAL;
1456
1457 if (vblwait->request.type &
1458 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1459 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1460 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1461 vblwait->request.type,
1462 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1463 _DRM_VBLANK_HIGH_CRTC_MASK));
1464 return -EINVAL;
1465 }
1466
1467 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1468 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1469 if (high_pipe)
1470 pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1471 else
1472 pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1473 if (pipe >= dev->num_crtcs)
1474 return -EINVAL;
1475
1476 vblank = &dev->vblank[pipe];
1477
1478 /* If the counter is currently enabled and accurate, short-circuit
1479 * queries to return the cached timestamp of the last vblank.
1480 */
1481 if (dev->vblank_disable_immediate &&
1482 drm_wait_vblank_is_query(vblwait) &&
1483 READ_ONCE(vblank->enabled)) {
Arnd Bergmann67680d32017-10-11 17:20:12 +02001484 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001485 return 0;
1486 }
1487
1488 ret = drm_vblank_get(dev, pipe);
1489 if (ret) {
1490 DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1491 return ret;
1492 }
1493 seq = drm_vblank_count(dev, pipe);
1494
1495 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1496 case _DRM_VBLANK_RELATIVE:
Keith Packard570e8692017-10-12 11:57:49 -07001497 req_seq = seq + vblwait->request.sequence;
1498 vblwait->request.sequence = req_seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001499 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Keith Packard570e8692017-10-12 11:57:49 -07001500 break;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001501 case _DRM_VBLANK_ABSOLUTE:
Keith Packard570e8692017-10-12 11:57:49 -07001502 req_seq = widen_32_to_64(vblwait->request.sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001503 break;
1504 default:
1505 ret = -EINVAL;
1506 goto done;
1507 }
1508
1509 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Keith Packard570e8692017-10-12 11:57:49 -07001510 vblank_passed(seq, req_seq)) {
1511 req_seq = seq + 1;
1512 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
1513 vblwait->request.sequence = req_seq;
1514 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001515
1516 if (flags & _DRM_VBLANK_EVENT) {
1517 /* must hold on to the vblank ref until the event fires
1518 * drm_vblank_put will be called asynchronously
1519 */
Keith Packard570e8692017-10-12 11:57:49 -07001520 return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001521 }
1522
Keith Packard570e8692017-10-12 11:57:49 -07001523 if (req_seq != seq) {
1524 DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
1525 req_seq, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001526 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1527 vblank_passed(drm_vblank_count(dev, pipe),
Keith Packard570e8692017-10-12 11:57:49 -07001528 req_seq) ||
Daniel Vetter3ed43512017-05-31 11:21:46 +02001529 !READ_ONCE(vblank->enabled));
1530 }
1531
1532 if (ret != -EINTR) {
Arnd Bergmann67680d32017-10-11 17:20:12 +02001533 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001534
1535 DRM_DEBUG("crtc %d returning %u to client\n",
1536 pipe, vblwait->reply.sequence);
1537 } else {
1538 DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
1539 }
1540
1541done:
1542 drm_vblank_put(dev, pipe);
1543 return ret;
1544}
1545
1546static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1547{
1548 struct drm_pending_vblank_event *e, *t;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001549 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001550 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001551
1552 assert_spin_locked(&dev->event_lock);
1553
1554 seq = drm_vblank_count_and_time(dev, pipe, &now);
1555
1556 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1557 if (e->pipe != pipe)
1558 continue;
Keith Packard570e8692017-10-12 11:57:49 -07001559 if (!vblank_passed(seq, e->sequence))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001560 continue;
1561
Keith Packard570e8692017-10-12 11:57:49 -07001562 DRM_DEBUG("vblank event on %llu, current %llu\n",
1563 e->sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001564
1565 list_del(&e->base.link);
1566 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001567 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001568 }
1569
1570 trace_drm_vblank_event(pipe, seq);
1571}
1572
1573/**
1574 * drm_handle_vblank - handle a vblank event
1575 * @dev: DRM device
1576 * @pipe: index of CRTC where this event occurred
1577 *
1578 * Drivers should call this routine in their vblank interrupt handlers to
1579 * update the vblank counter and send any signals that may be pending.
1580 *
1581 * This is the legacy version of drm_crtc_handle_vblank().
1582 */
1583bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1584{
1585 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1586 unsigned long irqflags;
1587 bool disable_irq;
1588
1589 if (WARN_ON_ONCE(!dev->num_crtcs))
1590 return false;
1591
1592 if (WARN_ON(pipe >= dev->num_crtcs))
1593 return false;
1594
1595 spin_lock_irqsave(&dev->event_lock, irqflags);
1596
1597 /* Need timestamp lock to prevent concurrent execution with
1598 * vblank enable/disable, as this would cause inconsistent
1599 * or corrupted timestamps and vblank counts.
1600 */
1601 spin_lock(&dev->vblank_time_lock);
1602
1603 /* Vblank irq handling disabled. Nothing to do. */
1604 if (!vblank->enabled) {
1605 spin_unlock(&dev->vblank_time_lock);
1606 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1607 return false;
1608 }
1609
1610 drm_update_vblank_count(dev, pipe, true);
1611
1612 spin_unlock(&dev->vblank_time_lock);
1613
1614 wake_up(&vblank->queue);
1615
1616 /* With instant-off, we defer disabling the interrupt until after
1617 * we finish processing the following vblank after all events have
1618 * been signaled. The disable has to be last (after
1619 * drm_handle_vblank_events) so that the timestamp is always accurate.
1620 */
1621 disable_irq = (dev->vblank_disable_immediate &&
1622 drm_vblank_offdelay > 0 &&
1623 !atomic_read(&vblank->refcount));
1624
1625 drm_handle_vblank_events(dev, pipe);
1626
1627 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1628
1629 if (disable_irq)
1630 vblank_disable_fn((unsigned long)vblank);
1631
1632 return true;
1633}
1634EXPORT_SYMBOL(drm_handle_vblank);
1635
1636/**
1637 * drm_crtc_handle_vblank - handle a vblank event
1638 * @crtc: where this event occurred
1639 *
1640 * Drivers should call this routine in their vblank interrupt handlers to
1641 * update the vblank counter and send any signals that may be pending.
1642 *
1643 * This is the native KMS version of drm_handle_vblank().
1644 *
1645 * Returns:
1646 * True if the event was successfully handled, false on failure.
1647 */
1648bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1649{
1650 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1651}
1652EXPORT_SYMBOL(drm_crtc_handle_vblank);