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