blob: 87d148cec877a3349866b88400a5f75c46b3fdfb [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;
Mario Kleiner2368ffb2014-08-06 03:22:46 +0200242 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200243
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700244 /* Bail if the driver didn't call drm_vblank_init() */
245 if (dev->num_crtcs == 0)
246 return;
247
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200248 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300249 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
250
251 del_timer_sync(&vblank->disable_timer);
Mario Kleiner2368ffb2014-08-06 03:22:46 +0200252
253 spin_lock_irqsave(&dev->vbl_lock, irqflags);
254 vblank_disable_and_save(dev, crtc);
255 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200256 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700257
Ville Syrjälä5380e922013-10-04 14:53:36 +0300258 kfree(dev->vblank);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700259
260 dev->num_crtcs = 0;
261}
Jerome Glissee77cef92010-01-07 15:39:13 +0100262EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700263
Daniel Vetterf5752b32014-05-08 16:41:51 +0200264/**
265 * drm_vblank_init - initialize vblank support
266 * @dev: drm_device
267 * @num_crtcs: number of crtcs supported by @dev
268 *
269 * This function initializes vblank support for @num_crtcs display pipelines.
270 *
271 * Returns:
272 * Zero on success or a negative error code on failure.
273 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700274int drm_vblank_init(struct drm_device *dev, int num_crtcs)
275{
276 int i, ret = -ENOMEM;
277
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700278 spin_lock_init(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +0200279 spin_lock_init(&dev->vblank_time_lock);
280
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700281 dev->num_crtcs = num_crtcs;
282
Ville Syrjälä5380e922013-10-04 14:53:36 +0300283 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
284 if (!dev->vblank)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700285 goto err;
286
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200287 for (i = 0; i < num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300288 struct drm_vblank_crtc *vblank = &dev->vblank[i];
289
290 vblank->dev = dev;
291 vblank->crtc = i;
292 init_waitqueue_head(&vblank->queue);
293 setup_timer(&vblank->disable_timer, vblank_disable_fn,
294 (unsigned long)vblank);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200295 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200296
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100297 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
Mario Kleiner27641c32010-10-23 04:20:23 +0200298
299 /* Driver specific high-precision vblank timestamping supported? */
300 if (dev->driver->get_vblank_timestamp)
301 DRM_INFO("Driver supports precise vblank timestamp query.\n");
302 else
303 DRM_INFO("No driver support for vblank timestamp query.\n");
304
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300305 dev->vblank_disable_allowed = false;
306
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700307 return 0;
308
309err:
Mario Kleiner79a093ae2014-08-06 03:22:44 +0200310 dev->num_crtcs = 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700311 return ret;
312}
313EXPORT_SYMBOL(drm_vblank_init);
314
Dave Airlie28d52042009-09-21 14:33:58 +1000315static void drm_irq_vgaarb_nokms(void *cookie, bool state)
316{
317 struct drm_device *dev = cookie;
318
319 if (dev->driver->vgaarb_irq) {
320 dev->driver->vgaarb_irq(dev, state);
321 return;
322 }
323
324 if (!dev->irq_enabled)
325 return;
326
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000327 if (state) {
328 if (dev->driver->irq_uninstall)
329 dev->driver->irq_uninstall(dev);
330 } else {
331 if (dev->driver->irq_preinstall)
332 dev->driver->irq_preinstall(dev);
333 if (dev->driver->irq_postinstall)
334 dev->driver->irq_postinstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +1000335 }
336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200339 * drm_irq_install - install IRQ handler
340 * @dev: DRM device
341 * @irq: IRQ number to install the handler for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700343 * Initializes the IRQ related data. Installs the handler, calling the driver
Daniel Vetterf5752b32014-05-08 16:41:51 +0200344 * irq_preinstall() and irq_postinstall() functions before and after the
345 * installation.
346 *
347 * This is the simplified helper interface provided for drivers with no special
348 * needs. Drivers which need to install interrupt handlers for multiple
349 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
350 * that vblank interrupts are available.
351 *
352 * Returns:
353 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100355int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200357 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
361 return -EINVAL;
362
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100363 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return -EINVAL;
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100367 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Daniel Vettere090c532013-11-03 20:27:05 +0100370 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300372 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100374 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000377 if (dev->driver->irq_preinstall)
378 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700382 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100384 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100385 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300388 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ret;
390 }
391
Dave Airlie28d52042009-09-21 14:33:58 +1000392 if (!drm_core_check_feature(dev, DRIVER_MODESET))
393 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
394
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000396 if (dev->driver->irq_postinstall)
397 ret = dev->driver->irq_postinstall(dev);
398
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700399 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300400 dev->irq_enabled = false;
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000401 if (!drm_core_check_feature(dev, DRIVER_MODESET))
402 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100403 free_irq(irq, dev);
404 } else {
405 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700410EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200413 * drm_irq_uninstall - uninstall the IRQ handler
414 * @dev: DRM device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200416 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
417 * This should only be called by drivers which used drm_irq_install() to set up
418 * their interrupt handler. Other drivers must only reset
419 * drm_device->irq_enabled to false.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200421 * Note that for kernel modesetting drivers it is a bug if this function fails.
422 * The sanity checks are only to catch buggy user modesetting drivers which call
423 * the same function through an ioctl.
424 *
425 * Returns:
426 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200428int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800430 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300431 bool irq_enabled;
432 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
435 return -EINVAL;
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300438 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800440 /*
441 * Wake up any waiters so they don't hang.
442 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000443 if (dev->num_crtcs) {
444 spin_lock_irqsave(&dev->vbl_lock, irqflags);
445 for (i = 0; i < dev->num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300446 struct drm_vblank_crtc *vblank = &dev->vblank[i];
447
448 wake_up(&vblank->queue);
449 vblank->enabled = false;
450 vblank->last =
Ben Skeggsbde48892011-07-04 12:52:27 +1000451 dev->driver->get_vblank_counter(dev, i);
452 }
453 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800454 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800455
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return -EINVAL;
458
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100459 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dave Airlie28d52042009-09-21 14:33:58 +1000461 if (!drm_core_check_feature(dev, DRIVER_MODESET))
462 vga_client_register(dev->pdev, NULL, NULL, NULL);
463
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000464 if (dev->driver->irq_uninstall)
465 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100467 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 return 0;
470}
471EXPORT_SYMBOL(drm_irq_uninstall);
472
Daniel Vetterf5752b32014-05-08 16:41:51 +0200473/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * IRQ control ioctl.
475 *
476 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000477 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * \param cmd command.
479 * \param arg user argument, pointing to a drm_control structure.
480 * \return zero on success or a negative number on failure.
481 *
482 * Calls irq_install() or irq_uninstall() according to \p arg.
483 */
Eric Anholtc153f452007-09-03 12:06:45 +1000484int drm_control(struct drm_device *dev, void *data,
485 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Eric Anholtc153f452007-09-03 12:06:45 +1000487 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100488 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489
Mario Kleiner27641c32010-10-23 04:20:23 +0200490 /* if we haven't irq we fallback for compatibility reasons -
491 * this used to be a separate function in drm_dma.h
492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Daniel Vetter22471cf2013-11-03 20:02:50 +0100494 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
495 return 0;
496 if (drm_core_check_feature(dev, DRIVER_MODESET))
497 return 0;
498 /* UMS was only ever support on pci devices. */
499 if (WARN_ON(!dev->pdev))
500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Eric Anholtc153f452007-09-03 12:06:45 +1000502 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100504 irq = dev->pdev->irq;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100507 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100509 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100510 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100511 mutex_unlock(&dev->struct_mutex);
512
513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100515 mutex_lock(&dev->struct_mutex);
516 ret = drm_irq_uninstall(dev);
517 mutex_unlock(&dev->struct_mutex);
518
519 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 default:
521 return -EINVAL;
522 }
523}
524
525/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200526 * drm_calc_timestamping_constants - calculate vblank timestamp constants
527 * @crtc: drm_crtc whose timestamp constants should be updated.
528 * @mode: display mode containing the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200529 *
Ville Syrjälä21b21562013-10-26 17:22:52 +0300530 * Calculate and store various constants which are later
531 * needed by vblank and swap-completion timestamping, e.g,
532 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
Daniel Vetterf5752b32014-05-08 16:41:51 +0200533 * derived from CRTC's true scanout timing, so they take
Ville Syrjälä21b21562013-10-26 17:22:52 +0300534 * things like panel scaling or other adjustments into account.
Mario Kleiner27641c32010-10-23 04:20:23 +0200535 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300536void drm_calc_timestamping_constants(struct drm_crtc *crtc,
537 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200538{
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300539 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
Ville Syrjälä77666b72013-10-27 21:22:58 +0200540 int dotclock = mode->crtc_clock;
Mario Kleiner27641c32010-10-23 04:20:23 +0200541
542 /* Valid dotclock? */
543 if (dotclock > 0) {
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300544 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
545
546 /*
547 * Convert scanline length in pixels and video
548 * dot clock to line duration, frame duration
549 * and pixel duration in nanoseconds:
Mario Kleiner27641c32010-10-23 04:20:23 +0200550 */
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300551 pixeldur_ns = 1000000 / dotclock;
552 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
553 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
Ville Syrjäläc0ae24c2013-10-28 19:53:25 +0200554
555 /*
556 * Fields of interlaced scanout modes are only half a frame duration.
557 */
558 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
559 framedur_ns /= 2;
Mario Kleiner27641c32010-10-23 04:20:23 +0200560 } else
561 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
562 crtc->base.id);
563
564 crtc->pixeldur_ns = pixeldur_ns;
565 crtc->linedur_ns = linedur_ns;
566 crtc->framedur_ns = framedur_ns;
567
568 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300569 crtc->base.id, mode->crtc_htotal,
570 mode->crtc_vtotal, mode->crtc_vdisplay);
Mario Kleiner27641c32010-10-23 04:20:23 +0200571 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300572 crtc->base.id, dotclock, framedur_ns,
573 linedur_ns, pixeldur_ns);
Mario Kleiner27641c32010-10-23 04:20:23 +0200574}
575EXPORT_SYMBOL(drm_calc_timestamping_constants);
576
577/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200578 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
579 * @dev: DRM device
580 * @crtc: Which CRTC's vblank timestamp to retrieve
581 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
582 * On return contains true maximum error of timestamp
583 * @vblank_time: Pointer to struct timeval which should receive the timestamp
584 * @flags: Flags to pass to driver:
585 * 0 = Default,
586 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
587 * @refcrtc: CRTC which defines scanout timing
588 * @mode: mode which defines the scanout timings
589 *
590 * Implements calculation of exact vblank timestamps from given drm_display_mode
591 * timings and current video scanout position of a CRTC. This can be called from
592 * within get_vblank_timestamp() implementation of a kms driver to implement the
593 * actual timestamping.
Mario Kleiner27641c32010-10-23 04:20:23 +0200594 *
595 * Should return timestamps conforming to the OML_sync_control OpenML
596 * extension specification. The timestamp corresponds to the end of
597 * the vblank interval, aka start of scanout of topmost-leftmost display
598 * pixel in the following video frame.
599 *
600 * Requires support for optional dev->driver->get_scanout_position()
601 * in kms driver, plus a bit of setup code to provide a drm_display_mode
602 * that corresponds to the true scanout timing.
603 *
604 * The current implementation only handles standard video modes. It
605 * returns as no operation if a doublescan or interlaced video mode is
606 * active. Higher level code is expected to handle this.
607 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200608 * Returns:
609 * Negative value on error, failure or if not supported in current
Mario Kleiner27641c32010-10-23 04:20:23 +0200610 * video mode:
611 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200612 * -EINVAL - Invalid CRTC.
Mario Kleiner27641c32010-10-23 04:20:23 +0200613 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
614 * -ENOTSUPP - Function not supported in current display mode.
615 * -EIO - Failed, e.g., due to failed scanout position query.
616 *
617 * Returns or'ed positive status flags on success:
618 *
619 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
620 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
621 *
622 */
623int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
624 int *max_error,
625 struct timeval *vblank_time,
626 unsigned flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300627 const struct drm_crtc *refcrtc,
628 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200629{
Imre Deake62f2f52012-10-23 18:53:25 +0000630 ktime_t stime, etime, mono_time_offset;
631 struct timeval tv_etime;
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200632 int vbl_status;
Mario Kleiner27641c32010-10-23 04:20:23 +0200633 int vpos, hpos, i;
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300634 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200635 bool invbl;
636
637 if (crtc < 0 || crtc >= dev->num_crtcs) {
638 DRM_ERROR("Invalid crtc %d\n", crtc);
639 return -EINVAL;
640 }
641
642 /* Scanout position query not supported? Should not happen. */
643 if (!dev->driver->get_scanout_position) {
644 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
645 return -EIO;
646 }
647
Mario Kleiner27641c32010-10-23 04:20:23 +0200648 /* Durations of frames, lines, pixels in nanoseconds. */
649 framedur_ns = refcrtc->framedur_ns;
650 linedur_ns = refcrtc->linedur_ns;
651 pixeldur_ns = refcrtc->pixeldur_ns;
652
653 /* If mode timing undefined, just return as no-op:
654 * Happens during initial modesetting of a crtc.
655 */
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200656 if (framedur_ns == 0) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200657 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
658 return -EAGAIN;
659 }
660
Mario Kleiner27641c32010-10-23 04:20:23 +0200661 /* Get current scanout position with system timestamp.
662 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
663 * if single query takes longer than max_error nanoseconds.
664 *
665 * This guarantees a tight bound on maximum error if
666 * code gets preempted or delayed for some reason.
667 */
668 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100669 /*
670 * Get vertical and horizontal scanout position vpos, hpos,
671 * and bounding timestamps stime, etime, pre/post query.
672 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200673 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100674 &hpos, &stime, &etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200675
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100676 /*
677 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
678 * CLOCK_REALTIME is requested.
679 */
Imre Deakc61eef72012-10-23 18:53:26 +0000680 if (!drm_timestamp_monotonic)
681 mono_time_offset = ktime_get_monotonic_offset();
Mario Kleiner27641c32010-10-23 04:20:23 +0200682
Mario Kleiner27641c32010-10-23 04:20:23 +0200683 /* Return as no-op if scanout query unsupported or failed. */
684 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
685 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
686 crtc, vbl_status);
687 return -EIO;
688 }
689
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100690 /* Compute uncertainty in timestamp of scanout position query. */
Imre Deake62f2f52012-10-23 18:53:25 +0000691 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200692
693 /* Accept result with < max_error nsecs timing uncertainty. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300694 if (duration_ns <= *max_error)
Mario Kleiner27641c32010-10-23 04:20:23 +0200695 break;
696 }
697
698 /* Noisy system timing? */
699 if (i == DRM_TIMESTAMP_MAXRETRIES) {
700 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300701 crtc, duration_ns/1000, *max_error/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200702 }
703
704 /* Return upper bound of timestamp precision error. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300705 *max_error = duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200706
707 /* Check if in vblank area:
708 * vpos is >=0 in video scanout area, but negative
709 * within vblank area, counting down the number of lines until
710 * start of scanout.
711 */
712 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
713
714 /* Convert scanout position into elapsed time at raw_time query
715 * since start of scanout at first display scanline. delta_ns
716 * can be negative if start of scanout hasn't happened yet.
717 */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300718 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200719
Imre Deakc61eef72012-10-23 18:53:26 +0000720 if (!drm_timestamp_monotonic)
721 etime = ktime_sub(etime, mono_time_offset);
722
Imre Deake62f2f52012-10-23 18:53:25 +0000723 /* save this only for debugging purposes */
724 tv_etime = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200725 /* Subtract time delta from raw timestamp to get final
726 * vblank_time timestamp for end of vblank.
727 */
Michel Dänzere91abf82013-06-12 11:58:44 +0200728 if (delta_ns < 0)
729 etime = ktime_add_ns(etime, -delta_ns);
730 else
731 etime = ktime_sub_ns(etime, delta_ns);
Imre Deake62f2f52012-10-23 18:53:25 +0000732 *vblank_time = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200733
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700734 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
735 crtc, (int)vbl_status, hpos, vpos,
Imre Deake62f2f52012-10-23 18:53:25 +0000736 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700737 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300738 duration_ns/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200739
740 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
741 if (invbl)
742 vbl_status |= DRM_VBLANKTIME_INVBL;
743
744 return vbl_status;
745}
746EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
747
Imre Deakc61eef72012-10-23 18:53:26 +0000748static struct timeval get_drm_timestamp(void)
749{
750 ktime_t now;
751
752 now = ktime_get();
753 if (!drm_timestamp_monotonic)
754 now = ktime_sub(now, ktime_get_monotonic_offset());
755
756 return ktime_to_timeval(now);
757}
758
Mario Kleiner27641c32010-10-23 04:20:23 +0200759/**
760 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200761 * vblank interval
Mario Kleiner27641c32010-10-23 04:20:23 +0200762 * @dev: DRM device
Daniel Vetterf5752b32014-05-08 16:41:51 +0200763 * @crtc: which CRTC's vblank timestamp to retrieve
Mario Kleiner27641c32010-10-23 04:20:23 +0200764 * @tvblank: Pointer to target struct timeval which should receive the timestamp
765 * @flags: Flags to pass to driver:
Daniel Vetterf5752b32014-05-08 16:41:51 +0200766 * 0 = Default,
767 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
Mario Kleiner27641c32010-10-23 04:20:23 +0200768 *
769 * Fetches the system timestamp corresponding to the time of the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200770 * vblank interval on specified CRTC. May call into kms-driver to
Mario Kleiner27641c32010-10-23 04:20:23 +0200771 * compute the timestamp with a high-precision GPU specific method.
772 *
773 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
774 * call, i.e., it isn't very precisely locked to the true vblank.
775 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200776 * Returns:
777 * Non-zero if timestamp is considered to be very precise, zero otherwise.
Mario Kleiner27641c32010-10-23 04:20:23 +0200778 */
779u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
780 struct timeval *tvblank, unsigned flags)
781{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200782 int ret;
Mario Kleiner27641c32010-10-23 04:20:23 +0200783
784 /* Define requested maximum error on timestamps (nanoseconds). */
785 int max_error = (int) drm_timestamp_precision * 1000;
786
787 /* Query driver if possible and precision timestamping enabled. */
788 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
789 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
790 tvblank, flags);
791 if (ret > 0)
792 return (u32) ret;
793 }
794
795 /* GPU high precision timestamp query unsupported or failed.
Imre Deakc61eef72012-10-23 18:53:26 +0000796 * Return current monotonic/gettimeofday timestamp as best estimate.
Mario Kleiner27641c32010-10-23 04:20:23 +0200797 */
Imre Deakc61eef72012-10-23 18:53:26 +0000798 *tvblank = get_drm_timestamp();
Mario Kleiner27641c32010-10-23 04:20:23 +0200799
800 return 0;
801}
802EXPORT_SYMBOL(drm_get_last_vbltimestamp);
803
804/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700805 * drm_vblank_count - retrieve "cooked" vblank counter value
806 * @dev: DRM device
807 * @crtc: which counter to retrieve
808 *
809 * Fetches the "cooked" vblank count value that represents the number of
810 * vblank events since the system was booted, including lost events due to
811 * modesetting activity.
Daniel Vetterf5752b32014-05-08 16:41:51 +0200812 *
813 * Returns:
814 * The software vblank counter.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700815 */
816u32 drm_vblank_count(struct drm_device *dev, int crtc)
817{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300818 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
819
820 return atomic_read(&vblank->count);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700821}
822EXPORT_SYMBOL(drm_vblank_count);
823
824/**
Mario Kleiner27641c32010-10-23 04:20:23 +0200825 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
826 * and the system timestamp corresponding to that vblank counter value.
827 *
828 * @dev: DRM device
829 * @crtc: which counter to retrieve
830 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
831 *
832 * Fetches the "cooked" vblank count value that represents the number of
833 * vblank events since the system was booted, including lost events due to
834 * modesetting activity. Returns corresponding system timestamp of the time
Daniel Vetterf5752b32014-05-08 16:41:51 +0200835 * of the vblank interval that corresponds to the current vblank counter value.
Mario Kleiner27641c32010-10-23 04:20:23 +0200836 */
837u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
838 struct timeval *vblanktime)
839{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300840 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +0200841 u32 cur_vblank;
842
843 /* Read timestamp from slot of _vblank_time ringbuffer
844 * that corresponds to current vblank count. Retry if
845 * count has incremented during readout. This works like
846 * a seqlock.
847 */
848 do {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300849 cur_vblank = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200850 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
851 smp_rmb();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300852 } while (cur_vblank != atomic_read(&vblank->count));
Mario Kleiner27641c32010-10-23 04:20:23 +0200853
854 return cur_vblank;
855}
856EXPORT_SYMBOL(drm_vblank_count_and_time);
857
Rob Clarkc6eefa12012-10-16 22:48:40 +0000858static void send_vblank_event(struct drm_device *dev,
859 struct drm_pending_vblank_event *e,
860 unsigned long seq, struct timeval *now)
861{
862 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
863 e->event.sequence = seq;
864 e->event.tv_sec = now->tv_sec;
865 e->event.tv_usec = now->tv_usec;
866
867 list_add_tail(&e->base.link,
868 &e->base.file_priv->event_list);
869 wake_up_interruptible(&e->base.file_priv->event_wait);
870 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
871 e->event.sequence);
872}
873
874/**
875 * drm_send_vblank_event - helper to send vblank event after pageflip
876 * @dev: DRM device
877 * @crtc: CRTC in question
878 * @e: the event to send
879 *
880 * Updates sequence # and timestamp on event, and sends it to userspace.
881 * Caller must hold event lock.
882 */
883void drm_send_vblank_event(struct drm_device *dev, int crtc,
884 struct drm_pending_vblank_event *e)
885{
886 struct timeval now;
887 unsigned int seq;
888 if (crtc >= 0) {
889 seq = drm_vblank_count_and_time(dev, crtc, &now);
890 } else {
891 seq = 0;
Imre Deakc61eef72012-10-23 18:53:26 +0000892
893 now = get_drm_timestamp();
Rob Clarkc6eefa12012-10-16 22:48:40 +0000894 }
Rob Clark21a245d2012-12-10 10:49:46 -0600895 e->pipe = crtc;
Rob Clarkc6eefa12012-10-16 22:48:40 +0000896 send_vblank_event(dev, e, seq, &now);
897}
898EXPORT_SYMBOL(drm_send_vblank_event);
899
Mario Kleiner27641c32010-10-23 04:20:23 +0200900/**
Ville Syrjäläf2752282014-02-19 21:29:49 +0200901 * drm_vblank_enable - enable the vblank interrupt on a CRTC
902 * @dev: DRM device
903 * @crtc: CRTC in question
904 */
905static int drm_vblank_enable(struct drm_device *dev, int crtc)
906{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300907 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjäläf2752282014-02-19 21:29:49 +0200908 int ret = 0;
909
910 assert_spin_locked(&dev->vbl_lock);
911
912 spin_lock(&dev->vblank_time_lock);
913
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300914 if (!vblank->enabled) {
Daniel Vetter1cfb80b2014-05-21 15:11:33 +0200915 /*
916 * Enable vblank irqs under vblank_time_lock protection.
Ville Syrjäläf2752282014-02-19 21:29:49 +0200917 * All vblank count & timestamp updates are held off
918 * until we are done reinitializing master counter and
919 * timestamps. Filtercode in drm_handle_vblank() will
920 * prevent double-accounting of same vblank interval.
921 */
922 ret = dev->driver->enable_vblank(dev, crtc);
923 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
924 if (ret)
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300925 atomic_dec(&vblank->refcount);
Ville Syrjäläf2752282014-02-19 21:29:49 +0200926 else {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300927 vblank->enabled = true;
Ville Syrjäläf2752282014-02-19 21:29:49 +0200928 drm_update_vblank_count(dev, crtc);
929 }
930 }
931
932 spin_unlock(&dev->vblank_time_lock);
933
934 return ret;
935}
936
937/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700938 * drm_vblank_get - get a reference count on vblank events
939 * @dev: DRM device
940 * @crtc: which CRTC to own
941 *
942 * Acquire a reference count on vblank events to avoid having them disabled
943 * while in use.
944 *
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200945 * This is the legacy version of drm_crtc_vblank_get().
946 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200947 * Returns:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700948 * Zero on success, nonzero on failure.
949 */
950int drm_vblank_get(struct drm_device *dev, int crtc)
951{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300952 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Peter Hurley97cbc882013-08-20 21:51:12 -0400953 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700954 int ret = 0;
955
956 spin_lock_irqsave(&dev->vbl_lock, irqflags);
957 /* Going from 0->1 means we have to enable interrupts again */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300958 if (atomic_add_return(1, &vblank->refcount) == 1) {
Ville Syrjäläf2752282014-02-19 21:29:49 +0200959 ret = drm_vblank_enable(dev, crtc);
Li Peng778c9022009-11-09 12:51:22 +0800960 } else {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300961 if (!vblank->enabled) {
962 atomic_dec(&vblank->refcount);
Li Peng778c9022009-11-09 12:51:22 +0800963 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700964 }
965 }
966 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
967
968 return ret;
969}
970EXPORT_SYMBOL(drm_vblank_get);
971
972/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200973 * drm_crtc_vblank_get - get a reference count on vblank events
974 * @crtc: which CRTC to own
975 *
976 * Acquire a reference count on vblank events to avoid having them disabled
977 * while in use.
978 *
979 * This is the native kms version of drm_vblank_off().
980 *
981 * Returns:
982 * Zero on success, nonzero on failure.
983 */
984int drm_crtc_vblank_get(struct drm_crtc *crtc)
985{
986 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
987}
988EXPORT_SYMBOL(drm_crtc_vblank_get);
989
990/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700991 * drm_vblank_put - give up ownership of vblank events
992 * @dev: DRM device
993 * @crtc: which counter to give up
994 *
995 * Release ownership of a given vblank counter, turning off interrupts
Mario Kleiner27641c32010-10-23 04:20:23 +0200996 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200997 *
998 * This is the legacy version of drm_crtc_vblank_put().
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700999 */
1000void drm_vblank_put(struct drm_device *dev, int crtc)
1001{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001002 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
1003
1004 BUG_ON(atomic_read(&vblank->refcount) == 0);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001005
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001006 /* Last user schedules interrupt disable */
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001007 if (atomic_dec_and_test(&vblank->refcount)) {
Daniel Vetterab8905f2014-09-10 17:36:08 +02001008 if (drm_vblank_offdelay == 0)
1009 return;
1010 else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001011 vblank_disable_fn((unsigned long)vblank);
Daniel Vetterab8905f2014-09-10 17:36:08 +02001012 else
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001013 mod_timer(&vblank->disable_timer,
1014 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1015 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001016}
1017EXPORT_SYMBOL(drm_vblank_put);
1018
Rob Clarkc6eefa12012-10-16 22:48:40 +00001019/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001020 * drm_crtc_vblank_put - give up ownership of vblank events
1021 * @crtc: which counter to give up
1022 *
1023 * Release ownership of a given vblank counter, turning off interrupts
1024 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1025 *
1026 * This is the native kms version of drm_vblank_put().
1027 */
1028void drm_crtc_vblank_put(struct drm_crtc *crtc)
1029{
1030 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1031}
1032EXPORT_SYMBOL(drm_crtc_vblank_put);
1033
1034/**
Rob Clarkc6eefa12012-10-16 22:48:40 +00001035 * drm_vblank_off - disable vblank events on a CRTC
1036 * @dev: DRM device
1037 * @crtc: CRTC in question
1038 *
Daniel Vetterf5752b32014-05-08 16:41:51 +02001039 * Drivers can use this function to shut down the vblank interrupt handling when
1040 * disabling a crtc. This function ensures that the latest vblank frame count is
1041 * stored so that drm_vblank_on() can restore it again.
1042 *
1043 * Drivers must use this function when the hardware vblank counter can get
1044 * reset, e.g. when suspending.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001045 *
1046 * This is the legacy version of drm_crtc_vblank_off().
Rob Clarkc6eefa12012-10-16 22:48:40 +00001047 */
Li Peng778c9022009-11-09 12:51:22 +08001048void drm_vblank_off(struct drm_device *dev, int crtc)
1049{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001050 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001051 struct drm_pending_vblank_event *e, *t;
1052 struct timeval now;
Li Peng778c9022009-11-09 12:51:22 +08001053 unsigned long irqflags;
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001054 unsigned int seq;
Li Peng778c9022009-11-09 12:51:22 +08001055
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001056 spin_lock_irqsave(&dev->event_lock, irqflags);
1057
1058 spin_lock(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +02001059 vblank_disable_and_save(dev, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001060 wake_up(&vblank->queue);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001061
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001062 /*
1063 * Prevent subsequent drm_vblank_get() from re-enabling
1064 * the vblank interrupt by bumping the refcount.
1065 */
1066 if (!vblank->inmodeset) {
1067 atomic_inc(&vblank->refcount);
1068 vblank->inmodeset = 1;
1069 }
1070 spin_unlock(&dev->vbl_lock);
1071
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001072 /* Send any queued vblank events, lest the natives grow disquiet */
1073 seq = drm_vblank_count_and_time(dev, crtc, &now);
Imre Deake7783ba2012-11-02 13:30:50 +02001074
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001075 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1076 if (e->pipe != crtc)
1077 continue;
1078 DRM_DEBUG("Sending premature vblank event on disable: \
1079 wanted %d, current %d\n",
1080 e->event.sequence, seq);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001081 list_del(&e->base.link);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001082 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001083 send_vblank_event(dev, e, seq, &now);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001084 }
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001085 spin_unlock_irqrestore(&dev->event_lock, irqflags);
Li Peng778c9022009-11-09 12:51:22 +08001086}
1087EXPORT_SYMBOL(drm_vblank_off);
1088
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001089/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001090 * drm_crtc_vblank_off - disable vblank events on a CRTC
1091 * @crtc: CRTC in question
1092 *
1093 * Drivers can use this function to shut down the vblank interrupt handling when
1094 * disabling a crtc. This function ensures that the latest vblank frame count is
1095 * stored so that drm_vblank_on can restore it again.
1096 *
1097 * Drivers must use this function when the hardware vblank counter can get
1098 * reset, e.g. when suspending.
1099 *
1100 * This is the native kms version of drm_vblank_off().
1101 */
1102void drm_crtc_vblank_off(struct drm_crtc *crtc)
1103{
1104 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1105}
1106EXPORT_SYMBOL(drm_crtc_vblank_off);
1107
1108/**
Ville Syrjäläf2752282014-02-19 21:29:49 +02001109 * drm_vblank_on - enable vblank events on a CRTC
1110 * @dev: DRM device
1111 * @crtc: CRTC in question
Daniel Vetterf5752b32014-05-08 16:41:51 +02001112 *
1113 * This functions restores the vblank interrupt state captured with
1114 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1115 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1116 * in driver load code to reflect the current hardware state of the crtc.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001117 *
1118 * This is the legacy version of drm_crtc_vblank_on().
Ville Syrjäläf2752282014-02-19 21:29:49 +02001119 */
1120void drm_vblank_on(struct drm_device *dev, int crtc)
1121{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001122 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjäläf2752282014-02-19 21:29:49 +02001123 unsigned long irqflags;
1124
1125 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001126 /* Drop our private "prevent drm_vblank_get" refcount */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001127 if (vblank->inmodeset) {
1128 atomic_dec(&vblank->refcount);
1129 vblank->inmodeset = 0;
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001130 }
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001131
1132 /*
1133 * sample the current counter to avoid random jumps
1134 * when drm_vblank_enable() applies the diff
1135 *
1136 * -1 to make sure user will never see the same
1137 * vblank counter value before and after a modeset
1138 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001139 vblank->last =
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001140 (dev->driver->get_vblank_counter(dev, crtc) - 1) &
1141 dev->max_vblank_count;
Ville Syrjäläcd19e522014-08-06 14:49:56 +03001142 /*
1143 * re-enable interrupts if there are users left, or the
1144 * user wishes vblank interrupts to be enabled all the time.
1145 */
1146 if (atomic_read(&vblank->refcount) != 0 ||
1147 (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
Ville Syrjäläf2752282014-02-19 21:29:49 +02001148 WARN_ON(drm_vblank_enable(dev, crtc));
1149 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1150}
1151EXPORT_SYMBOL(drm_vblank_on);
1152
1153/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001154 * drm_crtc_vblank_on - enable vblank events on a CRTC
1155 * @crtc: CRTC in question
1156 *
1157 * This functions restores the vblank interrupt state captured with
1158 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1159 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1160 * in driver load code to reflect the current hardware state of the crtc.
1161 *
1162 * This is the native kms version of drm_vblank_on().
1163 */
1164void drm_crtc_vblank_on(struct drm_crtc *crtc)
1165{
1166 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1167}
1168EXPORT_SYMBOL(drm_crtc_vblank_on);
1169
1170/**
Dave Airlief453ba02008-11-07 14:05:41 -08001171 * drm_vblank_pre_modeset - account for vblanks across mode sets
1172 * @dev: DRM device
1173 * @crtc: CRTC in question
Dave Airlief453ba02008-11-07 14:05:41 -08001174 *
1175 * Account for vblank events across mode setting events, which will likely
1176 * reset the hardware frame counter.
Daniel Vetterf5752b32014-05-08 16:41:51 +02001177 *
1178 * This is done by grabbing a temporary vblank reference to ensure that the
1179 * vblank interrupt keeps running across the modeset sequence. With this the
1180 * software-side vblank frame counting will ensure that there are no jumps or
1181 * discontinuities.
1182 *
1183 * Unfortunately this approach is racy and also doesn't work when the vblank
1184 * interrupt stops running, e.g. across system suspend resume. It is therefore
1185 * highly recommended that drivers use the newer drm_vblank_off() and
1186 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1187 * using "cooked" software vblank frame counters and not relying on any hardware
1188 * counters.
1189 *
1190 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1191 * again.
Dave Airlief453ba02008-11-07 14:05:41 -08001192 */
1193void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1194{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001195 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
1196
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001197 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Jerome Glissee77cef92010-01-07 15:39:13 +01001198 if (!dev->num_crtcs)
1199 return;
Dave Airlief453ba02008-11-07 14:05:41 -08001200 /*
1201 * To avoid all the problems that might happen if interrupts
1202 * were enabled/disabled around or between these calls, we just
1203 * have the kernel take a reference on the CRTC (just once though
1204 * to avoid corrupting the count if multiple, mismatch calls occur),
1205 * so that interrupts remain enabled in the interim.
1206 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001207 if (!vblank->inmodeset) {
1208 vblank->inmodeset = 0x1;
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001209 if (drm_vblank_get(dev, crtc) == 0)
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001210 vblank->inmodeset |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -08001211 }
1212}
1213EXPORT_SYMBOL(drm_vblank_pre_modeset);
1214
Daniel Vetterf5752b32014-05-08 16:41:51 +02001215/**
1216 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1217 * @dev: DRM device
1218 * @crtc: CRTC in question
1219 *
1220 * This function again drops the temporary vblank reference acquired in
1221 * drm_vblank_pre_modeset.
1222 */
Dave Airlief453ba02008-11-07 14:05:41 -08001223void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1224{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001225 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Dave Airlief453ba02008-11-07 14:05:41 -08001226 unsigned long irqflags;
1227
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001228 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1229 if (!dev->num_crtcs)
1230 return;
1231
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001232 if (vblank->inmodeset) {
Dave Airlief453ba02008-11-07 14:05:41 -08001233 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001234 dev->vblank_disable_allowed = true;
Dave Airlief453ba02008-11-07 14:05:41 -08001235 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001236
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001237 if (vblank->inmodeset & 0x2)
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001238 drm_vblank_put(dev, crtc);
1239
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001240 vblank->inmodeset = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001241 }
1242}
1243EXPORT_SYMBOL(drm_vblank_post_modeset);
1244
Daniel Vetterf5752b32014-05-08 16:41:51 +02001245/*
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001246 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1247 * @DRM_IOCTL_ARGS: standard ioctl arguments
1248 *
1249 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1250 * ioctls around modesetting so that any lost vblank events are accounted for.
1251 *
1252 * Generally the counter will reset across mode sets. If interrupts are
1253 * enabled around this call, we don't have to do anything since the counter
1254 * will have already been incremented.
1255 */
1256int drm_modeset_ctl(struct drm_device *dev, void *data,
1257 struct drm_file *file_priv)
1258{
1259 struct drm_modeset_ctl *modeset = data;
Dave Airlie19227562011-02-24 08:35:06 +10001260 unsigned int crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001261
1262 /* If drm_vblank_init() hasn't been called yet, just no-op */
1263 if (!dev->num_crtcs)
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001264 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001265
Laurent Pinchart29935552012-05-30 00:58:09 +02001266 /* KMS drivers handle this internally */
1267 if (drm_core_check_feature(dev, DRIVER_MODESET))
1268 return 0;
1269
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001270 crtc = modeset->crtc;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001271 if (crtc >= dev->num_crtcs)
1272 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001273
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001274 switch (modeset->cmd) {
1275 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001276 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001277 break;
1278 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001279 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001280 break;
1281 default:
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001282 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001283 }
1284
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001285 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001286}
1287
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001288static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1289 union drm_wait_vblank *vblwait,
1290 struct drm_file *file_priv)
1291{
Ville Syrjäläffe7c732014-08-06 14:49:52 +03001292 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001293 struct drm_pending_vblank_event *e;
1294 struct timeval now;
1295 unsigned long flags;
1296 unsigned int seq;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001297 int ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001298
1299 e = kzalloc(sizeof *e, GFP_KERNEL);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001300 if (e == NULL) {
1301 ret = -ENOMEM;
1302 goto err_put;
1303 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001304
1305 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001306 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001307 e->event.base.type = DRM_EVENT_VBLANK;
1308 e->event.base.length = sizeof e->event;
1309 e->event.user_data = vblwait->request.signal;
1310 e->base.event = &e->event.base;
1311 e->base.file_priv = file_priv;
1312 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1313
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001314 spin_lock_irqsave(&dev->event_lock, flags);
1315
Ville Syrjäläffe7c732014-08-06 14:49:52 +03001316 /*
1317 * drm_vblank_off() might have been called after we called
1318 * drm_vblank_get(). drm_vblank_off() holds event_lock
1319 * around the vblank disable, so no need for further locking.
1320 * The reference from drm_vblank_get() protects against
1321 * vblank disable from another source.
1322 */
1323 if (!vblank->enabled) {
1324 ret = -EINVAL;
1325 goto err_unlock;
1326 }
1327
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001328 if (file_priv->event_space < sizeof e->event) {
Chris Wilsonea5d5522010-12-01 19:41:31 +00001329 ret = -EBUSY;
1330 goto err_unlock;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001331 }
1332
1333 file_priv->event_space -= sizeof e->event;
Mario Kleiner27641c32010-10-23 04:20:23 +02001334 seq = drm_vblank_count_and_time(dev, pipe, &now);
1335
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001336 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1337 (seq - vblwait->request.sequence) <= (1 << 23)) {
1338 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +00001339 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001340 }
1341
1342 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1343 vblwait->request.sequence, seq, pipe);
1344
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001345 trace_drm_vblank_event_queued(current->pid, pipe,
1346 vblwait->request.sequence);
1347
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001348 e->event.sequence = vblwait->request.sequence;
1349 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
Dan Carpenter6f331622010-12-09 08:35:40 +03001350 drm_vblank_put(dev, pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001351 send_vblank_event(dev, e, seq, &now);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001352 vblwait->reply.sequence = seq;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001353 } else {
Ilija Hadzica6778e92011-10-31 13:11:57 -04001354 /* drm_handle_vblank_events will call drm_vblank_put */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001355 list_add_tail(&e->base.link, &dev->vblank_event_list);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001356 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001357 }
1358
1359 spin_unlock_irqrestore(&dev->event_lock, flags);
1360
1361 return 0;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001362
1363err_unlock:
1364 spin_unlock_irqrestore(&dev->event_lock, flags);
1365 kfree(e);
1366err_put:
Dan Carpenter6f331622010-12-09 08:35:40 +03001367 drm_vblank_put(dev, pipe);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001368 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001369}
1370
Daniel Vetterf5752b32014-05-08 16:41:51 +02001371/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 * Wait for VBLANK.
1373 *
1374 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001375 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * \param cmd command.
1377 * \param data user argument, pointing to a drm_wait_vblank structure.
1378 * \return zero on success or a negative number on failure.
1379 *
Eric Anholt30b23632009-01-27 21:19:41 -08001380 * This function enables the vblank interrupt on the pipe requested, then
1381 * sleeps waiting for the requested sequence number to occur, and drops
Daniel Vetterf5752b32014-05-08 16:41:51 +02001382 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
Eric Anholt30b23632009-01-27 21:19:41 -08001383 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001385int drm_wait_vblank(struct drm_device *dev, void *data,
1386 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001388 struct drm_vblank_crtc *vblank;
Eric Anholtc153f452007-09-03 12:06:45 +10001389 union drm_wait_vblank *vblwait = data;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001390 int ret;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001391 unsigned int flags, seq, crtc, high_crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Daniel Vetter49849792013-08-28 15:02:37 +02001393 if (!dev->irq_enabled)
1394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Eric Anholt30b23632009-01-27 21:19:41 -08001396 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1397 return -EINVAL;
1398
Eric Anholtc153f452007-09-03 12:06:45 +10001399 if (vblwait->request.type &
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001400 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1401 _DRM_VBLANK_HIGH_CRTC_MASK)) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001402 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001403 vblwait->request.type,
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001404 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1405 _DRM_VBLANK_HIGH_CRTC_MASK));
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001406 return -EINVAL;
1407 }
1408
Eric Anholtc153f452007-09-03 12:06:45 +10001409 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001410 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1411 if (high_crtc)
1412 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1413 else
1414 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001415 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001416 return -EINVAL;
1417
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001418 vblank = &dev->vblank[crtc];
1419
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001420 ret = drm_vblank_get(dev, crtc);
1421 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +10001422 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001423 return ret;
1424 }
1425 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001426
Eric Anholtc153f452007-09-03 12:06:45 +10001427 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +10001429 vblwait->request.sequence += seq;
1430 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 case _DRM_VBLANK_ABSOLUTE:
1432 break;
1433 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001434 ret = -EINVAL;
1435 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437
Ilija Hadzica6778e92011-10-31 13:11:57 -04001438 if (flags & _DRM_VBLANK_EVENT) {
1439 /* must hold on to the vblank ref until the event fires
1440 * drm_vblank_put will be called asynchronously
1441 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001442 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
Ilija Hadzica6778e92011-10-31 13:11:57 -04001443 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001444
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001445 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +10001446 (seq - vblwait->request.sequence) <= (1<<23)) {
1447 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001448 }
1449
Eric Anholt30b23632009-01-27 21:19:41 -08001450 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1451 vblwait->request.sequence, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001452 vblank->last_wait = vblwait->request.sequence;
1453 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
Eric Anholt30b23632009-01-27 21:19:41 -08001454 (((drm_vblank_count(dev, crtc) -
1455 vblwait->request.sequence) <= (1 << 23)) ||
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001456 !vblank->enabled ||
Eric Anholt30b23632009-01-27 21:19:41 -08001457 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Eric Anholt30b23632009-01-27 21:19:41 -08001459 if (ret != -EINTR) {
1460 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Mario Kleiner27641c32010-10-23 04:20:23 +02001462 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
Eric Anholt30b23632009-01-27 21:19:41 -08001463 vblwait->reply.tval_sec = now.tv_sec;
1464 vblwait->reply.tval_usec = now.tv_usec;
Mario Kleiner27641c32010-10-23 04:20:23 +02001465
Eric Anholt30b23632009-01-27 21:19:41 -08001466 DRM_DEBUG("returning %d to client\n",
1467 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 } else {
Eric Anholt30b23632009-01-27 21:19:41 -08001469 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001472done:
1473 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return ret;
1475}
1476
Sachin Kamatea7f7ab2012-08-01 17:15:31 +05301477static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001478{
1479 struct drm_pending_vblank_event *e, *t;
1480 struct timeval now;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001481 unsigned int seq;
1482
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001483 assert_spin_locked(&dev->event_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001484
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001485 seq = drm_vblank_count_and_time(dev, crtc, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001486
1487 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1488 if (e->pipe != crtc)
1489 continue;
1490 if ((seq - e->event.sequence) > (1<<23))
1491 continue;
1492
1493 DRM_DEBUG("vblank event on %d, current %d\n",
1494 e->event.sequence, seq);
1495
Rob Clarkc6eefa12012-10-16 22:48:40 +00001496 list_del(&e->base.link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001497 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001498 send_vblank_event(dev, e, seq, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001499 }
1500
Jesse Barnesac2874b2010-07-01 16:47:31 -07001501 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001505 * drm_handle_vblank - handle a vblank event
1506 * @dev: DRM device
1507 * @crtc: where this event occurred
1508 *
1509 * Drivers should call this routine in their vblank interrupt handlers to
1510 * update the vblank counter and send any signals that may be pending.
1511 */
Chris Wilson78c6e172011-01-31 10:48:04 +00001512bool drm_handle_vblank(struct drm_device *dev, int crtc)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001513{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001514 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +02001515 u32 vblcount;
1516 s64 diff_ns;
1517 struct timeval tvblank;
1518 unsigned long irqflags;
1519
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001520 if (!dev->num_crtcs)
Chris Wilson78c6e172011-01-31 10:48:04 +00001521 return false;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001522
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001523 spin_lock_irqsave(&dev->event_lock, irqflags);
1524
Mario Kleiner27641c32010-10-23 04:20:23 +02001525 /* Need timestamp lock to prevent concurrent execution with
1526 * vblank enable/disable, as this would cause inconsistent
1527 * or corrupted timestamps and vblank counts.
1528 */
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001529 spin_lock(&dev->vblank_time_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +02001530
1531 /* Vblank irq handling disabled. Nothing to do. */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001532 if (!vblank->enabled) {
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001533 spin_unlock(&dev->vblank_time_lock);
1534 spin_unlock_irqrestore(&dev->event_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001535 return false;
Mario Kleiner27641c32010-10-23 04:20:23 +02001536 }
1537
1538 /* Fetch corresponding timestamp for this vblank interval from
1539 * driver and store it in proper slot of timestamp ringbuffer.
1540 */
1541
1542 /* Get current timestamp and count. */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001543 vblcount = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +02001544 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1545
1546 /* Compute time difference to timestamp of last vblank */
1547 diff_ns = timeval_to_ns(&tvblank) -
1548 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1549
1550 /* Update vblank timestamp and count if at least
1551 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1552 * difference between last stored timestamp and current
1553 * timestamp. A smaller difference means basically
1554 * identical timestamps. Happens if this vblank has
1555 * been already processed and this is a redundant call,
1556 * e.g., due to spurious vblank interrupts. We need to
1557 * ignore those for accounting.
1558 */
Mario Kleinerc4cc3832011-02-21 05:42:00 +01001559 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001560 /* Store new timestamp in ringbuffer. */
1561 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
Mario Kleiner27641c32010-10-23 04:20:23 +02001562
1563 /* Increment cooked vblank count. This also atomically commits
1564 * the timestamp computed above.
1565 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001566 smp_mb__before_atomic();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001567 atomic_inc(&vblank->count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001568 smp_mb__after_atomic();
Mario Kleiner27641c32010-10-23 04:20:23 +02001569 } else {
1570 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1571 crtc, (int) diff_ns);
1572 }
1573
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001574 spin_unlock(&dev->vblank_time_lock);
1575
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001576 wake_up(&vblank->queue);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001577 drm_handle_vblank_events(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +02001578
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001579 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1580
Chris Wilson78c6e172011-01-31 10:48:04 +00001581 return true;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001582}
1583EXPORT_SYMBOL(drm_handle_vblank);