blob: 6473089e5fd3e32499389c04dd16a005993f80aa [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{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +030076 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjälä13b030a2014-08-06 14:49:47 +030077 u32 cur_vblank, diff, tslot, rc;
78 struct timeval t_vblank;
79
80 /*
81 * Interrupts were disabled prior to this call, so deal with counter
82 * wrap if needed.
83 * NOTE! It's possible we lost a full dev->max_vblank_count events
84 * here if the register is small or we had vblank interrupts off for
85 * a long time.
86 *
87 * We repeat the hardware vblank counter & timestamp query until
88 * we get consistent results. This to prevent races between gpu
89 * updating its hardware counter while we are retrieving the
90 * corresponding vblank timestamp.
91 */
92 do {
93 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
94 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
95 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
96
97 /* Deal with counter wrap */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +030098 diff = cur_vblank - vblank->last;
99 if (cur_vblank < vblank->last) {
Ville Syrjälä13b030a2014-08-06 14:49:47 +0300100 diff += dev->max_vblank_count;
101
102 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300103 crtc, vblank->last, cur_vblank, diff);
Ville Syrjälä13b030a2014-08-06 14:49:47 +0300104 }
105
Ville Syrjälä96a9fdd2014-08-06 14:50:02 +0300106 DRM_DEBUG("updating vblank count on crtc %d, missed %d\n",
Ville Syrjälä13b030a2014-08-06 14:49:47 +0300107 crtc, diff);
108
109 /* Reinitialize corresponding vblank timestamp if high-precision query
110 * available. Skip this step if query unsupported or failed. Will
111 * reinitialize delayed at next vblank interrupt in that case.
112 */
113 if (rc) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300114 tslot = atomic_read(&vblank->count) + diff;
Ville Syrjälä13b030a2014-08-06 14:49:47 +0300115 vblanktimestamp(dev, crtc, tslot) = t_vblank;
116 }
117
118 smp_mb__before_atomic();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300119 atomic_add(diff, &vblank->count);
Ville Syrjälä13b030a2014-08-06 14:49:47 +0300120 smp_mb__after_atomic();
121}
122
Mario Kleiner27641c32010-10-23 04:20:23 +0200123/*
Mario Kleiner27641c32010-10-23 04:20:23 +0200124 * Disable vblank irq's on crtc, make sure that last vblank count
125 * of hardware and corresponding consistent software vblank counter
126 * are preserved, even if there are any spurious vblank irq's after
127 * disable.
128 */
129static void vblank_disable_and_save(struct drm_device *dev, int crtc)
130{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300131 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +0200132 unsigned long irqflags;
133 u32 vblcount;
134 s64 diff_ns;
135 int vblrc;
136 struct timeval tvblank;
Egbert Eich0355cf32012-10-13 11:36:14 +0000137 int count = DRM_TIMESTAMP_MAXRETRIES;
Mario Kleiner27641c32010-10-23 04:20:23 +0200138
139 /* Prevent vblank irq processing while disabling vblank irqs,
140 * so no updates of timestamps or count can happen after we've
141 * disabled. Needed to prevent races in case of delayed irq's.
Mario Kleiner27641c32010-10-23 04:20:23 +0200142 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200143 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
144
Ville Syrjälä812e7462014-08-06 14:49:48 +0300145 /*
146 * If the vblank interrupt was already disbled update the count
147 * and timestamp to maintain the appearance that the counter
148 * has been ticking all along until this time. This makes the
149 * count account for the entire time between drm_vblank_on() and
150 * drm_vblank_off().
151 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300152 if (!vblank->enabled) {
Ville Syrjälä812e7462014-08-06 14:49:48 +0300153 drm_update_vblank_count(dev, crtc);
154 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
155 return;
156 }
157
Mario Kleiner27641c32010-10-23 04:20:23 +0200158 dev->driver->disable_vblank(dev, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300159 vblank->enabled = false;
Mario Kleiner27641c32010-10-23 04:20:23 +0200160
161 /* No further vblank irq's will be processed after
162 * this point. Get current hardware vblank count and
163 * vblank timestamp, repeat until they are consistent.
164 *
165 * FIXME: There is still a race condition here and in
166 * drm_update_vblank_count() which can cause off-by-one
167 * reinitialization of software vblank counter. If gpu
168 * vblank counter doesn't increment exactly at the leading
169 * edge of a vblank interval, then we can lose 1 count if
170 * we happen to execute between start of vblank and the
171 * delayed gpu counter increment.
172 */
173 do {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300174 vblank->last = dev->driver->get_vblank_counter(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +0200175 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300176 } while (vblank->last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
Egbert Eich0355cf32012-10-13 11:36:14 +0000177
178 if (!count)
179 vblrc = 0;
Mario Kleiner27641c32010-10-23 04:20:23 +0200180
181 /* Compute time difference to stored timestamp of last vblank
182 * as updated by last invocation of drm_handle_vblank() in vblank irq.
183 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300184 vblcount = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200185 diff_ns = timeval_to_ns(&tvblank) -
186 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
187
188 /* If there is at least 1 msec difference between the last stored
189 * timestamp and tvblank, then we are currently executing our
190 * disable inside a new vblank interval, the tvblank timestamp
191 * corresponds to this new vblank interval and the irq handler
192 * for this vblank didn't run yet and won't run due to our disable.
193 * Therefore we need to do the job of drm_handle_vblank() and
194 * increment the vblank counter by one to account for this vblank.
195 *
196 * Skip this step if there isn't any high precision timestamp
197 * available. In that case we can't account for this and just
198 * hope for the best.
199 */
Mario Kleinerbc215122011-02-21 05:42:01 +0100200 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
Ville Syrjäläc50d7522014-08-06 14:49:59 +0300201 /* Store new timestamp in ringbuffer. */
202 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
203
204 /* Increment cooked vblank count. This also atomically commits
205 * the timestamp computed above.
206 */
207 smp_mb__before_atomic();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300208 atomic_inc(&vblank->count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100209 smp_mb__after_atomic();
Mario Kleinerbc215122011-02-21 05:42:01 +0100210 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200211
Mario Kleiner27641c32010-10-23 04:20:23 +0200212 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +0200213}
214
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700215static void vblank_disable_fn(unsigned long arg)
216{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200217 struct drm_vblank_crtc *vblank = (void *)arg;
218 struct drm_device *dev = vblank->dev;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700219 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200220 int crtc = vblank->crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700221
222 if (!dev->vblank_disable_allowed)
223 return;
224
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200225 spin_lock_irqsave(&dev->vbl_lock, irqflags);
226 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
227 DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
228 vblank_disable_and_save(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700229 }
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200230 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700231}
232
Daniel Vetterf5752b32014-05-08 16:41:51 +0200233/**
234 * drm_vblank_cleanup - cleanup vblank support
235 * @dev: DRM device
236 *
237 * This function cleans up any resources allocated in drm_vblank_init.
238 */
Keith Packard52440212008-11-18 09:30:25 -0800239void drm_vblank_cleanup(struct drm_device *dev)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700240{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200241 int crtc;
242
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700243 /* Bail if the driver didn't call drm_vblank_init() */
244 if (dev->num_crtcs == 0)
245 return;
246
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200247 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300248 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
249
250 del_timer_sync(&vblank->disable_timer);
251 vblank_disable_fn((unsigned long)vblank);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200252 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700253
Ville Syrjälä5380e922013-10-04 14:53:36 +0300254 kfree(dev->vblank);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700255
256 dev->num_crtcs = 0;
257}
Jerome Glissee77cef92010-01-07 15:39:13 +0100258EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700259
Daniel Vetterf5752b32014-05-08 16:41:51 +0200260/**
261 * drm_vblank_init - initialize vblank support
262 * @dev: drm_device
263 * @num_crtcs: number of crtcs supported by @dev
264 *
265 * This function initializes vblank support for @num_crtcs display pipelines.
266 *
267 * Returns:
268 * Zero on success or a negative error code on failure.
269 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700270int drm_vblank_init(struct drm_device *dev, int num_crtcs)
271{
272 int i, ret = -ENOMEM;
273
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700274 spin_lock_init(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +0200275 spin_lock_init(&dev->vblank_time_lock);
276
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700277 dev->num_crtcs = num_crtcs;
278
Ville Syrjälä5380e922013-10-04 14:53:36 +0300279 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
280 if (!dev->vblank)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700281 goto err;
282
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200283 for (i = 0; i < num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300284 struct drm_vblank_crtc *vblank = &dev->vblank[i];
285
286 vblank->dev = dev;
287 vblank->crtc = i;
288 init_waitqueue_head(&vblank->queue);
289 setup_timer(&vblank->disable_timer, vblank_disable_fn,
290 (unsigned long)vblank);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200291 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200292
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100293 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
Mario Kleiner27641c32010-10-23 04:20:23 +0200294
295 /* Driver specific high-precision vblank timestamping supported? */
296 if (dev->driver->get_vblank_timestamp)
297 DRM_INFO("Driver supports precise vblank timestamp query.\n");
298 else
299 DRM_INFO("No driver support for vblank timestamp query.\n");
300
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300301 dev->vblank_disable_allowed = false;
302
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700303 return 0;
304
305err:
Mario Kleiner79a093ae2014-08-06 03:22:44 +0200306 dev->num_crtcs = 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700307 return ret;
308}
309EXPORT_SYMBOL(drm_vblank_init);
310
Dave Airlie28d52042009-09-21 14:33:58 +1000311static void drm_irq_vgaarb_nokms(void *cookie, bool state)
312{
313 struct drm_device *dev = cookie;
314
315 if (dev->driver->vgaarb_irq) {
316 dev->driver->vgaarb_irq(dev, state);
317 return;
318 }
319
320 if (!dev->irq_enabled)
321 return;
322
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000323 if (state) {
324 if (dev->driver->irq_uninstall)
325 dev->driver->irq_uninstall(dev);
326 } else {
327 if (dev->driver->irq_preinstall)
328 dev->driver->irq_preinstall(dev);
329 if (dev->driver->irq_postinstall)
330 dev->driver->irq_postinstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +1000331 }
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200335 * drm_irq_install - install IRQ handler
336 * @dev: DRM device
337 * @irq: IRQ number to install the handler for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700339 * Initializes the IRQ related data. Installs the handler, calling the driver
Daniel Vetterf5752b32014-05-08 16:41:51 +0200340 * irq_preinstall() and irq_postinstall() functions before and after the
341 * installation.
342 *
343 * This is the simplified helper interface provided for drivers with no special
344 * needs. Drivers which need to install interrupt handlers for multiple
345 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
346 * that vblank interrupts are available.
347 *
348 * Returns:
349 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100351int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200353 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
357 return -EINVAL;
358
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100359 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return -EINVAL;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100363 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Daniel Vettere090c532013-11-03 20:27:05 +0100366 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300368 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100370 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000373 if (dev->driver->irq_preinstall)
374 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700378 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100380 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100381 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700382
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300384 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return ret;
386 }
387
Dave Airlie28d52042009-09-21 14:33:58 +1000388 if (!drm_core_check_feature(dev, DRIVER_MODESET))
389 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
390
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000391 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000392 if (dev->driver->irq_postinstall)
393 ret = dev->driver->irq_postinstall(dev);
394
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700395 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300396 dev->irq_enabled = false;
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000397 if (!drm_core_check_feature(dev, DRIVER_MODESET))
398 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100399 free_irq(irq, dev);
400 } else {
401 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700404 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700406EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200409 * drm_irq_uninstall - uninstall the IRQ handler
410 * @dev: DRM device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200412 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
413 * This should only be called by drivers which used drm_irq_install() to set up
414 * their interrupt handler. Other drivers must only reset
415 * drm_device->irq_enabled to false.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200417 * Note that for kernel modesetting drivers it is a bug if this function fails.
418 * The sanity checks are only to catch buggy user modesetting drivers which call
419 * the same function through an ioctl.
420 *
421 * Returns:
422 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200424int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800426 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300427 bool irq_enabled;
428 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
431 return -EINVAL;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300434 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800436 /*
437 * Wake up any waiters so they don't hang.
438 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000439 if (dev->num_crtcs) {
440 spin_lock_irqsave(&dev->vbl_lock, irqflags);
441 for (i = 0; i < dev->num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300442 struct drm_vblank_crtc *vblank = &dev->vblank[i];
443
444 wake_up(&vblank->queue);
445 vblank->enabled = false;
446 vblank->last =
Ben Skeggsbde48892011-07-04 12:52:27 +1000447 dev->driver->get_vblank_counter(dev, i);
448 }
449 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800450 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800451
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EINVAL;
454
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100455 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Dave Airlie28d52042009-09-21 14:33:58 +1000457 if (!drm_core_check_feature(dev, DRIVER_MODESET))
458 vga_client_register(dev->pdev, NULL, NULL, NULL);
459
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000460 if (dev->driver->irq_uninstall)
461 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100463 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return 0;
466}
467EXPORT_SYMBOL(drm_irq_uninstall);
468
Daniel Vetterf5752b32014-05-08 16:41:51 +0200469/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * IRQ control ioctl.
471 *
472 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000473 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * \param cmd command.
475 * \param arg user argument, pointing to a drm_control structure.
476 * \return zero on success or a negative number on failure.
477 *
478 * Calls irq_install() or irq_uninstall() according to \p arg.
479 */
Eric Anholtc153f452007-09-03 12:06:45 +1000480int drm_control(struct drm_device *dev, void *data,
481 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Eric Anholtc153f452007-09-03 12:06:45 +1000483 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100484 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485
Mario Kleiner27641c32010-10-23 04:20:23 +0200486 /* if we haven't irq we fallback for compatibility reasons -
487 * this used to be a separate function in drm_dma.h
488 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Daniel Vetter22471cf2013-11-03 20:02:50 +0100490 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
491 return 0;
492 if (drm_core_check_feature(dev, DRIVER_MODESET))
493 return 0;
494 /* UMS was only ever support on pci devices. */
495 if (WARN_ON(!dev->pdev))
496 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Eric Anholtc153f452007-09-03 12:06:45 +1000498 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100500 irq = dev->pdev->irq;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100503 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100505 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100506 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100507 mutex_unlock(&dev->struct_mutex);
508
509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100511 mutex_lock(&dev->struct_mutex);
512 ret = drm_irq_uninstall(dev);
513 mutex_unlock(&dev->struct_mutex);
514
515 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 default:
517 return -EINVAL;
518 }
519}
520
521/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200522 * drm_calc_timestamping_constants - calculate vblank timestamp constants
523 * @crtc: drm_crtc whose timestamp constants should be updated.
524 * @mode: display mode containing the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200525 *
Ville Syrjälä21b21562013-10-26 17:22:52 +0300526 * Calculate and store various constants which are later
527 * needed by vblank and swap-completion timestamping, e.g,
528 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
Daniel Vetterf5752b32014-05-08 16:41:51 +0200529 * derived from CRTC's true scanout timing, so they take
Ville Syrjälä21b21562013-10-26 17:22:52 +0300530 * things like panel scaling or other adjustments into account.
Mario Kleiner27641c32010-10-23 04:20:23 +0200531 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300532void drm_calc_timestamping_constants(struct drm_crtc *crtc,
533 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200534{
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300535 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
Ville Syrjälä77666b72013-10-27 21:22:58 +0200536 int dotclock = mode->crtc_clock;
Mario Kleiner27641c32010-10-23 04:20:23 +0200537
538 /* Valid dotclock? */
539 if (dotclock > 0) {
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300540 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
541
542 /*
543 * Convert scanline length in pixels and video
544 * dot clock to line duration, frame duration
545 * and pixel duration in nanoseconds:
Mario Kleiner27641c32010-10-23 04:20:23 +0200546 */
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300547 pixeldur_ns = 1000000 / dotclock;
548 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
549 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
Ville Syrjäläc0ae24c2013-10-28 19:53:25 +0200550
551 /*
552 * Fields of interlaced scanout modes are only half a frame duration.
553 */
554 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
555 framedur_ns /= 2;
Mario Kleiner27641c32010-10-23 04:20:23 +0200556 } else
557 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
558 crtc->base.id);
559
560 crtc->pixeldur_ns = pixeldur_ns;
561 crtc->linedur_ns = linedur_ns;
562 crtc->framedur_ns = framedur_ns;
563
564 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300565 crtc->base.id, mode->crtc_htotal,
566 mode->crtc_vtotal, mode->crtc_vdisplay);
Mario Kleiner27641c32010-10-23 04:20:23 +0200567 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300568 crtc->base.id, dotclock, framedur_ns,
569 linedur_ns, pixeldur_ns);
Mario Kleiner27641c32010-10-23 04:20:23 +0200570}
571EXPORT_SYMBOL(drm_calc_timestamping_constants);
572
573/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200574 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
575 * @dev: DRM device
576 * @crtc: Which CRTC's vblank timestamp to retrieve
577 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
578 * On return contains true maximum error of timestamp
579 * @vblank_time: Pointer to struct timeval which should receive the timestamp
580 * @flags: Flags to pass to driver:
581 * 0 = Default,
582 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
583 * @refcrtc: CRTC which defines scanout timing
584 * @mode: mode which defines the scanout timings
585 *
586 * Implements calculation of exact vblank timestamps from given drm_display_mode
587 * timings and current video scanout position of a CRTC. This can be called from
588 * within get_vblank_timestamp() implementation of a kms driver to implement the
589 * actual timestamping.
Mario Kleiner27641c32010-10-23 04:20:23 +0200590 *
591 * Should return timestamps conforming to the OML_sync_control OpenML
592 * extension specification. The timestamp corresponds to the end of
593 * the vblank interval, aka start of scanout of topmost-leftmost display
594 * pixel in the following video frame.
595 *
596 * Requires support for optional dev->driver->get_scanout_position()
597 * in kms driver, plus a bit of setup code to provide a drm_display_mode
598 * that corresponds to the true scanout timing.
599 *
600 * The current implementation only handles standard video modes. It
601 * returns as no operation if a doublescan or interlaced video mode is
602 * active. Higher level code is expected to handle this.
603 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200604 * Returns:
605 * Negative value on error, failure or if not supported in current
Mario Kleiner27641c32010-10-23 04:20:23 +0200606 * video mode:
607 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200608 * -EINVAL - Invalid CRTC.
Mario Kleiner27641c32010-10-23 04:20:23 +0200609 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
610 * -ENOTSUPP - Function not supported in current display mode.
611 * -EIO - Failed, e.g., due to failed scanout position query.
612 *
613 * Returns or'ed positive status flags on success:
614 *
615 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
616 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
617 *
618 */
619int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
620 int *max_error,
621 struct timeval *vblank_time,
622 unsigned flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300623 const struct drm_crtc *refcrtc,
624 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200625{
Imre Deake62f2f52012-10-23 18:53:25 +0000626 ktime_t stime, etime, mono_time_offset;
627 struct timeval tv_etime;
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200628 int vbl_status;
Mario Kleiner27641c32010-10-23 04:20:23 +0200629 int vpos, hpos, i;
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300630 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200631 bool invbl;
632
633 if (crtc < 0 || crtc >= dev->num_crtcs) {
634 DRM_ERROR("Invalid crtc %d\n", crtc);
635 return -EINVAL;
636 }
637
638 /* Scanout position query not supported? Should not happen. */
639 if (!dev->driver->get_scanout_position) {
640 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
641 return -EIO;
642 }
643
Mario Kleiner27641c32010-10-23 04:20:23 +0200644 /* Durations of frames, lines, pixels in nanoseconds. */
645 framedur_ns = refcrtc->framedur_ns;
646 linedur_ns = refcrtc->linedur_ns;
647 pixeldur_ns = refcrtc->pixeldur_ns;
648
649 /* If mode timing undefined, just return as no-op:
650 * Happens during initial modesetting of a crtc.
651 */
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200652 if (framedur_ns == 0) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200653 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
654 return -EAGAIN;
655 }
656
Mario Kleiner27641c32010-10-23 04:20:23 +0200657 /* Get current scanout position with system timestamp.
658 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
659 * if single query takes longer than max_error nanoseconds.
660 *
661 * This guarantees a tight bound on maximum error if
662 * code gets preempted or delayed for some reason.
663 */
664 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100665 /*
666 * Get vertical and horizontal scanout position vpos, hpos,
667 * and bounding timestamps stime, etime, pre/post query.
668 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200669 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100670 &hpos, &stime, &etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200671
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100672 /*
673 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
674 * CLOCK_REALTIME is requested.
675 */
Imre Deakc61eef72012-10-23 18:53:26 +0000676 if (!drm_timestamp_monotonic)
677 mono_time_offset = ktime_get_monotonic_offset();
Mario Kleiner27641c32010-10-23 04:20:23 +0200678
Mario Kleiner27641c32010-10-23 04:20:23 +0200679 /* Return as no-op if scanout query unsupported or failed. */
680 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
681 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
682 crtc, vbl_status);
683 return -EIO;
684 }
685
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100686 /* Compute uncertainty in timestamp of scanout position query. */
Imre Deake62f2f52012-10-23 18:53:25 +0000687 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200688
689 /* Accept result with < max_error nsecs timing uncertainty. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300690 if (duration_ns <= *max_error)
Mario Kleiner27641c32010-10-23 04:20:23 +0200691 break;
692 }
693
694 /* Noisy system timing? */
695 if (i == DRM_TIMESTAMP_MAXRETRIES) {
696 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300697 crtc, duration_ns/1000, *max_error/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200698 }
699
700 /* Return upper bound of timestamp precision error. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300701 *max_error = duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200702
703 /* Check if in vblank area:
704 * vpos is >=0 in video scanout area, but negative
705 * within vblank area, counting down the number of lines until
706 * start of scanout.
707 */
708 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
709
710 /* Convert scanout position into elapsed time at raw_time query
711 * since start of scanout at first display scanline. delta_ns
712 * can be negative if start of scanout hasn't happened yet.
713 */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300714 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200715
Imre Deakc61eef72012-10-23 18:53:26 +0000716 if (!drm_timestamp_monotonic)
717 etime = ktime_sub(etime, mono_time_offset);
718
Imre Deake62f2f52012-10-23 18:53:25 +0000719 /* save this only for debugging purposes */
720 tv_etime = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200721 /* Subtract time delta from raw timestamp to get final
722 * vblank_time timestamp for end of vblank.
723 */
Michel Dänzere91abf82013-06-12 11:58:44 +0200724 if (delta_ns < 0)
725 etime = ktime_add_ns(etime, -delta_ns);
726 else
727 etime = ktime_sub_ns(etime, delta_ns);
Imre Deake62f2f52012-10-23 18:53:25 +0000728 *vblank_time = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200729
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700730 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
731 crtc, (int)vbl_status, hpos, vpos,
Imre Deake62f2f52012-10-23 18:53:25 +0000732 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700733 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300734 duration_ns/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200735
736 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
737 if (invbl)
738 vbl_status |= DRM_VBLANKTIME_INVBL;
739
740 return vbl_status;
741}
742EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
743
Imre Deakc61eef72012-10-23 18:53:26 +0000744static struct timeval get_drm_timestamp(void)
745{
746 ktime_t now;
747
748 now = ktime_get();
749 if (!drm_timestamp_monotonic)
750 now = ktime_sub(now, ktime_get_monotonic_offset());
751
752 return ktime_to_timeval(now);
753}
754
Mario Kleiner27641c32010-10-23 04:20:23 +0200755/**
756 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200757 * vblank interval
Mario Kleiner27641c32010-10-23 04:20:23 +0200758 * @dev: DRM device
Daniel Vetterf5752b32014-05-08 16:41:51 +0200759 * @crtc: which CRTC's vblank timestamp to retrieve
Mario Kleiner27641c32010-10-23 04:20:23 +0200760 * @tvblank: Pointer to target struct timeval which should receive the timestamp
761 * @flags: Flags to pass to driver:
Daniel Vetterf5752b32014-05-08 16:41:51 +0200762 * 0 = Default,
763 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
Mario Kleiner27641c32010-10-23 04:20:23 +0200764 *
765 * Fetches the system timestamp corresponding to the time of the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200766 * vblank interval on specified CRTC. May call into kms-driver to
Mario Kleiner27641c32010-10-23 04:20:23 +0200767 * compute the timestamp with a high-precision GPU specific method.
768 *
769 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
770 * call, i.e., it isn't very precisely locked to the true vblank.
771 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200772 * Returns:
773 * Non-zero if timestamp is considered to be very precise, zero otherwise.
Mario Kleiner27641c32010-10-23 04:20:23 +0200774 */
775u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
776 struct timeval *tvblank, unsigned flags)
777{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200778 int ret;
Mario Kleiner27641c32010-10-23 04:20:23 +0200779
780 /* Define requested maximum error on timestamps (nanoseconds). */
781 int max_error = (int) drm_timestamp_precision * 1000;
782
783 /* Query driver if possible and precision timestamping enabled. */
784 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
785 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
786 tvblank, flags);
787 if (ret > 0)
788 return (u32) ret;
789 }
790
791 /* GPU high precision timestamp query unsupported or failed.
Imre Deakc61eef72012-10-23 18:53:26 +0000792 * Return current monotonic/gettimeofday timestamp as best estimate.
Mario Kleiner27641c32010-10-23 04:20:23 +0200793 */
Imre Deakc61eef72012-10-23 18:53:26 +0000794 *tvblank = get_drm_timestamp();
Mario Kleiner27641c32010-10-23 04:20:23 +0200795
796 return 0;
797}
798EXPORT_SYMBOL(drm_get_last_vbltimestamp);
799
800/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700801 * drm_vblank_count - retrieve "cooked" vblank counter value
802 * @dev: DRM device
803 * @crtc: which counter to retrieve
804 *
805 * Fetches the "cooked" vblank count value that represents the number of
806 * vblank events since the system was booted, including lost events due to
807 * modesetting activity.
Daniel Vetterf5752b32014-05-08 16:41:51 +0200808 *
809 * Returns:
810 * The software vblank counter.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700811 */
812u32 drm_vblank_count(struct drm_device *dev, int crtc)
813{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300814 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
815
816 return atomic_read(&vblank->count);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700817}
818EXPORT_SYMBOL(drm_vblank_count);
819
820/**
Mario Kleiner27641c32010-10-23 04:20:23 +0200821 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
822 * and the system timestamp corresponding to that vblank counter value.
823 *
824 * @dev: DRM device
825 * @crtc: which counter to retrieve
826 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
827 *
828 * Fetches the "cooked" vblank count value that represents the number of
829 * vblank events since the system was booted, including lost events due to
830 * modesetting activity. Returns corresponding system timestamp of the time
Daniel Vetterf5752b32014-05-08 16:41:51 +0200831 * of the vblank interval that corresponds to the current vblank counter value.
Mario Kleiner27641c32010-10-23 04:20:23 +0200832 */
833u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
834 struct timeval *vblanktime)
835{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300836 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +0200837 u32 cur_vblank;
838
839 /* Read timestamp from slot of _vblank_time ringbuffer
840 * that corresponds to current vblank count. Retry if
841 * count has incremented during readout. This works like
842 * a seqlock.
843 */
844 do {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300845 cur_vblank = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200846 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
847 smp_rmb();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300848 } while (cur_vblank != atomic_read(&vblank->count));
Mario Kleiner27641c32010-10-23 04:20:23 +0200849
850 return cur_vblank;
851}
852EXPORT_SYMBOL(drm_vblank_count_and_time);
853
Rob Clarkc6eefa12012-10-16 22:48:40 +0000854static void send_vblank_event(struct drm_device *dev,
855 struct drm_pending_vblank_event *e,
856 unsigned long seq, struct timeval *now)
857{
858 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
859 e->event.sequence = seq;
860 e->event.tv_sec = now->tv_sec;
861 e->event.tv_usec = now->tv_usec;
862
863 list_add_tail(&e->base.link,
864 &e->base.file_priv->event_list);
865 wake_up_interruptible(&e->base.file_priv->event_wait);
866 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
867 e->event.sequence);
868}
869
870/**
871 * drm_send_vblank_event - helper to send vblank event after pageflip
872 * @dev: DRM device
873 * @crtc: CRTC in question
874 * @e: the event to send
875 *
876 * Updates sequence # and timestamp on event, and sends it to userspace.
877 * Caller must hold event lock.
878 */
879void drm_send_vblank_event(struct drm_device *dev, int crtc,
880 struct drm_pending_vblank_event *e)
881{
882 struct timeval now;
883 unsigned int seq;
884 if (crtc >= 0) {
885 seq = drm_vblank_count_and_time(dev, crtc, &now);
886 } else {
887 seq = 0;
Imre Deakc61eef72012-10-23 18:53:26 +0000888
889 now = get_drm_timestamp();
Rob Clarkc6eefa12012-10-16 22:48:40 +0000890 }
Rob Clark21a245d2012-12-10 10:49:46 -0600891 e->pipe = crtc;
Rob Clarkc6eefa12012-10-16 22:48:40 +0000892 send_vblank_event(dev, e, seq, &now);
893}
894EXPORT_SYMBOL(drm_send_vblank_event);
895
Mario Kleiner27641c32010-10-23 04:20:23 +0200896/**
Ville Syrjäläf2752282014-02-19 21:29:49 +0200897 * drm_vblank_enable - enable the vblank interrupt on a CRTC
898 * @dev: DRM device
899 * @crtc: CRTC in question
900 */
901static int drm_vblank_enable(struct drm_device *dev, int crtc)
902{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300903 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjäläf2752282014-02-19 21:29:49 +0200904 int ret = 0;
905
906 assert_spin_locked(&dev->vbl_lock);
907
908 spin_lock(&dev->vblank_time_lock);
909
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300910 if (!vblank->enabled) {
Daniel Vetter1cfb80b2014-05-21 15:11:33 +0200911 /*
912 * Enable vblank irqs under vblank_time_lock protection.
Ville Syrjäläf2752282014-02-19 21:29:49 +0200913 * All vblank count & timestamp updates are held off
914 * until we are done reinitializing master counter and
915 * timestamps. Filtercode in drm_handle_vblank() will
916 * prevent double-accounting of same vblank interval.
917 */
918 ret = dev->driver->enable_vblank(dev, crtc);
919 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
920 if (ret)
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300921 atomic_dec(&vblank->refcount);
Ville Syrjäläf2752282014-02-19 21:29:49 +0200922 else {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300923 vblank->enabled = true;
Ville Syrjäläf2752282014-02-19 21:29:49 +0200924 drm_update_vblank_count(dev, crtc);
925 }
926 }
927
928 spin_unlock(&dev->vblank_time_lock);
929
930 return ret;
931}
932
933/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700934 * drm_vblank_get - get a reference count on vblank events
935 * @dev: DRM device
936 * @crtc: which CRTC to own
937 *
938 * Acquire a reference count on vblank events to avoid having them disabled
939 * while in use.
940 *
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200941 * This is the legacy version of drm_crtc_vblank_get().
942 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200943 * Returns:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700944 * Zero on success, nonzero on failure.
945 */
946int drm_vblank_get(struct drm_device *dev, int crtc)
947{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300948 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Peter Hurley97cbc882013-08-20 21:51:12 -0400949 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700950 int ret = 0;
951
952 spin_lock_irqsave(&dev->vbl_lock, irqflags);
953 /* Going from 0->1 means we have to enable interrupts again */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300954 if (atomic_add_return(1, &vblank->refcount) == 1) {
Ville Syrjäläf2752282014-02-19 21:29:49 +0200955 ret = drm_vblank_enable(dev, crtc);
Li Peng778c9022009-11-09 12:51:22 +0800956 } else {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300957 if (!vblank->enabled) {
958 atomic_dec(&vblank->refcount);
Li Peng778c9022009-11-09 12:51:22 +0800959 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700960 }
961 }
962 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
963
964 return ret;
965}
966EXPORT_SYMBOL(drm_vblank_get);
967
968/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200969 * drm_crtc_vblank_get - get a reference count on vblank events
970 * @crtc: which CRTC to own
971 *
972 * Acquire a reference count on vblank events to avoid having them disabled
973 * while in use.
974 *
975 * This is the native kms version of drm_vblank_off().
976 *
977 * Returns:
978 * Zero on success, nonzero on failure.
979 */
980int drm_crtc_vblank_get(struct drm_crtc *crtc)
981{
982 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
983}
984EXPORT_SYMBOL(drm_crtc_vblank_get);
985
986/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700987 * drm_vblank_put - give up ownership of vblank events
988 * @dev: DRM device
989 * @crtc: which counter to give up
990 *
991 * Release ownership of a given vblank counter, turning off interrupts
Mario Kleiner27641c32010-10-23 04:20:23 +0200992 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200993 *
994 * This is the legacy version of drm_crtc_vblank_put().
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700995 */
996void drm_vblank_put(struct drm_device *dev, int crtc)
997{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300998 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
999
1000 BUG_ON(atomic_read(&vblank->refcount) == 0);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001001
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001002 /* Last user schedules interrupt disable */
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001003 if (atomic_dec_and_test(&vblank->refcount)) {
Ville Syrjälä00185e62014-08-06 14:49:54 +03001004 if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001005 vblank_disable_fn((unsigned long)vblank);
1006 else if (drm_vblank_offdelay > 0)
1007 mod_timer(&vblank->disable_timer,
1008 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1009 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001010}
1011EXPORT_SYMBOL(drm_vblank_put);
1012
Rob Clarkc6eefa12012-10-16 22:48:40 +00001013/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001014 * drm_crtc_vblank_put - give up ownership of vblank events
1015 * @crtc: which counter to give up
1016 *
1017 * Release ownership of a given vblank counter, turning off interrupts
1018 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1019 *
1020 * This is the native kms version of drm_vblank_put().
1021 */
1022void drm_crtc_vblank_put(struct drm_crtc *crtc)
1023{
1024 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1025}
1026EXPORT_SYMBOL(drm_crtc_vblank_put);
1027
1028/**
Rob Clarkc6eefa12012-10-16 22:48:40 +00001029 * drm_vblank_off - disable vblank events on a CRTC
1030 * @dev: DRM device
1031 * @crtc: CRTC in question
1032 *
Daniel Vetterf5752b32014-05-08 16:41:51 +02001033 * Drivers can use this function to shut down the vblank interrupt handling when
1034 * disabling a crtc. This function ensures that the latest vblank frame count is
1035 * stored so that drm_vblank_on() can restore it again.
1036 *
1037 * Drivers must use this function when the hardware vblank counter can get
1038 * reset, e.g. when suspending.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001039 *
1040 * This is the legacy version of drm_crtc_vblank_off().
Rob Clarkc6eefa12012-10-16 22:48:40 +00001041 */
Li Peng778c9022009-11-09 12:51:22 +08001042void drm_vblank_off(struct drm_device *dev, int crtc)
1043{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001044 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001045 struct drm_pending_vblank_event *e, *t;
1046 struct timeval now;
Li Peng778c9022009-11-09 12:51:22 +08001047 unsigned long irqflags;
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001048 unsigned int seq;
Li Peng778c9022009-11-09 12:51:22 +08001049
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001050 spin_lock_irqsave(&dev->event_lock, irqflags);
1051
1052 spin_lock(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +02001053 vblank_disable_and_save(dev, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001054 wake_up(&vblank->queue);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001055
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001056 /*
1057 * Prevent subsequent drm_vblank_get() from re-enabling
1058 * the vblank interrupt by bumping the refcount.
1059 */
1060 if (!vblank->inmodeset) {
1061 atomic_inc(&vblank->refcount);
1062 vblank->inmodeset = 1;
1063 }
1064 spin_unlock(&dev->vbl_lock);
1065
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001066 /* Send any queued vblank events, lest the natives grow disquiet */
1067 seq = drm_vblank_count_and_time(dev, crtc, &now);
Imre Deake7783ba2012-11-02 13:30:50 +02001068
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001069 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1070 if (e->pipe != crtc)
1071 continue;
1072 DRM_DEBUG("Sending premature vblank event on disable: \
1073 wanted %d, current %d\n",
1074 e->event.sequence, seq);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001075 list_del(&e->base.link);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001076 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001077 send_vblank_event(dev, e, seq, &now);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001078 }
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001079 spin_unlock_irqrestore(&dev->event_lock, irqflags);
Li Peng778c9022009-11-09 12:51:22 +08001080}
1081EXPORT_SYMBOL(drm_vblank_off);
1082
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001083/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001084 * drm_crtc_vblank_off - disable vblank events on a CRTC
1085 * @crtc: CRTC in question
1086 *
1087 * Drivers can use this function to shut down the vblank interrupt handling when
1088 * disabling a crtc. This function ensures that the latest vblank frame count is
1089 * stored so that drm_vblank_on can restore it again.
1090 *
1091 * Drivers must use this function when the hardware vblank counter can get
1092 * reset, e.g. when suspending.
1093 *
1094 * This is the native kms version of drm_vblank_off().
1095 */
1096void drm_crtc_vblank_off(struct drm_crtc *crtc)
1097{
1098 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1099}
1100EXPORT_SYMBOL(drm_crtc_vblank_off);
1101
1102/**
Ville Syrjäläf2752282014-02-19 21:29:49 +02001103 * drm_vblank_on - enable vblank events on a CRTC
1104 * @dev: DRM device
1105 * @crtc: CRTC in question
Daniel Vetterf5752b32014-05-08 16:41:51 +02001106 *
1107 * This functions restores the vblank interrupt state captured with
1108 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1109 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1110 * in driver load code to reflect the current hardware state of the crtc.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001111 *
1112 * This is the legacy version of drm_crtc_vblank_on().
Ville Syrjäläf2752282014-02-19 21:29:49 +02001113 */
1114void drm_vblank_on(struct drm_device *dev, int crtc)
1115{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001116 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjäläf2752282014-02-19 21:29:49 +02001117 unsigned long irqflags;
1118
1119 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001120 /* Drop our private "prevent drm_vblank_get" refcount */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001121 if (vblank->inmodeset) {
1122 atomic_dec(&vblank->refcount);
1123 vblank->inmodeset = 0;
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001124 }
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001125
1126 /*
1127 * sample the current counter to avoid random jumps
1128 * when drm_vblank_enable() applies the diff
1129 *
1130 * -1 to make sure user will never see the same
1131 * vblank counter value before and after a modeset
1132 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001133 vblank->last =
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001134 (dev->driver->get_vblank_counter(dev, crtc) - 1) &
1135 dev->max_vblank_count;
Ville Syrjäläcd19e522014-08-06 14:49:56 +03001136 /*
1137 * re-enable interrupts if there are users left, or the
1138 * user wishes vblank interrupts to be enabled all the time.
1139 */
1140 if (atomic_read(&vblank->refcount) != 0 ||
1141 (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
Ville Syrjäläf2752282014-02-19 21:29:49 +02001142 WARN_ON(drm_vblank_enable(dev, crtc));
1143 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1144}
1145EXPORT_SYMBOL(drm_vblank_on);
1146
1147/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001148 * drm_crtc_vblank_on - enable vblank events on a CRTC
1149 * @crtc: CRTC in question
1150 *
1151 * This functions restores the vblank interrupt state captured with
1152 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1153 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1154 * in driver load code to reflect the current hardware state of the crtc.
1155 *
1156 * This is the native kms version of drm_vblank_on().
1157 */
1158void drm_crtc_vblank_on(struct drm_crtc *crtc)
1159{
1160 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1161}
1162EXPORT_SYMBOL(drm_crtc_vblank_on);
1163
1164/**
Dave Airlief453ba02008-11-07 14:05:41 -08001165 * drm_vblank_pre_modeset - account for vblanks across mode sets
1166 * @dev: DRM device
1167 * @crtc: CRTC in question
Dave Airlief453ba02008-11-07 14:05:41 -08001168 *
1169 * Account for vblank events across mode setting events, which will likely
1170 * reset the hardware frame counter.
Daniel Vetterf5752b32014-05-08 16:41:51 +02001171 *
1172 * This is done by grabbing a temporary vblank reference to ensure that the
1173 * vblank interrupt keeps running across the modeset sequence. With this the
1174 * software-side vblank frame counting will ensure that there are no jumps or
1175 * discontinuities.
1176 *
1177 * Unfortunately this approach is racy and also doesn't work when the vblank
1178 * interrupt stops running, e.g. across system suspend resume. It is therefore
1179 * highly recommended that drivers use the newer drm_vblank_off() and
1180 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1181 * using "cooked" software vblank frame counters and not relying on any hardware
1182 * counters.
1183 *
1184 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1185 * again.
Dave Airlief453ba02008-11-07 14:05:41 -08001186 */
1187void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1188{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001189 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
1190
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001191 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Jerome Glissee77cef92010-01-07 15:39:13 +01001192 if (!dev->num_crtcs)
1193 return;
Dave Airlief453ba02008-11-07 14:05:41 -08001194 /*
1195 * To avoid all the problems that might happen if interrupts
1196 * were enabled/disabled around or between these calls, we just
1197 * have the kernel take a reference on the CRTC (just once though
1198 * to avoid corrupting the count if multiple, mismatch calls occur),
1199 * so that interrupts remain enabled in the interim.
1200 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001201 if (!vblank->inmodeset) {
1202 vblank->inmodeset = 0x1;
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001203 if (drm_vblank_get(dev, crtc) == 0)
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001204 vblank->inmodeset |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -08001205 }
1206}
1207EXPORT_SYMBOL(drm_vblank_pre_modeset);
1208
Daniel Vetterf5752b32014-05-08 16:41:51 +02001209/**
1210 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1211 * @dev: DRM device
1212 * @crtc: CRTC in question
1213 *
1214 * This function again drops the temporary vblank reference acquired in
1215 * drm_vblank_pre_modeset.
1216 */
Dave Airlief453ba02008-11-07 14:05:41 -08001217void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1218{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001219 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Dave Airlief453ba02008-11-07 14:05:41 -08001220 unsigned long irqflags;
1221
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001222 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1223 if (!dev->num_crtcs)
1224 return;
1225
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001226 if (vblank->inmodeset) {
Dave Airlief453ba02008-11-07 14:05:41 -08001227 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001228 dev->vblank_disable_allowed = true;
Dave Airlief453ba02008-11-07 14:05:41 -08001229 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001230
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001231 if (vblank->inmodeset & 0x2)
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001232 drm_vblank_put(dev, crtc);
1233
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001234 vblank->inmodeset = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001235 }
1236}
1237EXPORT_SYMBOL(drm_vblank_post_modeset);
1238
Daniel Vetterf5752b32014-05-08 16:41:51 +02001239/*
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001240 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1241 * @DRM_IOCTL_ARGS: standard ioctl arguments
1242 *
1243 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1244 * ioctls around modesetting so that any lost vblank events are accounted for.
1245 *
1246 * Generally the counter will reset across mode sets. If interrupts are
1247 * enabled around this call, we don't have to do anything since the counter
1248 * will have already been incremented.
1249 */
1250int drm_modeset_ctl(struct drm_device *dev, void *data,
1251 struct drm_file *file_priv)
1252{
1253 struct drm_modeset_ctl *modeset = data;
Dave Airlie19227562011-02-24 08:35:06 +10001254 unsigned int crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001255
1256 /* If drm_vblank_init() hasn't been called yet, just no-op */
1257 if (!dev->num_crtcs)
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001258 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001259
Laurent Pinchart29935552012-05-30 00:58:09 +02001260 /* KMS drivers handle this internally */
1261 if (drm_core_check_feature(dev, DRIVER_MODESET))
1262 return 0;
1263
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001264 crtc = modeset->crtc;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001265 if (crtc >= dev->num_crtcs)
1266 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001267
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001268 switch (modeset->cmd) {
1269 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001270 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001271 break;
1272 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001273 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001274 break;
1275 default:
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001276 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001277 }
1278
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001279 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001280}
1281
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001282static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1283 union drm_wait_vblank *vblwait,
1284 struct drm_file *file_priv)
1285{
Ville Syrjäläffe7c732014-08-06 14:49:52 +03001286 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001287 struct drm_pending_vblank_event *e;
1288 struct timeval now;
1289 unsigned long flags;
1290 unsigned int seq;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001291 int ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001292
1293 e = kzalloc(sizeof *e, GFP_KERNEL);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001294 if (e == NULL) {
1295 ret = -ENOMEM;
1296 goto err_put;
1297 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001298
1299 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001300 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001301 e->event.base.type = DRM_EVENT_VBLANK;
1302 e->event.base.length = sizeof e->event;
1303 e->event.user_data = vblwait->request.signal;
1304 e->base.event = &e->event.base;
1305 e->base.file_priv = file_priv;
1306 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1307
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001308 spin_lock_irqsave(&dev->event_lock, flags);
1309
Ville Syrjäläffe7c732014-08-06 14:49:52 +03001310 /*
1311 * drm_vblank_off() might have been called after we called
1312 * drm_vblank_get(). drm_vblank_off() holds event_lock
1313 * around the vblank disable, so no need for further locking.
1314 * The reference from drm_vblank_get() protects against
1315 * vblank disable from another source.
1316 */
1317 if (!vblank->enabled) {
1318 ret = -EINVAL;
1319 goto err_unlock;
1320 }
1321
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001322 if (file_priv->event_space < sizeof e->event) {
Chris Wilsonea5d5522010-12-01 19:41:31 +00001323 ret = -EBUSY;
1324 goto err_unlock;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001325 }
1326
1327 file_priv->event_space -= sizeof e->event;
Mario Kleiner27641c32010-10-23 04:20:23 +02001328 seq = drm_vblank_count_and_time(dev, pipe, &now);
1329
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001330 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1331 (seq - vblwait->request.sequence) <= (1 << 23)) {
1332 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +00001333 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001334 }
1335
1336 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1337 vblwait->request.sequence, seq, pipe);
1338
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001339 trace_drm_vblank_event_queued(current->pid, pipe,
1340 vblwait->request.sequence);
1341
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001342 e->event.sequence = vblwait->request.sequence;
1343 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
Dan Carpenter6f331622010-12-09 08:35:40 +03001344 drm_vblank_put(dev, pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001345 send_vblank_event(dev, e, seq, &now);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001346 vblwait->reply.sequence = seq;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001347 } else {
Ilija Hadzica6778e92011-10-31 13:11:57 -04001348 /* drm_handle_vblank_events will call drm_vblank_put */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001349 list_add_tail(&e->base.link, &dev->vblank_event_list);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001350 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001351 }
1352
1353 spin_unlock_irqrestore(&dev->event_lock, flags);
1354
1355 return 0;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001356
1357err_unlock:
1358 spin_unlock_irqrestore(&dev->event_lock, flags);
1359 kfree(e);
1360err_put:
Dan Carpenter6f331622010-12-09 08:35:40 +03001361 drm_vblank_put(dev, pipe);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001362 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001363}
1364
Daniel Vetterf5752b32014-05-08 16:41:51 +02001365/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * Wait for VBLANK.
1367 *
1368 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001369 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 * \param cmd command.
1371 * \param data user argument, pointing to a drm_wait_vblank structure.
1372 * \return zero on success or a negative number on failure.
1373 *
Eric Anholt30b23632009-01-27 21:19:41 -08001374 * This function enables the vblank interrupt on the pipe requested, then
1375 * sleeps waiting for the requested sequence number to occur, and drops
Daniel Vetterf5752b32014-05-08 16:41:51 +02001376 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
Eric Anholt30b23632009-01-27 21:19:41 -08001377 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001379int drm_wait_vblank(struct drm_device *dev, void *data,
1380 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001382 struct drm_vblank_crtc *vblank;
Eric Anholtc153f452007-09-03 12:06:45 +10001383 union drm_wait_vblank *vblwait = data;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001384 int ret;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001385 unsigned int flags, seq, crtc, high_crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Daniel Vetter49849792013-08-28 15:02:37 +02001387 if (!dev->irq_enabled)
1388 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Eric Anholt30b23632009-01-27 21:19:41 -08001390 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1391 return -EINVAL;
1392
Eric Anholtc153f452007-09-03 12:06:45 +10001393 if (vblwait->request.type &
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001394 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1395 _DRM_VBLANK_HIGH_CRTC_MASK)) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001396 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001397 vblwait->request.type,
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001398 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1399 _DRM_VBLANK_HIGH_CRTC_MASK));
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001400 return -EINVAL;
1401 }
1402
Eric Anholtc153f452007-09-03 12:06:45 +10001403 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001404 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1405 if (high_crtc)
1406 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1407 else
1408 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001409 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001410 return -EINVAL;
1411
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001412 vblank = &dev->vblank[crtc];
1413
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001414 ret = drm_vblank_get(dev, crtc);
1415 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +10001416 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001417 return ret;
1418 }
1419 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001420
Eric Anholtc153f452007-09-03 12:06:45 +10001421 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +10001423 vblwait->request.sequence += seq;
1424 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 case _DRM_VBLANK_ABSOLUTE:
1426 break;
1427 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001428 ret = -EINVAL;
1429 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
Ilija Hadzica6778e92011-10-31 13:11:57 -04001432 if (flags & _DRM_VBLANK_EVENT) {
1433 /* must hold on to the vblank ref until the event fires
1434 * drm_vblank_put will be called asynchronously
1435 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001436 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
Ilija Hadzica6778e92011-10-31 13:11:57 -04001437 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001438
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001439 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +10001440 (seq - vblwait->request.sequence) <= (1<<23)) {
1441 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001442 }
1443
Eric Anholt30b23632009-01-27 21:19:41 -08001444 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1445 vblwait->request.sequence, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001446 vblank->last_wait = vblwait->request.sequence;
1447 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
Eric Anholt30b23632009-01-27 21:19:41 -08001448 (((drm_vblank_count(dev, crtc) -
1449 vblwait->request.sequence) <= (1 << 23)) ||
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001450 !vblank->enabled ||
Eric Anholt30b23632009-01-27 21:19:41 -08001451 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Eric Anholt30b23632009-01-27 21:19:41 -08001453 if (ret != -EINTR) {
1454 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Mario Kleiner27641c32010-10-23 04:20:23 +02001456 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
Eric Anholt30b23632009-01-27 21:19:41 -08001457 vblwait->reply.tval_sec = now.tv_sec;
1458 vblwait->reply.tval_usec = now.tv_usec;
Mario Kleiner27641c32010-10-23 04:20:23 +02001459
Eric Anholt30b23632009-01-27 21:19:41 -08001460 DRM_DEBUG("returning %d to client\n",
1461 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 } else {
Eric Anholt30b23632009-01-27 21:19:41 -08001463 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001466done:
1467 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469}
1470
Sachin Kamatea7f7ab2012-08-01 17:15:31 +05301471static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001472{
1473 struct drm_pending_vblank_event *e, *t;
1474 struct timeval now;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001475 unsigned int seq;
1476
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001477 assert_spin_locked(&dev->event_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001478
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001479 seq = drm_vblank_count_and_time(dev, crtc, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001480
1481 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1482 if (e->pipe != crtc)
1483 continue;
1484 if ((seq - e->event.sequence) > (1<<23))
1485 continue;
1486
1487 DRM_DEBUG("vblank event on %d, current %d\n",
1488 e->event.sequence, seq);
1489
Rob Clarkc6eefa12012-10-16 22:48:40 +00001490 list_del(&e->base.link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001491 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001492 send_vblank_event(dev, e, seq, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001493 }
1494
Jesse Barnesac2874b2010-07-01 16:47:31 -07001495 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001496}
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001499 * drm_handle_vblank - handle a vblank event
1500 * @dev: DRM device
1501 * @crtc: where this event occurred
1502 *
1503 * Drivers should call this routine in their vblank interrupt handlers to
1504 * update the vblank counter and send any signals that may be pending.
1505 */
Chris Wilson78c6e172011-01-31 10:48:04 +00001506bool drm_handle_vblank(struct drm_device *dev, int crtc)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001507{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001508 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +02001509 u32 vblcount;
1510 s64 diff_ns;
1511 struct timeval tvblank;
1512 unsigned long irqflags;
1513
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001514 if (!dev->num_crtcs)
Chris Wilson78c6e172011-01-31 10:48:04 +00001515 return false;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001516
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001517 spin_lock_irqsave(&dev->event_lock, irqflags);
1518
Mario Kleiner27641c32010-10-23 04:20:23 +02001519 /* Need timestamp lock to prevent concurrent execution with
1520 * vblank enable/disable, as this would cause inconsistent
1521 * or corrupted timestamps and vblank counts.
1522 */
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001523 spin_lock(&dev->vblank_time_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +02001524
1525 /* Vblank irq handling disabled. Nothing to do. */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001526 if (!vblank->enabled) {
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001527 spin_unlock(&dev->vblank_time_lock);
1528 spin_unlock_irqrestore(&dev->event_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001529 return false;
Mario Kleiner27641c32010-10-23 04:20:23 +02001530 }
1531
1532 /* Fetch corresponding timestamp for this vblank interval from
1533 * driver and store it in proper slot of timestamp ringbuffer.
1534 */
1535
1536 /* Get current timestamp and count. */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001537 vblcount = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +02001538 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1539
1540 /* Compute time difference to timestamp of last vblank */
1541 diff_ns = timeval_to_ns(&tvblank) -
1542 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1543
1544 /* Update vblank timestamp and count if at least
1545 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1546 * difference between last stored timestamp and current
1547 * timestamp. A smaller difference means basically
1548 * identical timestamps. Happens if this vblank has
1549 * been already processed and this is a redundant call,
1550 * e.g., due to spurious vblank interrupts. We need to
1551 * ignore those for accounting.
1552 */
Mario Kleinerc4cc3832011-02-21 05:42:00 +01001553 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001554 /* Store new timestamp in ringbuffer. */
1555 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
Mario Kleiner27641c32010-10-23 04:20:23 +02001556
1557 /* Increment cooked vblank count. This also atomically commits
1558 * the timestamp computed above.
1559 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001560 smp_mb__before_atomic();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001561 atomic_inc(&vblank->count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001562 smp_mb__after_atomic();
Mario Kleiner27641c32010-10-23 04:20:23 +02001563 } else {
1564 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1565 crtc, (int) diff_ns);
1566 }
1567
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001568 spin_unlock(&dev->vblank_time_lock);
1569
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001570 wake_up(&vblank->queue);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001571 drm_handle_vblank_events(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +02001572
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001573 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1574
Chris Wilson78c6e172011-01-31 10:48:04 +00001575 return true;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001576}
1577EXPORT_SYMBOL(drm_handle_vblank);