xref: /dragonfly/sys/dev/drm/drm_irq.c (revision 62dc643e)
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 
8 /*
9  * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
10  *
11  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
12  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
13  * All Rights Reserved.
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a
16  * copy of this software and associated documentation files (the "Software"),
17  * to deal in the Software without restriction, including without limitation
18  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19  * and/or sell copies of the Software, and to permit persons to whom the
20  * Software is furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the next
23  * paragraph) shall be included in all copies or substantial portions of the
24  * Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
29  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32  * OTHER DEALINGS IN THE SOFTWARE.
33  */
34 
35 #include <drm/drmP.h>
36 #include "drm_trace.h"
37 #include "drm_internal.h"
38 
39 #include <linux/slab.h>
40 
41 #include <linux/export.h>
42 
43 /* Access macro for slots in vblank timestamp ringbuffer. */
44 #define vblanktimestamp(dev, pipe, count) \
45 	((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
46 
47 /* Retry timestamp calculation up to 3 times to satisfy
48  * drm_timestamp_precision before giving up.
49  */
50 #define DRM_TIMESTAMP_MAXRETRIES 3
51 
52 /* Threshold in nanoseconds for detection of redundant
53  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
54  */
55 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
56 
57 static bool
58 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
59 			  struct timeval *tvblank, unsigned flags);
60 
61 unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
62 
63 /*
64  * Default to use monotonic timestamps for wait-for-vblank and page-flip
65  * complete events.
66  */
67 unsigned int drm_timestamp_monotonic = 1;
68 
69 int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
70 
71 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
72 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
73 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
74 MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
75 MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
76 MODULE_PARM_DESC(timestamp_monotonic, "Use monotonic timestamps");
77 
78 static void store_vblank(struct drm_device *dev, unsigned int pipe,
79 			 u32 vblank_count_inc,
80 			 struct timeval *t_vblank, u32 last)
81 {
82 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
83 	u32 tslot;
84 
85 	assert_spin_locked(&dev->vblank_time_lock);
86 
87 	vblank->last = last;
88 
89 	/* All writers hold the spinlock, but readers are serialized by
90 	 * the latching of vblank->count below.
91 	 */
92 	tslot = vblank->count + vblank_count_inc;
93 	vblanktimestamp(dev, pipe, tslot) = *t_vblank;
94 
95 	/*
96 	 * vblank timestamp updates are protected on the write side with
97 	 * vblank_time_lock, but on the read side done locklessly using a
98 	 * sequence-lock on the vblank counter. Ensure correct ordering using
99 	 * memory barrriers. We need the barrier both before and also after the
100 	 * counter update to synchronize with the next timestamp write.
101 	 * The read-side barriers for this are in drm_vblank_count_and_time.
102 	 */
103 	smp_wmb();
104 	vblank->count += vblank_count_inc;
105 	smp_wmb();
106 }
107 
108 /**
109  * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
110  * @dev: DRM device
111  * @pipe: index of CRTC for which to reset the timestamp
112  *
113  * Reset the stored timestamp for the current vblank count to correspond
114  * to the last vblank occurred.
115  *
116  * Only to be called from drm_vblank_on().
117  *
118  * Note: caller must hold dev->vbl_lock since this reads & writes
119  * device vblank fields.
120  */
121 static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
122 {
123 	u32 cur_vblank;
124 	bool rc;
125 	struct timeval t_vblank;
126 	int count = DRM_TIMESTAMP_MAXRETRIES;
127 
128 	lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
129 
130 	/*
131 	 * sample the current counter to avoid random jumps
132 	 * when drm_vblank_enable() applies the diff
133 	 */
134 	do {
135 		cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
136 		rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
137 	} while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
138 
139 	/*
140 	 * Only reinitialize corresponding vblank timestamp if high-precision query
141 	 * available and didn't fail. Otherwise reinitialize delayed at next vblank
142 	 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
143 	 */
144 	if (!rc)
145 		t_vblank = (struct timeval) {0, 0};
146 
147 	/*
148 	 * +1 to make sure user will never see the same
149 	 * vblank counter value before and after a modeset
150 	 */
151 	store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
152 
153 	lockmgr(&dev->vblank_time_lock, LK_RELEASE);
154 }
155 
156 /**
157  * drm_update_vblank_count - update the master vblank counter
158  * @dev: DRM device
159  * @pipe: counter to update
160  *
161  * Call back into the driver to update the appropriate vblank counter
162  * (specified by @pipe).  Deal with wraparound, if it occurred, and
163  * update the last read value so we can deal with wraparound on the next
164  * call if necessary.
165  *
166  * Only necessary when going from off->on, to account for frames we
167  * didn't get an interrupt for.
168  *
169  * Note: caller must hold dev->vbl_lock since this reads & writes
170  * device vblank fields.
171  */
172 static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
173 				    unsigned long flags)
174 {
175 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
176 	u32 cur_vblank, diff;
177 	bool rc;
178 	struct timeval t_vblank;
179 	int count = DRM_TIMESTAMP_MAXRETRIES;
180 	int framedur_ns = vblank->framedur_ns;
181 
182 	/*
183 	 * Interrupts were disabled prior to this call, so deal with counter
184 	 * wrap if needed.
185 	 * NOTE!  It's possible we lost a full dev->max_vblank_count + 1 events
186 	 * here if the register is small or we had vblank interrupts off for
187 	 * a long time.
188 	 *
189 	 * We repeat the hardware vblank counter & timestamp query until
190 	 * we get consistent results. This to prevent races between gpu
191 	 * updating its hardware counter while we are retrieving the
192 	 * corresponding vblank timestamp.
193 	 */
194 	do {
195 		cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
196 		rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
197 	} while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
198 
199 	if (dev->max_vblank_count != 0) {
200 		/* trust the hw counter when it's around */
201 		diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
202 	} else if (rc && framedur_ns) {
203 		const struct timeval *t_old;
204 		u64 diff_ns;
205 
206 		t_old = &vblanktimestamp(dev, pipe, vblank->count);
207 		diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
208 
209 		/*
210 		 * Figure out how many vblanks we've missed based
211 		 * on the difference in the timestamps and the
212 		 * frame/field duration.
213 		 */
214 		diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
215 
216 		if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
217 			DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
218 				      " diff_ns = %lld, framedur_ns = %d)\n",
219 				      pipe, (long long) diff_ns, framedur_ns);
220 	} else {
221 		/* some kind of default for drivers w/o accurate vbl timestamping */
222 		diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
223 	}
224 
225 	/*
226 	 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
227 	 * interval? If so then vblank irqs keep running and it will likely
228 	 * happen that the hardware vblank counter is not trustworthy as it
229 	 * might reset at some point in that interval and vblank timestamps
230 	 * are not trustworthy either in that interval. Iow. this can result
231 	 * in a bogus diff >> 1 which must be avoided as it would cause
232 	 * random large forward jumps of the software vblank counter.
233 	 */
234 	if (diff > 1 && (vblank->inmodeset & 0x2)) {
235 		DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
236 			      " due to pre-modeset.\n", pipe, diff);
237 		diff = 1;
238 	}
239 
240 	/*
241 	 * FIMXE: Need to replace this hack with proper seqlocks.
242 	 *
243 	 * Restrict the bump of the software vblank counter to a safe maximum
244 	 * value of +1 whenever there is the possibility that concurrent readers
245 	 * of vblank timestamps could be active at the moment, as the current
246 	 * implementation of the timestamp caching and updating is not safe
247 	 * against concurrent readers for calls to store_vblank() with a bump
248 	 * of anything but +1. A bump != 1 would very likely return corrupted
249 	 * timestamps to userspace, because the same slot in the cache could
250 	 * be concurrently written by store_vblank() and read by one of those
251 	 * readers without the read-retry logic detecting the collision.
252 	 *
253 	 * Concurrent readers can exist when we are called from the
254 	 * drm_vblank_off() or drm_vblank_on() functions and other non-vblank-
255 	 * irq callers. However, all those calls to us are happening with the
256 	 * vbl_lock locked to prevent drm_vblank_get(), so the vblank refcount
257 	 * can't increase while we are executing. Therefore a zero refcount at
258 	 * this point is safe for arbitrary counter bumps if we are called
259 	 * outside vblank irq, a non-zero count is not 100% safe. Unfortunately
260 	 * we must also accept a refcount of 1, as whenever we are called from
261 	 * drm_vblank_get() -> drm_vblank_enable() the refcount will be 1 and
262 	 * we must let that one pass through in order to not lose vblank counts
263 	 * during vblank irq off - which would completely defeat the whole
264 	 * point of this routine.
265 	 *
266 	 * Whenever we are called from vblank irq, we have to assume concurrent
267 	 * readers exist or can show up any time during our execution, even if
268 	 * the refcount is currently zero, as vblank irqs are usually only
269 	 * enabled due to the presence of readers, and because when we are called
270 	 * from vblank irq we can't hold the vbl_lock to protect us from sudden
271 	 * bumps in vblank refcount. Therefore also restrict bumps to +1 when
272 	 * called from vblank irq.
273 	 */
274 	if ((diff > 1) && (atomic_read(&vblank->refcount) > 1 ||
275 	    (flags & DRM_CALLED_FROM_VBLIRQ))) {
276 		DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u "
277 			      "refcount %u, vblirq %u\n", pipe, diff,
278 			      atomic_read(&vblank->refcount),
279 			      (flags & DRM_CALLED_FROM_VBLIRQ) != 0);
280 		diff = 1;
281 	}
282 
283 	DRM_DEBUG_VBL("updating vblank count on crtc %u:"
284 		      " current=%u, diff=%u, hw=%u hw_last=%u\n",
285 		      pipe, vblank->count, diff, cur_vblank, vblank->last);
286 
287 	if (diff == 0) {
288 		WARN_ON_ONCE(cur_vblank != vblank->last);
289 		return;
290 	}
291 
292 	/*
293 	 * Only reinitialize corresponding vblank timestamp if high-precision query
294 	 * available and didn't fail, or we were called from the vblank interrupt.
295 	 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
296 	 * for now, to mark the vblanktimestamp as invalid.
297 	 */
298 	if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
299 		t_vblank = (struct timeval) {0, 0};
300 
301 	store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
302 }
303 
304 /*
305  * Disable vblank irq's on crtc, make sure that last vblank count
306  * of hardware and corresponding consistent software vblank counter
307  * are preserved, even if there are any spurious vblank irq's after
308  * disable.
309  */
310 static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
311 {
312 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
313 	unsigned long irqflags;
314 
315 	/* Prevent vblank irq processing while disabling vblank irqs,
316 	 * so no updates of timestamps or count can happen after we've
317 	 * disabled. Needed to prevent races in case of delayed irq's.
318 	 */
319 	spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
320 
321 	/*
322 	 * Only disable vblank interrupts if they're enabled. This avoids
323 	 * calling the ->disable_vblank() operation in atomic context with the
324 	 * hardware potentially runtime suspended.
325 	 */
326 	if (vblank->enabled) {
327 		dev->driver->disable_vblank(dev, pipe);
328 		vblank->enabled = false;
329 	}
330 
331 	/*
332 	 * Always update the count and timestamp to maintain the
333 	 * appearance that the counter has been ticking all along until
334 	 * this time. This makes the count account for the entire time
335 	 * between drm_vblank_on() and drm_vblank_off().
336 	 */
337 	drm_update_vblank_count(dev, pipe, 0);
338 
339 	spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
340 }
341 
342 static void vblank_disable_fn(unsigned long arg)
343 {
344 	struct drm_vblank_crtc *vblank = (void *)arg;
345 	struct drm_device *dev = vblank->dev;
346 	unsigned int pipe = vblank->pipe;
347 	unsigned long irqflags;
348 
349 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
350 	if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
351 		DRM_DEBUG_VBLANK("disabling vblank on crtc %u\n", pipe);
352 		vblank_disable_and_save(dev, pipe);
353 	}
354 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
355 }
356 
357 /**
358  * drm_vblank_cleanup - cleanup vblank support
359  * @dev: DRM device
360  *
361  * This function cleans up any resources allocated in drm_vblank_init.
362  */
363 void drm_vblank_cleanup(struct drm_device *dev)
364 {
365 	unsigned int pipe;
366 
367 	/* Bail if the driver didn't call drm_vblank_init() */
368 	if (dev->num_crtcs == 0)
369 		return;
370 
371 	for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
372 		struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
373 
374 		WARN_ON(vblank->enabled &&
375 			drm_core_check_feature(dev, DRIVER_MODESET));
376 
377 		del_timer_sync(&vblank->disable_timer);
378 	}
379 
380 	kfree(dev->vblank);
381 
382 	dev->num_crtcs = 0;
383 }
384 EXPORT_SYMBOL(drm_vblank_cleanup);
385 
386 /**
387  * drm_vblank_init - initialize vblank support
388  * @dev: DRM device
389  * @num_crtcs: number of CRTCs supported by @dev
390  *
391  * This function initializes vblank support for @num_crtcs display pipelines.
392  *
393  * Returns:
394  * Zero on success or a negative error code on failure.
395  */
396 int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
397 {
398 	int ret = -ENOMEM;
399 	unsigned int i;
400 
401 	lockinit(&dev->vbl_lock, "drmvbl", 0, LK_CANRECURSE);
402 	lockinit(&dev->vblank_time_lock, "drmvtl", 0, LK_CANRECURSE);
403 
404 	dev->num_crtcs = num_crtcs;
405 
406 	dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
407 	if (!dev->vblank)
408 		goto err;
409 
410 	for (i = 0; i < num_crtcs; i++) {
411 		struct drm_vblank_crtc *vblank = &dev->vblank[i];
412 
413 		vblank->dev = dev;
414 		vblank->pipe = i;
415 		init_waitqueue_head(&vblank->queue);
416 		setup_timer(&vblank->disable_timer, vblank_disable_fn,
417 			    (unsigned long)vblank);
418 	}
419 
420 	DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
421 
422 	/* Driver specific high-precision vblank timestamping supported? */
423 	if (dev->driver->get_vblank_timestamp)
424 		DRM_INFO("Driver supports precise vblank timestamp query.\n");
425 	else
426 		DRM_INFO("No driver support for vblank timestamp query.\n");
427 
428 	/* Must have precise timestamping for reliable vblank instant disable */
429 	if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
430 		dev->vblank_disable_immediate = false;
431 		DRM_INFO("Setting vblank_disable_immediate to false because "
432 			 "get_vblank_timestamp == NULL\n");
433 	}
434 
435 	return 0;
436 
437 err:
438 	dev->num_crtcs = 0;
439 	return ret;
440 }
441 EXPORT_SYMBOL(drm_vblank_init);
442 
443 #if 0
444 static void drm_irq_vgaarb_nokms(void *cookie, bool state)
445 {
446 	struct drm_device *dev = cookie;
447 
448 	if (dev->driver->vgaarb_irq) {
449 		dev->driver->vgaarb_irq(dev, state);
450 		return;
451 	}
452 
453 	if (!dev->irq_enabled)
454 		return;
455 
456 	if (state) {
457 		if (dev->driver->irq_uninstall)
458 			dev->driver->irq_uninstall(dev);
459 	} else {
460 		if (dev->driver->irq_preinstall)
461 			dev->driver->irq_preinstall(dev);
462 		if (dev->driver->irq_postinstall)
463 			dev->driver->irq_postinstall(dev);
464 	}
465 }
466 #endif
467 
468 /**
469  * drm_irq_install - install IRQ handler
470  * @dev: DRM device
471  * @irq: IRQ number to install the handler for
472  *
473  * Initializes the IRQ related data. Installs the handler, calling the driver
474  * irq_preinstall() and irq_postinstall() functions before and after the
475  * installation.
476  *
477  * This is the simplified helper interface provided for drivers with no special
478  * needs. Drivers which need to install interrupt handlers for multiple
479  * interrupts must instead set drm_device->irq_enabled to signal the DRM core
480  * that vblank interrupts are available.
481  *
482  * Returns:
483  * Zero on success or a negative error code on failure.
484  */
485 int drm_irq_install(struct drm_device *dev, int irq)
486 {
487 	int ret;
488 
489 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
490 		return -EINVAL;
491 
492 	if (irq == 0)
493 		return -EINVAL;
494 
495 	/* Driver must have been initialized */
496 	if (!dev->dev_private)
497 		return -EINVAL;
498 
499 	if (dev->irq_enabled)
500 		return -EBUSY;
501 	dev->irq_enabled = true;
502 
503 	DRM_DEBUG("irq=%d\n", irq);
504 
505 	/* Before installing handler */
506 	if (dev->driver->irq_preinstall)
507 		dev->driver->irq_preinstall(dev);
508 
509 	/* Install handler */
510 	ret = -bus_setup_intr(dev->dev->bsddev, dev->irqr, INTR_MPSAFE,
511 	    dev->driver->irq_handler, dev, &dev->irqh, &dev->irq_lock);
512 
513 	if (ret != 0) {
514 		dev->irq_enabled = false;
515 		return ret;
516 	}
517 
518 	/* After installing handler */
519 	if (dev->driver->irq_postinstall)
520 		ret = dev->driver->irq_postinstall(dev);
521 
522 	if (ret < 0) {
523 		dev->irq_enabled = false;
524 		bus_teardown_intr(dev->dev->bsddev, dev->irqr, dev->irqh);
525 	} else {
526 		dev->irq = irq;
527 	}
528 
529 	return ret;
530 }
531 EXPORT_SYMBOL(drm_irq_install);
532 
533 /**
534  * drm_irq_uninstall - uninstall the IRQ handler
535  * @dev: DRM device
536  *
537  * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
538  * This should only be called by drivers which used drm_irq_install() to set up
539  * their interrupt handler. Other drivers must only reset
540  * drm_device->irq_enabled to false.
541  *
542  * Note that for kernel modesetting drivers it is a bug if this function fails.
543  * The sanity checks are only to catch buggy user modesetting drivers which call
544  * the same function through an ioctl.
545  *
546  * Returns:
547  * Zero on success or a negative error code on failure.
548  */
549 int drm_irq_uninstall(struct drm_device *dev)
550 {
551 	unsigned long irqflags;
552 	bool irq_enabled;
553 	int i;
554 
555 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
556 		return -EINVAL;
557 
558 	irq_enabled = dev->irq_enabled;
559 	dev->irq_enabled = false;
560 
561 	/*
562 	 * Wake up any waiters so they don't hang. This is just to paper over
563 	 * isssues for UMS drivers which aren't in full control of their
564 	 * vblank/irq handling. KMS drivers must ensure that vblanks are all
565 	 * disabled when uninstalling the irq handler.
566 	 */
567 	if (dev->num_crtcs) {
568 		spin_lock_irqsave(&dev->vbl_lock, irqflags);
569 		for (i = 0; i < dev->num_crtcs; i++) {
570 			struct drm_vblank_crtc *vblank = &dev->vblank[i];
571 
572 			if (!vblank->enabled)
573 				continue;
574 
575 			WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
576 
577 			vblank_disable_and_save(dev, i);
578 			wake_up(&vblank->queue);
579 		}
580 		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
581 	}
582 
583 	if (!irq_enabled)
584 		return -EINVAL;
585 
586 	DRM_DEBUG("irq=%d\n", dev->irq);
587 
588 	if (dev->driver->irq_uninstall)
589 		dev->driver->irq_uninstall(dev);
590 
591 	bus_teardown_intr(dev->dev->bsddev, dev->irqr, dev->irqh);
592 
593 	return 0;
594 }
595 EXPORT_SYMBOL(drm_irq_uninstall);
596 
597 /*
598  * IRQ control ioctl.
599  *
600  * \param inode device inode.
601  * \param file_priv DRM file private.
602  * \param cmd command.
603  * \param arg user argument, pointing to a drm_control structure.
604  * \return zero on success or a negative number on failure.
605  *
606  * Calls irq_install() or irq_uninstall() according to \p arg.
607  */
608 int drm_control(struct drm_device *dev, void *data,
609 		struct drm_file *file_priv)
610 {
611 	struct drm_control *ctl = data;
612 	int ret = 0, irq;
613 
614 	/* if we haven't irq we fallback for compatibility reasons -
615 	 * this used to be a separate function in drm_dma.h
616 	 */
617 
618 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
619 		return 0;
620 	if (drm_core_check_feature(dev, DRIVER_MODESET))
621 		return 0;
622 	/* UMS was only ever support on pci devices. */
623 	if (WARN_ON(!dev->pdev))
624 		return -EINVAL;
625 
626 	switch (ctl->func) {
627 	case DRM_INST_HANDLER:
628 		irq = dev->irq;
629 
630 		if (dev->if_version < DRM_IF_VERSION(1, 2) &&
631 		    ctl->irq != irq)
632 			return -EINVAL;
633 		mutex_lock(&dev->struct_mutex);
634 		ret = drm_irq_install(dev, irq);
635 		mutex_unlock(&dev->struct_mutex);
636 
637 		return ret;
638 	case DRM_UNINST_HANDLER:
639 		mutex_lock(&dev->struct_mutex);
640 		ret = drm_irq_uninstall(dev);
641 		mutex_unlock(&dev->struct_mutex);
642 
643 		return ret;
644 	default:
645 		return -EINVAL;
646 	}
647 }
648 
649 /**
650  * drm_calc_timestamping_constants - calculate vblank timestamp constants
651  * @crtc: drm_crtc whose timestamp constants should be updated.
652  * @mode: display mode containing the scanout timings
653  *
654  * Calculate and store various constants which are later
655  * needed by vblank and swap-completion timestamping, e.g,
656  * by drm_calc_vbltimestamp_from_scanoutpos(). They are
657  * derived from CRTC's true scanout timing, so they take
658  * things like panel scaling or other adjustments into account.
659  */
660 void drm_calc_timestamping_constants(struct drm_crtc *crtc,
661 				     const struct drm_display_mode *mode)
662 {
663 	struct drm_device *dev = crtc->dev;
664 	unsigned int pipe = drm_crtc_index(crtc);
665 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
666 	int linedur_ns = 0, framedur_ns = 0;
667 	int dotclock = mode->crtc_clock;
668 
669 	if (!dev->num_crtcs)
670 		return;
671 
672 	if (WARN_ON(pipe >= dev->num_crtcs))
673 		return;
674 
675 	/* Valid dotclock? */
676 	if (dotclock > 0) {
677 		int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
678 
679 		/*
680 		 * Convert scanline length in pixels and video
681 		 * dot clock to line duration and frame duration
682 		 * in nanoseconds:
683 		 */
684 		linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
685 		framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
686 
687 		/*
688 		 * Fields of interlaced scanout modes are only half a frame duration.
689 		 */
690 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
691 			framedur_ns /= 2;
692 	} else
693 		DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
694 			  crtc->base.id);
695 
696 	vblank->linedur_ns  = linedur_ns;
697 	vblank->framedur_ns = framedur_ns;
698 
699 	DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
700 		  crtc->base.id, mode->crtc_htotal,
701 		  mode->crtc_vtotal, mode->crtc_vdisplay);
702 	DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
703 		  crtc->base.id, dotclock, framedur_ns, linedur_ns);
704 }
705 EXPORT_SYMBOL(drm_calc_timestamping_constants);
706 
707 /**
708  * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
709  * @dev: DRM device
710  * @pipe: index of CRTC whose vblank timestamp to retrieve
711  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
712  *             On return contains true maximum error of timestamp
713  * @vblank_time: Pointer to struct timeval which should receive the timestamp
714  * @flags: Flags to pass to driver:
715  *         0 = Default,
716  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
717  * @mode: mode which defines the scanout timings
718  *
719  * Implements calculation of exact vblank timestamps from given drm_display_mode
720  * timings and current video scanout position of a CRTC. This can be called from
721  * within get_vblank_timestamp() implementation of a kms driver to implement the
722  * actual timestamping.
723  *
724  * Should return timestamps conforming to the OML_sync_control OpenML
725  * extension specification. The timestamp corresponds to the end of
726  * the vblank interval, aka start of scanout of topmost-leftmost display
727  * pixel in the following video frame.
728  *
729  * Requires support for optional dev->driver->get_scanout_position()
730  * in kms driver, plus a bit of setup code to provide a drm_display_mode
731  * that corresponds to the true scanout timing.
732  *
733  * The current implementation only handles standard video modes. It
734  * returns as no operation if a doublescan or interlaced video mode is
735  * active. Higher level code is expected to handle this.
736  *
737  * Returns:
738  * Negative value on error, failure or if not supported in current
739  * video mode:
740  *
741  * -EINVAL   - Invalid CRTC.
742  * -EAGAIN   - Temporary unavailable, e.g., called before initial modeset.
743  * -ENOTSUPP - Function not supported in current display mode.
744  * -EIO      - Failed, e.g., due to failed scanout position query.
745  *
746  * Returns or'ed positive status flags on success:
747  *
748  * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
749  * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
750  *
751  */
752 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
753 					  unsigned int pipe,
754 					  int *max_error,
755 					  struct timeval *vblank_time,
756 					  unsigned flags,
757 					  const struct drm_display_mode *mode)
758 {
759 	struct timeval tv_etime;
760 	ktime_t stime, etime;
761 	unsigned int vbl_status;
762 	int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
763 	int vpos, hpos, i;
764 	int delta_ns, duration_ns;
765 
766 	if (pipe >= dev->num_crtcs) {
767 		DRM_ERROR("Invalid crtc %u\n", pipe);
768 		return -EINVAL;
769 	}
770 
771 	/* Scanout position query not supported? Should not happen. */
772 	if (!dev->driver->get_scanout_position) {
773 		DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
774 		return -EIO;
775 	}
776 
777 	/* If mode timing undefined, just return as no-op:
778 	 * Happens during initial modesetting of a crtc.
779 	 */
780 	if (mode->crtc_clock == 0) {
781 		DRM_DEBUG_VBLANK("crtc %u: Noop due to uninitialized mode.\n", pipe);
782 		return -EAGAIN;
783 	}
784 
785 	/* Get current scanout position with system timestamp.
786 	 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
787 	 * if single query takes longer than max_error nanoseconds.
788 	 *
789 	 * This guarantees a tight bound on maximum error if
790 	 * code gets preempted or delayed for some reason.
791 	 */
792 	for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
793 		/*
794 		 * Get vertical and horizontal scanout position vpos, hpos,
795 		 * and bounding timestamps stime, etime, pre/post query.
796 		 */
797 		vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
798 							       &vpos, &hpos,
799 							       &stime, &etime,
800 							       mode);
801 
802 		/* Return as no-op if scanout query unsupported or failed. */
803 		if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
804 			DRM_DEBUG_VBLANK("crtc %u : scanoutpos query failed [0x%x].\n",
805 				  pipe, vbl_status);
806 			return -EIO;
807 		}
808 
809 		/* Compute uncertainty in timestamp of scanout position query. */
810 		duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
811 
812 		/* Accept result with <  max_error nsecs timing uncertainty. */
813 		if (duration_ns <= *max_error)
814 			break;
815 	}
816 
817 	/* Noisy system timing? */
818 	if (i == DRM_TIMESTAMP_MAXRETRIES) {
819 		DRM_DEBUG_VBLANK("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
820 			  pipe, duration_ns/1000, *max_error/1000, i);
821 	}
822 
823 	/* Return upper bound of timestamp precision error. */
824 	*max_error = duration_ns;
825 
826 	/* Check if in vblank area:
827 	 * vpos is >=0 in video scanout area, but negative
828 	 * within vblank area, counting down the number of lines until
829 	 * start of scanout.
830 	 */
831 	if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
832 		ret |= DRM_VBLANKTIME_IN_VBLANK;
833 
834 	/* Convert scanout position into elapsed time at raw_time query
835 	 * since start of scanout at first display scanline. delta_ns
836 	 * can be negative if start of scanout hasn't happened yet.
837 	 */
838 	delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
839 			   mode->crtc_clock);
840 
841 	if (!drm_timestamp_monotonic)
842 		etime = ktime_mono_to_real(etime);
843 
844 	/* save this only for debugging purposes */
845 	tv_etime = ktime_to_timeval(etime);
846 	/* Subtract time delta from raw timestamp to get final
847 	 * vblank_time timestamp for end of vblank.
848 	 */
849 	etime = ktime_sub_ns(etime, delta_ns);
850 	*vblank_time = ktime_to_timeval(etime);
851 
852 	DRM_DEBUG_VBLANK("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
853 		  pipe, vbl_status, hpos, vpos,
854 		  (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
855 		  (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
856 		  duration_ns/1000, i);
857 
858 	return ret;
859 }
860 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
861 
862 static struct timeval get_drm_timestamp(void)
863 {
864 	ktime_t now;
865 
866 	now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
867 	return ktime_to_timeval(now);
868 }
869 
870 /**
871  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
872  *                             vblank interval
873  * @dev: DRM device
874  * @pipe: index of CRTC whose vblank timestamp to retrieve
875  * @tvblank: Pointer to target struct timeval which should receive the timestamp
876  * @flags: Flags to pass to driver:
877  *         0 = Default,
878  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
879  *
880  * Fetches the system timestamp corresponding to the time of the most recent
881  * vblank interval on specified CRTC. May call into kms-driver to
882  * compute the timestamp with a high-precision GPU specific method.
883  *
884  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
885  * call, i.e., it isn't very precisely locked to the true vblank.
886  *
887  * Returns:
888  * True if timestamp is considered to be very precise, false otherwise.
889  */
890 static bool
891 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
892 			  struct timeval *tvblank, unsigned flags)
893 {
894 	int ret;
895 
896 	/* Define requested maximum error on timestamps (nanoseconds). */
897 	int max_error = (int) drm_timestamp_precision * 1000;
898 
899 	/* Query driver if possible and precision timestamping enabled. */
900 	if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
901 		ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
902 							tvblank, flags);
903 		if (ret > 0)
904 			return true;
905 	}
906 
907 	/* GPU high precision timestamp query unsupported or failed.
908 	 * Return current monotonic/gettimeofday timestamp as best estimate.
909 	 */
910 	*tvblank = get_drm_timestamp();
911 
912 	return false;
913 }
914 
915 /**
916  * drm_vblank_count - retrieve "cooked" vblank counter value
917  * @dev: DRM device
918  * @pipe: index of CRTC for which to retrieve the counter
919  *
920  * Fetches the "cooked" vblank count value that represents the number of
921  * vblank events since the system was booted, including lost events due to
922  * modesetting activity.
923  *
924  * This is the legacy version of drm_crtc_vblank_count().
925  *
926  * Returns:
927  * The software vblank counter.
928  */
929 u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
930 {
931 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
932 
933 	if (WARN_ON(pipe >= dev->num_crtcs))
934 		return 0;
935 
936 	return vblank->count;
937 }
938 EXPORT_SYMBOL(drm_vblank_count);
939 
940 /**
941  * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
942  * @crtc: which counter to retrieve
943  *
944  * Fetches the "cooked" vblank count value that represents the number of
945  * vblank events since the system was booted, including lost events due to
946  * modesetting activity.
947  *
948  * This is the native KMS version of drm_vblank_count().
949  *
950  * Returns:
951  * The software vblank counter.
952  */
953 u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
954 {
955 	return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
956 }
957 EXPORT_SYMBOL(drm_crtc_vblank_count);
958 
959 /**
960  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
961  *     system timestamp corresponding to that vblank counter value.
962  * @dev: DRM device
963  * @pipe: index of CRTC whose counter to retrieve
964  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
965  *
966  * Fetches the "cooked" vblank count value that represents the number of
967  * vblank events since the system was booted, including lost events due to
968  * modesetting activity. Returns corresponding system timestamp of the time
969  * of the vblank interval that corresponds to the current vblank counter value.
970  *
971  * This is the legacy version of drm_crtc_vblank_count_and_time().
972  */
973 u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
974 			      struct timeval *vblanktime)
975 {
976 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
977 	int count = DRM_TIMESTAMP_MAXRETRIES;
978 	u32 cur_vblank;
979 
980 	vblanktime->tv_sec = 0;		/* silence gcc warning */
981 	vblanktime->tv_usec = 0;	/* silence gcc warning */
982 	if (WARN_ON(pipe >= dev->num_crtcs))
983 		return 0;
984 
985 	/*
986 	 * Vblank timestamps are read lockless. To ensure consistency the vblank
987 	 * counter is rechecked and ordering is ensured using memory barriers.
988 	 * This works like a seqlock. The write-side barriers are in store_vblank.
989 	 */
990 	do {
991 		cur_vblank = vblank->count;
992 		smp_rmb();
993 		*vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
994 		smp_rmb();
995 	} while (cur_vblank != vblank->count && --count > 0);
996 
997 	return cur_vblank;
998 }
999 EXPORT_SYMBOL(drm_vblank_count_and_time);
1000 
1001 /**
1002  * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
1003  *     and the system timestamp corresponding to that vblank counter value
1004  * @crtc: which counter to retrieve
1005  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1006  *
1007  * Fetches the "cooked" vblank count value that represents the number of
1008  * vblank events since the system was booted, including lost events due to
1009  * modesetting activity. Returns corresponding system timestamp of the time
1010  * of the vblank interval that corresponds to the current vblank counter value.
1011  *
1012  * This is the native KMS version of drm_vblank_count_and_time().
1013  */
1014 u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1015 				   struct timeval *vblanktime)
1016 {
1017 	return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
1018 					 vblanktime);
1019 }
1020 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
1021 
1022 static void send_vblank_event(struct drm_device *dev,
1023 		struct drm_pending_vblank_event *e,
1024 		unsigned long seq, struct timeval *now)
1025 {
1026 	e->event.sequence = seq;
1027 	e->event.tv_sec = now->tv_sec;
1028 	e->event.tv_usec = now->tv_usec;
1029 
1030 	drm_send_event_locked(dev, &e->base);
1031 
1032 	trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
1033 					 e->event.sequence);
1034 }
1035 
1036 /**
1037  * drm_arm_vblank_event - arm vblank event after pageflip
1038  * @dev: DRM device
1039  * @pipe: CRTC index
1040  * @e: the event to prepare to send
1041  *
1042  * A lot of drivers need to generate vblank events for the very next vblank
1043  * interrupt. For example when the page flip interrupt happens when the page
1044  * flip gets armed, but not when it actually executes within the next vblank
1045  * period. This helper function implements exactly the required vblank arming
1046  * behaviour.
1047  *
1048  * Caller must hold event lock. Caller must also hold a vblank reference for
1049  * the event @e, which will be dropped when the next vblank arrives.
1050  *
1051  * This is the legacy version of drm_crtc_arm_vblank_event().
1052  */
1053 void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1054 			  struct drm_pending_vblank_event *e)
1055 {
1056 	assert_spin_locked(&dev->event_lock);
1057 
1058 	e->pipe = pipe;
1059 	e->event.sequence = drm_vblank_count(dev, pipe);
1060 	list_add_tail(&e->base.link, &dev->vblank_event_list);
1061 }
1062 EXPORT_SYMBOL(drm_arm_vblank_event);
1063 
1064 /**
1065  * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1066  * @crtc: the source CRTC of the vblank event
1067  * @e: the event to send
1068  *
1069  * A lot of drivers need to generate vblank events for the very next vblank
1070  * interrupt. For example when the page flip interrupt happens when the page
1071  * flip gets armed, but not when it actually executes within the next vblank
1072  * period. This helper function implements exactly the required vblank arming
1073  * behaviour.
1074  *
1075  * Caller must hold event lock. Caller must also hold a vblank reference for
1076  * the event @e, which will be dropped when the next vblank arrives.
1077  *
1078  * This is the native KMS version of drm_arm_vblank_event().
1079  */
1080 void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1081 			       struct drm_pending_vblank_event *e)
1082 {
1083 	drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
1084 }
1085 EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1086 
1087 /**
1088  * drm_send_vblank_event - helper to send vblank event after pageflip
1089  * @dev: DRM device
1090  * @pipe: CRTC index
1091  * @e: the event to send
1092  *
1093  * Updates sequence # and timestamp on event, and sends it to userspace.
1094  * Caller must hold event lock.
1095  *
1096  * This is the legacy version of drm_crtc_send_vblank_event().
1097  */
1098 void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1099 			   struct drm_pending_vblank_event *e)
1100 {
1101 	struct timeval now;
1102 	unsigned int seq;
1103 
1104 	if (dev->num_crtcs > 0) {
1105 		seq = drm_vblank_count_and_time(dev, pipe, &now);
1106 	} else {
1107 		seq = 0;
1108 
1109 		now = get_drm_timestamp();
1110 	}
1111 	e->pipe = pipe;
1112 	send_vblank_event(dev, e, seq, &now);
1113 }
1114 EXPORT_SYMBOL(drm_send_vblank_event);
1115 
1116 /**
1117  * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1118  * @crtc: the source CRTC of the vblank event
1119  * @e: the event to send
1120  *
1121  * Updates sequence # and timestamp on event, and sends it to userspace.
1122  * Caller must hold event lock.
1123  *
1124  * This is the native KMS version of drm_send_vblank_event().
1125  */
1126 void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1127 				struct drm_pending_vblank_event *e)
1128 {
1129 	drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
1130 }
1131 EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1132 
1133 /**
1134  * drm_vblank_enable - enable the vblank interrupt on a CRTC
1135  * @dev: DRM device
1136  * @pipe: CRTC index
1137  *
1138  * Returns:
1139  * Zero on success or a negative error code on failure.
1140  */
1141 static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1142 {
1143 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1144 	int ret = 0;
1145 
1146 	assert_spin_locked(&dev->vbl_lock);
1147 
1148 	lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
1149 
1150 	if (!vblank->enabled) {
1151 		/*
1152 		 * Enable vblank irqs under vblank_time_lock protection.
1153 		 * All vblank count & timestamp updates are held off
1154 		 * until we are done reinitializing master counter and
1155 		 * timestamps. Filtercode in drm_handle_vblank() will
1156 		 * prevent double-accounting of same vblank interval.
1157 		 */
1158 		ret = dev->driver->enable_vblank(dev, pipe);
1159 		DRM_DEBUG_VBLANK("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
1160 		if (ret)
1161 			atomic_dec(&vblank->refcount);
1162 		else {
1163 			vblank->enabled = true;
1164 			drm_update_vblank_count(dev, pipe, 0);
1165 		}
1166 	}
1167 
1168 	lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1169 
1170 	return ret;
1171 }
1172 
1173 /**
1174  * drm_vblank_get - get a reference count on vblank events
1175  * @dev: DRM device
1176  * @pipe: index of CRTC to own
1177  *
1178  * Acquire a reference count on vblank events to avoid having them disabled
1179  * while in use.
1180  *
1181  * This is the legacy version of drm_crtc_vblank_get().
1182  *
1183  * Returns:
1184  * Zero on success or a negative error code on failure.
1185  */
1186 int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
1187 {
1188 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1189 	unsigned long irqflags;
1190 	int ret = 0;
1191 
1192 	if (!dev->num_crtcs)
1193 		return -EINVAL;
1194 
1195 	if (WARN_ON(pipe >= dev->num_crtcs))
1196 		return -EINVAL;
1197 
1198 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
1199 	/* Going from 0->1 means we have to enable interrupts again */
1200 	if (atomic_add_return(1, &vblank->refcount) == 1) {
1201 		ret = drm_vblank_enable(dev, pipe);
1202 	} else {
1203 		if (!vblank->enabled) {
1204 			atomic_dec(&vblank->refcount);
1205 			ret = -EINVAL;
1206 		}
1207 	}
1208 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1209 
1210 	return ret;
1211 }
1212 EXPORT_SYMBOL(drm_vblank_get);
1213 
1214 /**
1215  * drm_crtc_vblank_get - get a reference count on vblank events
1216  * @crtc: which CRTC to own
1217  *
1218  * Acquire a reference count on vblank events to avoid having them disabled
1219  * while in use.
1220  *
1221  * This is the native kms version of drm_vblank_get().
1222  *
1223  * Returns:
1224  * Zero on success or a negative error code on failure.
1225  */
1226 int drm_crtc_vblank_get(struct drm_crtc *crtc)
1227 {
1228 	return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1229 }
1230 EXPORT_SYMBOL(drm_crtc_vblank_get);
1231 
1232 /**
1233  * drm_vblank_put - release ownership of vblank events
1234  * @dev: DRM device
1235  * @pipe: index of CRTC to release
1236  *
1237  * Release ownership of a given vblank counter, turning off interrupts
1238  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1239  *
1240  * This is the legacy version of drm_crtc_vblank_put().
1241  */
1242 void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1243 {
1244 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1245 
1246 	if (WARN_ON(pipe >= dev->num_crtcs))
1247 		return;
1248 
1249 	if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1250 		return;
1251 
1252 	/* Last user schedules interrupt disable */
1253 	if (atomic_dec_and_test(&vblank->refcount)) {
1254 		if (drm_vblank_offdelay == 0)
1255 			return;
1256 		else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
1257 			vblank_disable_fn((unsigned long)vblank);
1258 		else
1259 			mod_timer(&vblank->disable_timer,
1260 				  jiffies + ((drm_vblank_offdelay * HZ)/1000));
1261 	}
1262 }
1263 EXPORT_SYMBOL(drm_vblank_put);
1264 
1265 /**
1266  * drm_crtc_vblank_put - give up ownership of vblank events
1267  * @crtc: which counter to give up
1268  *
1269  * Release ownership of a given vblank counter, turning off interrupts
1270  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1271  *
1272  * This is the native kms version of drm_vblank_put().
1273  */
1274 void drm_crtc_vblank_put(struct drm_crtc *crtc)
1275 {
1276 	drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1277 }
1278 EXPORT_SYMBOL(drm_crtc_vblank_put);
1279 
1280 /**
1281  * drm_wait_one_vblank - wait for one vblank
1282  * @dev: DRM device
1283  * @pipe: CRTC index
1284  *
1285  * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1286  * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1287  * due to lack of driver support or because the crtc is off.
1288  */
1289 void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1290 {
1291 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1292 	int ret;
1293 	u32 last;
1294 
1295 	if (WARN_ON(pipe >= dev->num_crtcs))
1296 		return;
1297 
1298 	ret = drm_vblank_get(dev, pipe);
1299 	if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1300 		return;
1301 
1302 	last = drm_vblank_count(dev, pipe);
1303 
1304 	ret = wait_event_timeout(vblank->queue,
1305 				 last != drm_vblank_count(dev, pipe),
1306 				 msecs_to_jiffies(100));
1307 
1308 	WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1309 
1310 	drm_vblank_put(dev, pipe);
1311 }
1312 EXPORT_SYMBOL(drm_wait_one_vblank);
1313 
1314 /**
1315  * drm_crtc_wait_one_vblank - wait for one vblank
1316  * @crtc: DRM crtc
1317  *
1318  * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1319  * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1320  * due to lack of driver support or because the crtc is off.
1321  */
1322 void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1323 {
1324 	drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1325 }
1326 EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1327 
1328 /**
1329  * drm_vblank_off - disable vblank events on a CRTC
1330  * @dev: DRM device
1331  * @pipe: CRTC index
1332  *
1333  * Drivers can use this function to shut down the vblank interrupt handling when
1334  * disabling a crtc. This function ensures that the latest vblank frame count is
1335  * stored so that drm_vblank_on() can restore it again.
1336  *
1337  * Drivers must use this function when the hardware vblank counter can get
1338  * reset, e.g. when suspending.
1339  *
1340  * This is the legacy version of drm_crtc_vblank_off().
1341  */
1342 void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
1343 {
1344 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1345 	struct drm_pending_vblank_event *e, *t;
1346 	struct timeval now;
1347 	unsigned long irqflags;
1348 	unsigned int seq;
1349 
1350 	if (WARN_ON(pipe >= dev->num_crtcs))
1351 		return;
1352 
1353 	spin_lock_irqsave(&dev->event_lock, irqflags);
1354 
1355 	lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
1356 	DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1357 		      pipe, vblank->enabled, vblank->inmodeset);
1358 
1359 	/* Avoid redundant vblank disables without previous drm_vblank_on(). */
1360 	if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1361 		vblank_disable_and_save(dev, pipe);
1362 
1363 	wake_up(&vblank->queue);
1364 
1365 	/*
1366 	 * Prevent subsequent drm_vblank_get() from re-enabling
1367 	 * the vblank interrupt by bumping the refcount.
1368 	 */
1369 	if (!vblank->inmodeset) {
1370 		atomic_inc(&vblank->refcount);
1371 		vblank->inmodeset = 1;
1372 	}
1373 	lockmgr(&dev->vbl_lock, LK_RELEASE);
1374 
1375 	/* Send any queued vblank events, lest the natives grow disquiet */
1376 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1377 
1378 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1379 		if (e->pipe != pipe)
1380 			continue;
1381 		DRM_DEBUG_VBLANK("Sending premature vblank event on disable: \
1382 			  wanted %d, current %d\n",
1383 			  e->event.sequence, seq);
1384 		list_del(&e->base.link);
1385 		drm_vblank_put(dev, pipe);
1386 		send_vblank_event(dev, e, seq, &now);
1387 	}
1388 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
1389 }
1390 EXPORT_SYMBOL(drm_vblank_off);
1391 
1392 /**
1393  * drm_crtc_vblank_off - disable vblank events on a CRTC
1394  * @crtc: CRTC in question
1395  *
1396  * Drivers can use this function to shut down the vblank interrupt handling when
1397  * disabling a crtc. This function ensures that the latest vblank frame count is
1398  * stored so that drm_vblank_on can restore it again.
1399  *
1400  * Drivers must use this function when the hardware vblank counter can get
1401  * reset, e.g. when suspending.
1402  *
1403  * This is the native kms version of drm_vblank_off().
1404  */
1405 void drm_crtc_vblank_off(struct drm_crtc *crtc)
1406 {
1407 	drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1408 }
1409 EXPORT_SYMBOL(drm_crtc_vblank_off);
1410 
1411 /**
1412  * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1413  * @crtc: CRTC in question
1414  *
1415  * Drivers can use this function to reset the vblank state to off at load time.
1416  * Drivers should use this together with the drm_crtc_vblank_off() and
1417  * drm_crtc_vblank_on() functions. The difference compared to
1418  * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1419  * and hence doesn't need to call any driver hooks.
1420  */
1421 void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1422 {
1423 	struct drm_device *dev = crtc->dev;
1424 	unsigned long irqflags;
1425 	unsigned int pipe = drm_crtc_index(crtc);
1426 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1427 
1428 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
1429 	/*
1430 	 * Prevent subsequent drm_vblank_get() from enabling the vblank
1431 	 * interrupt by bumping the refcount.
1432 	 */
1433 	if (!vblank->inmodeset) {
1434 		atomic_inc(&vblank->refcount);
1435 		vblank->inmodeset = 1;
1436 	}
1437 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1438 
1439 	WARN_ON(!list_empty(&dev->vblank_event_list));
1440 }
1441 EXPORT_SYMBOL(drm_crtc_vblank_reset);
1442 
1443 /**
1444  * drm_vblank_on - enable vblank events on a CRTC
1445  * @dev: DRM device
1446  * @pipe: CRTC index
1447  *
1448  * This functions restores the vblank interrupt state captured with
1449  * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1450  * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1451  * in driver load code to reflect the current hardware state of the crtc.
1452  *
1453  * This is the legacy version of drm_crtc_vblank_on().
1454  */
1455 void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
1456 {
1457 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1458 	unsigned long irqflags;
1459 
1460 	if (WARN_ON(pipe >= dev->num_crtcs))
1461 		return;
1462 
1463 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
1464 	DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1465 		      pipe, vblank->enabled, vblank->inmodeset);
1466 
1467 	/* Drop our private "prevent drm_vblank_get" refcount */
1468 	if (vblank->inmodeset) {
1469 		atomic_dec(&vblank->refcount);
1470 		vblank->inmodeset = 0;
1471 	}
1472 
1473 	drm_reset_vblank_timestamp(dev, pipe);
1474 
1475 	/*
1476 	 * re-enable interrupts if there are users left, or the
1477 	 * user wishes vblank interrupts to be enabled all the time.
1478 	 */
1479 	if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1480 		WARN_ON(drm_vblank_enable(dev, pipe));
1481 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1482 }
1483 EXPORT_SYMBOL(drm_vblank_on);
1484 
1485 /**
1486  * drm_crtc_vblank_on - enable vblank events on a CRTC
1487  * @crtc: CRTC in question
1488  *
1489  * This functions restores the vblank interrupt state captured with
1490  * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1491  * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1492  * in driver load code to reflect the current hardware state of the crtc.
1493  *
1494  * This is the native kms version of drm_vblank_on().
1495  */
1496 void drm_crtc_vblank_on(struct drm_crtc *crtc)
1497 {
1498 	drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1499 }
1500 EXPORT_SYMBOL(drm_crtc_vblank_on);
1501 
1502 /**
1503  * drm_vblank_pre_modeset - account for vblanks across mode sets
1504  * @dev: DRM device
1505  * @pipe: CRTC index
1506  *
1507  * Account for vblank events across mode setting events, which will likely
1508  * reset the hardware frame counter.
1509  *
1510  * This is done by grabbing a temporary vblank reference to ensure that the
1511  * vblank interrupt keeps running across the modeset sequence. With this the
1512  * software-side vblank frame counting will ensure that there are no jumps or
1513  * discontinuities.
1514  *
1515  * Unfortunately this approach is racy and also doesn't work when the vblank
1516  * interrupt stops running, e.g. across system suspend resume. It is therefore
1517  * highly recommended that drivers use the newer drm_vblank_off() and
1518  * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1519  * using "cooked" software vblank frame counters and not relying on any hardware
1520  * counters.
1521  *
1522  * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1523  * again.
1524  */
1525 void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
1526 {
1527 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1528 
1529 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
1530 	if (!dev->num_crtcs)
1531 		return;
1532 
1533 	if (WARN_ON(pipe >= dev->num_crtcs))
1534 		return;
1535 
1536 	/*
1537 	 * To avoid all the problems that might happen if interrupts
1538 	 * were enabled/disabled around or between these calls, we just
1539 	 * have the kernel take a reference on the CRTC (just once though
1540 	 * to avoid corrupting the count if multiple, mismatch calls occur),
1541 	 * so that interrupts remain enabled in the interim.
1542 	 */
1543 	if (!vblank->inmodeset) {
1544 		vblank->inmodeset = 0x1;
1545 		if (drm_vblank_get(dev, pipe) == 0)
1546 			vblank->inmodeset |= 0x2;
1547 	}
1548 }
1549 EXPORT_SYMBOL(drm_vblank_pre_modeset);
1550 
1551 /**
1552  * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1553  * @dev: DRM device
1554  * @pipe: CRTC index
1555  *
1556  * This function again drops the temporary vblank reference acquired in
1557  * drm_vblank_pre_modeset.
1558  */
1559 void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
1560 {
1561 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1562 	unsigned long irqflags;
1563 
1564 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
1565 	if (!dev->num_crtcs)
1566 		return;
1567 
1568 	if (WARN_ON(pipe >= dev->num_crtcs))
1569 		return;
1570 
1571 	if (vblank->inmodeset) {
1572 		spin_lock_irqsave(&dev->vbl_lock, irqflags);
1573 		drm_reset_vblank_timestamp(dev, pipe);
1574 		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1575 
1576 		if (vblank->inmodeset & 0x2)
1577 			drm_vblank_put(dev, pipe);
1578 
1579 		vblank->inmodeset = 0;
1580 	}
1581 }
1582 EXPORT_SYMBOL(drm_vblank_post_modeset);
1583 
1584 /*
1585  * drm_modeset_ctl - handle vblank event counter changes across mode switch
1586  * @DRM_IOCTL_ARGS: standard ioctl arguments
1587  *
1588  * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1589  * ioctls around modesetting so that any lost vblank events are accounted for.
1590  *
1591  * Generally the counter will reset across mode sets.  If interrupts are
1592  * enabled around this call, we don't have to do anything since the counter
1593  * will have already been incremented.
1594  */
1595 int drm_modeset_ctl(struct drm_device *dev, void *data,
1596 		    struct drm_file *file_priv)
1597 {
1598 	struct drm_modeset_ctl *modeset = data;
1599 	unsigned int pipe;
1600 
1601 	/* If drm_vblank_init() hasn't been called yet, just no-op */
1602 	if (!dev->num_crtcs)
1603 		return 0;
1604 
1605 	/* KMS drivers handle this internally */
1606 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1607 		return 0;
1608 
1609 	pipe = modeset->crtc;
1610 	if (pipe >= dev->num_crtcs)
1611 		return -EINVAL;
1612 
1613 	switch (modeset->cmd) {
1614 	case _DRM_PRE_MODESET:
1615 		drm_vblank_pre_modeset(dev, pipe);
1616 		break;
1617 	case _DRM_POST_MODESET:
1618 		drm_vblank_post_modeset(dev, pipe);
1619 		break;
1620 	default:
1621 		return -EINVAL;
1622 	}
1623 
1624 	return 0;
1625 }
1626 
1627 static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1628 				  union drm_wait_vblank *vblwait,
1629 				  struct drm_file *file_priv)
1630 {
1631 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1632 	struct drm_pending_vblank_event *e;
1633 	struct timeval now;
1634 	unsigned long flags;
1635 	unsigned int seq;
1636 	int ret;
1637 
1638 	e = kzalloc(sizeof(*e), GFP_KERNEL);
1639 	if (e == NULL) {
1640 		ret = -ENOMEM;
1641 		goto err_put;
1642 	}
1643 
1644 	e->pipe = pipe;
1645 	e->base.pid = curproc->p_pid;
1646 	e->event.base.type = DRM_EVENT_VBLANK;
1647 	e->event.base.length = sizeof(e->event);
1648 	e->event.user_data = vblwait->request.signal;
1649 
1650 	spin_lock_irqsave(&dev->event_lock, flags);
1651 
1652 	/*
1653 	 * drm_vblank_off() might have been called after we called
1654 	 * drm_vblank_get(). drm_vblank_off() holds event_lock
1655 	 * around the vblank disable, so no need for further locking.
1656 	 * The reference from drm_vblank_get() protects against
1657 	 * vblank disable from another source.
1658 	 */
1659 	if (!vblank->enabled) {
1660 		ret = -EINVAL;
1661 		goto err_unlock;
1662 	}
1663 
1664 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1665 					    &e->event.base);
1666 
1667 	if (ret)
1668 		goto err_unlock;
1669 
1670 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1671 
1672 	if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1673 	    (seq - vblwait->request.sequence) <= (1 << 23)) {
1674 		vblwait->request.sequence = seq + 1;
1675 		vblwait->reply.sequence = vblwait->request.sequence;
1676 	}
1677 
1678 	DRM_DEBUG_VBLANK("event on vblank count %d, current %d, crtc %u\n",
1679 		  vblwait->request.sequence, seq, pipe);
1680 
1681 	trace_drm_vblank_event_queued(current->pid, pipe,
1682 				      vblwait->request.sequence);
1683 
1684 	e->event.sequence = vblwait->request.sequence;
1685 	if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1686 		drm_vblank_put(dev, pipe);
1687 		send_vblank_event(dev, e, seq, &now);
1688 		vblwait->reply.sequence = seq;
1689 	} else {
1690 		/* drm_handle_vblank_events will call drm_vblank_put */
1691 		list_add_tail(&e->base.link, &dev->vblank_event_list);
1692 		vblwait->reply.sequence = vblwait->request.sequence;
1693 	}
1694 
1695 	spin_unlock_irqrestore(&dev->event_lock, flags);
1696 
1697 	return 0;
1698 
1699 err_unlock:
1700 	spin_unlock_irqrestore(&dev->event_lock, flags);
1701 	kfree(e);
1702 err_put:
1703 	drm_vblank_put(dev, pipe);
1704 	return ret;
1705 }
1706 
1707 /*
1708  * Wait for VBLANK.
1709  *
1710  * \param inode device inode.
1711  * \param file_priv DRM file private.
1712  * \param cmd command.
1713  * \param data user argument, pointing to a drm_wait_vblank structure.
1714  * \return zero on success or a negative number on failure.
1715  *
1716  * This function enables the vblank interrupt on the pipe requested, then
1717  * sleeps waiting for the requested sequence number to occur, and drops
1718  * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
1719  * after a timeout with no further vblank waits scheduled).
1720  */
1721 int drm_wait_vblank(struct drm_device *dev, void *data,
1722 		    struct drm_file *file_priv)
1723 {
1724 	struct drm_vblank_crtc *vblank;
1725 	union drm_wait_vblank *vblwait = data;
1726 	int ret;
1727 	unsigned int flags, seq, pipe, high_pipe;
1728 
1729 	if (!dev->irq_enabled)
1730 		return -EINVAL;
1731 
1732 	if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1733 		return -EINVAL;
1734 
1735 	if (vblwait->request.type &
1736 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1737 	      _DRM_VBLANK_HIGH_CRTC_MASK)) {
1738 		DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1739 			  vblwait->request.type,
1740 			  (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1741 			   _DRM_VBLANK_HIGH_CRTC_MASK));
1742 		return -EINVAL;
1743 	}
1744 
1745 	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1746 	high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1747 	if (high_pipe)
1748 		pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1749 	else
1750 		pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1751 	if (pipe >= dev->num_crtcs)
1752 		return -EINVAL;
1753 
1754 	vblank = &dev->vblank[pipe];
1755 
1756 	ret = drm_vblank_get(dev, pipe);
1757 	if (ret) {
1758 		DRM_DEBUG_VBLANK("failed to acquire vblank counter, %d\n", ret);
1759 		return ret;
1760 	}
1761 	seq = drm_vblank_count(dev, pipe);
1762 
1763 	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1764 	case _DRM_VBLANK_RELATIVE:
1765 		vblwait->request.sequence += seq;
1766 		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1767 	case _DRM_VBLANK_ABSOLUTE:
1768 		break;
1769 	default:
1770 		ret = -EINVAL;
1771 		goto done;
1772 	}
1773 
1774 	if (flags & _DRM_VBLANK_EVENT) {
1775 		/* must hold on to the vblank ref until the event fires
1776 		 * drm_vblank_put will be called asynchronously
1777 		 */
1778 		return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
1779 	}
1780 
1781 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1782 	    (seq - vblwait->request.sequence) <= (1<<23)) {
1783 		vblwait->request.sequence = seq + 1;
1784 	}
1785 
1786 	DRM_DEBUG_VBLANK("waiting on vblank count %d, crtc %u\n",
1787 		  vblwait->request.sequence, pipe);
1788 	vblank->last_wait = vblwait->request.sequence;
1789 	DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1790 		    (((drm_vblank_count(dev, pipe) -
1791 		       vblwait->request.sequence) <= (1 << 23)) ||
1792 		     !vblank->enabled ||
1793 		     !dev->irq_enabled));
1794 
1795 	if (ret != -EINTR) {
1796 		struct timeval now;
1797 
1798 		vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
1799 		vblwait->reply.tval_sec = now.tv_sec;
1800 		vblwait->reply.tval_usec = now.tv_usec;
1801 
1802 		DRM_DEBUG_VBLANK("returning %d to client\n",
1803 			  vblwait->reply.sequence);
1804 	} else {
1805 		DRM_DEBUG_VBLANK("vblank wait interrupted by signal\n");
1806 	}
1807 
1808 done:
1809 	drm_vblank_put(dev, pipe);
1810 	return ret;
1811 }
1812 
1813 static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1814 {
1815 	struct drm_pending_vblank_event *e, *t;
1816 	struct timeval now;
1817 	unsigned int seq;
1818 
1819 	assert_spin_locked(&dev->event_lock);
1820 
1821 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1822 
1823 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1824 		if (e->pipe != pipe)
1825 			continue;
1826 		if ((seq - e->event.sequence) > (1<<23))
1827 			continue;
1828 
1829 		DRM_DEBUG_VBLANK("vblank event on %d, current %d\n",
1830 			  e->event.sequence, seq);
1831 
1832 		list_del(&e->base.link);
1833 		drm_vblank_put(dev, pipe);
1834 		send_vblank_event(dev, e, seq, &now);
1835 	}
1836 
1837 	trace_drm_vblank_event(pipe, seq);
1838 }
1839 
1840 /**
1841  * drm_handle_vblank - handle a vblank event
1842  * @dev: DRM device
1843  * @pipe: index of CRTC where this event occurred
1844  *
1845  * Drivers should call this routine in their vblank interrupt handlers to
1846  * update the vblank counter and send any signals that may be pending.
1847  *
1848  * This is the legacy version of drm_crtc_handle_vblank().
1849  */
1850 bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1851 {
1852 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1853 	unsigned long irqflags;
1854 
1855 	if (WARN_ON_ONCE(!dev->num_crtcs))
1856 		return false;
1857 
1858 	if (WARN_ON(pipe >= dev->num_crtcs))
1859 		return false;
1860 
1861 	spin_lock_irqsave(&dev->event_lock, irqflags);
1862 
1863 	/* Need timestamp lock to prevent concurrent execution with
1864 	 * vblank enable/disable, as this would cause inconsistent
1865 	 * or corrupted timestamps and vblank counts.
1866 	 */
1867 	lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
1868 
1869 	/* Vblank irq handling disabled. Nothing to do. */
1870 	if (!vblank->enabled) {
1871 		lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1872 		spin_unlock_irqrestore(&dev->event_lock, irqflags);
1873 		return false;
1874 	}
1875 
1876 	drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
1877 
1878 	lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1879 
1880 	wake_up(&vblank->queue);
1881 	drm_handle_vblank_events(dev, pipe);
1882 
1883 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
1884 
1885 	return true;
1886 }
1887 EXPORT_SYMBOL(drm_handle_vblank);
1888 
1889 /**
1890  * drm_crtc_handle_vblank - handle a vblank event
1891  * @crtc: where this event occurred
1892  *
1893  * Drivers should call this routine in their vblank interrupt handlers to
1894  * update the vblank counter and send any signals that may be pending.
1895  *
1896  * This is the native KMS version of drm_handle_vblank().
1897  *
1898  * Returns:
1899  * True if the event was successfully handled, false on failure.
1900  */
1901 bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1902 {
1903 	return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1904 }
1905 EXPORT_SYMBOL(drm_crtc_handle_vblank);
1906 
1907 /**
1908  * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
1909  * @dev: DRM device
1910  * @pipe: CRTC for which to read the counter
1911  *
1912  * Drivers can plug this into the .get_vblank_counter() function if
1913  * there is no useable hardware frame counter available.
1914  *
1915  * Returns:
1916  * 0
1917  */
1918 u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
1919 {
1920 	return 0;
1921 }
1922 EXPORT_SYMBOL(drm_vblank_no_hw_counter);
1923