blob: af965174b0830deb82ab662170d746dd81bc794e [file] [log] [blame]
Daniel Vetterf5752b32014-05-08 16:41:51 +02001/*
2 * drm_irq.c IRQ and vblank support
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnesac2874b2010-07-01 16:47:31 -070036#include "drm_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <linux/interrupt.h> /* For task queue support */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dave Airlie28d52042009-09-21 14:33:58 +100041#include <linux/vgaarb.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040042#include <linux/export.h>
Mario Kleiner27641c32010-10-23 04:20:23 +020043
44/* Access macro for slots in vblank timestamp ringbuffer. */
Ville Syrjälä5380e922013-10-04 14:53:36 +030045#define vblanktimestamp(dev, crtc, count) \
46 ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
Mario Kleiner27641c32010-10-23 04:20:23 +020047
48/* Retry timestamp calculation up to 3 times to satisfy
49 * drm_timestamp_precision before giving up.
50 */
51#define DRM_TIMESTAMP_MAXRETRIES 3
52
53/* Threshold in nanoseconds for detection of redundant
54 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
55 */
56#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
57
Ville Syrjälä13b030a2014-08-06 14:49:47 +030058/**
59 * drm_update_vblank_count - update the master vblank counter
60 * @dev: DRM device
61 * @crtc: counter to update
62 *
63 * Call back into the driver to update the appropriate vblank counter
64 * (specified by @crtc). Deal with wraparound, if it occurred, and
65 * update the last read value so we can deal with wraparound on the next
66 * call if necessary.
67 *
68 * Only necessary when going from off->on, to account for frames we
69 * didn't get an interrupt for.
70 *
71 * Note: caller must hold dev->vbl_lock since this reads & writes
72 * device vblank fields.
73 */
74static void drm_update_vblank_count(struct drm_device *dev, int crtc)
75{
76 u32 cur_vblank, diff, tslot, rc;
77 struct timeval t_vblank;
78
79 /*
80 * Interrupts were disabled prior to this call, so deal with counter
81 * wrap if needed.
82 * NOTE! It's possible we lost a full dev->max_vblank_count events
83 * here if the register is small or we had vblank interrupts off for
84 * a long time.
85 *
86 * We repeat the hardware vblank counter & timestamp query until
87 * we get consistent results. This to prevent races between gpu
88 * updating its hardware counter while we are retrieving the
89 * corresponding vblank timestamp.
90 */
91 do {
92 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
93 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
94 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
95
96 /* Deal with counter wrap */
97 diff = cur_vblank - dev->vblank[crtc].last;
98 if (cur_vblank < dev->vblank[crtc].last) {
99 diff += dev->max_vblank_count;
100
101 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
102 crtc, dev->vblank[crtc].last, cur_vblank, diff);
103 }
104
105 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
106 crtc, diff);
107
108 /* Reinitialize corresponding vblank timestamp if high-precision query
109 * available. Skip this step if query unsupported or failed. Will
110 * reinitialize delayed at next vblank interrupt in that case.
111 */
112 if (rc) {
113 tslot = atomic_read(&dev->vblank[crtc].count) + diff;
114 vblanktimestamp(dev, crtc, tslot) = t_vblank;
115 }
116
117 smp_mb__before_atomic();
118 atomic_add(diff, &dev->vblank[crtc].count);
119 smp_mb__after_atomic();
120}
121
Mario Kleiner27641c32010-10-23 04:20:23 +0200122/*
Mario Kleiner27641c32010-10-23 04:20:23 +0200123 * Disable vblank irq's on crtc, make sure that last vblank count
124 * of hardware and corresponding consistent software vblank counter
125 * are preserved, even if there are any spurious vblank irq's after
126 * disable.
127 */
128static void vblank_disable_and_save(struct drm_device *dev, int crtc)
129{
130 unsigned long irqflags;
131 u32 vblcount;
132 s64 diff_ns;
133 int vblrc;
134 struct timeval tvblank;
Egbert Eich0355cf32012-10-13 11:36:14 +0000135 int count = DRM_TIMESTAMP_MAXRETRIES;
Mario Kleiner27641c32010-10-23 04:20:23 +0200136
137 /* Prevent vblank irq processing while disabling vblank irqs,
138 * so no updates of timestamps or count can happen after we've
139 * disabled. Needed to prevent races in case of delayed irq's.
Mario Kleiner27641c32010-10-23 04:20:23 +0200140 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200141 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
142
143 dev->driver->disable_vblank(dev, crtc);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300144 dev->vblank[crtc].enabled = false;
Mario Kleiner27641c32010-10-23 04:20:23 +0200145
146 /* No further vblank irq's will be processed after
147 * this point. Get current hardware vblank count and
148 * vblank timestamp, repeat until they are consistent.
149 *
150 * FIXME: There is still a race condition here and in
151 * drm_update_vblank_count() which can cause off-by-one
152 * reinitialization of software vblank counter. If gpu
153 * vblank counter doesn't increment exactly at the leading
154 * edge of a vblank interval, then we can lose 1 count if
155 * we happen to execute between start of vblank and the
156 * delayed gpu counter increment.
157 */
158 do {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300159 dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +0200160 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300161 } while (dev->vblank[crtc].last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
Egbert Eich0355cf32012-10-13 11:36:14 +0000162
163 if (!count)
164 vblrc = 0;
Mario Kleiner27641c32010-10-23 04:20:23 +0200165
166 /* Compute time difference to stored timestamp of last vblank
167 * as updated by last invocation of drm_handle_vblank() in vblank irq.
168 */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300169 vblcount = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200170 diff_ns = timeval_to_ns(&tvblank) -
171 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
172
173 /* If there is at least 1 msec difference between the last stored
174 * timestamp and tvblank, then we are currently executing our
175 * disable inside a new vblank interval, the tvblank timestamp
176 * corresponds to this new vblank interval and the irq handler
177 * for this vblank didn't run yet and won't run due to our disable.
178 * Therefore we need to do the job of drm_handle_vblank() and
179 * increment the vblank counter by one to account for this vblank.
180 *
181 * Skip this step if there isn't any high precision timestamp
182 * available. In that case we can't account for this and just
183 * hope for the best.
184 */
Mario Kleinerbc215122011-02-21 05:42:01 +0100185 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300186 atomic_inc(&dev->vblank[crtc].count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100187 smp_mb__after_atomic();
Mario Kleinerbc215122011-02-21 05:42:01 +0100188 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200189
Mario Kleiner27641c32010-10-23 04:20:23 +0200190 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +0200191}
192
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700193static void vblank_disable_fn(unsigned long arg)
194{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200195 struct drm_vblank_crtc *vblank = (void *)arg;
196 struct drm_device *dev = vblank->dev;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700197 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200198 int crtc = vblank->crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700199
200 if (!dev->vblank_disable_allowed)
201 return;
202
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200203 spin_lock_irqsave(&dev->vbl_lock, irqflags);
204 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
205 DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
206 vblank_disable_and_save(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700207 }
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200208 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700209}
210
Daniel Vetterf5752b32014-05-08 16:41:51 +0200211/**
212 * drm_vblank_cleanup - cleanup vblank support
213 * @dev: DRM device
214 *
215 * This function cleans up any resources allocated in drm_vblank_init.
216 */
Keith Packard52440212008-11-18 09:30:25 -0800217void drm_vblank_cleanup(struct drm_device *dev)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700218{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200219 int crtc;
220
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700221 /* Bail if the driver didn't call drm_vblank_init() */
222 if (dev->num_crtcs == 0)
223 return;
224
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200225 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
226 del_timer_sync(&dev->vblank[crtc].disable_timer);
227 vblank_disable_fn((unsigned long)&dev->vblank[crtc]);
228 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700229
Ville Syrjälä5380e922013-10-04 14:53:36 +0300230 kfree(dev->vblank);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700231
232 dev->num_crtcs = 0;
233}
Jerome Glissee77cef92010-01-07 15:39:13 +0100234EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700235
Daniel Vetterf5752b32014-05-08 16:41:51 +0200236/**
237 * drm_vblank_init - initialize vblank support
238 * @dev: drm_device
239 * @num_crtcs: number of crtcs supported by @dev
240 *
241 * This function initializes vblank support for @num_crtcs display pipelines.
242 *
243 * Returns:
244 * Zero on success or a negative error code on failure.
245 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700246int drm_vblank_init(struct drm_device *dev, int num_crtcs)
247{
248 int i, ret = -ENOMEM;
249
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700250 spin_lock_init(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +0200251 spin_lock_init(&dev->vblank_time_lock);
252
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700253 dev->num_crtcs = num_crtcs;
254
Ville Syrjälä5380e922013-10-04 14:53:36 +0300255 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
256 if (!dev->vblank)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700257 goto err;
258
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200259 for (i = 0; i < num_crtcs; i++) {
260 dev->vblank[i].dev = dev;
261 dev->vblank[i].crtc = i;
Ville Syrjälä5380e922013-10-04 14:53:36 +0300262 init_waitqueue_head(&dev->vblank[i].queue);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200263 setup_timer(&dev->vblank[i].disable_timer, vblank_disable_fn,
264 (unsigned long)&dev->vblank[i]);
265 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200266
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100267 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
Mario Kleiner27641c32010-10-23 04:20:23 +0200268
269 /* Driver specific high-precision vblank timestamping supported? */
270 if (dev->driver->get_vblank_timestamp)
271 DRM_INFO("Driver supports precise vblank timestamp query.\n");
272 else
273 DRM_INFO("No driver support for vblank timestamp query.\n");
274
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300275 dev->vblank_disable_allowed = false;
276
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700277 return 0;
278
279err:
280 drm_vblank_cleanup(dev);
281 return ret;
282}
283EXPORT_SYMBOL(drm_vblank_init);
284
Dave Airlie28d52042009-09-21 14:33:58 +1000285static void drm_irq_vgaarb_nokms(void *cookie, bool state)
286{
287 struct drm_device *dev = cookie;
288
289 if (dev->driver->vgaarb_irq) {
290 dev->driver->vgaarb_irq(dev, state);
291 return;
292 }
293
294 if (!dev->irq_enabled)
295 return;
296
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000297 if (state) {
298 if (dev->driver->irq_uninstall)
299 dev->driver->irq_uninstall(dev);
300 } else {
301 if (dev->driver->irq_preinstall)
302 dev->driver->irq_preinstall(dev);
303 if (dev->driver->irq_postinstall)
304 dev->driver->irq_postinstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +1000305 }
306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200309 * drm_irq_install - install IRQ handler
310 * @dev: DRM device
311 * @irq: IRQ number to install the handler for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700313 * Initializes the IRQ related data. Installs the handler, calling the driver
Daniel Vetterf5752b32014-05-08 16:41:51 +0200314 * irq_preinstall() and irq_postinstall() functions before and after the
315 * installation.
316 *
317 * This is the simplified helper interface provided for drivers with no special
318 * needs. Drivers which need to install interrupt handlers for multiple
319 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
320 * that vblank interrupts are available.
321 *
322 * Returns:
323 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100325int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200327 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
331 return -EINVAL;
332
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100333 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EINVAL;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100337 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Daniel Vettere090c532013-11-03 20:27:05 +0100340 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300342 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100344 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000347 if (dev->driver->irq_preinstall)
348 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e3a2006-07-01 19:29:34 -0700352 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100354 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100355 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700356
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300358 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return ret;
360 }
361
Dave Airlie28d52042009-09-21 14:33:58 +1000362 if (!drm_core_check_feature(dev, DRIVER_MODESET))
363 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
364
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000366 if (dev->driver->irq_postinstall)
367 ret = dev->driver->irq_postinstall(dev);
368
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700369 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300370 dev->irq_enabled = false;
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000371 if (!drm_core_check_feature(dev, DRIVER_MODESET))
372 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100373 free_irq(irq, dev);
374 } else {
375 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700378 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700380EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200383 * drm_irq_uninstall - uninstall the IRQ handler
384 * @dev: DRM device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200386 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
387 * This should only be called by drivers which used drm_irq_install() to set up
388 * their interrupt handler. Other drivers must only reset
389 * drm_device->irq_enabled to false.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200391 * Note that for kernel modesetting drivers it is a bug if this function fails.
392 * The sanity checks are only to catch buggy user modesetting drivers which call
393 * the same function through an ioctl.
394 *
395 * Returns:
396 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200398int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800400 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300401 bool irq_enabled;
402 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
405 return -EINVAL;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300408 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800410 /*
411 * Wake up any waiters so they don't hang.
412 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000413 if (dev->num_crtcs) {
414 spin_lock_irqsave(&dev->vbl_lock, irqflags);
415 for (i = 0; i < dev->num_crtcs; i++) {
Daniel Vetter57ed0f72013-12-11 11:34:43 +0100416 wake_up(&dev->vblank[i].queue);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300417 dev->vblank[i].enabled = false;
418 dev->vblank[i].last =
Ben Skeggsbde48892011-07-04 12:52:27 +1000419 dev->driver->get_vblank_counter(dev, i);
420 }
421 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800422 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800423
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000424 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return -EINVAL;
426
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100427 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Dave Airlie28d52042009-09-21 14:33:58 +1000429 if (!drm_core_check_feature(dev, DRIVER_MODESET))
430 vga_client_register(dev->pdev, NULL, NULL, NULL);
431
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000432 if (dev->driver->irq_uninstall)
433 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100435 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return 0;
438}
439EXPORT_SYMBOL(drm_irq_uninstall);
440
Daniel Vetterf5752b32014-05-08 16:41:51 +0200441/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * IRQ control ioctl.
443 *
444 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000445 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * \param cmd command.
447 * \param arg user argument, pointing to a drm_control structure.
448 * \return zero on success or a negative number on failure.
449 *
450 * Calls irq_install() or irq_uninstall() according to \p arg.
451 */
Eric Anholtc153f452007-09-03 12:06:45 +1000452int drm_control(struct drm_device *dev, void *data,
453 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Eric Anholtc153f452007-09-03 12:06:45 +1000455 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100456 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457
Mario Kleiner27641c32010-10-23 04:20:23 +0200458 /* if we haven't irq we fallback for compatibility reasons -
459 * this used to be a separate function in drm_dma.h
460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Daniel Vetter22471cf2013-11-03 20:02:50 +0100462 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
463 return 0;
464 if (drm_core_check_feature(dev, DRIVER_MODESET))
465 return 0;
466 /* UMS was only ever support on pci devices. */
467 if (WARN_ON(!dev->pdev))
468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Eric Anholtc153f452007-09-03 12:06:45 +1000470 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100472 irq = dev->pdev->irq;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100475 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100477 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100478 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100479 mutex_unlock(&dev->struct_mutex);
480
481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100483 mutex_lock(&dev->struct_mutex);
484 ret = drm_irq_uninstall(dev);
485 mutex_unlock(&dev->struct_mutex);
486
487 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 default:
489 return -EINVAL;
490 }
491}
492
493/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200494 * drm_calc_timestamping_constants - calculate vblank timestamp constants
495 * @crtc: drm_crtc whose timestamp constants should be updated.
496 * @mode: display mode containing the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200497 *
Ville Syrjälä21b21562013-10-26 17:22:52 +0300498 * Calculate and store various constants which are later
499 * needed by vblank and swap-completion timestamping, e.g,
500 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
Daniel Vetterf5752b32014-05-08 16:41:51 +0200501 * derived from CRTC's true scanout timing, so they take
Ville Syrjälä21b21562013-10-26 17:22:52 +0300502 * things like panel scaling or other adjustments into account.
Mario Kleiner27641c32010-10-23 04:20:23 +0200503 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300504void drm_calc_timestamping_constants(struct drm_crtc *crtc,
505 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200506{
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300507 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
Ville Syrjälä77666b72013-10-27 21:22:58 +0200508 int dotclock = mode->crtc_clock;
Mario Kleiner27641c32010-10-23 04:20:23 +0200509
510 /* Valid dotclock? */
511 if (dotclock > 0) {
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300512 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
513
514 /*
515 * Convert scanline length in pixels and video
516 * dot clock to line duration, frame duration
517 * and pixel duration in nanoseconds:
Mario Kleiner27641c32010-10-23 04:20:23 +0200518 */
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300519 pixeldur_ns = 1000000 / dotclock;
520 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
521 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
Ville Syrjäläc0ae24c2013-10-28 19:53:25 +0200522
523 /*
524 * Fields of interlaced scanout modes are only half a frame duration.
525 */
526 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
527 framedur_ns /= 2;
Mario Kleiner27641c32010-10-23 04:20:23 +0200528 } else
529 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
530 crtc->base.id);
531
532 crtc->pixeldur_ns = pixeldur_ns;
533 crtc->linedur_ns = linedur_ns;
534 crtc->framedur_ns = framedur_ns;
535
536 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300537 crtc->base.id, mode->crtc_htotal,
538 mode->crtc_vtotal, mode->crtc_vdisplay);
Mario Kleiner27641c32010-10-23 04:20:23 +0200539 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300540 crtc->base.id, dotclock, framedur_ns,
541 linedur_ns, pixeldur_ns);
Mario Kleiner27641c32010-10-23 04:20:23 +0200542}
543EXPORT_SYMBOL(drm_calc_timestamping_constants);
544
545/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200546 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
547 * @dev: DRM device
548 * @crtc: Which CRTC's vblank timestamp to retrieve
549 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
550 * On return contains true maximum error of timestamp
551 * @vblank_time: Pointer to struct timeval which should receive the timestamp
552 * @flags: Flags to pass to driver:
553 * 0 = Default,
554 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
555 * @refcrtc: CRTC which defines scanout timing
556 * @mode: mode which defines the scanout timings
557 *
558 * Implements calculation of exact vblank timestamps from given drm_display_mode
559 * timings and current video scanout position of a CRTC. This can be called from
560 * within get_vblank_timestamp() implementation of a kms driver to implement the
561 * actual timestamping.
Mario Kleiner27641c32010-10-23 04:20:23 +0200562 *
563 * Should return timestamps conforming to the OML_sync_control OpenML
564 * extension specification. The timestamp corresponds to the end of
565 * the vblank interval, aka start of scanout of topmost-leftmost display
566 * pixel in the following video frame.
567 *
568 * Requires support for optional dev->driver->get_scanout_position()
569 * in kms driver, plus a bit of setup code to provide a drm_display_mode
570 * that corresponds to the true scanout timing.
571 *
572 * The current implementation only handles standard video modes. It
573 * returns as no operation if a doublescan or interlaced video mode is
574 * active. Higher level code is expected to handle this.
575 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200576 * Returns:
577 * Negative value on error, failure or if not supported in current
Mario Kleiner27641c32010-10-23 04:20:23 +0200578 * video mode:
579 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200580 * -EINVAL - Invalid CRTC.
Mario Kleiner27641c32010-10-23 04:20:23 +0200581 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
582 * -ENOTSUPP - Function not supported in current display mode.
583 * -EIO - Failed, e.g., due to failed scanout position query.
584 *
585 * Returns or'ed positive status flags on success:
586 *
587 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
588 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
589 *
590 */
591int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
592 int *max_error,
593 struct timeval *vblank_time,
594 unsigned flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300595 const struct drm_crtc *refcrtc,
596 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200597{
Imre Deake62f2f52012-10-23 18:53:25 +0000598 ktime_t stime, etime, mono_time_offset;
599 struct timeval tv_etime;
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200600 int vbl_status;
Mario Kleiner27641c32010-10-23 04:20:23 +0200601 int vpos, hpos, i;
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300602 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200603 bool invbl;
604
605 if (crtc < 0 || crtc >= dev->num_crtcs) {
606 DRM_ERROR("Invalid crtc %d\n", crtc);
607 return -EINVAL;
608 }
609
610 /* Scanout position query not supported? Should not happen. */
611 if (!dev->driver->get_scanout_position) {
612 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
613 return -EIO;
614 }
615
Mario Kleiner27641c32010-10-23 04:20:23 +0200616 /* Durations of frames, lines, pixels in nanoseconds. */
617 framedur_ns = refcrtc->framedur_ns;
618 linedur_ns = refcrtc->linedur_ns;
619 pixeldur_ns = refcrtc->pixeldur_ns;
620
621 /* If mode timing undefined, just return as no-op:
622 * Happens during initial modesetting of a crtc.
623 */
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200624 if (framedur_ns == 0) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200625 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
626 return -EAGAIN;
627 }
628
Mario Kleiner27641c32010-10-23 04:20:23 +0200629 /* Get current scanout position with system timestamp.
630 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
631 * if single query takes longer than max_error nanoseconds.
632 *
633 * This guarantees a tight bound on maximum error if
634 * code gets preempted or delayed for some reason.
635 */
636 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100637 /*
638 * Get vertical and horizontal scanout position vpos, hpos,
639 * and bounding timestamps stime, etime, pre/post query.
640 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200641 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100642 &hpos, &stime, &etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200643
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100644 /*
645 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
646 * CLOCK_REALTIME is requested.
647 */
Imre Deakc61eef72012-10-23 18:53:26 +0000648 if (!drm_timestamp_monotonic)
649 mono_time_offset = ktime_get_monotonic_offset();
Mario Kleiner27641c32010-10-23 04:20:23 +0200650
Mario Kleiner27641c32010-10-23 04:20:23 +0200651 /* Return as no-op if scanout query unsupported or failed. */
652 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
653 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
654 crtc, vbl_status);
655 return -EIO;
656 }
657
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100658 /* Compute uncertainty in timestamp of scanout position query. */
Imre Deake62f2f52012-10-23 18:53:25 +0000659 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200660
661 /* Accept result with < max_error nsecs timing uncertainty. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300662 if (duration_ns <= *max_error)
Mario Kleiner27641c32010-10-23 04:20:23 +0200663 break;
664 }
665
666 /* Noisy system timing? */
667 if (i == DRM_TIMESTAMP_MAXRETRIES) {
668 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300669 crtc, duration_ns/1000, *max_error/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200670 }
671
672 /* Return upper bound of timestamp precision error. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300673 *max_error = duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200674
675 /* Check if in vblank area:
676 * vpos is >=0 in video scanout area, but negative
677 * within vblank area, counting down the number of lines until
678 * start of scanout.
679 */
680 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
681
682 /* Convert scanout position into elapsed time at raw_time query
683 * since start of scanout at first display scanline. delta_ns
684 * can be negative if start of scanout hasn't happened yet.
685 */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300686 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200687
Imre Deakc61eef72012-10-23 18:53:26 +0000688 if (!drm_timestamp_monotonic)
689 etime = ktime_sub(etime, mono_time_offset);
690
Imre Deake62f2f52012-10-23 18:53:25 +0000691 /* save this only for debugging purposes */
692 tv_etime = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200693 /* Subtract time delta from raw timestamp to get final
694 * vblank_time timestamp for end of vblank.
695 */
Michel Dänzere91abf82013-06-12 11:58:44 +0200696 if (delta_ns < 0)
697 etime = ktime_add_ns(etime, -delta_ns);
698 else
699 etime = ktime_sub_ns(etime, delta_ns);
Imre Deake62f2f52012-10-23 18:53:25 +0000700 *vblank_time = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200701
Joe Perchesbbb0aef52011-04-17 20:35:52 -0700702 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
703 crtc, (int)vbl_status, hpos, vpos,
Imre Deake62f2f52012-10-23 18:53:25 +0000704 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
Joe Perchesbbb0aef52011-04-17 20:35:52 -0700705 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300706 duration_ns/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200707
708 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
709 if (invbl)
710 vbl_status |= DRM_VBLANKTIME_INVBL;
711
712 return vbl_status;
713}
714EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
715
Imre Deakc61eef72012-10-23 18:53:26 +0000716static struct timeval get_drm_timestamp(void)
717{
718 ktime_t now;
719
720 now = ktime_get();
721 if (!drm_timestamp_monotonic)
722 now = ktime_sub(now, ktime_get_monotonic_offset());
723
724 return ktime_to_timeval(now);
725}
726
Mario Kleiner27641c32010-10-23 04:20:23 +0200727/**
728 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200729 * vblank interval
Mario Kleiner27641c32010-10-23 04:20:23 +0200730 * @dev: DRM device
Daniel Vetterf5752b32014-05-08 16:41:51 +0200731 * @crtc: which CRTC's vblank timestamp to retrieve
Mario Kleiner27641c32010-10-23 04:20:23 +0200732 * @tvblank: Pointer to target struct timeval which should receive the timestamp
733 * @flags: Flags to pass to driver:
Daniel Vetterf5752b32014-05-08 16:41:51 +0200734 * 0 = Default,
735 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
Mario Kleiner27641c32010-10-23 04:20:23 +0200736 *
737 * Fetches the system timestamp corresponding to the time of the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200738 * vblank interval on specified CRTC. May call into kms-driver to
Mario Kleiner27641c32010-10-23 04:20:23 +0200739 * compute the timestamp with a high-precision GPU specific method.
740 *
741 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
742 * call, i.e., it isn't very precisely locked to the true vblank.
743 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200744 * Returns:
745 * Non-zero if timestamp is considered to be very precise, zero otherwise.
Mario Kleiner27641c32010-10-23 04:20:23 +0200746 */
747u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
748 struct timeval *tvblank, unsigned flags)
749{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200750 int ret;
Mario Kleiner27641c32010-10-23 04:20:23 +0200751
752 /* Define requested maximum error on timestamps (nanoseconds). */
753 int max_error = (int) drm_timestamp_precision * 1000;
754
755 /* Query driver if possible and precision timestamping enabled. */
756 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
757 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
758 tvblank, flags);
759 if (ret > 0)
760 return (u32) ret;
761 }
762
763 /* GPU high precision timestamp query unsupported or failed.
Imre Deakc61eef72012-10-23 18:53:26 +0000764 * Return current monotonic/gettimeofday timestamp as best estimate.
Mario Kleiner27641c32010-10-23 04:20:23 +0200765 */
Imre Deakc61eef72012-10-23 18:53:26 +0000766 *tvblank = get_drm_timestamp();
Mario Kleiner27641c32010-10-23 04:20:23 +0200767
768 return 0;
769}
770EXPORT_SYMBOL(drm_get_last_vbltimestamp);
771
772/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700773 * drm_vblank_count - retrieve "cooked" vblank counter value
774 * @dev: DRM device
775 * @crtc: which counter to retrieve
776 *
777 * Fetches the "cooked" vblank count value that represents the number of
778 * vblank events since the system was booted, including lost events due to
779 * modesetting activity.
Daniel Vetterf5752b32014-05-08 16:41:51 +0200780 *
781 * Returns:
782 * The software vblank counter.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700783 */
784u32 drm_vblank_count(struct drm_device *dev, int crtc)
785{
Ville Syrjälä5380e922013-10-04 14:53:36 +0300786 return atomic_read(&dev->vblank[crtc].count);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700787}
788EXPORT_SYMBOL(drm_vblank_count);
789
790/**
Mario Kleiner27641c32010-10-23 04:20:23 +0200791 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
792 * and the system timestamp corresponding to that vblank counter value.
793 *
794 * @dev: DRM device
795 * @crtc: which counter to retrieve
796 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
797 *
798 * Fetches the "cooked" vblank count value that represents the number of
799 * vblank events since the system was booted, including lost events due to
800 * modesetting activity. Returns corresponding system timestamp of the time
Daniel Vetterf5752b32014-05-08 16:41:51 +0200801 * of the vblank interval that corresponds to the current vblank counter value.
Mario Kleiner27641c32010-10-23 04:20:23 +0200802 */
803u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
804 struct timeval *vblanktime)
805{
806 u32 cur_vblank;
807
808 /* Read timestamp from slot of _vblank_time ringbuffer
809 * that corresponds to current vblank count. Retry if
810 * count has incremented during readout. This works like
811 * a seqlock.
812 */
813 do {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300814 cur_vblank = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200815 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
816 smp_rmb();
Ville Syrjälä5380e922013-10-04 14:53:36 +0300817 } while (cur_vblank != atomic_read(&dev->vblank[crtc].count));
Mario Kleiner27641c32010-10-23 04:20:23 +0200818
819 return cur_vblank;
820}
821EXPORT_SYMBOL(drm_vblank_count_and_time);
822
Rob Clarkc6eefa12012-10-16 22:48:40 +0000823static void send_vblank_event(struct drm_device *dev,
824 struct drm_pending_vblank_event *e,
825 unsigned long seq, struct timeval *now)
826{
827 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
828 e->event.sequence = seq;
829 e->event.tv_sec = now->tv_sec;
830 e->event.tv_usec = now->tv_usec;
831
832 list_add_tail(&e->base.link,
833 &e->base.file_priv->event_list);
834 wake_up_interruptible(&e->base.file_priv->event_wait);
835 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
836 e->event.sequence);
837}
838
839/**
840 * drm_send_vblank_event - helper to send vblank event after pageflip
841 * @dev: DRM device
842 * @crtc: CRTC in question
843 * @e: the event to send
844 *
845 * Updates sequence # and timestamp on event, and sends it to userspace.
846 * Caller must hold event lock.
847 */
848void drm_send_vblank_event(struct drm_device *dev, int crtc,
849 struct drm_pending_vblank_event *e)
850{
851 struct timeval now;
852 unsigned int seq;
853 if (crtc >= 0) {
854 seq = drm_vblank_count_and_time(dev, crtc, &now);
855 } else {
856 seq = 0;
Imre Deakc61eef72012-10-23 18:53:26 +0000857
858 now = get_drm_timestamp();
Rob Clarkc6eefa12012-10-16 22:48:40 +0000859 }
Rob Clark21a245d2012-12-10 10:49:46 -0600860 e->pipe = crtc;
Rob Clarkc6eefa12012-10-16 22:48:40 +0000861 send_vblank_event(dev, e, seq, &now);
862}
863EXPORT_SYMBOL(drm_send_vblank_event);
864
Mario Kleiner27641c32010-10-23 04:20:23 +0200865/**
Ville Syrjäläf2752282014-02-19 21:29:49 +0200866 * drm_vblank_enable - enable the vblank interrupt on a CRTC
867 * @dev: DRM device
868 * @crtc: CRTC in question
869 */
870static int drm_vblank_enable(struct drm_device *dev, int crtc)
871{
872 int ret = 0;
873
874 assert_spin_locked(&dev->vbl_lock);
875
876 spin_lock(&dev->vblank_time_lock);
877
878 if (!dev->vblank[crtc].enabled) {
Daniel Vetter1cfb80b2014-05-21 15:11:33 +0200879 /*
880 * Enable vblank irqs under vblank_time_lock protection.
Ville Syrjäläf2752282014-02-19 21:29:49 +0200881 * All vblank count & timestamp updates are held off
882 * until we are done reinitializing master counter and
883 * timestamps. Filtercode in drm_handle_vblank() will
884 * prevent double-accounting of same vblank interval.
885 */
886 ret = dev->driver->enable_vblank(dev, crtc);
887 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
888 if (ret)
889 atomic_dec(&dev->vblank[crtc].refcount);
890 else {
891 dev->vblank[crtc].enabled = true;
892 drm_update_vblank_count(dev, crtc);
893 }
894 }
895
896 spin_unlock(&dev->vblank_time_lock);
897
898 return ret;
899}
900
901/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700902 * drm_vblank_get - get a reference count on vblank events
903 * @dev: DRM device
904 * @crtc: which CRTC to own
905 *
906 * Acquire a reference count on vblank events to avoid having them disabled
907 * while in use.
908 *
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200909 * This is the legacy version of drm_crtc_vblank_get().
910 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200911 * Returns:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700912 * Zero on success, nonzero on failure.
913 */
914int drm_vblank_get(struct drm_device *dev, int crtc)
915{
Peter Hurley97cbc882013-08-20 21:51:12 -0400916 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700917 int ret = 0;
918
919 spin_lock_irqsave(&dev->vbl_lock, irqflags);
920 /* Going from 0->1 means we have to enable interrupts again */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300921 if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) {
Ville Syrjäläf2752282014-02-19 21:29:49 +0200922 ret = drm_vblank_enable(dev, crtc);
Li Peng778c9022009-11-09 12:51:22 +0800923 } else {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300924 if (!dev->vblank[crtc].enabled) {
925 atomic_dec(&dev->vblank[crtc].refcount);
Li Peng778c9022009-11-09 12:51:22 +0800926 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700927 }
928 }
929 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
930
931 return ret;
932}
933EXPORT_SYMBOL(drm_vblank_get);
934
935/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200936 * drm_crtc_vblank_get - get a reference count on vblank events
937 * @crtc: which CRTC to own
938 *
939 * Acquire a reference count on vblank events to avoid having them disabled
940 * while in use.
941 *
942 * This is the native kms version of drm_vblank_off().
943 *
944 * Returns:
945 * Zero on success, nonzero on failure.
946 */
947int drm_crtc_vblank_get(struct drm_crtc *crtc)
948{
949 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
950}
951EXPORT_SYMBOL(drm_crtc_vblank_get);
952
953/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700954 * drm_vblank_put - give up ownership of vblank events
955 * @dev: DRM device
956 * @crtc: which counter to give up
957 *
958 * Release ownership of a given vblank counter, turning off interrupts
Mario Kleiner27641c32010-10-23 04:20:23 +0200959 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200960 *
961 * This is the legacy version of drm_crtc_vblank_put().
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700962 */
963void drm_vblank_put(struct drm_device *dev, int crtc)
964{
Ville Syrjälä5380e922013-10-04 14:53:36 +0300965 BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0);
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000966
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700967 /* Last user schedules interrupt disable */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300968 if (atomic_dec_and_test(&dev->vblank[crtc].refcount) &&
Mario Kleiner27641c32010-10-23 04:20:23 +0200969 (drm_vblank_offdelay > 0))
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200970 mod_timer(&dev->vblank[crtc].disable_timer,
Daniel Vetterbfd83032013-12-11 11:34:41 +0100971 jiffies + ((drm_vblank_offdelay * HZ)/1000));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700972}
973EXPORT_SYMBOL(drm_vblank_put);
974
Rob Clarkc6eefa12012-10-16 22:48:40 +0000975/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200976 * drm_crtc_vblank_put - give up ownership of vblank events
977 * @crtc: which counter to give up
978 *
979 * Release ownership of a given vblank counter, turning off interrupts
980 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
981 *
982 * This is the native kms version of drm_vblank_put().
983 */
984void drm_crtc_vblank_put(struct drm_crtc *crtc)
985{
986 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
987}
988EXPORT_SYMBOL(drm_crtc_vblank_put);
989
990/**
Rob Clarkc6eefa12012-10-16 22:48:40 +0000991 * drm_vblank_off - disable vblank events on a CRTC
992 * @dev: DRM device
993 * @crtc: CRTC in question
994 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200995 * Drivers can use this function to shut down the vblank interrupt handling when
996 * disabling a crtc. This function ensures that the latest vblank frame count is
997 * stored so that drm_vblank_on() can restore it again.
998 *
999 * Drivers must use this function when the hardware vblank counter can get
1000 * reset, e.g. when suspending.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001001 *
1002 * This is the legacy version of drm_crtc_vblank_off().
Rob Clarkc6eefa12012-10-16 22:48:40 +00001003 */
Li Peng778c9022009-11-09 12:51:22 +08001004void drm_vblank_off(struct drm_device *dev, int crtc)
1005{
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001006 struct drm_pending_vblank_event *e, *t;
1007 struct timeval now;
Li Peng778c9022009-11-09 12:51:22 +08001008 unsigned long irqflags;
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001009 unsigned int seq;
Li Peng778c9022009-11-09 12:51:22 +08001010
1011 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +02001012 vblank_disable_and_save(dev, crtc);
Daniel Vetter57ed0f72013-12-11 11:34:43 +01001013 wake_up(&dev->vblank[crtc].queue);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001014
1015 /* Send any queued vblank events, lest the natives grow disquiet */
1016 seq = drm_vblank_count_and_time(dev, crtc, &now);
Imre Deake7783ba2012-11-02 13:30:50 +02001017
1018 spin_lock(&dev->event_lock);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001019 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1020 if (e->pipe != crtc)
1021 continue;
1022 DRM_DEBUG("Sending premature vblank event on disable: \
1023 wanted %d, current %d\n",
1024 e->event.sequence, seq);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001025 list_del(&e->base.link);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001026 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001027 send_vblank_event(dev, e, seq, &now);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001028 }
Imre Deake7783ba2012-11-02 13:30:50 +02001029 spin_unlock(&dev->event_lock);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001030
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001031 /*
1032 * Prevent subsequent drm_vblank_get() from re-enabling
1033 * the vblank interrupt by bumping the refcount.
1034 */
1035 if (!dev->vblank[crtc].inmodeset) {
1036 atomic_inc(&dev->vblank[crtc].refcount);
1037 dev->vblank[crtc].inmodeset = 1;
1038 }
1039
Li Peng778c9022009-11-09 12:51:22 +08001040 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1041}
1042EXPORT_SYMBOL(drm_vblank_off);
1043
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001044/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001045 * drm_crtc_vblank_off - disable vblank events on a CRTC
1046 * @crtc: CRTC in question
1047 *
1048 * Drivers can use this function to shut down the vblank interrupt handling when
1049 * disabling a crtc. This function ensures that the latest vblank frame count is
1050 * stored so that drm_vblank_on can restore it again.
1051 *
1052 * Drivers must use this function when the hardware vblank counter can get
1053 * reset, e.g. when suspending.
1054 *
1055 * This is the native kms version of drm_vblank_off().
1056 */
1057void drm_crtc_vblank_off(struct drm_crtc *crtc)
1058{
1059 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1060}
1061EXPORT_SYMBOL(drm_crtc_vblank_off);
1062
1063/**
Ville Syrjäläf2752282014-02-19 21:29:49 +02001064 * drm_vblank_on - enable vblank events on a CRTC
1065 * @dev: DRM device
1066 * @crtc: CRTC in question
Daniel Vetterf5752b32014-05-08 16:41:51 +02001067 *
1068 * This functions restores the vblank interrupt state captured with
1069 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1070 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1071 * in driver load code to reflect the current hardware state of the crtc.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001072 *
1073 * This is the legacy version of drm_crtc_vblank_on().
Ville Syrjäläf2752282014-02-19 21:29:49 +02001074 */
1075void drm_vblank_on(struct drm_device *dev, int crtc)
1076{
1077 unsigned long irqflags;
1078
1079 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001080 /* Drop our private "prevent drm_vblank_get" refcount */
1081 if (dev->vblank[crtc].inmodeset) {
1082 atomic_dec(&dev->vblank[crtc].refcount);
1083 dev->vblank[crtc].inmodeset = 0;
1084 }
Ville Syrjäläf2752282014-02-19 21:29:49 +02001085 /* re-enable interrupts if there's are users left */
1086 if (atomic_read(&dev->vblank[crtc].refcount) != 0)
1087 WARN_ON(drm_vblank_enable(dev, crtc));
1088 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1089}
1090EXPORT_SYMBOL(drm_vblank_on);
1091
1092/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001093 * drm_crtc_vblank_on - enable vblank events on a CRTC
1094 * @crtc: CRTC in question
1095 *
1096 * This functions restores the vblank interrupt state captured with
1097 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1098 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1099 * in driver load code to reflect the current hardware state of the crtc.
1100 *
1101 * This is the native kms version of drm_vblank_on().
1102 */
1103void drm_crtc_vblank_on(struct drm_crtc *crtc)
1104{
1105 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1106}
1107EXPORT_SYMBOL(drm_crtc_vblank_on);
1108
1109/**
Dave Airlief453ba02008-11-07 14:05:41 -08001110 * drm_vblank_pre_modeset - account for vblanks across mode sets
1111 * @dev: DRM device
1112 * @crtc: CRTC in question
Dave Airlief453ba02008-11-07 14:05:41 -08001113 *
1114 * Account for vblank events across mode setting events, which will likely
1115 * reset the hardware frame counter.
Daniel Vetterf5752b32014-05-08 16:41:51 +02001116 *
1117 * This is done by grabbing a temporary vblank reference to ensure that the
1118 * vblank interrupt keeps running across the modeset sequence. With this the
1119 * software-side vblank frame counting will ensure that there are no jumps or
1120 * discontinuities.
1121 *
1122 * Unfortunately this approach is racy and also doesn't work when the vblank
1123 * interrupt stops running, e.g. across system suspend resume. It is therefore
1124 * highly recommended that drivers use the newer drm_vblank_off() and
1125 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1126 * using "cooked" software vblank frame counters and not relying on any hardware
1127 * counters.
1128 *
1129 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1130 * again.
Dave Airlief453ba02008-11-07 14:05:41 -08001131 */
1132void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1133{
Huacai Chenb7ea85a2013-05-21 06:23:43 +00001134 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Jerome Glissee77cef92010-01-07 15:39:13 +01001135 if (!dev->num_crtcs)
1136 return;
Dave Airlief453ba02008-11-07 14:05:41 -08001137 /*
1138 * To avoid all the problems that might happen if interrupts
1139 * were enabled/disabled around or between these calls, we just
1140 * have the kernel take a reference on the CRTC (just once though
1141 * to avoid corrupting the count if multiple, mismatch calls occur),
1142 * so that interrupts remain enabled in the interim.
1143 */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001144 if (!dev->vblank[crtc].inmodeset) {
1145 dev->vblank[crtc].inmodeset = 0x1;
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001146 if (drm_vblank_get(dev, crtc) == 0)
Ville Syrjälä5380e922013-10-04 14:53:36 +03001147 dev->vblank[crtc].inmodeset |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -08001148 }
1149}
1150EXPORT_SYMBOL(drm_vblank_pre_modeset);
1151
Daniel Vetterf5752b32014-05-08 16:41:51 +02001152/**
1153 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1154 * @dev: DRM device
1155 * @crtc: CRTC in question
1156 *
1157 * This function again drops the temporary vblank reference acquired in
1158 * drm_vblank_pre_modeset.
1159 */
Dave Airlief453ba02008-11-07 14:05:41 -08001160void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1161{
1162 unsigned long irqflags;
1163
Huacai Chenb7ea85a2013-05-21 06:23:43 +00001164 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1165 if (!dev->num_crtcs)
1166 return;
1167
Ville Syrjälä5380e922013-10-04 14:53:36 +03001168 if (dev->vblank[crtc].inmodeset) {
Dave Airlief453ba02008-11-07 14:05:41 -08001169 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001170 dev->vblank_disable_allowed = true;
Dave Airlief453ba02008-11-07 14:05:41 -08001171 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001172
Ville Syrjälä5380e922013-10-04 14:53:36 +03001173 if (dev->vblank[crtc].inmodeset & 0x2)
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001174 drm_vblank_put(dev, crtc);
1175
Ville Syrjälä5380e922013-10-04 14:53:36 +03001176 dev->vblank[crtc].inmodeset = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001177 }
1178}
1179EXPORT_SYMBOL(drm_vblank_post_modeset);
1180
Daniel Vetterf5752b32014-05-08 16:41:51 +02001181/*
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001182 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1183 * @DRM_IOCTL_ARGS: standard ioctl arguments
1184 *
1185 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1186 * ioctls around modesetting so that any lost vblank events are accounted for.
1187 *
1188 * Generally the counter will reset across mode sets. If interrupts are
1189 * enabled around this call, we don't have to do anything since the counter
1190 * will have already been incremented.
1191 */
1192int drm_modeset_ctl(struct drm_device *dev, void *data,
1193 struct drm_file *file_priv)
1194{
1195 struct drm_modeset_ctl *modeset = data;
Dave Airlie19227562011-02-24 08:35:06 +10001196 unsigned int crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001197
1198 /* If drm_vblank_init() hasn't been called yet, just no-op */
1199 if (!dev->num_crtcs)
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001200 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001201
Laurent Pinchart29935552012-05-30 00:58:09 +02001202 /* KMS drivers handle this internally */
1203 if (drm_core_check_feature(dev, DRIVER_MODESET))
1204 return 0;
1205
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001206 crtc = modeset->crtc;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001207 if (crtc >= dev->num_crtcs)
1208 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001209
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001210 switch (modeset->cmd) {
1211 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001212 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001213 break;
1214 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001215 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001216 break;
1217 default:
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001218 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001219 }
1220
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001221 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001222}
1223
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001224static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1225 union drm_wait_vblank *vblwait,
1226 struct drm_file *file_priv)
1227{
1228 struct drm_pending_vblank_event *e;
1229 struct timeval now;
1230 unsigned long flags;
1231 unsigned int seq;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001232 int ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001233
1234 e = kzalloc(sizeof *e, GFP_KERNEL);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001235 if (e == NULL) {
1236 ret = -ENOMEM;
1237 goto err_put;
1238 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001239
1240 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001241 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001242 e->event.base.type = DRM_EVENT_VBLANK;
1243 e->event.base.length = sizeof e->event;
1244 e->event.user_data = vblwait->request.signal;
1245 e->base.event = &e->event.base;
1246 e->base.file_priv = file_priv;
1247 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1248
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001249 spin_lock_irqsave(&dev->event_lock, flags);
1250
1251 if (file_priv->event_space < sizeof e->event) {
Chris Wilsonea5d5522010-12-01 19:41:31 +00001252 ret = -EBUSY;
1253 goto err_unlock;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001254 }
1255
1256 file_priv->event_space -= sizeof e->event;
Mario Kleiner27641c32010-10-23 04:20:23 +02001257 seq = drm_vblank_count_and_time(dev, pipe, &now);
1258
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001259 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1260 (seq - vblwait->request.sequence) <= (1 << 23)) {
1261 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +00001262 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001263 }
1264
1265 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1266 vblwait->request.sequence, seq, pipe);
1267
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001268 trace_drm_vblank_event_queued(current->pid, pipe,
1269 vblwait->request.sequence);
1270
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001271 e->event.sequence = vblwait->request.sequence;
1272 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
Dan Carpenter6f331622010-12-09 08:35:40 +03001273 drm_vblank_put(dev, pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001274 send_vblank_event(dev, e, seq, &now);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001275 vblwait->reply.sequence = seq;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001276 } else {
Ilija Hadzica6778e92011-10-31 13:11:57 -04001277 /* drm_handle_vblank_events will call drm_vblank_put */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001278 list_add_tail(&e->base.link, &dev->vblank_event_list);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001279 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001280 }
1281
1282 spin_unlock_irqrestore(&dev->event_lock, flags);
1283
1284 return 0;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001285
1286err_unlock:
1287 spin_unlock_irqrestore(&dev->event_lock, flags);
1288 kfree(e);
1289err_put:
Dan Carpenter6f331622010-12-09 08:35:40 +03001290 drm_vblank_put(dev, pipe);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001291 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001292}
1293
Daniel Vetterf5752b32014-05-08 16:41:51 +02001294/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 * Wait for VBLANK.
1296 *
1297 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001298 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * \param cmd command.
1300 * \param data user argument, pointing to a drm_wait_vblank structure.
1301 * \return zero on success or a negative number on failure.
1302 *
Eric Anholt30b23632009-01-27 21:19:41 -08001303 * This function enables the vblank interrupt on the pipe requested, then
1304 * sleeps waiting for the requested sequence number to occur, and drops
Daniel Vetterf5752b32014-05-08 16:41:51 +02001305 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
Eric Anholt30b23632009-01-27 21:19:41 -08001306 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001308int drm_wait_vblank(struct drm_device *dev, void *data,
1309 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Eric Anholtc153f452007-09-03 12:06:45 +10001311 union drm_wait_vblank *vblwait = data;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001312 int ret;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001313 unsigned int flags, seq, crtc, high_crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Daniel Vetter49849792013-08-28 15:02:37 +02001315 if (!dev->irq_enabled)
1316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Eric Anholt30b23632009-01-27 21:19:41 -08001318 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1319 return -EINVAL;
1320
Eric Anholtc153f452007-09-03 12:06:45 +10001321 if (vblwait->request.type &
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001322 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1323 _DRM_VBLANK_HIGH_CRTC_MASK)) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001324 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001325 vblwait->request.type,
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001326 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1327 _DRM_VBLANK_HIGH_CRTC_MASK));
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001328 return -EINVAL;
1329 }
1330
Eric Anholtc153f452007-09-03 12:06:45 +10001331 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001332 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1333 if (high_crtc)
1334 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1335 else
1336 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001337 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001338 return -EINVAL;
1339
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001340 ret = drm_vblank_get(dev, crtc);
1341 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +10001342 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001343 return ret;
1344 }
1345 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001346
Eric Anholtc153f452007-09-03 12:06:45 +10001347 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +10001349 vblwait->request.sequence += seq;
1350 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 case _DRM_VBLANK_ABSOLUTE:
1352 break;
1353 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001354 ret = -EINVAL;
1355 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357
Ilija Hadzica6778e92011-10-31 13:11:57 -04001358 if (flags & _DRM_VBLANK_EVENT) {
1359 /* must hold on to the vblank ref until the event fires
1360 * drm_vblank_put will be called asynchronously
1361 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001362 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
Ilija Hadzica6778e92011-10-31 13:11:57 -04001363 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001364
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001365 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +10001366 (seq - vblwait->request.sequence) <= (1<<23)) {
1367 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001368 }
1369
Eric Anholt30b23632009-01-27 21:19:41 -08001370 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1371 vblwait->request.sequence, crtc);
Ville Syrjälä5380e922013-10-04 14:53:36 +03001372 dev->vblank[crtc].last_wait = vblwait->request.sequence;
Daniel Vetterbfd83032013-12-11 11:34:41 +01001373 DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * HZ,
Eric Anholt30b23632009-01-27 21:19:41 -08001374 (((drm_vblank_count(dev, crtc) -
1375 vblwait->request.sequence) <= (1 << 23)) ||
Ville Syrjälä3212a222014-03-06 17:27:39 +02001376 !dev->vblank[crtc].enabled ||
Eric Anholt30b23632009-01-27 21:19:41 -08001377 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Eric Anholt30b23632009-01-27 21:19:41 -08001379 if (ret != -EINTR) {
1380 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Mario Kleiner27641c32010-10-23 04:20:23 +02001382 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
Eric Anholt30b23632009-01-27 21:19:41 -08001383 vblwait->reply.tval_sec = now.tv_sec;
1384 vblwait->reply.tval_usec = now.tv_usec;
Mario Kleiner27641c32010-10-23 04:20:23 +02001385
Eric Anholt30b23632009-01-27 21:19:41 -08001386 DRM_DEBUG("returning %d to client\n",
1387 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 } else {
Eric Anholt30b23632009-01-27 21:19:41 -08001389 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001392done:
1393 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return ret;
1395}
1396
Sachin Kamatea7f7ab2012-08-01 17:15:31 +05301397static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001398{
1399 struct drm_pending_vblank_event *e, *t;
1400 struct timeval now;
1401 unsigned long flags;
1402 unsigned int seq;
1403
Mario Kleiner27641c32010-10-23 04:20:23 +02001404 seq = drm_vblank_count_and_time(dev, crtc, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001405
1406 spin_lock_irqsave(&dev->event_lock, flags);
1407
1408 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1409 if (e->pipe != crtc)
1410 continue;
1411 if ((seq - e->event.sequence) > (1<<23))
1412 continue;
1413
1414 DRM_DEBUG("vblank event on %d, current %d\n",
1415 e->event.sequence, seq);
1416
Rob Clarkc6eefa12012-10-16 22:48:40 +00001417 list_del(&e->base.link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001418 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001419 send_vblank_event(dev, e, seq, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001420 }
1421
1422 spin_unlock_irqrestore(&dev->event_lock, flags);
Jesse Barnesac2874b2010-07-01 16:47:31 -07001423
1424 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001428 * drm_handle_vblank - handle a vblank event
1429 * @dev: DRM device
1430 * @crtc: where this event occurred
1431 *
1432 * Drivers should call this routine in their vblank interrupt handlers to
1433 * update the vblank counter and send any signals that may be pending.
1434 */
Chris Wilson78c6e172011-01-31 10:48:04 +00001435bool drm_handle_vblank(struct drm_device *dev, int crtc)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001436{
Mario Kleiner27641c32010-10-23 04:20:23 +02001437 u32 vblcount;
1438 s64 diff_ns;
1439 struct timeval tvblank;
1440 unsigned long irqflags;
1441
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001442 if (!dev->num_crtcs)
Chris Wilson78c6e172011-01-31 10:48:04 +00001443 return false;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001444
Mario Kleiner27641c32010-10-23 04:20:23 +02001445 /* Need timestamp lock to prevent concurrent execution with
1446 * vblank enable/disable, as this would cause inconsistent
1447 * or corrupted timestamps and vblank counts.
1448 */
1449 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1450
1451 /* Vblank irq handling disabled. Nothing to do. */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001452 if (!dev->vblank[crtc].enabled) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001453 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001454 return false;
Mario Kleiner27641c32010-10-23 04:20:23 +02001455 }
1456
1457 /* Fetch corresponding timestamp for this vblank interval from
1458 * driver and store it in proper slot of timestamp ringbuffer.
1459 */
1460
1461 /* Get current timestamp and count. */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001462 vblcount = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +02001463 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1464
1465 /* Compute time difference to timestamp of last vblank */
1466 diff_ns = timeval_to_ns(&tvblank) -
1467 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1468
1469 /* Update vblank timestamp and count if at least
1470 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1471 * difference between last stored timestamp and current
1472 * timestamp. A smaller difference means basically
1473 * identical timestamps. Happens if this vblank has
1474 * been already processed and this is a redundant call,
1475 * e.g., due to spurious vblank interrupts. We need to
1476 * ignore those for accounting.
1477 */
Mario Kleinerc4cc3832011-02-21 05:42:00 +01001478 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001479 /* Store new timestamp in ringbuffer. */
1480 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
Mario Kleiner27641c32010-10-23 04:20:23 +02001481
1482 /* Increment cooked vblank count. This also atomically commits
1483 * the timestamp computed above.
1484 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001485 smp_mb__before_atomic();
Ville Syrjälä5380e922013-10-04 14:53:36 +03001486 atomic_inc(&dev->vblank[crtc].count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001487 smp_mb__after_atomic();
Mario Kleiner27641c32010-10-23 04:20:23 +02001488 } else {
1489 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1490 crtc, (int) diff_ns);
1491 }
1492
Daniel Vetter57ed0f72013-12-11 11:34:43 +01001493 wake_up(&dev->vblank[crtc].queue);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001494 drm_handle_vblank_events(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +02001495
1496 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001497 return true;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001498}
1499EXPORT_SYMBOL(drm_handle_vblank);