blob: ad699b7292784d673ec6873e2aa50ab741d9532e [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().
Daniel Vetter855d30b2014-09-10 17:36:09 +0200151 *
152 * But only do this if precise vblank timestamps are available.
153 * Otherwise we might read a totally bogus timestamp since drivers
154 * lacking precise timestamp support rely upon sampling the system clock
155 * at vblank interrupt time. Which obviously won't work out well if the
156 * vblank interrupt is disabled.
Ville Syrjälä812e7462014-08-06 14:49:48 +0300157 */
Daniel Vetter855d30b2014-09-10 17:36:09 +0200158 if (!vblank->enabled &&
159 drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0) > 0) {
Ville Syrjälä812e7462014-08-06 14:49:48 +0300160 drm_update_vblank_count(dev, crtc);
161 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
162 return;
163 }
164
Mario Kleiner27641c32010-10-23 04:20:23 +0200165 dev->driver->disable_vblank(dev, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300166 vblank->enabled = false;
Mario Kleiner27641c32010-10-23 04:20:23 +0200167
168 /* No further vblank irq's will be processed after
169 * this point. Get current hardware vblank count and
170 * vblank timestamp, repeat until they are consistent.
171 *
172 * FIXME: There is still a race condition here and in
173 * drm_update_vblank_count() which can cause off-by-one
174 * reinitialization of software vblank counter. If gpu
175 * vblank counter doesn't increment exactly at the leading
176 * edge of a vblank interval, then we can lose 1 count if
177 * we happen to execute between start of vblank and the
178 * delayed gpu counter increment.
179 */
180 do {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300181 vblank->last = dev->driver->get_vblank_counter(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +0200182 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300183 } while (vblank->last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
Egbert Eich0355cf32012-10-13 11:36:14 +0000184
185 if (!count)
186 vblrc = 0;
Mario Kleiner27641c32010-10-23 04:20:23 +0200187
188 /* Compute time difference to stored timestamp of last vblank
189 * as updated by last invocation of drm_handle_vblank() in vblank irq.
190 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300191 vblcount = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200192 diff_ns = timeval_to_ns(&tvblank) -
193 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
194
195 /* If there is at least 1 msec difference between the last stored
196 * timestamp and tvblank, then we are currently executing our
197 * disable inside a new vblank interval, the tvblank timestamp
198 * corresponds to this new vblank interval and the irq handler
199 * for this vblank didn't run yet and won't run due to our disable.
200 * Therefore we need to do the job of drm_handle_vblank() and
201 * increment the vblank counter by one to account for this vblank.
202 *
203 * Skip this step if there isn't any high precision timestamp
204 * available. In that case we can't account for this and just
205 * hope for the best.
206 */
Mario Kleinerbc215122011-02-21 05:42:01 +0100207 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
Ville Syrjäläc50d7522014-08-06 14:49:59 +0300208 /* Store new timestamp in ringbuffer. */
209 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
210
211 /* Increment cooked vblank count. This also atomically commits
212 * the timestamp computed above.
213 */
214 smp_mb__before_atomic();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300215 atomic_inc(&vblank->count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100216 smp_mb__after_atomic();
Mario Kleinerbc215122011-02-21 05:42:01 +0100217 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200218
Mario Kleiner27641c32010-10-23 04:20:23 +0200219 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +0200220}
221
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700222static void vblank_disable_fn(unsigned long arg)
223{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200224 struct drm_vblank_crtc *vblank = (void *)arg;
225 struct drm_device *dev = vblank->dev;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700226 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200227 int crtc = vblank->crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700228
229 if (!dev->vblank_disable_allowed)
230 return;
231
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200232 spin_lock_irqsave(&dev->vbl_lock, irqflags);
233 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
234 DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
235 vblank_disable_and_save(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700236 }
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200237 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700238}
239
Daniel Vetterf5752b32014-05-08 16:41:51 +0200240/**
241 * drm_vblank_cleanup - cleanup vblank support
242 * @dev: DRM device
243 *
244 * This function cleans up any resources allocated in drm_vblank_init.
245 */
Keith Packard52440212008-11-18 09:30:25 -0800246void drm_vblank_cleanup(struct drm_device *dev)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700247{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200248 int crtc;
Mario Kleiner2368ffb2014-08-06 03:22:46 +0200249 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200250
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700251 /* Bail if the driver didn't call drm_vblank_init() */
252 if (dev->num_crtcs == 0)
253 return;
254
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200255 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300256 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
257
258 del_timer_sync(&vblank->disable_timer);
Mario Kleiner2368ffb2014-08-06 03:22:46 +0200259
260 spin_lock_irqsave(&dev->vbl_lock, irqflags);
261 vblank_disable_and_save(dev, crtc);
262 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200263 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700264
Ville Syrjälä5380e922013-10-04 14:53:36 +0300265 kfree(dev->vblank);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700266
267 dev->num_crtcs = 0;
268}
Jerome Glissee77cef92010-01-07 15:39:13 +0100269EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700270
Daniel Vetterf5752b32014-05-08 16:41:51 +0200271/**
272 * drm_vblank_init - initialize vblank support
273 * @dev: drm_device
274 * @num_crtcs: number of crtcs supported by @dev
275 *
276 * This function initializes vblank support for @num_crtcs display pipelines.
277 *
278 * Returns:
279 * Zero on success or a negative error code on failure.
280 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700281int drm_vblank_init(struct drm_device *dev, int num_crtcs)
282{
283 int i, ret = -ENOMEM;
284
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700285 spin_lock_init(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +0200286 spin_lock_init(&dev->vblank_time_lock);
287
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700288 dev->num_crtcs = num_crtcs;
289
Ville Syrjälä5380e922013-10-04 14:53:36 +0300290 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
291 if (!dev->vblank)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700292 goto err;
293
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200294 for (i = 0; i < num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300295 struct drm_vblank_crtc *vblank = &dev->vblank[i];
296
297 vblank->dev = dev;
298 vblank->crtc = i;
299 init_waitqueue_head(&vblank->queue);
300 setup_timer(&vblank->disable_timer, vblank_disable_fn,
301 (unsigned long)vblank);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200302 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200303
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100304 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
Mario Kleiner27641c32010-10-23 04:20:23 +0200305
306 /* Driver specific high-precision vblank timestamping supported? */
307 if (dev->driver->get_vblank_timestamp)
308 DRM_INFO("Driver supports precise vblank timestamp query.\n");
309 else
310 DRM_INFO("No driver support for vblank timestamp query.\n");
311
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300312 dev->vblank_disable_allowed = false;
313
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700314 return 0;
315
316err:
Mario Kleiner79a093ae2014-08-06 03:22:44 +0200317 dev->num_crtcs = 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700318 return ret;
319}
320EXPORT_SYMBOL(drm_vblank_init);
321
Dave Airlie28d52042009-09-21 14:33:58 +1000322static void drm_irq_vgaarb_nokms(void *cookie, bool state)
323{
324 struct drm_device *dev = cookie;
325
326 if (dev->driver->vgaarb_irq) {
327 dev->driver->vgaarb_irq(dev, state);
328 return;
329 }
330
331 if (!dev->irq_enabled)
332 return;
333
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000334 if (state) {
335 if (dev->driver->irq_uninstall)
336 dev->driver->irq_uninstall(dev);
337 } else {
338 if (dev->driver->irq_preinstall)
339 dev->driver->irq_preinstall(dev);
340 if (dev->driver->irq_postinstall)
341 dev->driver->irq_postinstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +1000342 }
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200346 * drm_irq_install - install IRQ handler
347 * @dev: DRM device
348 * @irq: IRQ number to install the handler for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700350 * Initializes the IRQ related data. Installs the handler, calling the driver
Daniel Vetterf5752b32014-05-08 16:41:51 +0200351 * irq_preinstall() and irq_postinstall() functions before and after the
352 * installation.
353 *
354 * This is the simplified helper interface provided for drivers with no special
355 * needs. Drivers which need to install interrupt handlers for multiple
356 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
357 * that vblank interrupts are available.
358 *
359 * Returns:
360 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100362int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200364 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000365 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
368 return -EINVAL;
369
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100370 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -EINVAL;
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100374 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Daniel Vettere090c532013-11-03 20:27:05 +0100377 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300379 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100381 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000383 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000384 if (dev->driver->irq_preinstall)
385 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700389 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000390
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100391 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100392 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700393
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000394 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300395 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return ret;
397 }
398
Dave Airlie28d52042009-09-21 14:33:58 +1000399 if (!drm_core_check_feature(dev, DRIVER_MODESET))
400 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
401
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000403 if (dev->driver->irq_postinstall)
404 ret = dev->driver->irq_postinstall(dev);
405
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700406 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300407 dev->irq_enabled = false;
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000408 if (!drm_core_check_feature(dev, DRIVER_MODESET))
409 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100410 free_irq(irq, dev);
411 } else {
412 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700417EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200420 * drm_irq_uninstall - uninstall the IRQ handler
421 * @dev: DRM device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200423 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
424 * This should only be called by drivers which used drm_irq_install() to set up
425 * their interrupt handler. Other drivers must only reset
426 * drm_device->irq_enabled to false.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200428 * Note that for kernel modesetting drivers it is a bug if this function fails.
429 * The sanity checks are only to catch buggy user modesetting drivers which call
430 * the same function through an ioctl.
431 *
432 * Returns:
433 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200435int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800437 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300438 bool irq_enabled;
439 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
442 return -EINVAL;
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300445 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800447 /*
448 * Wake up any waiters so they don't hang.
449 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000450 if (dev->num_crtcs) {
451 spin_lock_irqsave(&dev->vbl_lock, irqflags);
452 for (i = 0; i < dev->num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300453 struct drm_vblank_crtc *vblank = &dev->vblank[i];
454
455 wake_up(&vblank->queue);
456 vblank->enabled = false;
457 vblank->last =
Ben Skeggsbde48892011-07-04 12:52:27 +1000458 dev->driver->get_vblank_counter(dev, i);
459 }
460 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800461 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800462
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return -EINVAL;
465
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100466 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dave Airlie28d52042009-09-21 14:33:58 +1000468 if (!drm_core_check_feature(dev, DRIVER_MODESET))
469 vga_client_register(dev->pdev, NULL, NULL, NULL);
470
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000471 if (dev->driver->irq_uninstall)
472 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100474 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return 0;
477}
478EXPORT_SYMBOL(drm_irq_uninstall);
479
Daniel Vetterf5752b32014-05-08 16:41:51 +0200480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * IRQ control ioctl.
482 *
483 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000484 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * \param cmd command.
486 * \param arg user argument, pointing to a drm_control structure.
487 * \return zero on success or a negative number on failure.
488 *
489 * Calls irq_install() or irq_uninstall() according to \p arg.
490 */
Eric Anholtc153f452007-09-03 12:06:45 +1000491int drm_control(struct drm_device *dev, void *data,
492 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Eric Anholtc153f452007-09-03 12:06:45 +1000494 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100495 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000496
Mario Kleiner27641c32010-10-23 04:20:23 +0200497 /* if we haven't irq we fallback for compatibility reasons -
498 * this used to be a separate function in drm_dma.h
499 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Daniel Vetter22471cf2013-11-03 20:02:50 +0100501 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
502 return 0;
503 if (drm_core_check_feature(dev, DRIVER_MODESET))
504 return 0;
505 /* UMS was only ever support on pci devices. */
506 if (WARN_ON(!dev->pdev))
507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Eric Anholtc153f452007-09-03 12:06:45 +1000509 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100511 irq = dev->pdev->irq;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100514 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100516 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100517 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100518 mutex_unlock(&dev->struct_mutex);
519
520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100522 mutex_lock(&dev->struct_mutex);
523 ret = drm_irq_uninstall(dev);
524 mutex_unlock(&dev->struct_mutex);
525
526 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 default:
528 return -EINVAL;
529 }
530}
531
532/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200533 * drm_calc_timestamping_constants - calculate vblank timestamp constants
534 * @crtc: drm_crtc whose timestamp constants should be updated.
535 * @mode: display mode containing the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200536 *
Ville Syrjälä21b21562013-10-26 17:22:52 +0300537 * Calculate and store various constants which are later
538 * needed by vblank and swap-completion timestamping, e.g,
539 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
Daniel Vetterf5752b32014-05-08 16:41:51 +0200540 * derived from CRTC's true scanout timing, so they take
Ville Syrjälä21b21562013-10-26 17:22:52 +0300541 * things like panel scaling or other adjustments into account.
Mario Kleiner27641c32010-10-23 04:20:23 +0200542 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300543void drm_calc_timestamping_constants(struct drm_crtc *crtc,
544 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200545{
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300546 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
Ville Syrjälä77666b72013-10-27 21:22:58 +0200547 int dotclock = mode->crtc_clock;
Mario Kleiner27641c32010-10-23 04:20:23 +0200548
549 /* Valid dotclock? */
550 if (dotclock > 0) {
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300551 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
552
553 /*
554 * Convert scanline length in pixels and video
555 * dot clock to line duration, frame duration
556 * and pixel duration in nanoseconds:
Mario Kleiner27641c32010-10-23 04:20:23 +0200557 */
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300558 pixeldur_ns = 1000000 / dotclock;
559 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
560 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
Ville Syrjäläc0ae24c2013-10-28 19:53:25 +0200561
562 /*
563 * Fields of interlaced scanout modes are only half a frame duration.
564 */
565 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
566 framedur_ns /= 2;
Mario Kleiner27641c32010-10-23 04:20:23 +0200567 } else
568 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
569 crtc->base.id);
570
571 crtc->pixeldur_ns = pixeldur_ns;
572 crtc->linedur_ns = linedur_ns;
573 crtc->framedur_ns = framedur_ns;
574
575 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300576 crtc->base.id, mode->crtc_htotal,
577 mode->crtc_vtotal, mode->crtc_vdisplay);
Mario Kleiner27641c32010-10-23 04:20:23 +0200578 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300579 crtc->base.id, dotclock, framedur_ns,
580 linedur_ns, pixeldur_ns);
Mario Kleiner27641c32010-10-23 04:20:23 +0200581}
582EXPORT_SYMBOL(drm_calc_timestamping_constants);
583
584/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200585 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
586 * @dev: DRM device
587 * @crtc: Which CRTC's vblank timestamp to retrieve
588 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
589 * On return contains true maximum error of timestamp
590 * @vblank_time: Pointer to struct timeval which should receive the timestamp
591 * @flags: Flags to pass to driver:
592 * 0 = Default,
593 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
594 * @refcrtc: CRTC which defines scanout timing
595 * @mode: mode which defines the scanout timings
596 *
597 * Implements calculation of exact vblank timestamps from given drm_display_mode
598 * timings and current video scanout position of a CRTC. This can be called from
599 * within get_vblank_timestamp() implementation of a kms driver to implement the
600 * actual timestamping.
Mario Kleiner27641c32010-10-23 04:20:23 +0200601 *
602 * Should return timestamps conforming to the OML_sync_control OpenML
603 * extension specification. The timestamp corresponds to the end of
604 * the vblank interval, aka start of scanout of topmost-leftmost display
605 * pixel in the following video frame.
606 *
607 * Requires support for optional dev->driver->get_scanout_position()
608 * in kms driver, plus a bit of setup code to provide a drm_display_mode
609 * that corresponds to the true scanout timing.
610 *
611 * The current implementation only handles standard video modes. It
612 * returns as no operation if a doublescan or interlaced video mode is
613 * active. Higher level code is expected to handle this.
614 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200615 * Returns:
616 * Negative value on error, failure or if not supported in current
Mario Kleiner27641c32010-10-23 04:20:23 +0200617 * video mode:
618 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200619 * -EINVAL - Invalid CRTC.
Mario Kleiner27641c32010-10-23 04:20:23 +0200620 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
621 * -ENOTSUPP - Function not supported in current display mode.
622 * -EIO - Failed, e.g., due to failed scanout position query.
623 *
624 * Returns or'ed positive status flags on success:
625 *
626 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
627 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
628 *
629 */
630int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
631 int *max_error,
632 struct timeval *vblank_time,
633 unsigned flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300634 const struct drm_crtc *refcrtc,
635 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200636{
Imre Deake62f2f52012-10-23 18:53:25 +0000637 ktime_t stime, etime, mono_time_offset;
638 struct timeval tv_etime;
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200639 int vbl_status;
Mario Kleiner27641c32010-10-23 04:20:23 +0200640 int vpos, hpos, i;
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300641 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200642 bool invbl;
643
644 if (crtc < 0 || crtc >= dev->num_crtcs) {
645 DRM_ERROR("Invalid crtc %d\n", crtc);
646 return -EINVAL;
647 }
648
649 /* Scanout position query not supported? Should not happen. */
650 if (!dev->driver->get_scanout_position) {
651 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
652 return -EIO;
653 }
654
Mario Kleiner27641c32010-10-23 04:20:23 +0200655 /* Durations of frames, lines, pixels in nanoseconds. */
656 framedur_ns = refcrtc->framedur_ns;
657 linedur_ns = refcrtc->linedur_ns;
658 pixeldur_ns = refcrtc->pixeldur_ns;
659
660 /* If mode timing undefined, just return as no-op:
661 * Happens during initial modesetting of a crtc.
662 */
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200663 if (framedur_ns == 0) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200664 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
665 return -EAGAIN;
666 }
667
Mario Kleiner27641c32010-10-23 04:20:23 +0200668 /* Get current scanout position with system timestamp.
669 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
670 * if single query takes longer than max_error nanoseconds.
671 *
672 * This guarantees a tight bound on maximum error if
673 * code gets preempted or delayed for some reason.
674 */
675 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100676 /*
677 * Get vertical and horizontal scanout position vpos, hpos,
678 * and bounding timestamps stime, etime, pre/post query.
679 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200680 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100681 &hpos, &stime, &etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200682
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100683 /*
684 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
685 * CLOCK_REALTIME is requested.
686 */
Imre Deakc61eef72012-10-23 18:53:26 +0000687 if (!drm_timestamp_monotonic)
688 mono_time_offset = ktime_get_monotonic_offset();
Mario Kleiner27641c32010-10-23 04:20:23 +0200689
Mario Kleiner27641c32010-10-23 04:20:23 +0200690 /* Return as no-op if scanout query unsupported or failed. */
691 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
692 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
693 crtc, vbl_status);
694 return -EIO;
695 }
696
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100697 /* Compute uncertainty in timestamp of scanout position query. */
Imre Deake62f2f52012-10-23 18:53:25 +0000698 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200699
700 /* Accept result with < max_error nsecs timing uncertainty. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300701 if (duration_ns <= *max_error)
Mario Kleiner27641c32010-10-23 04:20:23 +0200702 break;
703 }
704
705 /* Noisy system timing? */
706 if (i == DRM_TIMESTAMP_MAXRETRIES) {
707 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300708 crtc, duration_ns/1000, *max_error/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200709 }
710
711 /* Return upper bound of timestamp precision error. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300712 *max_error = duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200713
714 /* Check if in vblank area:
715 * vpos is >=0 in video scanout area, but negative
716 * within vblank area, counting down the number of lines until
717 * start of scanout.
718 */
719 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
720
721 /* Convert scanout position into elapsed time at raw_time query
722 * since start of scanout at first display scanline. delta_ns
723 * can be negative if start of scanout hasn't happened yet.
724 */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300725 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200726
Imre Deakc61eef72012-10-23 18:53:26 +0000727 if (!drm_timestamp_monotonic)
728 etime = ktime_sub(etime, mono_time_offset);
729
Imre Deake62f2f52012-10-23 18:53:25 +0000730 /* save this only for debugging purposes */
731 tv_etime = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200732 /* Subtract time delta from raw timestamp to get final
733 * vblank_time timestamp for end of vblank.
734 */
Michel Dänzere91abf82013-06-12 11:58:44 +0200735 if (delta_ns < 0)
736 etime = ktime_add_ns(etime, -delta_ns);
737 else
738 etime = ktime_sub_ns(etime, delta_ns);
Imre Deake62f2f52012-10-23 18:53:25 +0000739 *vblank_time = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200740
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700741 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
742 crtc, (int)vbl_status, hpos, vpos,
Imre Deake62f2f52012-10-23 18:53:25 +0000743 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700744 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300745 duration_ns/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200746
747 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
748 if (invbl)
749 vbl_status |= DRM_VBLANKTIME_INVBL;
750
751 return vbl_status;
752}
753EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
754
Imre Deakc61eef72012-10-23 18:53:26 +0000755static struct timeval get_drm_timestamp(void)
756{
757 ktime_t now;
758
759 now = ktime_get();
760 if (!drm_timestamp_monotonic)
761 now = ktime_sub(now, ktime_get_monotonic_offset());
762
763 return ktime_to_timeval(now);
764}
765
Mario Kleiner27641c32010-10-23 04:20:23 +0200766/**
767 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200768 * vblank interval
Mario Kleiner27641c32010-10-23 04:20:23 +0200769 * @dev: DRM device
Daniel Vetterf5752b32014-05-08 16:41:51 +0200770 * @crtc: which CRTC's vblank timestamp to retrieve
Mario Kleiner27641c32010-10-23 04:20:23 +0200771 * @tvblank: Pointer to target struct timeval which should receive the timestamp
772 * @flags: Flags to pass to driver:
Daniel Vetterf5752b32014-05-08 16:41:51 +0200773 * 0 = Default,
774 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
Mario Kleiner27641c32010-10-23 04:20:23 +0200775 *
776 * Fetches the system timestamp corresponding to the time of the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200777 * vblank interval on specified CRTC. May call into kms-driver to
Mario Kleiner27641c32010-10-23 04:20:23 +0200778 * compute the timestamp with a high-precision GPU specific method.
779 *
780 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
781 * call, i.e., it isn't very precisely locked to the true vblank.
782 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200783 * Returns:
784 * Non-zero if timestamp is considered to be very precise, zero otherwise.
Mario Kleiner27641c32010-10-23 04:20:23 +0200785 */
786u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
787 struct timeval *tvblank, unsigned flags)
788{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200789 int ret;
Mario Kleiner27641c32010-10-23 04:20:23 +0200790
791 /* Define requested maximum error on timestamps (nanoseconds). */
792 int max_error = (int) drm_timestamp_precision * 1000;
793
794 /* Query driver if possible and precision timestamping enabled. */
795 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
796 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
797 tvblank, flags);
798 if (ret > 0)
799 return (u32) ret;
800 }
801
802 /* GPU high precision timestamp query unsupported or failed.
Imre Deakc61eef72012-10-23 18:53:26 +0000803 * Return current monotonic/gettimeofday timestamp as best estimate.
Mario Kleiner27641c32010-10-23 04:20:23 +0200804 */
Imre Deakc61eef72012-10-23 18:53:26 +0000805 *tvblank = get_drm_timestamp();
Mario Kleiner27641c32010-10-23 04:20:23 +0200806
807 return 0;
808}
809EXPORT_SYMBOL(drm_get_last_vbltimestamp);
810
811/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700812 * drm_vblank_count - retrieve "cooked" vblank counter value
813 * @dev: DRM device
814 * @crtc: which counter to retrieve
815 *
816 * Fetches the "cooked" vblank count value that represents the number of
817 * vblank events since the system was booted, including lost events due to
818 * modesetting activity.
Daniel Vetterf5752b32014-05-08 16:41:51 +0200819 *
820 * Returns:
821 * The software vblank counter.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700822 */
823u32 drm_vblank_count(struct drm_device *dev, int crtc)
824{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300825 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
826
827 return atomic_read(&vblank->count);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700828}
829EXPORT_SYMBOL(drm_vblank_count);
830
831/**
Mario Kleiner27641c32010-10-23 04:20:23 +0200832 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
833 * and the system timestamp corresponding to that vblank counter value.
834 *
835 * @dev: DRM device
836 * @crtc: which counter to retrieve
837 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
838 *
839 * Fetches the "cooked" vblank count value that represents the number of
840 * vblank events since the system was booted, including lost events due to
841 * modesetting activity. Returns corresponding system timestamp of the time
Daniel Vetterf5752b32014-05-08 16:41:51 +0200842 * of the vblank interval that corresponds to the current vblank counter value.
Mario Kleiner27641c32010-10-23 04:20:23 +0200843 */
844u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
845 struct timeval *vblanktime)
846{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300847 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +0200848 u32 cur_vblank;
849
850 /* Read timestamp from slot of _vblank_time ringbuffer
851 * that corresponds to current vblank count. Retry if
852 * count has incremented during readout. This works like
853 * a seqlock.
854 */
855 do {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300856 cur_vblank = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200857 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
858 smp_rmb();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300859 } while (cur_vblank != atomic_read(&vblank->count));
Mario Kleiner27641c32010-10-23 04:20:23 +0200860
861 return cur_vblank;
862}
863EXPORT_SYMBOL(drm_vblank_count_and_time);
864
Rob Clarkc6eefa12012-10-16 22:48:40 +0000865static void send_vblank_event(struct drm_device *dev,
866 struct drm_pending_vblank_event *e,
867 unsigned long seq, struct timeval *now)
868{
869 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
870 e->event.sequence = seq;
871 e->event.tv_sec = now->tv_sec;
872 e->event.tv_usec = now->tv_usec;
873
874 list_add_tail(&e->base.link,
875 &e->base.file_priv->event_list);
876 wake_up_interruptible(&e->base.file_priv->event_wait);
877 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
878 e->event.sequence);
879}
880
881/**
882 * drm_send_vblank_event - helper to send vblank event after pageflip
883 * @dev: DRM device
884 * @crtc: CRTC in question
885 * @e: the event to send
886 *
887 * Updates sequence # and timestamp on event, and sends it to userspace.
888 * Caller must hold event lock.
889 */
890void drm_send_vblank_event(struct drm_device *dev, int crtc,
891 struct drm_pending_vblank_event *e)
892{
893 struct timeval now;
894 unsigned int seq;
895 if (crtc >= 0) {
896 seq = drm_vblank_count_and_time(dev, crtc, &now);
897 } else {
898 seq = 0;
Imre Deakc61eef72012-10-23 18:53:26 +0000899
900 now = get_drm_timestamp();
Rob Clarkc6eefa12012-10-16 22:48:40 +0000901 }
Rob Clark21a245d2012-12-10 10:49:46 -0600902 e->pipe = crtc;
Rob Clarkc6eefa12012-10-16 22:48:40 +0000903 send_vblank_event(dev, e, seq, &now);
904}
905EXPORT_SYMBOL(drm_send_vblank_event);
906
Mario Kleiner27641c32010-10-23 04:20:23 +0200907/**
Ville Syrjäläf2752282014-02-19 21:29:49 +0200908 * drm_vblank_enable - enable the vblank interrupt on a CRTC
909 * @dev: DRM device
910 * @crtc: CRTC in question
911 */
912static int drm_vblank_enable(struct drm_device *dev, int crtc)
913{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300914 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjäläf2752282014-02-19 21:29:49 +0200915 int ret = 0;
916
917 assert_spin_locked(&dev->vbl_lock);
918
919 spin_lock(&dev->vblank_time_lock);
920
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300921 if (!vblank->enabled) {
Daniel Vetter1cfb80b2014-05-21 15:11:33 +0200922 /*
923 * Enable vblank irqs under vblank_time_lock protection.
Ville Syrjäläf2752282014-02-19 21:29:49 +0200924 * All vblank count & timestamp updates are held off
925 * until we are done reinitializing master counter and
926 * timestamps. Filtercode in drm_handle_vblank() will
927 * prevent double-accounting of same vblank interval.
928 */
929 ret = dev->driver->enable_vblank(dev, crtc);
930 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
931 if (ret)
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300932 atomic_dec(&vblank->refcount);
Ville Syrjäläf2752282014-02-19 21:29:49 +0200933 else {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300934 vblank->enabled = true;
Ville Syrjäläf2752282014-02-19 21:29:49 +0200935 drm_update_vblank_count(dev, crtc);
936 }
937 }
938
939 spin_unlock(&dev->vblank_time_lock);
940
941 return ret;
942}
943
944/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700945 * drm_vblank_get - get a reference count on vblank events
946 * @dev: DRM device
947 * @crtc: which CRTC to own
948 *
949 * Acquire a reference count on vblank events to avoid having them disabled
950 * while in use.
951 *
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200952 * This is the legacy version of drm_crtc_vblank_get().
953 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200954 * Returns:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700955 * Zero on success, nonzero on failure.
956 */
957int drm_vblank_get(struct drm_device *dev, int crtc)
958{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300959 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Peter Hurley97cbc882013-08-20 21:51:12 -0400960 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700961 int ret = 0;
962
963 spin_lock_irqsave(&dev->vbl_lock, irqflags);
964 /* Going from 0->1 means we have to enable interrupts again */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300965 if (atomic_add_return(1, &vblank->refcount) == 1) {
Ville Syrjäläf2752282014-02-19 21:29:49 +0200966 ret = drm_vblank_enable(dev, crtc);
Li Peng778c9022009-11-09 12:51:22 +0800967 } else {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300968 if (!vblank->enabled) {
969 atomic_dec(&vblank->refcount);
Li Peng778c9022009-11-09 12:51:22 +0800970 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700971 }
972 }
973 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
974
975 return ret;
976}
977EXPORT_SYMBOL(drm_vblank_get);
978
979/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200980 * drm_crtc_vblank_get - get a reference count on vblank events
981 * @crtc: which CRTC to own
982 *
983 * Acquire a reference count on vblank events to avoid having them disabled
984 * while in use.
985 *
986 * This is the native kms version of drm_vblank_off().
987 *
988 * Returns:
989 * Zero on success, nonzero on failure.
990 */
991int drm_crtc_vblank_get(struct drm_crtc *crtc)
992{
993 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
994}
995EXPORT_SYMBOL(drm_crtc_vblank_get);
996
997/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700998 * drm_vblank_put - give up ownership of vblank events
999 * @dev: DRM device
1000 * @crtc: which counter to give up
1001 *
1002 * Release ownership of a given vblank counter, turning off interrupts
Mario Kleiner27641c32010-10-23 04:20:23 +02001003 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001004 *
1005 * This is the legacy version of drm_crtc_vblank_put().
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001006 */
1007void drm_vblank_put(struct drm_device *dev, int crtc)
1008{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001009 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
1010
1011 BUG_ON(atomic_read(&vblank->refcount) == 0);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001012
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001013 /* Last user schedules interrupt disable */
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001014 if (atomic_dec_and_test(&vblank->refcount)) {
Daniel Vetterab8905f2014-09-10 17:36:08 +02001015 if (drm_vblank_offdelay == 0)
1016 return;
1017 else if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001018 vblank_disable_fn((unsigned long)vblank);
Daniel Vetterab8905f2014-09-10 17:36:08 +02001019 else
Ville Syrjälä4ed0ce32014-08-06 14:49:53 +03001020 mod_timer(&vblank->disable_timer,
1021 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1022 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001023}
1024EXPORT_SYMBOL(drm_vblank_put);
1025
Rob Clarkc6eefa12012-10-16 22:48:40 +00001026/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001027 * drm_crtc_vblank_put - give up ownership of vblank events
1028 * @crtc: which counter to give up
1029 *
1030 * Release ownership of a given vblank counter, turning off interrupts
1031 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1032 *
1033 * This is the native kms version of drm_vblank_put().
1034 */
1035void drm_crtc_vblank_put(struct drm_crtc *crtc)
1036{
1037 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1038}
1039EXPORT_SYMBOL(drm_crtc_vblank_put);
1040
1041/**
Rob Clarkc6eefa12012-10-16 22:48:40 +00001042 * drm_vblank_off - disable vblank events on a CRTC
1043 * @dev: DRM device
1044 * @crtc: CRTC in question
1045 *
Daniel Vetterf5752b32014-05-08 16:41:51 +02001046 * Drivers can use this function to shut down the vblank interrupt handling when
1047 * disabling a crtc. This function ensures that the latest vblank frame count is
1048 * stored so that drm_vblank_on() can restore it again.
1049 *
1050 * Drivers must use this function when the hardware vblank counter can get
1051 * reset, e.g. when suspending.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001052 *
1053 * This is the legacy version of drm_crtc_vblank_off().
Rob Clarkc6eefa12012-10-16 22:48:40 +00001054 */
Li Peng778c9022009-11-09 12:51:22 +08001055void drm_vblank_off(struct drm_device *dev, int crtc)
1056{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001057 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001058 struct drm_pending_vblank_event *e, *t;
1059 struct timeval now;
Li Peng778c9022009-11-09 12:51:22 +08001060 unsigned long irqflags;
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001061 unsigned int seq;
Li Peng778c9022009-11-09 12:51:22 +08001062
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001063 spin_lock_irqsave(&dev->event_lock, irqflags);
1064
1065 spin_lock(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +02001066 vblank_disable_and_save(dev, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001067 wake_up(&vblank->queue);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001068
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001069 /*
1070 * Prevent subsequent drm_vblank_get() from re-enabling
1071 * the vblank interrupt by bumping the refcount.
1072 */
1073 if (!vblank->inmodeset) {
1074 atomic_inc(&vblank->refcount);
1075 vblank->inmodeset = 1;
1076 }
1077 spin_unlock(&dev->vbl_lock);
1078
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001079 /* Send any queued vblank events, lest the natives grow disquiet */
1080 seq = drm_vblank_count_and_time(dev, crtc, &now);
Imre Deake7783ba2012-11-02 13:30:50 +02001081
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001082 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1083 if (e->pipe != crtc)
1084 continue;
1085 DRM_DEBUG("Sending premature vblank event on disable: \
1086 wanted %d, current %d\n",
1087 e->event.sequence, seq);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001088 list_del(&e->base.link);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001089 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001090 send_vblank_event(dev, e, seq, &now);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001091 }
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001092 spin_unlock_irqrestore(&dev->event_lock, irqflags);
Li Peng778c9022009-11-09 12:51:22 +08001093}
1094EXPORT_SYMBOL(drm_vblank_off);
1095
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001096/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001097 * drm_crtc_vblank_off - disable vblank events on a CRTC
1098 * @crtc: CRTC in question
1099 *
1100 * Drivers can use this function to shut down the vblank interrupt handling when
1101 * disabling a crtc. This function ensures that the latest vblank frame count is
1102 * stored so that drm_vblank_on can restore it again.
1103 *
1104 * Drivers must use this function when the hardware vblank counter can get
1105 * reset, e.g. when suspending.
1106 *
1107 * This is the native kms version of drm_vblank_off().
1108 */
1109void drm_crtc_vblank_off(struct drm_crtc *crtc)
1110{
1111 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1112}
1113EXPORT_SYMBOL(drm_crtc_vblank_off);
1114
1115/**
Ville Syrjäläf2752282014-02-19 21:29:49 +02001116 * drm_vblank_on - enable vblank events on a CRTC
1117 * @dev: DRM device
1118 * @crtc: CRTC in question
Daniel Vetterf5752b32014-05-08 16:41:51 +02001119 *
1120 * This functions restores the vblank interrupt state captured with
1121 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1122 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1123 * in driver load code to reflect the current hardware state of the crtc.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001124 *
1125 * This is the legacy version of drm_crtc_vblank_on().
Ville Syrjäläf2752282014-02-19 21:29:49 +02001126 */
1127void drm_vblank_on(struct drm_device *dev, int crtc)
1128{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001129 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Ville Syrjäläf2752282014-02-19 21:29:49 +02001130 unsigned long irqflags;
1131
1132 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001133 /* Drop our private "prevent drm_vblank_get" refcount */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001134 if (vblank->inmodeset) {
1135 atomic_dec(&vblank->refcount);
1136 vblank->inmodeset = 0;
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001137 }
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001138
1139 /*
1140 * sample the current counter to avoid random jumps
1141 * when drm_vblank_enable() applies the diff
1142 *
1143 * -1 to make sure user will never see the same
1144 * vblank counter value before and after a modeset
1145 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001146 vblank->last =
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001147 (dev->driver->get_vblank_counter(dev, crtc) - 1) &
1148 dev->max_vblank_count;
Ville Syrjäläcd19e522014-08-06 14:49:56 +03001149 /*
1150 * re-enable interrupts if there are users left, or the
1151 * user wishes vblank interrupts to be enabled all the time.
1152 */
1153 if (atomic_read(&vblank->refcount) != 0 ||
1154 (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
Ville Syrjäläf2752282014-02-19 21:29:49 +02001155 WARN_ON(drm_vblank_enable(dev, crtc));
1156 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1157}
1158EXPORT_SYMBOL(drm_vblank_on);
1159
1160/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001161 * drm_crtc_vblank_on - enable vblank events on a CRTC
1162 * @crtc: CRTC in question
1163 *
1164 * This functions restores the vblank interrupt state captured with
1165 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1166 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1167 * in driver load code to reflect the current hardware state of the crtc.
1168 *
1169 * This is the native kms version of drm_vblank_on().
1170 */
1171void drm_crtc_vblank_on(struct drm_crtc *crtc)
1172{
1173 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1174}
1175EXPORT_SYMBOL(drm_crtc_vblank_on);
1176
1177/**
Dave Airlief453ba02008-11-07 14:05:41 -08001178 * drm_vblank_pre_modeset - account for vblanks across mode sets
1179 * @dev: DRM device
1180 * @crtc: CRTC in question
Dave Airlief453ba02008-11-07 14:05:41 -08001181 *
1182 * Account for vblank events across mode setting events, which will likely
1183 * reset the hardware frame counter.
Daniel Vetterf5752b32014-05-08 16:41:51 +02001184 *
1185 * This is done by grabbing a temporary vblank reference to ensure that the
1186 * vblank interrupt keeps running across the modeset sequence. With this the
1187 * software-side vblank frame counting will ensure that there are no jumps or
1188 * discontinuities.
1189 *
1190 * Unfortunately this approach is racy and also doesn't work when the vblank
1191 * interrupt stops running, e.g. across system suspend resume. It is therefore
1192 * highly recommended that drivers use the newer drm_vblank_off() and
1193 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1194 * using "cooked" software vblank frame counters and not relying on any hardware
1195 * counters.
1196 *
1197 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1198 * again.
Dave Airlief453ba02008-11-07 14:05:41 -08001199 */
1200void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1201{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001202 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
1203
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001204 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Jerome Glissee77cef92010-01-07 15:39:13 +01001205 if (!dev->num_crtcs)
1206 return;
Dave Airlief453ba02008-11-07 14:05:41 -08001207 /*
1208 * To avoid all the problems that might happen if interrupts
1209 * were enabled/disabled around or between these calls, we just
1210 * have the kernel take a reference on the CRTC (just once though
1211 * to avoid corrupting the count if multiple, mismatch calls occur),
1212 * so that interrupts remain enabled in the interim.
1213 */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001214 if (!vblank->inmodeset) {
1215 vblank->inmodeset = 0x1;
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001216 if (drm_vblank_get(dev, crtc) == 0)
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001217 vblank->inmodeset |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -08001218 }
1219}
1220EXPORT_SYMBOL(drm_vblank_pre_modeset);
1221
Daniel Vetterf5752b32014-05-08 16:41:51 +02001222/**
1223 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1224 * @dev: DRM device
1225 * @crtc: CRTC in question
1226 *
1227 * This function again drops the temporary vblank reference acquired in
1228 * drm_vblank_pre_modeset.
1229 */
Dave Airlief453ba02008-11-07 14:05:41 -08001230void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1231{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001232 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Dave Airlief453ba02008-11-07 14:05:41 -08001233 unsigned long irqflags;
1234
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001235 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1236 if (!dev->num_crtcs)
1237 return;
1238
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001239 if (vblank->inmodeset) {
Dave Airlief453ba02008-11-07 14:05:41 -08001240 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001241 dev->vblank_disable_allowed = true;
Dave Airlief453ba02008-11-07 14:05:41 -08001242 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001243
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001244 if (vblank->inmodeset & 0x2)
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001245 drm_vblank_put(dev, crtc);
1246
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001247 vblank->inmodeset = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001248 }
1249}
1250EXPORT_SYMBOL(drm_vblank_post_modeset);
1251
Daniel Vetterf5752b32014-05-08 16:41:51 +02001252/*
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001253 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1254 * @DRM_IOCTL_ARGS: standard ioctl arguments
1255 *
1256 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1257 * ioctls around modesetting so that any lost vblank events are accounted for.
1258 *
1259 * Generally the counter will reset across mode sets. If interrupts are
1260 * enabled around this call, we don't have to do anything since the counter
1261 * will have already been incremented.
1262 */
1263int drm_modeset_ctl(struct drm_device *dev, void *data,
1264 struct drm_file *file_priv)
1265{
1266 struct drm_modeset_ctl *modeset = data;
Dave Airlie19227562011-02-24 08:35:06 +10001267 unsigned int crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001268
1269 /* If drm_vblank_init() hasn't been called yet, just no-op */
1270 if (!dev->num_crtcs)
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001271 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001272
Laurent Pinchart29935552012-05-30 00:58:09 +02001273 /* KMS drivers handle this internally */
1274 if (drm_core_check_feature(dev, DRIVER_MODESET))
1275 return 0;
1276
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001277 crtc = modeset->crtc;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001278 if (crtc >= dev->num_crtcs)
1279 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001280
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001281 switch (modeset->cmd) {
1282 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001283 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001284 break;
1285 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001286 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001287 break;
1288 default:
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001289 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001290 }
1291
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001292 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001293}
1294
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001295static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1296 union drm_wait_vblank *vblwait,
1297 struct drm_file *file_priv)
1298{
Ville Syrjäläffe7c732014-08-06 14:49:52 +03001299 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001300 struct drm_pending_vblank_event *e;
1301 struct timeval now;
1302 unsigned long flags;
1303 unsigned int seq;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001304 int ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001305
1306 e = kzalloc(sizeof *e, GFP_KERNEL);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001307 if (e == NULL) {
1308 ret = -ENOMEM;
1309 goto err_put;
1310 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001311
1312 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001313 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001314 e->event.base.type = DRM_EVENT_VBLANK;
1315 e->event.base.length = sizeof e->event;
1316 e->event.user_data = vblwait->request.signal;
1317 e->base.event = &e->event.base;
1318 e->base.file_priv = file_priv;
1319 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1320
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001321 spin_lock_irqsave(&dev->event_lock, flags);
1322
Ville Syrjäläffe7c732014-08-06 14:49:52 +03001323 /*
1324 * drm_vblank_off() might have been called after we called
1325 * drm_vblank_get(). drm_vblank_off() holds event_lock
1326 * around the vblank disable, so no need for further locking.
1327 * The reference from drm_vblank_get() protects against
1328 * vblank disable from another source.
1329 */
1330 if (!vblank->enabled) {
1331 ret = -EINVAL;
1332 goto err_unlock;
1333 }
1334
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001335 if (file_priv->event_space < sizeof e->event) {
Chris Wilsonea5d5522010-12-01 19:41:31 +00001336 ret = -EBUSY;
1337 goto err_unlock;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001338 }
1339
1340 file_priv->event_space -= sizeof e->event;
Mario Kleiner27641c32010-10-23 04:20:23 +02001341 seq = drm_vblank_count_and_time(dev, pipe, &now);
1342
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001343 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1344 (seq - vblwait->request.sequence) <= (1 << 23)) {
1345 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +00001346 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001347 }
1348
1349 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1350 vblwait->request.sequence, seq, pipe);
1351
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001352 trace_drm_vblank_event_queued(current->pid, pipe,
1353 vblwait->request.sequence);
1354
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001355 e->event.sequence = vblwait->request.sequence;
1356 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
Dan Carpenter6f331622010-12-09 08:35:40 +03001357 drm_vblank_put(dev, pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001358 send_vblank_event(dev, e, seq, &now);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001359 vblwait->reply.sequence = seq;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001360 } else {
Ilija Hadzica6778e92011-10-31 13:11:57 -04001361 /* drm_handle_vblank_events will call drm_vblank_put */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001362 list_add_tail(&e->base.link, &dev->vblank_event_list);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001363 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001364 }
1365
1366 spin_unlock_irqrestore(&dev->event_lock, flags);
1367
1368 return 0;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001369
1370err_unlock:
1371 spin_unlock_irqrestore(&dev->event_lock, flags);
1372 kfree(e);
1373err_put:
Dan Carpenter6f331622010-12-09 08:35:40 +03001374 drm_vblank_put(dev, pipe);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001375 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001376}
1377
Daniel Vetterf5752b32014-05-08 16:41:51 +02001378/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * Wait for VBLANK.
1380 *
1381 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001382 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * \param cmd command.
1384 * \param data user argument, pointing to a drm_wait_vblank structure.
1385 * \return zero on success or a negative number on failure.
1386 *
Eric Anholt30b23632009-01-27 21:19:41 -08001387 * This function enables the vblank interrupt on the pipe requested, then
1388 * sleeps waiting for the requested sequence number to occur, and drops
Daniel Vetterf5752b32014-05-08 16:41:51 +02001389 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
Eric Anholt30b23632009-01-27 21:19:41 -08001390 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001392int drm_wait_vblank(struct drm_device *dev, void *data,
1393 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001395 struct drm_vblank_crtc *vblank;
Eric Anholtc153f452007-09-03 12:06:45 +10001396 union drm_wait_vblank *vblwait = data;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001397 int ret;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001398 unsigned int flags, seq, crtc, high_crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Daniel Vetter49849792013-08-28 15:02:37 +02001400 if (!dev->irq_enabled)
1401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Eric Anholt30b23632009-01-27 21:19:41 -08001403 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1404 return -EINVAL;
1405
Eric Anholtc153f452007-09-03 12:06:45 +10001406 if (vblwait->request.type &
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001407 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1408 _DRM_VBLANK_HIGH_CRTC_MASK)) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001409 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001410 vblwait->request.type,
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001411 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1412 _DRM_VBLANK_HIGH_CRTC_MASK));
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001413 return -EINVAL;
1414 }
1415
Eric Anholtc153f452007-09-03 12:06:45 +10001416 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001417 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1418 if (high_crtc)
1419 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1420 else
1421 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001422 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001423 return -EINVAL;
1424
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001425 vblank = &dev->vblank[crtc];
1426
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001427 ret = drm_vblank_get(dev, crtc);
1428 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +10001429 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001430 return ret;
1431 }
1432 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001433
Eric Anholtc153f452007-09-03 12:06:45 +10001434 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +10001436 vblwait->request.sequence += seq;
1437 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 case _DRM_VBLANK_ABSOLUTE:
1439 break;
1440 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001441 ret = -EINVAL;
1442 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 }
1444
Ilija Hadzica6778e92011-10-31 13:11:57 -04001445 if (flags & _DRM_VBLANK_EVENT) {
1446 /* must hold on to the vblank ref until the event fires
1447 * drm_vblank_put will be called asynchronously
1448 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001449 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
Ilija Hadzica6778e92011-10-31 13:11:57 -04001450 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001451
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001452 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +10001453 (seq - vblwait->request.sequence) <= (1<<23)) {
1454 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001455 }
1456
Eric Anholt30b23632009-01-27 21:19:41 -08001457 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1458 vblwait->request.sequence, crtc);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001459 vblank->last_wait = vblwait->request.sequence;
1460 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
Eric Anholt30b23632009-01-27 21:19:41 -08001461 (((drm_vblank_count(dev, crtc) -
1462 vblwait->request.sequence) <= (1 << 23)) ||
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001463 !vblank->enabled ||
Eric Anholt30b23632009-01-27 21:19:41 -08001464 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Eric Anholt30b23632009-01-27 21:19:41 -08001466 if (ret != -EINTR) {
1467 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Mario Kleiner27641c32010-10-23 04:20:23 +02001469 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
Eric Anholt30b23632009-01-27 21:19:41 -08001470 vblwait->reply.tval_sec = now.tv_sec;
1471 vblwait->reply.tval_usec = now.tv_usec;
Mario Kleiner27641c32010-10-23 04:20:23 +02001472
Eric Anholt30b23632009-01-27 21:19:41 -08001473 DRM_DEBUG("returning %d to client\n",
1474 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 } else {
Eric Anholt30b23632009-01-27 21:19:41 -08001476 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001479done:
1480 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return ret;
1482}
1483
Sachin Kamatea7f7ab2012-08-01 17:15:31 +05301484static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001485{
1486 struct drm_pending_vblank_event *e, *t;
1487 struct timeval now;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001488 unsigned int seq;
1489
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001490 assert_spin_locked(&dev->event_lock);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001491
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001492 seq = drm_vblank_count_and_time(dev, crtc, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001493
1494 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1495 if (e->pipe != crtc)
1496 continue;
1497 if ((seq - e->event.sequence) > (1<<23))
1498 continue;
1499
1500 DRM_DEBUG("vblank event on %d, current %d\n",
1501 e->event.sequence, seq);
1502
Rob Clarkc6eefa12012-10-16 22:48:40 +00001503 list_del(&e->base.link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001504 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001505 send_vblank_event(dev, e, seq, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001506 }
1507
Jesse Barnesac2874b2010-07-01 16:47:31 -07001508 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001512 * drm_handle_vblank - handle a vblank event
1513 * @dev: DRM device
1514 * @crtc: where this event occurred
1515 *
1516 * Drivers should call this routine in their vblank interrupt handlers to
1517 * update the vblank counter and send any signals that may be pending.
1518 */
Chris Wilson78c6e172011-01-31 10:48:04 +00001519bool drm_handle_vblank(struct drm_device *dev, int crtc)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001520{
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001521 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
Mario Kleiner27641c32010-10-23 04:20:23 +02001522 u32 vblcount;
1523 s64 diff_ns;
1524 struct timeval tvblank;
1525 unsigned long irqflags;
1526
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001527 if (!dev->num_crtcs)
Chris Wilson78c6e172011-01-31 10:48:04 +00001528 return false;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001529
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001530 spin_lock_irqsave(&dev->event_lock, irqflags);
1531
Mario Kleiner27641c32010-10-23 04:20:23 +02001532 /* Need timestamp lock to prevent concurrent execution with
1533 * vblank enable/disable, as this would cause inconsistent
1534 * or corrupted timestamps and vblank counts.
1535 */
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001536 spin_lock(&dev->vblank_time_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +02001537
1538 /* Vblank irq handling disabled. Nothing to do. */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001539 if (!vblank->enabled) {
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001540 spin_unlock(&dev->vblank_time_lock);
1541 spin_unlock_irqrestore(&dev->event_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001542 return false;
Mario Kleiner27641c32010-10-23 04:20:23 +02001543 }
1544
1545 /* Fetch corresponding timestamp for this vblank interval from
1546 * driver and store it in proper slot of timestamp ringbuffer.
1547 */
1548
1549 /* Get current timestamp and count. */
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001550 vblcount = atomic_read(&vblank->count);
Mario Kleiner27641c32010-10-23 04:20:23 +02001551 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1552
1553 /* Compute time difference to timestamp of last vblank */
1554 diff_ns = timeval_to_ns(&tvblank) -
1555 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1556
1557 /* Update vblank timestamp and count if at least
1558 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1559 * difference between last stored timestamp and current
1560 * timestamp. A smaller difference means basically
1561 * identical timestamps. Happens if this vblank has
1562 * been already processed and this is a redundant call,
1563 * e.g., due to spurious vblank interrupts. We need to
1564 * ignore those for accounting.
1565 */
Mario Kleinerc4cc3832011-02-21 05:42:00 +01001566 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001567 /* Store new timestamp in ringbuffer. */
1568 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
Mario Kleiner27641c32010-10-23 04:20:23 +02001569
1570 /* Increment cooked vblank count. This also atomically commits
1571 * the timestamp computed above.
1572 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001573 smp_mb__before_atomic();
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001574 atomic_inc(&vblank->count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001575 smp_mb__after_atomic();
Mario Kleiner27641c32010-10-23 04:20:23 +02001576 } else {
1577 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1578 crtc, (int) diff_ns);
1579 }
1580
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001581 spin_unlock(&dev->vblank_time_lock);
1582
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +03001583 wake_up(&vblank->queue);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001584 drm_handle_vblank_events(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +02001585
Ville Syrjälä56cc2792014-08-06 14:49:51 +03001586 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1587
Chris Wilson78c6e172011-01-31 10:48:04 +00001588 return true;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001589}
1590EXPORT_SYMBOL(drm_handle_vblank);