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