xref: /freebsd/sys/dev/drm2/drm_irq.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 /**
2  * \file drm_irq.c
3  * IRQ support
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8 
9 /*
10  * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
11  *
12  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  */
35 
36 #include <sys/cdefs.h>
37 #include <dev/drm2/drmP.h>
38 
39 /* Access macro for slots in vblank timestamp ringbuffer. */
40 #define vblanktimestamp(dev, crtc, count) ( \
41 	(dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
42 	((count) % DRM_VBLANKTIME_RBSIZE)])
43 
44 /* Retry timestamp calculation up to 3 times to satisfy
45  * drm_timestamp_precision before giving up.
46  */
47 #define DRM_TIMESTAMP_MAXRETRIES 3
48 
49 /* Threshold in nanoseconds for detection of redundant
50  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
51  */
52 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
53 
54 /**
55  * Get interrupt from bus id.
56  *
57  * \param inode device inode.
58  * \param file_priv DRM file private.
59  * \param cmd command.
60  * \param arg user argument, pointing to a drm_irq_busid structure.
61  * \return zero on success or a negative number on failure.
62  *
63  * Finds the PCI device with the specified bus id and gets its IRQ number.
64  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
65  * to that of the device that this DRM instance attached to.
66  */
67 int drm_irq_by_busid(struct drm_device *dev, void *data,
68 		     struct drm_file *file_priv)
69 {
70 	struct drm_irq_busid *p = data;
71 
72 	if (!dev->driver->bus->irq_by_busid)
73 		return -EINVAL;
74 
75 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
76 		return -EINVAL;
77 
78 	return dev->driver->bus->irq_by_busid(dev, p);
79 }
80 
81 /*
82  * Clear vblank timestamp buffer for a crtc.
83  */
84 static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
85 {
86 	memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], 0,
87 		DRM_VBLANKTIME_RBSIZE * sizeof(struct timeval));
88 }
89 
90 /*
91  * Disable vblank irq's on crtc, make sure that last vblank count
92  * of hardware and corresponding consistent software vblank counter
93  * are preserved, even if there are any spurious vblank irq's after
94  * disable.
95  */
96 static void vblank_disable_and_save(struct drm_device *dev, int crtc)
97 {
98 	u32 vblcount;
99 	s64 diff_ns;
100 	int vblrc;
101 	struct timeval tvblank;
102 	int count = DRM_TIMESTAMP_MAXRETRIES;
103 
104 	/* Prevent vblank irq processing while disabling vblank irqs,
105 	 * so no updates of timestamps or count can happen after we've
106 	 * disabled. Needed to prevent races in case of delayed irq's.
107 	 */
108 	mtx_lock(&dev->vblank_time_lock);
109 
110 	dev->driver->disable_vblank(dev, crtc);
111 	dev->vblank_enabled[crtc] = 0;
112 
113 	/* No further vblank irq's will be processed after
114 	 * this point. Get current hardware vblank count and
115 	 * vblank timestamp, repeat until they are consistent.
116 	 *
117 	 * FIXME: There is still a race condition here and in
118 	 * drm_update_vblank_count() which can cause off-by-one
119 	 * reinitialization of software vblank counter. If gpu
120 	 * vblank counter doesn't increment exactly at the leading
121 	 * edge of a vblank interval, then we can lose 1 count if
122 	 * we happen to execute between start of vblank and the
123 	 * delayed gpu counter increment.
124 	 */
125 	do {
126 		dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
127 		vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
128 	} while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
129 
130 	if (!count)
131 		vblrc = 0;
132 
133 	/* Compute time difference to stored timestamp of last vblank
134 	 * as updated by last invocation of drm_handle_vblank() in vblank irq.
135 	 */
136 	vblcount = atomic_read(&dev->_vblank_count[crtc]);
137 	diff_ns = timeval_to_ns(&tvblank) -
138 		  timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
139 
140 	/* If there is at least 1 msec difference between the last stored
141 	 * timestamp and tvblank, then we are currently executing our
142 	 * disable inside a new vblank interval, the tvblank timestamp
143 	 * corresponds to this new vblank interval and the irq handler
144 	 * for this vblank didn't run yet and won't run due to our disable.
145 	 * Therefore we need to do the job of drm_handle_vblank() and
146 	 * increment the vblank counter by one to account for this vblank.
147 	 *
148 	 * Skip this step if there isn't any high precision timestamp
149 	 * available. In that case we can't account for this and just
150 	 * hope for the best.
151 	 */
152 	if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
153 		atomic_inc(&dev->_vblank_count[crtc]);
154 		smp_mb__after_atomic_inc();
155 	}
156 
157 	/* Invalidate all timestamps while vblank irq's are off. */
158 	clear_vblank_timestamps(dev, crtc);
159 
160 	mtx_unlock(&dev->vblank_time_lock);
161 }
162 
163 static void vblank_disable_fn(void *arg)
164 {
165 	struct drm_device *dev = (struct drm_device *)arg;
166 	int i;
167 
168 	if (!dev->vblank_disable_allowed)
169 		return;
170 
171 	for (i = 0; i < dev->num_crtcs; i++) {
172 		mtx_lock(&dev->vbl_lock);
173 		if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
174 		    dev->vblank_enabled[i]) {
175 			DRM_DEBUG("disabling vblank on crtc %d\n", i);
176 			vblank_disable_and_save(dev, i);
177 		}
178 		mtx_unlock(&dev->vbl_lock);
179 	}
180 }
181 
182 void drm_vblank_cleanup(struct drm_device *dev)
183 {
184 	/* Bail if the driver didn't call drm_vblank_init() */
185 	if (dev->num_crtcs == 0)
186 		return;
187 
188 	callout_stop(&dev->vblank_disable_callout);
189 
190 	vblank_disable_fn(dev);
191 
192 	free(dev->_vblank_count, DRM_MEM_VBLANK);
193 	free(dev->vblank_refcount, DRM_MEM_VBLANK);
194 	free(dev->vblank_enabled, DRM_MEM_VBLANK);
195 	free(dev->last_vblank, DRM_MEM_VBLANK);
196 	free(dev->last_vblank_wait, DRM_MEM_VBLANK);
197 	free(dev->vblank_inmodeset, DRM_MEM_VBLANK);
198 	free(dev->_vblank_time, DRM_MEM_VBLANK);
199 
200 	mtx_destroy(&dev->vbl_lock);
201 	mtx_destroy(&dev->vblank_time_lock);
202 
203 	dev->num_crtcs = 0;
204 }
205 EXPORT_SYMBOL(drm_vblank_cleanup);
206 
207 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
208 {
209 	int i, ret = -ENOMEM;
210 
211 	callout_init(&dev->vblank_disable_callout, 1);
212 	mtx_init(&dev->vbl_lock, "drmvbl", NULL, MTX_DEF);
213 	mtx_init(&dev->vblank_time_lock, "drmvtl", NULL, MTX_DEF);
214 
215 	dev->num_crtcs = num_crtcs;
216 
217 	dev->_vblank_count = malloc(sizeof(atomic_t) * num_crtcs,
218 	    DRM_MEM_VBLANK, M_NOWAIT);
219 	if (!dev->_vblank_count)
220 		goto err;
221 
222 	dev->vblank_refcount = malloc(sizeof(atomic_t) * num_crtcs,
223 	    DRM_MEM_VBLANK, M_NOWAIT);
224 	if (!dev->vblank_refcount)
225 		goto err;
226 
227 	dev->vblank_enabled = malloc(num_crtcs * sizeof(int),
228 	    DRM_MEM_VBLANK, M_NOWAIT | M_ZERO);
229 	if (!dev->vblank_enabled)
230 		goto err;
231 
232 	dev->last_vblank = malloc(num_crtcs * sizeof(u32),
233 	    DRM_MEM_VBLANK, M_NOWAIT | M_ZERO);
234 	if (!dev->last_vblank)
235 		goto err;
236 
237 	dev->last_vblank_wait = malloc(num_crtcs * sizeof(u32),
238 	    DRM_MEM_VBLANK, M_NOWAIT | M_ZERO);
239 	if (!dev->last_vblank_wait)
240 		goto err;
241 
242 	dev->vblank_inmodeset = malloc(num_crtcs * sizeof(int),
243 	    DRM_MEM_VBLANK, M_NOWAIT | M_ZERO);
244 	if (!dev->vblank_inmodeset)
245 		goto err;
246 
247 	dev->_vblank_time = malloc(num_crtcs * DRM_VBLANKTIME_RBSIZE *
248 	    sizeof(struct timeval), DRM_MEM_VBLANK, M_NOWAIT | M_ZERO);
249 	if (!dev->_vblank_time)
250 		goto err;
251 
252 	DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
253 
254 	/* Driver specific high-precision vblank timestamping supported? */
255 	if (dev->driver->get_vblank_timestamp)
256 		DRM_INFO("Driver supports precise vblank timestamp query.\n");
257 	else
258 		DRM_INFO("No driver support for vblank timestamp query.\n");
259 
260 	/* Zero per-crtc vblank stuff */
261 	for (i = 0; i < num_crtcs; i++) {
262 		atomic_set(&dev->_vblank_count[i], 0);
263 		atomic_set(&dev->vblank_refcount[i], 0);
264 	}
265 
266 	dev->vblank_disable_allowed = 0;
267 	return 0;
268 
269 err:
270 	drm_vblank_cleanup(dev);
271 	return ret;
272 }
273 EXPORT_SYMBOL(drm_vblank_init);
274 
275 /**
276  * Install IRQ handler.
277  *
278  * \param dev DRM device.
279  *
280  * Initializes the IRQ related data. Installs the handler, calling the driver
281  * \c irq_preinstall() and \c irq_postinstall() functions
282  * before and after the installation.
283  */
284 int drm_irq_install(struct drm_device *dev)
285 {
286 	int ret;
287 	unsigned long sh_flags = 0;
288 
289 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
290 		return -EINVAL;
291 
292 	if (drm_dev_to_irq(dev) == 0)
293 		return -EINVAL;
294 
295 	DRM_LOCK(dev);
296 
297 	/* Driver must have been initialized */
298 	if (!dev->dev_private) {
299 		DRM_UNLOCK(dev);
300 		return -EINVAL;
301 	}
302 
303 	if (dev->irq_enabled) {
304 		DRM_UNLOCK(dev);
305 		return -EBUSY;
306 	}
307 	dev->irq_enabled = 1;
308 	DRM_UNLOCK(dev);
309 
310 	DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
311 
312 	/* Before installing handler */
313 	if (dev->driver->irq_preinstall)
314 		dev->driver->irq_preinstall(dev);
315 
316 	/* Install handler */
317 	sh_flags = INTR_TYPE_TTY | INTR_MPSAFE;
318 	if (!drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
319 		/*
320 		 * FIXME Linux<->FreeBSD: This seems to make
321 		 * bus_setup_intr() unhappy: it was reported to return
322 		 * EINVAL on an i915 board (8086:2592 in a Thinkpad
323 		 * X41).
324 		 *
325 		 * For now, no driver we have use that.
326 		 */
327 		sh_flags |= INTR_EXCL;
328 
329 	ret = -bus_setup_intr(dev->dev, dev->irqr, sh_flags, NULL,
330 	    dev->driver->irq_handler, dev, &dev->irqh);
331 
332 	if (ret < 0) {
333 		device_printf(dev->dev, "Error setting interrupt: %d\n", -ret);
334 		DRM_LOCK(dev);
335 		dev->irq_enabled = 0;
336 		DRM_UNLOCK(dev);
337 		return ret;
338 	}
339 
340 	/* After installing handler */
341 	if (dev->driver->irq_postinstall)
342 		ret = dev->driver->irq_postinstall(dev);
343 
344 	if (ret < 0) {
345 		DRM_LOCK(dev);
346 		dev->irq_enabled = 0;
347 		DRM_UNLOCK(dev);
348 		bus_teardown_intr(dev->dev, dev->irqr, dev->irqh);
349 		dev->driver->bus->free_irq(dev);
350 	}
351 
352 	return ret;
353 }
354 EXPORT_SYMBOL(drm_irq_install);
355 
356 /**
357  * Uninstall the IRQ handler.
358  *
359  * \param dev DRM device.
360  *
361  * Calls the driver's \c irq_uninstall() function, and stops the irq.
362  */
363 int drm_irq_uninstall(struct drm_device *dev)
364 {
365 	int irq_enabled, i;
366 
367 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
368 		return -EINVAL;
369 
370 	DRM_LOCK(dev);
371 	irq_enabled = dev->irq_enabled;
372 	dev->irq_enabled = 0;
373 	DRM_UNLOCK(dev);
374 
375 	/*
376 	 * Wake up any waiters so they don't hang.
377 	 */
378 	if (dev->num_crtcs) {
379 		mtx_lock(&dev->vbl_lock);
380 		for (i = 0; i < dev->num_crtcs; i++) {
381 			DRM_WAKEUP(&dev->_vblank_count[i]);
382 			dev->vblank_enabled[i] = 0;
383 			dev->last_vblank[i] =
384 				dev->driver->get_vblank_counter(dev, i);
385 		}
386 		mtx_unlock(&dev->vbl_lock);
387 	}
388 
389 	if (!irq_enabled)
390 		return -EINVAL;
391 
392 	DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
393 
394 	if (dev->driver->irq_uninstall)
395 		dev->driver->irq_uninstall(dev);
396 
397 	bus_teardown_intr(dev->dev, dev->irqr, dev->irqh);
398 	dev->driver->bus->free_irq(dev);
399 
400 	return 0;
401 }
402 EXPORT_SYMBOL(drm_irq_uninstall);
403 
404 /**
405  * IRQ control ioctl.
406  *
407  * \param inode device inode.
408  * \param file_priv DRM file private.
409  * \param cmd command.
410  * \param arg user argument, pointing to a drm_control structure.
411  * \return zero on success or a negative number on failure.
412  *
413  * Calls irq_install() or irq_uninstall() according to \p arg.
414  */
415 int drm_control(struct drm_device *dev, void *data,
416 		struct drm_file *file_priv)
417 {
418 	struct drm_control *ctl = data;
419 
420 	/* if we haven't irq we fallback for compatibility reasons -
421 	 * this used to be a separate function in drm_dma.h
422 	 */
423 
424 
425 	switch (ctl->func) {
426 	case DRM_INST_HANDLER:
427 		if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
428 			return 0;
429 		if (drm_core_check_feature(dev, DRIVER_MODESET))
430 			return 0;
431 		if (dev->if_version < DRM_IF_VERSION(1, 2) &&
432 		    ctl->irq != drm_dev_to_irq(dev))
433 			return -EINVAL;
434 		return drm_irq_install(dev);
435 	case DRM_UNINST_HANDLER:
436 		if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
437 			return 0;
438 		if (drm_core_check_feature(dev, DRIVER_MODESET))
439 			return 0;
440 		return drm_irq_uninstall(dev);
441 	default:
442 		return -EINVAL;
443 	}
444 }
445 
446 /**
447  * drm_calc_timestamping_constants - Calculate and
448  * store various constants which are later needed by
449  * vblank and swap-completion timestamping, e.g, by
450  * drm_calc_vbltimestamp_from_scanoutpos().
451  * They are derived from crtc's true scanout timing,
452  * so they take things like panel scaling or other
453  * adjustments into account.
454  *
455  * @crtc drm_crtc whose timestamp constants should be updated.
456  *
457  */
458 void drm_calc_timestamping_constants(struct drm_crtc *crtc)
459 {
460 	s64 linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
461 	u64 dotclock;
462 
463 	/* Dot clock in Hz: */
464 	dotclock = (u64) crtc->hwmode.clock * 1000;
465 
466 	/* Fields of interlaced scanout modes are only halve a frame duration.
467 	 * Double the dotclock to get halve the frame-/line-/pixelduration.
468 	 */
469 	if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
470 		dotclock *= 2;
471 
472 	/* Valid dotclock? */
473 	if (dotclock > 0) {
474 		/* Convert scanline length in pixels and video dot clock to
475 		 * line duration, frame duration and pixel duration in
476 		 * nanoseconds:
477 		 */
478 		pixeldur_ns = (s64) div64_u64(1000000000, dotclock);
479 		linedur_ns  = (s64) div64_u64(((u64) crtc->hwmode.crtc_htotal *
480 					      1000000000), dotclock);
481 		framedur_ns = (s64) crtc->hwmode.crtc_vtotal * linedur_ns;
482 	} else
483 		DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
484 			  crtc->base.id);
485 
486 	crtc->pixeldur_ns = pixeldur_ns;
487 	crtc->linedur_ns  = linedur_ns;
488 	crtc->framedur_ns = framedur_ns;
489 
490 	DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
491 		  crtc->base.id, crtc->hwmode.crtc_htotal,
492 		  crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
493 	DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
494 		  crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
495 		  (int) linedur_ns, (int) pixeldur_ns);
496 }
497 EXPORT_SYMBOL(drm_calc_timestamping_constants);
498 
499 /**
500  * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
501  * drivers. Implements calculation of exact vblank timestamps from
502  * given drm_display_mode timings and current video scanout position
503  * of a crtc. This can be called from within get_vblank_timestamp()
504  * implementation of a kms driver to implement the actual timestamping.
505  *
506  * Should return timestamps conforming to the OML_sync_control OpenML
507  * extension specification. The timestamp corresponds to the end of
508  * the vblank interval, aka start of scanout of topmost-leftmost display
509  * pixel in the following video frame.
510  *
511  * Requires support for optional dev->driver->get_scanout_position()
512  * in kms driver, plus a bit of setup code to provide a drm_display_mode
513  * that corresponds to the true scanout timing.
514  *
515  * The current implementation only handles standard video modes. It
516  * returns as no operation if a doublescan or interlaced video mode is
517  * active. Higher level code is expected to handle this.
518  *
519  * @dev: DRM device.
520  * @crtc: Which crtc's vblank timestamp to retrieve.
521  * @max_error: Desired maximum allowable error in timestamps (nanosecs).
522  *             On return contains true maximum error of timestamp.
523  * @vblank_time: Pointer to struct timeval which should receive the timestamp.
524  * @flags: Flags to pass to driver:
525  *         0 = Default.
526  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
527  * @refcrtc: drm_crtc* of crtc which defines scanout timing.
528  *
529  * Returns negative value on error, failure or if not supported in current
530  * video mode:
531  *
532  * -EINVAL   - Invalid crtc.
533  * -EAGAIN   - Temporary unavailable, e.g., called before initial modeset.
534  * -ENOTSUPP - Function not supported in current display mode.
535  * -EIO      - Failed, e.g., due to failed scanout position query.
536  *
537  * Returns or'ed positive status flags on success:
538  *
539  * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
540  * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
541  *
542  */
543 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
544 					  int *max_error,
545 					  struct timeval *vblank_time,
546 					  unsigned flags,
547 					  struct drm_crtc *refcrtc)
548 {
549 	struct timeval stime, raw_time;
550 	struct drm_display_mode *mode;
551 	int vbl_status, vtotal, vdisplay;
552 	int vpos, hpos, i;
553 	s64 framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
554 	bool invbl;
555 
556 	if (crtc < 0 || crtc >= dev->num_crtcs) {
557 		DRM_ERROR("Invalid crtc %d\n", crtc);
558 		return -EINVAL;
559 	}
560 
561 	/* Scanout position query not supported? Should not happen. */
562 	if (!dev->driver->get_scanout_position) {
563 		DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
564 		return -EIO;
565 	}
566 
567 	mode = &refcrtc->hwmode;
568 	vtotal = mode->crtc_vtotal;
569 	vdisplay = mode->crtc_vdisplay;
570 
571 	/* Durations of frames, lines, pixels in nanoseconds. */
572 	framedur_ns = refcrtc->framedur_ns;
573 	linedur_ns  = refcrtc->linedur_ns;
574 	pixeldur_ns = refcrtc->pixeldur_ns;
575 
576 	/* If mode timing undefined, just return as no-op:
577 	 * Happens during initial modesetting of a crtc.
578 	 */
579 	if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
580 		DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
581 		return -EAGAIN;
582 	}
583 
584 	/* Get current scanout position with system timestamp.
585 	 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
586 	 * if single query takes longer than max_error nanoseconds.
587 	 *
588 	 * This guarantees a tight bound on maximum error if
589 	 * code gets preempted or delayed for some reason.
590 	 */
591 	for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
592 		/* Disable preemption to make it very likely to
593 		 * succeed in the first iteration even on PREEMPT_RT kernel.
594 		 */
595 		critical_enter();
596 
597 		/* Get system timestamp before query. */
598 		getmicrouptime(&stime);
599 
600 		/* Get vertical and horizontal scanout pos. vpos, hpos. */
601 		vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
602 
603 		/* Get system timestamp after query. */
604 		getmicrouptime(&raw_time);
605 #ifdef FREEBSD_NOTYET
606 		if (!drm_timestamp_monotonic)
607 			mono_time_offset = ktime_get_monotonic_offset();
608 #endif /* FREEBSD_NOTYET */
609 
610 		critical_exit();
611 
612 		/* Return as no-op if scanout query unsupported or failed. */
613 		if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
614 			DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
615 				  crtc, vbl_status);
616 			return -EIO;
617 		}
618 
619 		duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
620 
621 		/* Accept result with <  max_error nsecs timing uncertainty. */
622 		if (duration_ns <= (s64) *max_error)
623 			break;
624 	}
625 
626 	/* Noisy system timing? */
627 	if (i == DRM_TIMESTAMP_MAXRETRIES) {
628 		DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
629 			  crtc, (int) duration_ns/1000, *max_error/1000, i);
630 	}
631 
632 	/* Return upper bound of timestamp precision error. */
633 	*max_error = (int) duration_ns;
634 
635 	/* Check if in vblank area:
636 	 * vpos is >=0 in video scanout area, but negative
637 	 * within vblank area, counting down the number of lines until
638 	 * start of scanout.
639 	 */
640 	invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
641 
642 	/* Convert scanout position into elapsed time at raw_time query
643 	 * since start of scanout at first display scanline. delta_ns
644 	 * can be negative if start of scanout hasn't happened yet.
645 	 */
646 	delta_ns = (s64) vpos * linedur_ns + (s64) hpos * pixeldur_ns;
647 
648 	/* Is vpos outside nominal vblank area, but less than
649 	 * 1/100 of a frame height away from start of vblank?
650 	 * If so, assume this isn't a massively delayed vblank
651 	 * interrupt, but a vblank interrupt that fired a few
652 	 * microseconds before true start of vblank. Compensate
653 	 * by adding a full frame duration to the final timestamp.
654 	 * Happens, e.g., on ATI R500, R600.
655 	 *
656 	 * We only do this if DRM_CALLED_FROM_VBLIRQ.
657 	 */
658 	if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
659 	    ((vdisplay - vpos) < vtotal / 100)) {
660 		delta_ns = delta_ns - framedur_ns;
661 
662 		/* Signal this correction as "applied". */
663 		vbl_status |= 0x8;
664 	}
665 
666 #ifdef FREEBSD_NOTYET
667 	if (!drm_timestamp_monotonic)
668 		etime = ktime_sub(etime, mono_time_offset);
669 
670 	/* save this only for debugging purposes */
671 	tv_etime = ktime_to_timeval(etime);
672 #endif /* FREEBSD_NOTYET */
673 	/* Subtract time delta from raw timestamp to get final
674 	 * vblank_time timestamp for end of vblank.
675 	 */
676 	*vblank_time = ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns);
677 
678 	DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %jd.%jd -> %jd.%jd [e %d us, %d rep]\n",
679 		  crtc, (int)vbl_status, hpos, vpos, (uintmax_t)raw_time.tv_sec,
680 		  (uintmax_t)raw_time.tv_usec, (uintmax_t)vblank_time->tv_sec,
681 		  (uintmax_t)vblank_time->tv_usec, (int)duration_ns/1000, i);
682 
683 	vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
684 	if (invbl)
685 		vbl_status |= DRM_VBLANKTIME_INVBL;
686 
687 	return vbl_status;
688 }
689 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
690 
691 static struct timeval get_drm_timestamp(void)
692 {
693 	struct timeval now;
694 
695 	microtime(&now);
696 #ifdef FREEBSD_NOTYET
697 	if (!drm_timestamp_monotonic)
698 		now = ktime_sub(now, ktime_get_monotonic_offset());
699 #endif /* defined(FREEBSD_NOTYET) */
700 
701 	return now;
702 }
703 
704 /**
705  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
706  * vblank interval.
707  *
708  * @dev: DRM device
709  * @crtc: which crtc's vblank timestamp to retrieve
710  * @tvblank: Pointer to target struct timeval which should receive the timestamp
711  * @flags: Flags to pass to driver:
712  *         0 = Default.
713  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
714  *
715  * Fetches the system timestamp corresponding to the time of the most recent
716  * vblank interval on specified crtc. May call into kms-driver to
717  * compute the timestamp with a high-precision GPU specific method.
718  *
719  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
720  * call, i.e., it isn't very precisely locked to the true vblank.
721  *
722  * Returns non-zero if timestamp is considered to be very precise.
723  */
724 u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
725 			      struct timeval *tvblank, unsigned flags)
726 {
727 	int ret;
728 
729 	/* Define requested maximum error on timestamps (nanoseconds). */
730 	int max_error = (int) drm_timestamp_precision * 1000;
731 
732 	/* Query driver if possible and precision timestamping enabled. */
733 	if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
734 		ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
735 							tvblank, flags);
736 		if (ret > 0)
737 			return (u32) ret;
738 	}
739 
740 	/* GPU high precision timestamp query unsupported or failed.
741 	 * Return current monotonic/gettimeofday timestamp as best estimate.
742 	 */
743 	*tvblank = get_drm_timestamp();
744 
745 	return 0;
746 }
747 EXPORT_SYMBOL(drm_get_last_vbltimestamp);
748 
749 /**
750  * drm_vblank_count - retrieve "cooked" vblank counter value
751  * @dev: DRM device
752  * @crtc: which counter to retrieve
753  *
754  * Fetches the "cooked" vblank count value that represents the number of
755  * vblank events since the system was booted, including lost events due to
756  * modesetting activity.
757  */
758 u32 drm_vblank_count(struct drm_device *dev, int crtc)
759 {
760 	return atomic_read(&dev->_vblank_count[crtc]);
761 }
762 EXPORT_SYMBOL(drm_vblank_count);
763 
764 /**
765  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
766  * and the system timestamp corresponding to that vblank counter value.
767  *
768  * @dev: DRM device
769  * @crtc: which counter to retrieve
770  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
771  *
772  * Fetches the "cooked" vblank count value that represents the number of
773  * vblank events since the system was booted, including lost events due to
774  * modesetting activity. Returns corresponding system timestamp of the time
775  * of the vblank interval that corresponds to the current value vblank counter
776  * value.
777  */
778 u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
779 			      struct timeval *vblanktime)
780 {
781 	u32 cur_vblank;
782 
783 	/* Read timestamp from slot of _vblank_time ringbuffer
784 	 * that corresponds to current vblank count. Retry if
785 	 * count has incremented during readout. This works like
786 	 * a seqlock.
787 	 */
788 	do {
789 		cur_vblank = atomic_read(&dev->_vblank_count[crtc]);
790 		*vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
791 		smp_rmb();
792 	} while (cur_vblank != atomic_read(&dev->_vblank_count[crtc]));
793 
794 	return cur_vblank;
795 }
796 EXPORT_SYMBOL(drm_vblank_count_and_time);
797 
798 static void send_vblank_event(struct drm_device *dev,
799 		struct drm_pending_vblank_event *e,
800 		unsigned long seq, struct timeval *now)
801 {
802 	WARN_ON_SMP(!mtx_owned(&dev->event_lock));
803 	e->event.sequence = seq;
804 	e->event.tv_sec = now->tv_sec;
805 	e->event.tv_usec = now->tv_usec;
806 
807 	list_add_tail(&e->base.link,
808 		      &e->base.file_priv->event_list);
809 	drm_event_wakeup(&e->base);
810 	CTR3(KTR_DRM, "vblank_event_delivered %d %d %d",
811 	    e->base.pid, e->pipe, e->event.sequence);
812 }
813 
814 /**
815  * drm_send_vblank_event - helper to send vblank event after pageflip
816  * @dev: DRM device
817  * @crtc: CRTC in question
818  * @e: the event to send
819  *
820  * Updates sequence # and timestamp on event, and sends it to userspace.
821  * Caller must hold event lock.
822  */
823 void drm_send_vblank_event(struct drm_device *dev, int crtc,
824 		struct drm_pending_vblank_event *e)
825 {
826 	struct timeval now;
827 	unsigned int seq;
828 	if (crtc >= 0) {
829 		seq = drm_vblank_count_and_time(dev, crtc, &now);
830 	} else {
831 		seq = 0;
832 
833 		now = get_drm_timestamp();
834 	}
835 	send_vblank_event(dev, e, seq, &now);
836 }
837 EXPORT_SYMBOL(drm_send_vblank_event);
838 
839 /**
840  * drm_update_vblank_count - update the master vblank counter
841  * @dev: DRM device
842  * @crtc: counter to update
843  *
844  * Call back into the driver to update the appropriate vblank counter
845  * (specified by @crtc).  Deal with wraparound, if it occurred, and
846  * update the last read value so we can deal with wraparound on the next
847  * call if necessary.
848  *
849  * Only necessary when going from off->on, to account for frames we
850  * didn't get an interrupt for.
851  *
852  * Note: caller must hold dev->vbl_lock since this reads & writes
853  * device vblank fields.
854  */
855 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
856 {
857 	u32 cur_vblank, diff, tslot, rc;
858 	struct timeval t_vblank;
859 
860 	/*
861 	 * Interrupts were disabled prior to this call, so deal with counter
862 	 * wrap if needed.
863 	 * NOTE!  It's possible we lost a full dev->max_vblank_count events
864 	 * here if the register is small or we had vblank interrupts off for
865 	 * a long time.
866 	 *
867 	 * We repeat the hardware vblank counter & timestamp query until
868 	 * we get consistent results. This to prevent races between gpu
869 	 * updating its hardware counter while we are retrieving the
870 	 * corresponding vblank timestamp.
871 	 */
872 	do {
873 		cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
874 		rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
875 	} while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
876 
877 	/* Deal with counter wrap */
878 	diff = cur_vblank - dev->last_vblank[crtc];
879 	if (cur_vblank < dev->last_vblank[crtc]) {
880 		diff += dev->max_vblank_count;
881 
882 		DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
883 			  crtc, dev->last_vblank[crtc], cur_vblank, diff);
884 	}
885 
886 	DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
887 		  crtc, diff);
888 
889 	/* Reinitialize corresponding vblank timestamp if high-precision query
890 	 * available. Skip this step if query unsupported or failed. Will
891 	 * reinitialize delayed at next vblank interrupt in that case.
892 	 */
893 	if (rc) {
894 		tslot = atomic_read(&dev->_vblank_count[crtc]) + diff;
895 		vblanktimestamp(dev, crtc, tslot) = t_vblank;
896 	}
897 
898 	smp_mb__before_atomic_inc();
899 	atomic_add(diff, &dev->_vblank_count[crtc]);
900 	smp_mb__after_atomic_inc();
901 }
902 
903 /**
904  * drm_vblank_get - get a reference count on vblank events
905  * @dev: DRM device
906  * @crtc: which CRTC to own
907  *
908  * Acquire a reference count on vblank events to avoid having them disabled
909  * while in use.
910  *
911  * RETURNS
912  * Zero on success, nonzero on failure.
913  */
914 int drm_vblank_get(struct drm_device *dev, int crtc)
915 {
916 	int ret = 0;
917 
918 	mtx_lock(&dev->vbl_lock);
919 	/* Going from 0->1 means we have to enable interrupts again */
920 	if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
921 		mtx_lock(&dev->vblank_time_lock);
922 		if (!dev->vblank_enabled[crtc]) {
923 			/* Enable vblank irqs under vblank_time_lock protection.
924 			 * All vblank count & timestamp updates are held off
925 			 * until we are done reinitializing master counter and
926 			 * timestamps. Filtercode in drm_handle_vblank() will
927 			 * prevent double-accounting of same vblank interval.
928 			 */
929 			ret = dev->driver->enable_vblank(dev, crtc);
930 			DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
931 				  crtc, ret);
932 			if (ret)
933 				atomic_dec(&dev->vblank_refcount[crtc]);
934 			else {
935 				dev->vblank_enabled[crtc] = 1;
936 				drm_update_vblank_count(dev, crtc);
937 			}
938 		}
939 		mtx_unlock(&dev->vblank_time_lock);
940 	} else {
941 		if (!dev->vblank_enabled[crtc]) {
942 			atomic_dec(&dev->vblank_refcount[crtc]);
943 			ret = -EINVAL;
944 		}
945 	}
946 	mtx_unlock(&dev->vbl_lock);
947 
948 	return ret;
949 }
950 EXPORT_SYMBOL(drm_vblank_get);
951 
952 /**
953  * drm_vblank_put - give up ownership of vblank events
954  * @dev: DRM device
955  * @crtc: which counter to give up
956  *
957  * Release ownership of a given vblank counter, turning off interrupts
958  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
959  */
960 void drm_vblank_put(struct drm_device *dev, int crtc)
961 {
962 	BUG_ON(atomic_read(&dev->vblank_refcount[crtc]) == 0);
963 
964 	/* Last user schedules interrupt disable */
965 	if (atomic_dec_and_test(&dev->vblank_refcount[crtc]) &&
966 	    (drm_vblank_offdelay > 0))
967 		callout_reset(&dev->vblank_disable_callout,
968 		    (drm_vblank_offdelay * DRM_HZ) / 1000,
969 		    vblank_disable_fn, dev);
970 }
971 EXPORT_SYMBOL(drm_vblank_put);
972 
973 /**
974  * drm_vblank_off - disable vblank events on a CRTC
975  * @dev: DRM device
976  * @crtc: CRTC in question
977  *
978  * Caller must hold event lock.
979  */
980 void drm_vblank_off(struct drm_device *dev, int crtc)
981 {
982 	struct drm_pending_vblank_event *e, *t;
983 	struct timeval now;
984 	unsigned int seq;
985 
986 	mtx_lock(&dev->vbl_lock);
987 	vblank_disable_and_save(dev, crtc);
988 	DRM_WAKEUP(&dev->_vblank_count[crtc]);
989 
990 	/* Send any queued vblank events, lest the natives grow disquiet */
991 	seq = drm_vblank_count_and_time(dev, crtc, &now);
992 
993 	mtx_lock(&dev->event_lock);
994 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
995 		if (e->pipe != crtc)
996 			continue;
997 		DRM_DEBUG("Sending premature vblank event on disable: \
998 			  wanted %d, current %d\n",
999 			  e->event.sequence, seq);
1000 		list_del(&e->base.link);
1001 		drm_vblank_put(dev, e->pipe);
1002 		send_vblank_event(dev, e, seq, &now);
1003 	}
1004 	mtx_unlock(&dev->event_lock);
1005 
1006 	mtx_unlock(&dev->vbl_lock);
1007 }
1008 EXPORT_SYMBOL(drm_vblank_off);
1009 
1010 /**
1011  * drm_vblank_pre_modeset - account for vblanks across mode sets
1012  * @dev: DRM device
1013  * @crtc: CRTC in question
1014  *
1015  * Account for vblank events across mode setting events, which will likely
1016  * reset the hardware frame counter.
1017  */
1018 void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1019 {
1020 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
1021 	if (!dev->num_crtcs)
1022 		return;
1023 	/*
1024 	 * To avoid all the problems that might happen if interrupts
1025 	 * were enabled/disabled around or between these calls, we just
1026 	 * have the kernel take a reference on the CRTC (just once though
1027 	 * to avoid corrupting the count if multiple, mismatch calls occur),
1028 	 * so that interrupts remain enabled in the interim.
1029 	 */
1030 	if (!dev->vblank_inmodeset[crtc]) {
1031 		dev->vblank_inmodeset[crtc] = 0x1;
1032 		if (drm_vblank_get(dev, crtc) == 0)
1033 			dev->vblank_inmodeset[crtc] |= 0x2;
1034 	}
1035 }
1036 EXPORT_SYMBOL(drm_vblank_pre_modeset);
1037 
1038 void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1039 {
1040 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
1041 	if (!dev->num_crtcs)
1042 		return;
1043 
1044 	if (dev->vblank_inmodeset[crtc]) {
1045 		mtx_lock(&dev->vbl_lock);
1046 		dev->vblank_disable_allowed = 1;
1047 		mtx_unlock(&dev->vbl_lock);
1048 
1049 		if (dev->vblank_inmodeset[crtc] & 0x2)
1050 			drm_vblank_put(dev, crtc);
1051 
1052 		dev->vblank_inmodeset[crtc] = 0;
1053 	}
1054 }
1055 EXPORT_SYMBOL(drm_vblank_post_modeset);
1056 
1057 /**
1058  * drm_modeset_ctl - handle vblank event counter changes across mode switch
1059  * @DRM_IOCTL_ARGS: standard ioctl arguments
1060  *
1061  * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1062  * ioctls around modesetting so that any lost vblank events are accounted for.
1063  *
1064  * Generally the counter will reset across mode sets.  If interrupts are
1065  * enabled around this call, we don't have to do anything since the counter
1066  * will have already been incremented.
1067  */
1068 int drm_modeset_ctl(struct drm_device *dev, void *data,
1069 		    struct drm_file *file_priv)
1070 {
1071 	struct drm_modeset_ctl *modeset = data;
1072 	unsigned int crtc;
1073 
1074 	/* If drm_vblank_init() hasn't been called yet, just no-op */
1075 	if (!dev->num_crtcs)
1076 		return 0;
1077 
1078 	/* KMS drivers handle this internally */
1079 	if (drm_core_check_feature(dev, DRIVER_MODESET))
1080 		return 0;
1081 
1082 	crtc = modeset->crtc;
1083 	if (crtc >= dev->num_crtcs)
1084 		return -EINVAL;
1085 
1086 	switch (modeset->cmd) {
1087 	case _DRM_PRE_MODESET:
1088 		drm_vblank_pre_modeset(dev, crtc);
1089 		break;
1090 	case _DRM_POST_MODESET:
1091 		drm_vblank_post_modeset(dev, crtc);
1092 		break;
1093 	default:
1094 		return -EINVAL;
1095 	}
1096 
1097 	return 0;
1098 }
1099 
1100 static void
1101 drm_vblank_event_destroy(struct drm_pending_event *e)
1102 {
1103 
1104 	free(e, DRM_MEM_VBLANK);
1105 }
1106 
1107 static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1108 				  union drm_wait_vblank *vblwait,
1109 				  struct drm_file *file_priv)
1110 {
1111 	struct drm_pending_vblank_event *e;
1112 	struct timeval now;
1113 	unsigned int seq;
1114 	int ret;
1115 
1116 	e = malloc(sizeof *e, DRM_MEM_VBLANK, M_NOWAIT | M_ZERO);
1117 	if (e == NULL) {
1118 		ret = -ENOMEM;
1119 		goto err_put;
1120 	}
1121 
1122 	e->pipe = pipe;
1123 	e->base.pid = curproc->p_pid;
1124 	e->event.base.type = DRM_EVENT_VBLANK;
1125 	e->event.base.length = sizeof e->event;
1126 	e->event.user_data = vblwait->request.signal;
1127 	e->base.event = &e->event.base;
1128 	e->base.file_priv = file_priv;
1129 	e->base.destroy = drm_vblank_event_destroy;
1130 
1131 	mtx_lock(&dev->event_lock);
1132 
1133 	if (file_priv->event_space < sizeof e->event) {
1134 		ret = -EBUSY;
1135 		goto err_unlock;
1136 	}
1137 
1138 	file_priv->event_space -= sizeof e->event;
1139 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1140 
1141 	if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1142 	    (seq - vblwait->request.sequence) <= (1 << 23)) {
1143 		vblwait->request.sequence = seq + 1;
1144 		vblwait->reply.sequence = vblwait->request.sequence;
1145 	}
1146 
1147 	DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1148 		  vblwait->request.sequence, seq, pipe);
1149 
1150 	CTR4(KTR_DRM, "vblank_event_queued %d %d rt %x %d", curproc->p_pid, pipe,
1151 	    vblwait->request.type, vblwait->request.sequence);
1152 
1153 	e->event.sequence = vblwait->request.sequence;
1154 	if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1155 		drm_vblank_put(dev, pipe);
1156 		send_vblank_event(dev, e, seq, &now);
1157 		vblwait->reply.sequence = seq;
1158 	} else {
1159 		/* drm_handle_vblank_events will call drm_vblank_put */
1160 		list_add_tail(&e->base.link, &dev->vblank_event_list);
1161 		vblwait->reply.sequence = vblwait->request.sequence;
1162 	}
1163 
1164 	mtx_unlock(&dev->event_lock);
1165 
1166 	return 0;
1167 
1168 err_unlock:
1169 	mtx_unlock(&dev->event_lock);
1170 	free(e, DRM_MEM_VBLANK);
1171 err_put:
1172 	drm_vblank_put(dev, pipe);
1173 	return ret;
1174 }
1175 
1176 /**
1177  * Wait for VBLANK.
1178  *
1179  * \param inode device inode.
1180  * \param file_priv DRM file private.
1181  * \param cmd command.
1182  * \param data user argument, pointing to a drm_wait_vblank structure.
1183  * \return zero on success or a negative number on failure.
1184  *
1185  * This function enables the vblank interrupt on the pipe requested, then
1186  * sleeps waiting for the requested sequence number to occur, and drops
1187  * the vblank interrupt refcount afterwards. (vblank irq disable follows that
1188  * after a timeout with no further vblank waits scheduled).
1189  */
1190 int drm_wait_vblank(struct drm_device *dev, void *data,
1191 		    struct drm_file *file_priv)
1192 {
1193 	union drm_wait_vblank *vblwait = data;
1194 	int ret;
1195 	unsigned int flags, seq, crtc, high_crtc;
1196 
1197 	if (/*(!drm_dev_to_irq(dev)) || */(!dev->irq_enabled))
1198 		return -EINVAL;
1199 
1200 	if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1201 		return -EINVAL;
1202 
1203 	if (vblwait->request.type &
1204 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1205 	      _DRM_VBLANK_HIGH_CRTC_MASK)) {
1206 		DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1207 			  vblwait->request.type,
1208 			  (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1209 			   _DRM_VBLANK_HIGH_CRTC_MASK));
1210 		return -EINVAL;
1211 	}
1212 
1213 	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1214 	high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1215 	if (high_crtc)
1216 		crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1217 	else
1218 		crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1219 	if (crtc >= dev->num_crtcs)
1220 		return -EINVAL;
1221 
1222 	ret = drm_vblank_get(dev, crtc);
1223 	if (ret) {
1224 		DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1225 		return ret;
1226 	}
1227 	seq = drm_vblank_count(dev, crtc);
1228 
1229 	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1230 	case _DRM_VBLANK_RELATIVE:
1231 		vblwait->request.sequence += seq;
1232 		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1233 	case _DRM_VBLANK_ABSOLUTE:
1234 		break;
1235 	default:
1236 		ret = -EINVAL;
1237 		goto done;
1238 	}
1239 
1240 	if (flags & _DRM_VBLANK_EVENT) {
1241 		/* must hold on to the vblank ref until the event fires
1242 		 * drm_vblank_put will be called asynchronously
1243 		 */
1244 		return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
1245 	}
1246 
1247 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1248 	    (seq - vblwait->request.sequence) <= (1<<23)) {
1249 		vblwait->request.sequence = seq + 1;
1250 	}
1251 
1252 	DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1253 		  vblwait->request.sequence, crtc);
1254 	dev->last_vblank_wait[crtc] = vblwait->request.sequence;
1255 	mtx_lock(&dev->vblank_time_lock);
1256 	while (((drm_vblank_count(dev, crtc) - vblwait->request.sequence) >
1257 	    (1 << 23)) && dev->irq_enabled) {
1258 		/*
1259 		 * The wakeups from the drm_irq_uninstall() and
1260 		 * drm_vblank_off() may be lost there since vbl_lock
1261 		 * is not held.  Then, the timeout will wake us; the 3
1262 		 * seconds delay should not be a problem for
1263 		 * application when crtc is disabled or irq
1264 		 * uninstalled anyway.
1265 		 */
1266 		ret = -msleep(&dev->_vblank_count[crtc], &dev->vblank_time_lock,
1267 		    PCATCH, "drmvbl", 3 * hz);
1268 		if (ret == -ERESTART)
1269 			ret = -ERESTARTSYS;
1270 		if (ret != 0)
1271 			break;
1272 	}
1273 	mtx_unlock(&dev->vblank_time_lock);
1274 	if (ret != -EINTR) {
1275 		struct timeval now;
1276 		long reply_seq;
1277 
1278 		reply_seq = drm_vblank_count_and_time(dev, crtc, &now);
1279 		CTR5(KTR_DRM, "wait_vblank %d %d rt %x success %d %d",
1280 		    curproc->p_pid, crtc, vblwait->request.type,
1281 		    vblwait->request.sequence, reply_seq);
1282 
1283 		vblwait->reply.sequence = reply_seq;
1284 		vblwait->reply.tval_sec = now.tv_sec;
1285 		vblwait->reply.tval_usec = now.tv_usec;
1286 
1287 		DRM_DEBUG("returning %d to client\n",
1288 			  vblwait->reply.sequence);
1289 	} else {
1290 		CTR5(KTR_DRM, "wait_vblank %d %d rt %x error %d %d",
1291 		    curproc->p_pid, crtc, vblwait->request.type, ret,
1292 		    vblwait->request.sequence);
1293 
1294 		DRM_DEBUG("vblank wait interrupted by signal\n");
1295 	}
1296 
1297 done:
1298 	drm_vblank_put(dev, crtc);
1299 	return ret;
1300 }
1301 
1302 static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
1303 {
1304 	struct drm_pending_vblank_event *e, *t;
1305 	struct timeval now;
1306 	unsigned int seq;
1307 
1308 	seq = drm_vblank_count_and_time(dev, crtc, &now);
1309 
1310 	mtx_lock(&dev->event_lock);
1311 
1312 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1313 		if (e->pipe != crtc)
1314 			continue;
1315 		if ((seq - e->event.sequence) > (1<<23))
1316 			continue;
1317 
1318 		DRM_DEBUG("vblank event on %d, current %d\n",
1319 			  e->event.sequence, seq);
1320 
1321 		list_del(&e->base.link);
1322 		drm_vblank_put(dev, e->pipe);
1323 		send_vblank_event(dev, e, seq, &now);
1324 	}
1325 
1326 	mtx_unlock(&dev->event_lock);
1327 
1328 	CTR2(KTR_DRM, "drm_handle_vblank_events %d %d", seq, crtc);
1329 }
1330 
1331 /**
1332  * drm_handle_vblank - handle a vblank event
1333  * @dev: DRM device
1334  * @crtc: where this event occurred
1335  *
1336  * Drivers should call this routine in their vblank interrupt handlers to
1337  * update the vblank counter and send any signals that may be pending.
1338  */
1339 bool drm_handle_vblank(struct drm_device *dev, int crtc)
1340 {
1341 	u32 vblcount;
1342 	s64 diff_ns;
1343 	struct timeval tvblank;
1344 
1345 	if (!dev->num_crtcs)
1346 		return false;
1347 
1348 	/* Need timestamp lock to prevent concurrent execution with
1349 	 * vblank enable/disable, as this would cause inconsistent
1350 	 * or corrupted timestamps and vblank counts.
1351 	 */
1352 	mtx_lock(&dev->vblank_time_lock);
1353 
1354 	/* Vblank irq handling disabled. Nothing to do. */
1355 	if (!dev->vblank_enabled[crtc]) {
1356 		mtx_unlock(&dev->vblank_time_lock);
1357 		return false;
1358 	}
1359 
1360 	/* Fetch corresponding timestamp for this vblank interval from
1361 	 * driver and store it in proper slot of timestamp ringbuffer.
1362 	 */
1363 
1364 	/* Get current timestamp and count. */
1365 	vblcount = atomic_read(&dev->_vblank_count[crtc]);
1366 	drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1367 
1368 	/* Compute time difference to timestamp of last vblank */
1369 	diff_ns = timeval_to_ns(&tvblank) -
1370 		  timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1371 
1372 	/* Update vblank timestamp and count if at least
1373 	 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1374 	 * difference between last stored timestamp and current
1375 	 * timestamp. A smaller difference means basically
1376 	 * identical timestamps. Happens if this vblank has
1377 	 * been already processed and this is a redundant call,
1378 	 * e.g., due to spurious vblank interrupts. We need to
1379 	 * ignore those for accounting.
1380 	 */
1381 	if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
1382 		/* Store new timestamp in ringbuffer. */
1383 		vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
1384 
1385 		/* Increment cooked vblank count. This also atomically commits
1386 		 * the timestamp computed above.
1387 		 */
1388 		smp_mb__before_atomic_inc();
1389 		atomic_inc(&dev->_vblank_count[crtc]);
1390 		smp_mb__after_atomic_inc();
1391 	} else {
1392 		DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1393 			  crtc, (int) diff_ns);
1394 	}
1395 
1396 	DRM_WAKEUP(&dev->_vblank_count[crtc]);
1397 	drm_handle_vblank_events(dev, crtc);
1398 
1399 	mtx_unlock(&dev->vblank_time_lock);
1400 	return true;
1401 }
1402 EXPORT_SYMBOL(drm_handle_vblank);
1403