blob: fc1525a499d935b9514844fb89d375c3fe8c5f8d [file] [log] [blame]
Daniel Vetterf5752b32014-05-08 16:41:51 +02001/*
2 * drm_irq.c IRQ and vblank support
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
6 */
7
8/*
9 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
10 *
11 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
12 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
13 * All Rights Reserved.
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice (including the next
23 * paragraph) shall be included in all copies or substantial portions of the
24 * Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 * OTHER DEALINGS IN THE SOFTWARE.
33 */
34
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnesac2874b2010-07-01 16:47:31 -070036#include "drm_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <linux/interrupt.h> /* For task queue support */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Dave Airlie28d52042009-09-21 14:33:58 +100041#include <linux/vgaarb.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040042#include <linux/export.h>
Mario Kleiner27641c32010-10-23 04:20:23 +020043
44/* Access macro for slots in vblank timestamp ringbuffer. */
Ville Syrjälä5380e922013-10-04 14:53:36 +030045#define vblanktimestamp(dev, crtc, count) \
46 ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
Mario Kleiner27641c32010-10-23 04:20:23 +020047
48/* Retry timestamp calculation up to 3 times to satisfy
49 * drm_timestamp_precision before giving up.
50 */
51#define DRM_TIMESTAMP_MAXRETRIES 3
52
53/* Threshold in nanoseconds for detection of redundant
54 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
55 */
56#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
57
Ville Syrjälä13b030a2014-08-06 14:49:47 +030058/**
59 * drm_update_vblank_count - update the master vblank counter
60 * @dev: DRM device
61 * @crtc: counter to update
62 *
63 * Call back into the driver to update the appropriate vblank counter
64 * (specified by @crtc). Deal with wraparound, if it occurred, and
65 * update the last read value so we can deal with wraparound on the next
66 * call if necessary.
67 *
68 * Only necessary when going from off->on, to account for frames we
69 * didn't get an interrupt for.
70 *
71 * Note: caller must hold dev->vbl_lock since this reads & writes
72 * device vblank fields.
73 */
74static void drm_update_vblank_count(struct drm_device *dev, int crtc)
75{
76 u32 cur_vblank, diff, tslot, rc;
77 struct timeval t_vblank;
78
79 /*
80 * Interrupts were disabled prior to this call, so deal with counter
81 * wrap if needed.
82 * NOTE! It's possible we lost a full dev->max_vblank_count events
83 * here if the register is small or we had vblank interrupts off for
84 * a long time.
85 *
86 * We repeat the hardware vblank counter & timestamp query until
87 * we get consistent results. This to prevent races between gpu
88 * updating its hardware counter while we are retrieving the
89 * corresponding vblank timestamp.
90 */
91 do {
92 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
93 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
94 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
95
96 /* Deal with counter wrap */
97 diff = cur_vblank - dev->vblank[crtc].last;
98 if (cur_vblank < dev->vblank[crtc].last) {
99 diff += dev->max_vblank_count;
100
101 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
102 crtc, dev->vblank[crtc].last, cur_vblank, diff);
103 }
104
105 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
106 crtc, diff);
107
108 /* Reinitialize corresponding vblank timestamp if high-precision query
109 * available. Skip this step if query unsupported or failed. Will
110 * reinitialize delayed at next vblank interrupt in that case.
111 */
112 if (rc) {
113 tslot = atomic_read(&dev->vblank[crtc].count) + diff;
114 vblanktimestamp(dev, crtc, tslot) = t_vblank;
115 }
116
117 smp_mb__before_atomic();
118 atomic_add(diff, &dev->vblank[crtc].count);
119 smp_mb__after_atomic();
120}
121
Mario Kleiner27641c32010-10-23 04:20:23 +0200122/*
Mario Kleiner27641c32010-10-23 04:20:23 +0200123 * Disable vblank irq's on crtc, make sure that last vblank count
124 * of hardware and corresponding consistent software vblank counter
125 * are preserved, even if there are any spurious vblank irq's after
126 * disable.
127 */
128static void vblank_disable_and_save(struct drm_device *dev, int crtc)
129{
130 unsigned long irqflags;
131 u32 vblcount;
132 s64 diff_ns;
133 int vblrc;
134 struct timeval tvblank;
Egbert Eich0355cf32012-10-13 11:36:14 +0000135 int count = DRM_TIMESTAMP_MAXRETRIES;
Mario Kleiner27641c32010-10-23 04:20:23 +0200136
137 /* Prevent vblank irq processing while disabling vblank irqs,
138 * so no updates of timestamps or count can happen after we've
139 * disabled. Needed to prevent races in case of delayed irq's.
Mario Kleiner27641c32010-10-23 04:20:23 +0200140 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200141 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
142
Ville Syrjälä812e7462014-08-06 14:49:48 +0300143 /*
144 * If the vblank interrupt was already disbled update the count
145 * and timestamp to maintain the appearance that the counter
146 * has been ticking all along until this time. This makes the
147 * count account for the entire time between drm_vblank_on() and
148 * drm_vblank_off().
149 */
150 if (!dev->vblank[crtc].enabled) {
151 drm_update_vblank_count(dev, crtc);
152 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
153 return;
154 }
155
Mario Kleiner27641c32010-10-23 04:20:23 +0200156 dev->driver->disable_vblank(dev, crtc);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300157 dev->vblank[crtc].enabled = false;
Mario Kleiner27641c32010-10-23 04:20:23 +0200158
159 /* No further vblank irq's will be processed after
160 * this point. Get current hardware vblank count and
161 * vblank timestamp, repeat until they are consistent.
162 *
163 * FIXME: There is still a race condition here and in
164 * drm_update_vblank_count() which can cause off-by-one
165 * reinitialization of software vblank counter. If gpu
166 * vblank counter doesn't increment exactly at the leading
167 * edge of a vblank interval, then we can lose 1 count if
168 * we happen to execute between start of vblank and the
169 * delayed gpu counter increment.
170 */
171 do {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300172 dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +0200173 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300174 } while (dev->vblank[crtc].last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
Egbert Eich0355cf32012-10-13 11:36:14 +0000175
176 if (!count)
177 vblrc = 0;
Mario Kleiner27641c32010-10-23 04:20:23 +0200178
179 /* Compute time difference to stored timestamp of last vblank
180 * as updated by last invocation of drm_handle_vblank() in vblank irq.
181 */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300182 vblcount = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200183 diff_ns = timeval_to_ns(&tvblank) -
184 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
185
186 /* If there is at least 1 msec difference between the last stored
187 * timestamp and tvblank, then we are currently executing our
188 * disable inside a new vblank interval, the tvblank timestamp
189 * corresponds to this new vblank interval and the irq handler
190 * for this vblank didn't run yet and won't run due to our disable.
191 * Therefore we need to do the job of drm_handle_vblank() and
192 * increment the vblank counter by one to account for this vblank.
193 *
194 * Skip this step if there isn't any high precision timestamp
195 * available. In that case we can't account for this and just
196 * hope for the best.
197 */
Mario Kleinerbc215122011-02-21 05:42:01 +0100198 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300199 atomic_inc(&dev->vblank[crtc].count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100200 smp_mb__after_atomic();
Mario Kleinerbc215122011-02-21 05:42:01 +0100201 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200202
Mario Kleiner27641c32010-10-23 04:20:23 +0200203 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +0200204}
205
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700206static void vblank_disable_fn(unsigned long arg)
207{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200208 struct drm_vblank_crtc *vblank = (void *)arg;
209 struct drm_device *dev = vblank->dev;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700210 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200211 int crtc = vblank->crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700212
213 if (!dev->vblank_disable_allowed)
214 return;
215
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200216 spin_lock_irqsave(&dev->vbl_lock, irqflags);
217 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
218 DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
219 vblank_disable_and_save(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700220 }
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200221 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700222}
223
Daniel Vetterf5752b32014-05-08 16:41:51 +0200224/**
225 * drm_vblank_cleanup - cleanup vblank support
226 * @dev: DRM device
227 *
228 * This function cleans up any resources allocated in drm_vblank_init.
229 */
Keith Packard52440212008-11-18 09:30:25 -0800230void drm_vblank_cleanup(struct drm_device *dev)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700231{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200232 int crtc;
233
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700234 /* Bail if the driver didn't call drm_vblank_init() */
235 if (dev->num_crtcs == 0)
236 return;
237
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200238 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
239 del_timer_sync(&dev->vblank[crtc].disable_timer);
240 vblank_disable_fn((unsigned long)&dev->vblank[crtc]);
241 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700242
Ville Syrjälä5380e922013-10-04 14:53:36 +0300243 kfree(dev->vblank);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700244
245 dev->num_crtcs = 0;
246}
Jerome Glissee77cef92010-01-07 15:39:13 +0100247EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700248
Daniel Vetterf5752b32014-05-08 16:41:51 +0200249/**
250 * drm_vblank_init - initialize vblank support
251 * @dev: drm_device
252 * @num_crtcs: number of crtcs supported by @dev
253 *
254 * This function initializes vblank support for @num_crtcs display pipelines.
255 *
256 * Returns:
257 * Zero on success or a negative error code on failure.
258 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700259int drm_vblank_init(struct drm_device *dev, int num_crtcs)
260{
261 int i, ret = -ENOMEM;
262
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700263 spin_lock_init(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +0200264 spin_lock_init(&dev->vblank_time_lock);
265
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700266 dev->num_crtcs = num_crtcs;
267
Ville Syrjälä5380e922013-10-04 14:53:36 +0300268 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
269 if (!dev->vblank)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700270 goto err;
271
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200272 for (i = 0; i < num_crtcs; i++) {
273 dev->vblank[i].dev = dev;
274 dev->vblank[i].crtc = i;
Ville Syrjälä5380e922013-10-04 14:53:36 +0300275 init_waitqueue_head(&dev->vblank[i].queue);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200276 setup_timer(&dev->vblank[i].disable_timer, vblank_disable_fn,
277 (unsigned long)&dev->vblank[i]);
278 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200279
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100280 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
Mario Kleiner27641c32010-10-23 04:20:23 +0200281
282 /* Driver specific high-precision vblank timestamping supported? */
283 if (dev->driver->get_vblank_timestamp)
284 DRM_INFO("Driver supports precise vblank timestamp query.\n");
285 else
286 DRM_INFO("No driver support for vblank timestamp query.\n");
287
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300288 dev->vblank_disable_allowed = false;
289
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700290 return 0;
291
292err:
293 drm_vblank_cleanup(dev);
294 return ret;
295}
296EXPORT_SYMBOL(drm_vblank_init);
297
Dave Airlie28d52042009-09-21 14:33:58 +1000298static void drm_irq_vgaarb_nokms(void *cookie, bool state)
299{
300 struct drm_device *dev = cookie;
301
302 if (dev->driver->vgaarb_irq) {
303 dev->driver->vgaarb_irq(dev, state);
304 return;
305 }
306
307 if (!dev->irq_enabled)
308 return;
309
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000310 if (state) {
311 if (dev->driver->irq_uninstall)
312 dev->driver->irq_uninstall(dev);
313 } else {
314 if (dev->driver->irq_preinstall)
315 dev->driver->irq_preinstall(dev);
316 if (dev->driver->irq_postinstall)
317 dev->driver->irq_postinstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +1000318 }
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200322 * drm_irq_install - install IRQ handler
323 * @dev: DRM device
324 * @irq: IRQ number to install the handler for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700326 * Initializes the IRQ related data. Installs the handler, calling the driver
Daniel Vetterf5752b32014-05-08 16:41:51 +0200327 * irq_preinstall() and irq_postinstall() functions before and after the
328 * installation.
329 *
330 * This is the simplified helper interface provided for drivers with no special
331 * needs. Drivers which need to install interrupt handlers for multiple
332 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
333 * that vblank interrupts are available.
334 *
335 * Returns:
336 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100338int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200340 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
344 return -EINVAL;
345
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100346 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return -EINVAL;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100350 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Daniel Vettere090c532013-11-03 20:27:05 +0100353 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300355 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100357 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000360 if (dev->driver->irq_preinstall)
361 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700365 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100367 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100368 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700369
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300371 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return ret;
373 }
374
Dave Airlie28d52042009-09-21 14:33:58 +1000375 if (!drm_core_check_feature(dev, DRIVER_MODESET))
376 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
377
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000378 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000379 if (dev->driver->irq_postinstall)
380 ret = dev->driver->irq_postinstall(dev);
381
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700382 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300383 dev->irq_enabled = false;
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000384 if (!drm_core_check_feature(dev, DRIVER_MODESET))
385 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100386 free_irq(irq, dev);
387 } else {
388 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700393EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200396 * drm_irq_uninstall - uninstall the IRQ handler
397 * @dev: DRM device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200399 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
400 * This should only be called by drivers which used drm_irq_install() to set up
401 * their interrupt handler. Other drivers must only reset
402 * drm_device->irq_enabled to false.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200404 * Note that for kernel modesetting drivers it is a bug if this function fails.
405 * The sanity checks are only to catch buggy user modesetting drivers which call
406 * the same function through an ioctl.
407 *
408 * Returns:
409 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200411int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800413 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300414 bool irq_enabled;
415 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
418 return -EINVAL;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300421 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800423 /*
424 * Wake up any waiters so they don't hang.
425 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000426 if (dev->num_crtcs) {
427 spin_lock_irqsave(&dev->vbl_lock, irqflags);
428 for (i = 0; i < dev->num_crtcs; i++) {
Daniel Vetter57ed0f72013-12-11 11:34:43 +0100429 wake_up(&dev->vblank[i].queue);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300430 dev->vblank[i].enabled = false;
431 dev->vblank[i].last =
Ben Skeggsbde48892011-07-04 12:52:27 +1000432 dev->driver->get_vblank_counter(dev, i);
433 }
434 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800435 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800436
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return -EINVAL;
439
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100440 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dave Airlie28d52042009-09-21 14:33:58 +1000442 if (!drm_core_check_feature(dev, DRIVER_MODESET))
443 vga_client_register(dev->pdev, NULL, NULL, NULL);
444
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000445 if (dev->driver->irq_uninstall)
446 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100448 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 return 0;
451}
452EXPORT_SYMBOL(drm_irq_uninstall);
453
Daniel Vetterf5752b32014-05-08 16:41:51 +0200454/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 * IRQ control ioctl.
456 *
457 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000458 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 * \param cmd command.
460 * \param arg user argument, pointing to a drm_control structure.
461 * \return zero on success or a negative number on failure.
462 *
463 * Calls irq_install() or irq_uninstall() according to \p arg.
464 */
Eric Anholtc153f452007-09-03 12:06:45 +1000465int drm_control(struct drm_device *dev, void *data,
466 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Eric Anholtc153f452007-09-03 12:06:45 +1000468 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100469 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470
Mario Kleiner27641c32010-10-23 04:20:23 +0200471 /* if we haven't irq we fallback for compatibility reasons -
472 * this used to be a separate function in drm_dma.h
473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Daniel Vetter22471cf2013-11-03 20:02:50 +0100475 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
476 return 0;
477 if (drm_core_check_feature(dev, DRIVER_MODESET))
478 return 0;
479 /* UMS was only ever support on pci devices. */
480 if (WARN_ON(!dev->pdev))
481 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Eric Anholtc153f452007-09-03 12:06:45 +1000483 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100485 irq = dev->pdev->irq;
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100488 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100490 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100491 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100492 mutex_unlock(&dev->struct_mutex);
493
494 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100496 mutex_lock(&dev->struct_mutex);
497 ret = drm_irq_uninstall(dev);
498 mutex_unlock(&dev->struct_mutex);
499
500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 default:
502 return -EINVAL;
503 }
504}
505
506/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200507 * drm_calc_timestamping_constants - calculate vblank timestamp constants
508 * @crtc: drm_crtc whose timestamp constants should be updated.
509 * @mode: display mode containing the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200510 *
Ville Syrjälä21b21562013-10-26 17:22:52 +0300511 * Calculate and store various constants which are later
512 * needed by vblank and swap-completion timestamping, e.g,
513 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
Daniel Vetterf5752b32014-05-08 16:41:51 +0200514 * derived from CRTC's true scanout timing, so they take
Ville Syrjälä21b21562013-10-26 17:22:52 +0300515 * things like panel scaling or other adjustments into account.
Mario Kleiner27641c32010-10-23 04:20:23 +0200516 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300517void drm_calc_timestamping_constants(struct drm_crtc *crtc,
518 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200519{
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300520 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
Ville Syrjälä77666b72013-10-27 21:22:58 +0200521 int dotclock = mode->crtc_clock;
Mario Kleiner27641c32010-10-23 04:20:23 +0200522
523 /* Valid dotclock? */
524 if (dotclock > 0) {
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300525 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
526
527 /*
528 * Convert scanline length in pixels and video
529 * dot clock to line duration, frame duration
530 * and pixel duration in nanoseconds:
Mario Kleiner27641c32010-10-23 04:20:23 +0200531 */
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300532 pixeldur_ns = 1000000 / dotclock;
533 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
534 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
Ville Syrjäläc0ae24c2013-10-28 19:53:25 +0200535
536 /*
537 * Fields of interlaced scanout modes are only half a frame duration.
538 */
539 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
540 framedur_ns /= 2;
Mario Kleiner27641c32010-10-23 04:20:23 +0200541 } else
542 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
543 crtc->base.id);
544
545 crtc->pixeldur_ns = pixeldur_ns;
546 crtc->linedur_ns = linedur_ns;
547 crtc->framedur_ns = framedur_ns;
548
549 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300550 crtc->base.id, mode->crtc_htotal,
551 mode->crtc_vtotal, mode->crtc_vdisplay);
Mario Kleiner27641c32010-10-23 04:20:23 +0200552 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300553 crtc->base.id, dotclock, framedur_ns,
554 linedur_ns, pixeldur_ns);
Mario Kleiner27641c32010-10-23 04:20:23 +0200555}
556EXPORT_SYMBOL(drm_calc_timestamping_constants);
557
558/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200559 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
560 * @dev: DRM device
561 * @crtc: Which CRTC's vblank timestamp to retrieve
562 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
563 * On return contains true maximum error of timestamp
564 * @vblank_time: Pointer to struct timeval which should receive the timestamp
565 * @flags: Flags to pass to driver:
566 * 0 = Default,
567 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
568 * @refcrtc: CRTC which defines scanout timing
569 * @mode: mode which defines the scanout timings
570 *
571 * Implements calculation of exact vblank timestamps from given drm_display_mode
572 * timings and current video scanout position of a CRTC. This can be called from
573 * within get_vblank_timestamp() implementation of a kms driver to implement the
574 * actual timestamping.
Mario Kleiner27641c32010-10-23 04:20:23 +0200575 *
576 * Should return timestamps conforming to the OML_sync_control OpenML
577 * extension specification. The timestamp corresponds to the end of
578 * the vblank interval, aka start of scanout of topmost-leftmost display
579 * pixel in the following video frame.
580 *
581 * Requires support for optional dev->driver->get_scanout_position()
582 * in kms driver, plus a bit of setup code to provide a drm_display_mode
583 * that corresponds to the true scanout timing.
584 *
585 * The current implementation only handles standard video modes. It
586 * returns as no operation if a doublescan or interlaced video mode is
587 * active. Higher level code is expected to handle this.
588 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200589 * Returns:
590 * Negative value on error, failure or if not supported in current
Mario Kleiner27641c32010-10-23 04:20:23 +0200591 * video mode:
592 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200593 * -EINVAL - Invalid CRTC.
Mario Kleiner27641c32010-10-23 04:20:23 +0200594 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
595 * -ENOTSUPP - Function not supported in current display mode.
596 * -EIO - Failed, e.g., due to failed scanout position query.
597 *
598 * Returns or'ed positive status flags on success:
599 *
600 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
601 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
602 *
603 */
604int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
605 int *max_error,
606 struct timeval *vblank_time,
607 unsigned flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300608 const struct drm_crtc *refcrtc,
609 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200610{
Imre Deake62f2f52012-10-23 18:53:25 +0000611 ktime_t stime, etime, mono_time_offset;
612 struct timeval tv_etime;
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200613 int vbl_status;
Mario Kleiner27641c32010-10-23 04:20:23 +0200614 int vpos, hpos, i;
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300615 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200616 bool invbl;
617
618 if (crtc < 0 || crtc >= dev->num_crtcs) {
619 DRM_ERROR("Invalid crtc %d\n", crtc);
620 return -EINVAL;
621 }
622
623 /* Scanout position query not supported? Should not happen. */
624 if (!dev->driver->get_scanout_position) {
625 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
626 return -EIO;
627 }
628
Mario Kleiner27641c32010-10-23 04:20:23 +0200629 /* Durations of frames, lines, pixels in nanoseconds. */
630 framedur_ns = refcrtc->framedur_ns;
631 linedur_ns = refcrtc->linedur_ns;
632 pixeldur_ns = refcrtc->pixeldur_ns;
633
634 /* If mode timing undefined, just return as no-op:
635 * Happens during initial modesetting of a crtc.
636 */
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200637 if (framedur_ns == 0) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200638 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
639 return -EAGAIN;
640 }
641
Mario Kleiner27641c32010-10-23 04:20:23 +0200642 /* Get current scanout position with system timestamp.
643 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
644 * if single query takes longer than max_error nanoseconds.
645 *
646 * This guarantees a tight bound on maximum error if
647 * code gets preempted or delayed for some reason.
648 */
649 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100650 /*
651 * Get vertical and horizontal scanout position vpos, hpos,
652 * and bounding timestamps stime, etime, pre/post query.
653 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200654 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100655 &hpos, &stime, &etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200656
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100657 /*
658 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
659 * CLOCK_REALTIME is requested.
660 */
Imre Deakc61eef72012-10-23 18:53:26 +0000661 if (!drm_timestamp_monotonic)
662 mono_time_offset = ktime_get_monotonic_offset();
Mario Kleiner27641c32010-10-23 04:20:23 +0200663
Mario Kleiner27641c32010-10-23 04:20:23 +0200664 /* Return as no-op if scanout query unsupported or failed. */
665 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
666 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
667 crtc, vbl_status);
668 return -EIO;
669 }
670
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100671 /* Compute uncertainty in timestamp of scanout position query. */
Imre Deake62f2f52012-10-23 18:53:25 +0000672 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200673
674 /* Accept result with < max_error nsecs timing uncertainty. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300675 if (duration_ns <= *max_error)
Mario Kleiner27641c32010-10-23 04:20:23 +0200676 break;
677 }
678
679 /* Noisy system timing? */
680 if (i == DRM_TIMESTAMP_MAXRETRIES) {
681 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300682 crtc, duration_ns/1000, *max_error/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200683 }
684
685 /* Return upper bound of timestamp precision error. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300686 *max_error = duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200687
688 /* Check if in vblank area:
689 * vpos is >=0 in video scanout area, but negative
690 * within vblank area, counting down the number of lines until
691 * start of scanout.
692 */
693 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
694
695 /* Convert scanout position into elapsed time at raw_time query
696 * since start of scanout at first display scanline. delta_ns
697 * can be negative if start of scanout hasn't happened yet.
698 */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300699 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200700
Imre Deakc61eef72012-10-23 18:53:26 +0000701 if (!drm_timestamp_monotonic)
702 etime = ktime_sub(etime, mono_time_offset);
703
Imre Deake62f2f52012-10-23 18:53:25 +0000704 /* save this only for debugging purposes */
705 tv_etime = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200706 /* Subtract time delta from raw timestamp to get final
707 * vblank_time timestamp for end of vblank.
708 */
Michel Dänzere91abf82013-06-12 11:58:44 +0200709 if (delta_ns < 0)
710 etime = ktime_add_ns(etime, -delta_ns);
711 else
712 etime = ktime_sub_ns(etime, delta_ns);
Imre Deake62f2f52012-10-23 18:53:25 +0000713 *vblank_time = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200714
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700715 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
716 crtc, (int)vbl_status, hpos, vpos,
Imre Deake62f2f52012-10-23 18:53:25 +0000717 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700718 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300719 duration_ns/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200720
721 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
722 if (invbl)
723 vbl_status |= DRM_VBLANKTIME_INVBL;
724
725 return vbl_status;
726}
727EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
728
Imre Deakc61eef72012-10-23 18:53:26 +0000729static struct timeval get_drm_timestamp(void)
730{
731 ktime_t now;
732
733 now = ktime_get();
734 if (!drm_timestamp_monotonic)
735 now = ktime_sub(now, ktime_get_monotonic_offset());
736
737 return ktime_to_timeval(now);
738}
739
Mario Kleiner27641c32010-10-23 04:20:23 +0200740/**
741 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200742 * vblank interval
Mario Kleiner27641c32010-10-23 04:20:23 +0200743 * @dev: DRM device
Daniel Vetterf5752b32014-05-08 16:41:51 +0200744 * @crtc: which CRTC's vblank timestamp to retrieve
Mario Kleiner27641c32010-10-23 04:20:23 +0200745 * @tvblank: Pointer to target struct timeval which should receive the timestamp
746 * @flags: Flags to pass to driver:
Daniel Vetterf5752b32014-05-08 16:41:51 +0200747 * 0 = Default,
748 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
Mario Kleiner27641c32010-10-23 04:20:23 +0200749 *
750 * Fetches the system timestamp corresponding to the time of the most recent
Daniel Vetterf5752b32014-05-08 16:41:51 +0200751 * vblank interval on specified CRTC. May call into kms-driver to
Mario Kleiner27641c32010-10-23 04:20:23 +0200752 * compute the timestamp with a high-precision GPU specific method.
753 *
754 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
755 * call, i.e., it isn't very precisely locked to the true vblank.
756 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200757 * Returns:
758 * Non-zero if timestamp is considered to be very precise, zero otherwise.
Mario Kleiner27641c32010-10-23 04:20:23 +0200759 */
760u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
761 struct timeval *tvblank, unsigned flags)
762{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200763 int ret;
Mario Kleiner27641c32010-10-23 04:20:23 +0200764
765 /* Define requested maximum error on timestamps (nanoseconds). */
766 int max_error = (int) drm_timestamp_precision * 1000;
767
768 /* Query driver if possible and precision timestamping enabled. */
769 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
770 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
771 tvblank, flags);
772 if (ret > 0)
773 return (u32) ret;
774 }
775
776 /* GPU high precision timestamp query unsupported or failed.
Imre Deakc61eef72012-10-23 18:53:26 +0000777 * Return current monotonic/gettimeofday timestamp as best estimate.
Mario Kleiner27641c32010-10-23 04:20:23 +0200778 */
Imre Deakc61eef72012-10-23 18:53:26 +0000779 *tvblank = get_drm_timestamp();
Mario Kleiner27641c32010-10-23 04:20:23 +0200780
781 return 0;
782}
783EXPORT_SYMBOL(drm_get_last_vbltimestamp);
784
785/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700786 * drm_vblank_count - retrieve "cooked" vblank counter value
787 * @dev: DRM device
788 * @crtc: which counter to retrieve
789 *
790 * Fetches the "cooked" vblank count value that represents the number of
791 * vblank events since the system was booted, including lost events due to
792 * modesetting activity.
Daniel Vetterf5752b32014-05-08 16:41:51 +0200793 *
794 * Returns:
795 * The software vblank counter.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700796 */
797u32 drm_vblank_count(struct drm_device *dev, int crtc)
798{
Ville Syrjälä5380e922013-10-04 14:53:36 +0300799 return atomic_read(&dev->vblank[crtc].count);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700800}
801EXPORT_SYMBOL(drm_vblank_count);
802
803/**
Mario Kleiner27641c32010-10-23 04:20:23 +0200804 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
805 * and the system timestamp corresponding to that vblank counter value.
806 *
807 * @dev: DRM device
808 * @crtc: which counter to retrieve
809 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
810 *
811 * Fetches the "cooked" vblank count value that represents the number of
812 * vblank events since the system was booted, including lost events due to
813 * modesetting activity. Returns corresponding system timestamp of the time
Daniel Vetterf5752b32014-05-08 16:41:51 +0200814 * of the vblank interval that corresponds to the current vblank counter value.
Mario Kleiner27641c32010-10-23 04:20:23 +0200815 */
816u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
817 struct timeval *vblanktime)
818{
819 u32 cur_vblank;
820
821 /* Read timestamp from slot of _vblank_time ringbuffer
822 * that corresponds to current vblank count. Retry if
823 * count has incremented during readout. This works like
824 * a seqlock.
825 */
826 do {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300827 cur_vblank = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200828 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
829 smp_rmb();
Ville Syrjälä5380e922013-10-04 14:53:36 +0300830 } while (cur_vblank != atomic_read(&dev->vblank[crtc].count));
Mario Kleiner27641c32010-10-23 04:20:23 +0200831
832 return cur_vblank;
833}
834EXPORT_SYMBOL(drm_vblank_count_and_time);
835
Rob Clarkc6eefa12012-10-16 22:48:40 +0000836static void send_vblank_event(struct drm_device *dev,
837 struct drm_pending_vblank_event *e,
838 unsigned long seq, struct timeval *now)
839{
840 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
841 e->event.sequence = seq;
842 e->event.tv_sec = now->tv_sec;
843 e->event.tv_usec = now->tv_usec;
844
845 list_add_tail(&e->base.link,
846 &e->base.file_priv->event_list);
847 wake_up_interruptible(&e->base.file_priv->event_wait);
848 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
849 e->event.sequence);
850}
851
852/**
853 * drm_send_vblank_event - helper to send vblank event after pageflip
854 * @dev: DRM device
855 * @crtc: CRTC in question
856 * @e: the event to send
857 *
858 * Updates sequence # and timestamp on event, and sends it to userspace.
859 * Caller must hold event lock.
860 */
861void drm_send_vblank_event(struct drm_device *dev, int crtc,
862 struct drm_pending_vblank_event *e)
863{
864 struct timeval now;
865 unsigned int seq;
866 if (crtc >= 0) {
867 seq = drm_vblank_count_and_time(dev, crtc, &now);
868 } else {
869 seq = 0;
Imre Deakc61eef72012-10-23 18:53:26 +0000870
871 now = get_drm_timestamp();
Rob Clarkc6eefa12012-10-16 22:48:40 +0000872 }
Rob Clark21a245d2012-12-10 10:49:46 -0600873 e->pipe = crtc;
Rob Clarkc6eefa12012-10-16 22:48:40 +0000874 send_vblank_event(dev, e, seq, &now);
875}
876EXPORT_SYMBOL(drm_send_vblank_event);
877
Mario Kleiner27641c32010-10-23 04:20:23 +0200878/**
Ville Syrjäläf2752282014-02-19 21:29:49 +0200879 * drm_vblank_enable - enable the vblank interrupt on a CRTC
880 * @dev: DRM device
881 * @crtc: CRTC in question
882 */
883static int drm_vblank_enable(struct drm_device *dev, int crtc)
884{
885 int ret = 0;
886
887 assert_spin_locked(&dev->vbl_lock);
888
889 spin_lock(&dev->vblank_time_lock);
890
891 if (!dev->vblank[crtc].enabled) {
Daniel Vetter1cfb80b2014-05-21 15:11:33 +0200892 /*
893 * Enable vblank irqs under vblank_time_lock protection.
Ville Syrjäläf2752282014-02-19 21:29:49 +0200894 * All vblank count & timestamp updates are held off
895 * until we are done reinitializing master counter and
896 * timestamps. Filtercode in drm_handle_vblank() will
897 * prevent double-accounting of same vblank interval.
898 */
899 ret = dev->driver->enable_vblank(dev, crtc);
900 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
901 if (ret)
902 atomic_dec(&dev->vblank[crtc].refcount);
903 else {
904 dev->vblank[crtc].enabled = true;
905 drm_update_vblank_count(dev, crtc);
906 }
907 }
908
909 spin_unlock(&dev->vblank_time_lock);
910
911 return ret;
912}
913
914/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700915 * drm_vblank_get - get a reference count on vblank events
916 * @dev: DRM device
917 * @crtc: which CRTC to own
918 *
919 * Acquire a reference count on vblank events to avoid having them disabled
920 * while in use.
921 *
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200922 * This is the legacy version of drm_crtc_vblank_get().
923 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200924 * Returns:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700925 * Zero on success, nonzero on failure.
926 */
927int drm_vblank_get(struct drm_device *dev, int crtc)
928{
Peter Hurley97cbc882013-08-20 21:51:12 -0400929 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700930 int ret = 0;
931
932 spin_lock_irqsave(&dev->vbl_lock, irqflags);
933 /* Going from 0->1 means we have to enable interrupts again */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300934 if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) {
Ville Syrjäläf2752282014-02-19 21:29:49 +0200935 ret = drm_vblank_enable(dev, crtc);
Li Peng778c9022009-11-09 12:51:22 +0800936 } else {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300937 if (!dev->vblank[crtc].enabled) {
938 atomic_dec(&dev->vblank[crtc].refcount);
Li Peng778c9022009-11-09 12:51:22 +0800939 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700940 }
941 }
942 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
943
944 return ret;
945}
946EXPORT_SYMBOL(drm_vblank_get);
947
948/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200949 * drm_crtc_vblank_get - get a reference count on vblank events
950 * @crtc: which CRTC to own
951 *
952 * Acquire a reference count on vblank events to avoid having them disabled
953 * while in use.
954 *
955 * This is the native kms version of drm_vblank_off().
956 *
957 * Returns:
958 * Zero on success, nonzero on failure.
959 */
960int drm_crtc_vblank_get(struct drm_crtc *crtc)
961{
962 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
963}
964EXPORT_SYMBOL(drm_crtc_vblank_get);
965
966/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700967 * drm_vblank_put - give up ownership of vblank events
968 * @dev: DRM device
969 * @crtc: which counter to give up
970 *
971 * Release ownership of a given vblank counter, turning off interrupts
Mario Kleiner27641c32010-10-23 04:20:23 +0200972 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200973 *
974 * This is the legacy version of drm_crtc_vblank_put().
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700975 */
976void drm_vblank_put(struct drm_device *dev, int crtc)
977{
Ville Syrjälä5380e922013-10-04 14:53:36 +0300978 BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0);
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000979
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700980 /* Last user schedules interrupt disable */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300981 if (atomic_dec_and_test(&dev->vblank[crtc].refcount) &&
Mario Kleiner27641c32010-10-23 04:20:23 +0200982 (drm_vblank_offdelay > 0))
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200983 mod_timer(&dev->vblank[crtc].disable_timer,
Daniel Vetterbfd83032013-12-11 11:34:41 +0100984 jiffies + ((drm_vblank_offdelay * HZ)/1000));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700985}
986EXPORT_SYMBOL(drm_vblank_put);
987
Rob Clarkc6eefa12012-10-16 22:48:40 +0000988/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +0200989 * drm_crtc_vblank_put - give up ownership of vblank events
990 * @crtc: which counter to give up
991 *
992 * Release ownership of a given vblank counter, turning off interrupts
993 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
994 *
995 * This is the native kms version of drm_vblank_put().
996 */
997void drm_crtc_vblank_put(struct drm_crtc *crtc)
998{
999 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1000}
1001EXPORT_SYMBOL(drm_crtc_vblank_put);
1002
1003/**
Rob Clarkc6eefa12012-10-16 22:48:40 +00001004 * drm_vblank_off - disable vblank events on a CRTC
1005 * @dev: DRM device
1006 * @crtc: CRTC in question
1007 *
Daniel Vetterf5752b32014-05-08 16:41:51 +02001008 * Drivers can use this function to shut down the vblank interrupt handling when
1009 * disabling a crtc. This function ensures that the latest vblank frame count is
1010 * stored so that drm_vblank_on() can restore it again.
1011 *
1012 * Drivers must use this function when the hardware vblank counter can get
1013 * reset, e.g. when suspending.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001014 *
1015 * This is the legacy version of drm_crtc_vblank_off().
Rob Clarkc6eefa12012-10-16 22:48:40 +00001016 */
Li Peng778c9022009-11-09 12:51:22 +08001017void drm_vblank_off(struct drm_device *dev, int crtc)
1018{
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001019 struct drm_pending_vblank_event *e, *t;
1020 struct timeval now;
Li Peng778c9022009-11-09 12:51:22 +08001021 unsigned long irqflags;
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001022 unsigned int seq;
Li Peng778c9022009-11-09 12:51:22 +08001023
1024 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +02001025 vblank_disable_and_save(dev, crtc);
Daniel Vetter57ed0f72013-12-11 11:34:43 +01001026 wake_up(&dev->vblank[crtc].queue);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001027
1028 /* Send any queued vblank events, lest the natives grow disquiet */
1029 seq = drm_vblank_count_and_time(dev, crtc, &now);
Imre Deake7783ba2012-11-02 13:30:50 +02001030
1031 spin_lock(&dev->event_lock);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001032 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1033 if (e->pipe != crtc)
1034 continue;
1035 DRM_DEBUG("Sending premature vblank event on disable: \
1036 wanted %d, current %d\n",
1037 e->event.sequence, seq);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001038 list_del(&e->base.link);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001039 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001040 send_vblank_event(dev, e, seq, &now);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001041 }
Imre Deake7783ba2012-11-02 13:30:50 +02001042 spin_unlock(&dev->event_lock);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +10001043
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001044 /*
1045 * Prevent subsequent drm_vblank_get() from re-enabling
1046 * the vblank interrupt by bumping the refcount.
1047 */
1048 if (!dev->vblank[crtc].inmodeset) {
1049 atomic_inc(&dev->vblank[crtc].refcount);
1050 dev->vblank[crtc].inmodeset = 1;
1051 }
1052
Li Peng778c9022009-11-09 12:51:22 +08001053 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1054}
1055EXPORT_SYMBOL(drm_vblank_off);
1056
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001057/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001058 * drm_crtc_vblank_off - disable vblank events on a CRTC
1059 * @crtc: CRTC in question
1060 *
1061 * Drivers can use this function to shut down the vblank interrupt handling when
1062 * disabling a crtc. This function ensures that the latest vblank frame count is
1063 * stored so that drm_vblank_on can restore it again.
1064 *
1065 * Drivers must use this function when the hardware vblank counter can get
1066 * reset, e.g. when suspending.
1067 *
1068 * This is the native kms version of drm_vblank_off().
1069 */
1070void drm_crtc_vblank_off(struct drm_crtc *crtc)
1071{
1072 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1073}
1074EXPORT_SYMBOL(drm_crtc_vblank_off);
1075
1076/**
Ville Syrjäläf2752282014-02-19 21:29:49 +02001077 * drm_vblank_on - enable vblank events on a CRTC
1078 * @dev: DRM device
1079 * @crtc: CRTC in question
Daniel Vetterf5752b32014-05-08 16:41:51 +02001080 *
1081 * This functions restores the vblank interrupt state captured with
1082 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1083 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1084 * in driver load code to reflect the current hardware state of the crtc.
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001085 *
1086 * This is the legacy version of drm_crtc_vblank_on().
Ville Syrjäläf2752282014-02-19 21:29:49 +02001087 */
1088void drm_vblank_on(struct drm_device *dev, int crtc)
1089{
1090 unsigned long irqflags;
1091
1092 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjälä7ffd7a62014-08-06 14:49:44 +03001093 /* Drop our private "prevent drm_vblank_get" refcount */
1094 if (dev->vblank[crtc].inmodeset) {
1095 atomic_dec(&dev->vblank[crtc].refcount);
1096 dev->vblank[crtc].inmodeset = 0;
1097 }
Ville Syrjäläf8ad0282014-08-06 14:49:49 +03001098
1099 /*
1100 * sample the current counter to avoid random jumps
1101 * when drm_vblank_enable() applies the diff
1102 *
1103 * -1 to make sure user will never see the same
1104 * vblank counter value before and after a modeset
1105 */
1106 dev->vblank[crtc].last =
1107 (dev->driver->get_vblank_counter(dev, crtc) - 1) &
1108 dev->max_vblank_count;
1109
Ville Syrjäläf2752282014-02-19 21:29:49 +02001110 /* re-enable interrupts if there's are users left */
1111 if (atomic_read(&dev->vblank[crtc].refcount) != 0)
1112 WARN_ON(drm_vblank_enable(dev, crtc));
1113 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1114}
1115EXPORT_SYMBOL(drm_vblank_on);
1116
1117/**
Daniel Vetter89dd6a42014-05-15 15:32:12 +02001118 * drm_crtc_vblank_on - enable vblank events on a CRTC
1119 * @crtc: CRTC in question
1120 *
1121 * This functions restores the vblank interrupt state captured with
1122 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1123 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1124 * in driver load code to reflect the current hardware state of the crtc.
1125 *
1126 * This is the native kms version of drm_vblank_on().
1127 */
1128void drm_crtc_vblank_on(struct drm_crtc *crtc)
1129{
1130 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1131}
1132EXPORT_SYMBOL(drm_crtc_vblank_on);
1133
1134/**
Dave Airlief453ba02008-11-07 14:05:41 -08001135 * drm_vblank_pre_modeset - account for vblanks across mode sets
1136 * @dev: DRM device
1137 * @crtc: CRTC in question
Dave Airlief453ba02008-11-07 14:05:41 -08001138 *
1139 * Account for vblank events across mode setting events, which will likely
1140 * reset the hardware frame counter.
Daniel Vetterf5752b32014-05-08 16:41:51 +02001141 *
1142 * This is done by grabbing a temporary vblank reference to ensure that the
1143 * vblank interrupt keeps running across the modeset sequence. With this the
1144 * software-side vblank frame counting will ensure that there are no jumps or
1145 * discontinuities.
1146 *
1147 * Unfortunately this approach is racy and also doesn't work when the vblank
1148 * interrupt stops running, e.g. across system suspend resume. It is therefore
1149 * highly recommended that drivers use the newer drm_vblank_off() and
1150 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1151 * using "cooked" software vblank frame counters and not relying on any hardware
1152 * counters.
1153 *
1154 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1155 * again.
Dave Airlief453ba02008-11-07 14:05:41 -08001156 */
1157void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1158{
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001159 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Jerome Glissee77cef92010-01-07 15:39:13 +01001160 if (!dev->num_crtcs)
1161 return;
Dave Airlief453ba02008-11-07 14:05:41 -08001162 /*
1163 * To avoid all the problems that might happen if interrupts
1164 * were enabled/disabled around or between these calls, we just
1165 * have the kernel take a reference on the CRTC (just once though
1166 * to avoid corrupting the count if multiple, mismatch calls occur),
1167 * so that interrupts remain enabled in the interim.
1168 */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001169 if (!dev->vblank[crtc].inmodeset) {
1170 dev->vblank[crtc].inmodeset = 0x1;
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001171 if (drm_vblank_get(dev, crtc) == 0)
Ville Syrjälä5380e922013-10-04 14:53:36 +03001172 dev->vblank[crtc].inmodeset |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -08001173 }
1174}
1175EXPORT_SYMBOL(drm_vblank_pre_modeset);
1176
Daniel Vetterf5752b32014-05-08 16:41:51 +02001177/**
1178 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1179 * @dev: DRM device
1180 * @crtc: CRTC in question
1181 *
1182 * This function again drops the temporary vblank reference acquired in
1183 * drm_vblank_pre_modeset.
1184 */
Dave Airlief453ba02008-11-07 14:05:41 -08001185void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1186{
1187 unsigned long irqflags;
1188
Huacai Chenb7ea85a42013-05-21 06:23:43 +00001189 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1190 if (!dev->num_crtcs)
1191 return;
1192
Ville Syrjälä5380e922013-10-04 14:53:36 +03001193 if (dev->vblank[crtc].inmodeset) {
Dave Airlief453ba02008-11-07 14:05:41 -08001194 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjäläba0bf122013-10-04 14:53:33 +03001195 dev->vblank_disable_allowed = true;
Dave Airlief453ba02008-11-07 14:05:41 -08001196 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001197
Ville Syrjälä5380e922013-10-04 14:53:36 +03001198 if (dev->vblank[crtc].inmodeset & 0x2)
Chris Wilsonb3f5e732009-02-19 14:48:22 +00001199 drm_vblank_put(dev, crtc);
1200
Ville Syrjälä5380e922013-10-04 14:53:36 +03001201 dev->vblank[crtc].inmodeset = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001202 }
1203}
1204EXPORT_SYMBOL(drm_vblank_post_modeset);
1205
Daniel Vetterf5752b32014-05-08 16:41:51 +02001206/*
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001207 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1208 * @DRM_IOCTL_ARGS: standard ioctl arguments
1209 *
1210 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1211 * ioctls around modesetting so that any lost vblank events are accounted for.
1212 *
1213 * Generally the counter will reset across mode sets. If interrupts are
1214 * enabled around this call, we don't have to do anything since the counter
1215 * will have already been incremented.
1216 */
1217int drm_modeset_ctl(struct drm_device *dev, void *data,
1218 struct drm_file *file_priv)
1219{
1220 struct drm_modeset_ctl *modeset = data;
Dave Airlie19227562011-02-24 08:35:06 +10001221 unsigned int crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001222
1223 /* If drm_vblank_init() hasn't been called yet, just no-op */
1224 if (!dev->num_crtcs)
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001225 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001226
Laurent Pinchart29935552012-05-30 00:58:09 +02001227 /* KMS drivers handle this internally */
1228 if (drm_core_check_feature(dev, DRIVER_MODESET))
1229 return 0;
1230
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001231 crtc = modeset->crtc;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001232 if (crtc >= dev->num_crtcs)
1233 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001234
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001235 switch (modeset->cmd) {
1236 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001237 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001238 break;
1239 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001240 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001241 break;
1242 default:
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001243 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001244 }
1245
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001246 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001247}
1248
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001249static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1250 union drm_wait_vblank *vblwait,
1251 struct drm_file *file_priv)
1252{
1253 struct drm_pending_vblank_event *e;
1254 struct timeval now;
1255 unsigned long flags;
1256 unsigned int seq;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001257 int ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001258
1259 e = kzalloc(sizeof *e, GFP_KERNEL);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001260 if (e == NULL) {
1261 ret = -ENOMEM;
1262 goto err_put;
1263 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001264
1265 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001266 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001267 e->event.base.type = DRM_EVENT_VBLANK;
1268 e->event.base.length = sizeof e->event;
1269 e->event.user_data = vblwait->request.signal;
1270 e->base.event = &e->event.base;
1271 e->base.file_priv = file_priv;
1272 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1273
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001274 spin_lock_irqsave(&dev->event_lock, flags);
1275
1276 if (file_priv->event_space < sizeof e->event) {
Chris Wilsonea5d5522010-12-01 19:41:31 +00001277 ret = -EBUSY;
1278 goto err_unlock;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001279 }
1280
1281 file_priv->event_space -= sizeof e->event;
Mario Kleiner27641c32010-10-23 04:20:23 +02001282 seq = drm_vblank_count_and_time(dev, pipe, &now);
1283
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001284 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1285 (seq - vblwait->request.sequence) <= (1 << 23)) {
1286 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +00001287 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001288 }
1289
1290 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1291 vblwait->request.sequence, seq, pipe);
1292
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001293 trace_drm_vblank_event_queued(current->pid, pipe,
1294 vblwait->request.sequence);
1295
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001296 e->event.sequence = vblwait->request.sequence;
1297 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
Dan Carpenter6f331622010-12-09 08:35:40 +03001298 drm_vblank_put(dev, pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001299 send_vblank_event(dev, e, seq, &now);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001300 vblwait->reply.sequence = seq;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001301 } else {
Ilija Hadzica6778e92011-10-31 13:11:57 -04001302 /* drm_handle_vblank_events will call drm_vblank_put */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001303 list_add_tail(&e->base.link, &dev->vblank_event_list);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001304 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001305 }
1306
1307 spin_unlock_irqrestore(&dev->event_lock, flags);
1308
1309 return 0;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001310
1311err_unlock:
1312 spin_unlock_irqrestore(&dev->event_lock, flags);
1313 kfree(e);
1314err_put:
Dan Carpenter6f331622010-12-09 08:35:40 +03001315 drm_vblank_put(dev, pipe);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001316 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001317}
1318
Daniel Vetterf5752b32014-05-08 16:41:51 +02001319/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * Wait for VBLANK.
1321 *
1322 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001323 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 * \param cmd command.
1325 * \param data user argument, pointing to a drm_wait_vblank structure.
1326 * \return zero on success or a negative number on failure.
1327 *
Eric Anholt30b23632009-01-27 21:19:41 -08001328 * This function enables the vblank interrupt on the pipe requested, then
1329 * sleeps waiting for the requested sequence number to occur, and drops
Daniel Vetterf5752b32014-05-08 16:41:51 +02001330 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
Eric Anholt30b23632009-01-27 21:19:41 -08001331 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001333int drm_wait_vblank(struct drm_device *dev, void *data,
1334 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Eric Anholtc153f452007-09-03 12:06:45 +10001336 union drm_wait_vblank *vblwait = data;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001337 int ret;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001338 unsigned int flags, seq, crtc, high_crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Daniel Vetter49849792013-08-28 15:02:37 +02001340 if (!dev->irq_enabled)
1341 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Eric Anholt30b23632009-01-27 21:19:41 -08001343 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1344 return -EINVAL;
1345
Eric Anholtc153f452007-09-03 12:06:45 +10001346 if (vblwait->request.type &
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001347 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1348 _DRM_VBLANK_HIGH_CRTC_MASK)) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001349 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001350 vblwait->request.type,
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001351 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1352 _DRM_VBLANK_HIGH_CRTC_MASK));
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001353 return -EINVAL;
1354 }
1355
Eric Anholtc153f452007-09-03 12:06:45 +10001356 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001357 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1358 if (high_crtc)
1359 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1360 else
1361 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001362 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001363 return -EINVAL;
1364
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001365 ret = drm_vblank_get(dev, crtc);
1366 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +10001367 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001368 return ret;
1369 }
1370 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001371
Eric Anholtc153f452007-09-03 12:06:45 +10001372 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +10001374 vblwait->request.sequence += seq;
1375 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 case _DRM_VBLANK_ABSOLUTE:
1377 break;
1378 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001379 ret = -EINVAL;
1380 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Ilija Hadzica6778e92011-10-31 13:11:57 -04001383 if (flags & _DRM_VBLANK_EVENT) {
1384 /* must hold on to the vblank ref until the event fires
1385 * drm_vblank_put will be called asynchronously
1386 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001387 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
Ilija Hadzica6778e92011-10-31 13:11:57 -04001388 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001389
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001390 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +10001391 (seq - vblwait->request.sequence) <= (1<<23)) {
1392 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001393 }
1394
Eric Anholt30b23632009-01-27 21:19:41 -08001395 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1396 vblwait->request.sequence, crtc);
Ville Syrjälä5380e922013-10-04 14:53:36 +03001397 dev->vblank[crtc].last_wait = vblwait->request.sequence;
Daniel Vetterbfd83032013-12-11 11:34:41 +01001398 DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * HZ,
Eric Anholt30b23632009-01-27 21:19:41 -08001399 (((drm_vblank_count(dev, crtc) -
1400 vblwait->request.sequence) <= (1 << 23)) ||
Ville Syrjälä3212a222014-03-06 17:27:39 +02001401 !dev->vblank[crtc].enabled ||
Eric Anholt30b23632009-01-27 21:19:41 -08001402 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Eric Anholt30b23632009-01-27 21:19:41 -08001404 if (ret != -EINTR) {
1405 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Mario Kleiner27641c32010-10-23 04:20:23 +02001407 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
Eric Anholt30b23632009-01-27 21:19:41 -08001408 vblwait->reply.tval_sec = now.tv_sec;
1409 vblwait->reply.tval_usec = now.tv_usec;
Mario Kleiner27641c32010-10-23 04:20:23 +02001410
Eric Anholt30b23632009-01-27 21:19:41 -08001411 DRM_DEBUG("returning %d to client\n",
1412 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 } else {
Eric Anholt30b23632009-01-27 21:19:41 -08001414 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001417done:
1418 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return ret;
1420}
1421
Sachin Kamatea7f7ab2012-08-01 17:15:31 +05301422static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001423{
1424 struct drm_pending_vblank_event *e, *t;
1425 struct timeval now;
1426 unsigned long flags;
1427 unsigned int seq;
1428
Mario Kleiner27641c32010-10-23 04:20:23 +02001429 seq = drm_vblank_count_and_time(dev, crtc, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001430
1431 spin_lock_irqsave(&dev->event_lock, flags);
1432
1433 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1434 if (e->pipe != crtc)
1435 continue;
1436 if ((seq - e->event.sequence) > (1<<23))
1437 continue;
1438
1439 DRM_DEBUG("vblank event on %d, current %d\n",
1440 e->event.sequence, seq);
1441
Rob Clarkc6eefa12012-10-16 22:48:40 +00001442 list_del(&e->base.link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001443 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001444 send_vblank_event(dev, e, seq, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001445 }
1446
1447 spin_unlock_irqrestore(&dev->event_lock, flags);
Jesse Barnesac2874b2010-07-01 16:47:31 -07001448
1449 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001453 * drm_handle_vblank - handle a vblank event
1454 * @dev: DRM device
1455 * @crtc: where this event occurred
1456 *
1457 * Drivers should call this routine in their vblank interrupt handlers to
1458 * update the vblank counter and send any signals that may be pending.
1459 */
Chris Wilson78c6e172011-01-31 10:48:04 +00001460bool drm_handle_vblank(struct drm_device *dev, int crtc)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001461{
Mario Kleiner27641c32010-10-23 04:20:23 +02001462 u32 vblcount;
1463 s64 diff_ns;
1464 struct timeval tvblank;
1465 unsigned long irqflags;
1466
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001467 if (!dev->num_crtcs)
Chris Wilson78c6e172011-01-31 10:48:04 +00001468 return false;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001469
Mario Kleiner27641c32010-10-23 04:20:23 +02001470 /* Need timestamp lock to prevent concurrent execution with
1471 * vblank enable/disable, as this would cause inconsistent
1472 * or corrupted timestamps and vblank counts.
1473 */
1474 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1475
1476 /* Vblank irq handling disabled. Nothing to do. */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001477 if (!dev->vblank[crtc].enabled) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001478 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001479 return false;
Mario Kleiner27641c32010-10-23 04:20:23 +02001480 }
1481
1482 /* Fetch corresponding timestamp for this vblank interval from
1483 * driver and store it in proper slot of timestamp ringbuffer.
1484 */
1485
1486 /* Get current timestamp and count. */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001487 vblcount = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +02001488 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1489
1490 /* Compute time difference to timestamp of last vblank */
1491 diff_ns = timeval_to_ns(&tvblank) -
1492 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1493
1494 /* Update vblank timestamp and count if at least
1495 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1496 * difference between last stored timestamp and current
1497 * timestamp. A smaller difference means basically
1498 * identical timestamps. Happens if this vblank has
1499 * been already processed and this is a redundant call,
1500 * e.g., due to spurious vblank interrupts. We need to
1501 * ignore those for accounting.
1502 */
Mario Kleinerc4cc3832011-02-21 05:42:00 +01001503 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001504 /* Store new timestamp in ringbuffer. */
1505 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
Mario Kleiner27641c32010-10-23 04:20:23 +02001506
1507 /* Increment cooked vblank count. This also atomically commits
1508 * the timestamp computed above.
1509 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001510 smp_mb__before_atomic();
Ville Syrjälä5380e922013-10-04 14:53:36 +03001511 atomic_inc(&dev->vblank[crtc].count);
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001512 smp_mb__after_atomic();
Mario Kleiner27641c32010-10-23 04:20:23 +02001513 } else {
1514 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1515 crtc, (int) diff_ns);
1516 }
1517
Daniel Vetter57ed0f72013-12-11 11:34:43 +01001518 wake_up(&dev->vblank[crtc].queue);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001519 drm_handle_vblank_events(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +02001520
1521 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001522 return true;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001523}
1524EXPORT_SYMBOL(drm_handle_vblank);