blob: 13d671ed3421443ad341d73749bd176642a2995a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_irq.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IRQ support
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drmP.h>
Jesse Barnesac2874b2010-07-01 16:47:31 -070037#include "drm_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <linux/interrupt.h> /* For task queue support */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Dave Airlie28d52042009-09-21 14:33:58 +100042#include <linux/vgaarb.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040043#include <linux/export.h>
Mario Kleiner27641c32010-10-23 04:20:23 +020044
45/* Access macro for slots in vblank timestamp ringbuffer. */
Ville Syrjälä5380e922013-10-04 14:53:36 +030046#define vblanktimestamp(dev, crtc, count) \
47 ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
Mario Kleiner27641c32010-10-23 04:20:23 +020048
49/* Retry timestamp calculation up to 3 times to satisfy
50 * drm_timestamp_precision before giving up.
51 */
52#define DRM_TIMESTAMP_MAXRETRIES 3
53
54/* Threshold in nanoseconds for detection of redundant
55 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
56 */
57#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
58
Mario Kleiner27641c32010-10-23 04:20:23 +020059/*
60 * Clear vblank timestamp buffer for a crtc.
61 */
62static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
63{
Ville Syrjälä5380e922013-10-04 14:53:36 +030064 memset(dev->vblank[crtc].time, 0, sizeof(dev->vblank[crtc].time));
Mario Kleiner27641c32010-10-23 04:20:23 +020065}
66
67/*
68 * Disable vblank irq's on crtc, make sure that last vblank count
69 * of hardware and corresponding consistent software vblank counter
70 * are preserved, even if there are any spurious vblank irq's after
71 * disable.
72 */
73static void vblank_disable_and_save(struct drm_device *dev, int crtc)
74{
75 unsigned long irqflags;
76 u32 vblcount;
77 s64 diff_ns;
78 int vblrc;
79 struct timeval tvblank;
Egbert Eich0355cf32012-10-13 11:36:14 +000080 int count = DRM_TIMESTAMP_MAXRETRIES;
Mario Kleiner27641c32010-10-23 04:20:23 +020081
82 /* Prevent vblank irq processing while disabling vblank irqs,
83 * so no updates of timestamps or count can happen after we've
84 * disabled. Needed to prevent races in case of delayed irq's.
Mario Kleiner27641c32010-10-23 04:20:23 +020085 */
Mario Kleiner27641c32010-10-23 04:20:23 +020086 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
87
88 dev->driver->disable_vblank(dev, crtc);
Ville Syrjälä5380e922013-10-04 14:53:36 +030089 dev->vblank[crtc].enabled = false;
Mario Kleiner27641c32010-10-23 04:20:23 +020090
91 /* No further vblank irq's will be processed after
92 * this point. Get current hardware vblank count and
93 * vblank timestamp, repeat until they are consistent.
94 *
95 * FIXME: There is still a race condition here and in
96 * drm_update_vblank_count() which can cause off-by-one
97 * reinitialization of software vblank counter. If gpu
98 * vblank counter doesn't increment exactly at the leading
99 * edge of a vblank interval, then we can lose 1 count if
100 * we happen to execute between start of vblank and the
101 * delayed gpu counter increment.
102 */
103 do {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300104 dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +0200105 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300106 } while (dev->vblank[crtc].last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
Egbert Eich0355cf32012-10-13 11:36:14 +0000107
108 if (!count)
109 vblrc = 0;
Mario Kleiner27641c32010-10-23 04:20:23 +0200110
111 /* Compute time difference to stored timestamp of last vblank
112 * as updated by last invocation of drm_handle_vblank() in vblank irq.
113 */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300114 vblcount = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200115 diff_ns = timeval_to_ns(&tvblank) -
116 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
117
118 /* If there is at least 1 msec difference between the last stored
119 * timestamp and tvblank, then we are currently executing our
120 * disable inside a new vblank interval, the tvblank timestamp
121 * corresponds to this new vblank interval and the irq handler
122 * for this vblank didn't run yet and won't run due to our disable.
123 * Therefore we need to do the job of drm_handle_vblank() and
124 * increment the vblank counter by one to account for this vblank.
125 *
126 * Skip this step if there isn't any high precision timestamp
127 * available. In that case we can't account for this and just
128 * hope for the best.
129 */
Mario Kleinerbc215122011-02-21 05:42:01 +0100130 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300131 atomic_inc(&dev->vblank[crtc].count);
Mario Kleinerbc215122011-02-21 05:42:01 +0100132 smp_mb__after_atomic_inc();
133 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200134
135 /* Invalidate all timestamps while vblank irq's are off. */
136 clear_vblank_timestamps(dev, crtc);
137
138 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +0200139}
140
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700141static void vblank_disable_fn(unsigned long arg)
142{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200143 struct drm_vblank_crtc *vblank = (void *)arg;
144 struct drm_device *dev = vblank->dev;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700145 unsigned long irqflags;
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200146 int crtc = vblank->crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700147
148 if (!dev->vblank_disable_allowed)
149 return;
150
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200151 spin_lock_irqsave(&dev->vbl_lock, irqflags);
152 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
153 DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
154 vblank_disable_and_save(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700155 }
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200156 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700157}
158
Keith Packard52440212008-11-18 09:30:25 -0800159void drm_vblank_cleanup(struct drm_device *dev)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700160{
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200161 int crtc;
162
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700163 /* Bail if the driver didn't call drm_vblank_init() */
164 if (dev->num_crtcs == 0)
165 return;
166
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200167 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
168 del_timer_sync(&dev->vblank[crtc].disable_timer);
169 vblank_disable_fn((unsigned long)&dev->vblank[crtc]);
170 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700171
Ville Syrjälä5380e922013-10-04 14:53:36 +0300172 kfree(dev->vblank);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700173
174 dev->num_crtcs = 0;
175}
Jerome Glissee77cef92010-01-07 15:39:13 +0100176EXPORT_SYMBOL(drm_vblank_cleanup);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700177
178int drm_vblank_init(struct drm_device *dev, int num_crtcs)
179{
180 int i, ret = -ENOMEM;
181
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700182 spin_lock_init(&dev->vbl_lock);
Mario Kleiner27641c32010-10-23 04:20:23 +0200183 spin_lock_init(&dev->vblank_time_lock);
184
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700185 dev->num_crtcs = num_crtcs;
186
Ville Syrjälä5380e922013-10-04 14:53:36 +0300187 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
188 if (!dev->vblank)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700189 goto err;
190
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200191 for (i = 0; i < num_crtcs; i++) {
192 dev->vblank[i].dev = dev;
193 dev->vblank[i].crtc = i;
Ville Syrjälä5380e922013-10-04 14:53:36 +0300194 init_waitqueue_head(&dev->vblank[i].queue);
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200195 setup_timer(&dev->vblank[i].disable_timer, vblank_disable_fn,
196 (unsigned long)&dev->vblank[i]);
197 }
Mario Kleiner27641c32010-10-23 04:20:23 +0200198
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100199 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
Mario Kleiner27641c32010-10-23 04:20:23 +0200200
201 /* Driver specific high-precision vblank timestamping supported? */
202 if (dev->driver->get_vblank_timestamp)
203 DRM_INFO("Driver supports precise vblank timestamp query.\n");
204 else
205 DRM_INFO("No driver support for vblank timestamp query.\n");
206
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300207 dev->vblank_disable_allowed = false;
208
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700209 return 0;
210
211err:
212 drm_vblank_cleanup(dev);
213 return ret;
214}
215EXPORT_SYMBOL(drm_vblank_init);
216
Dave Airlie28d52042009-09-21 14:33:58 +1000217static void drm_irq_vgaarb_nokms(void *cookie, bool state)
218{
219 struct drm_device *dev = cookie;
220
221 if (dev->driver->vgaarb_irq) {
222 dev->driver->vgaarb_irq(dev, state);
223 return;
224 }
225
226 if (!dev->irq_enabled)
227 return;
228
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000229 if (state) {
230 if (dev->driver->irq_uninstall)
231 dev->driver->irq_uninstall(dev);
232 } else {
233 if (dev->driver->irq_preinstall)
234 dev->driver->irq_preinstall(dev);
235 if (dev->driver->irq_postinstall)
236 dev->driver->irq_postinstall(dev);
Dave Airlie28d52042009-09-21 14:33:58 +1000237 }
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/**
241 * Install IRQ handler.
242 *
243 * \param dev DRM device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700245 * Initializes the IRQ related data. Installs the handler, calling the driver
Sascha Hauer884a53e2012-02-29 09:06:21 +0100246 * \c irq_preinstall() and \c irq_postinstall() functions
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 * before and after the installation.
248 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100249int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100251 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
255 return -EINVAL;
256
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100257 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return -EINVAL;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100261 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Daniel Vettere090c532013-11-03 20:27:05 +0100264 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300266 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100268 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000271 if (dev->driver->irq_preinstall)
272 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700276 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100278 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100279 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700280
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300282 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return ret;
284 }
285
Dave Airlie28d52042009-09-21 14:33:58 +1000286 if (!drm_core_check_feature(dev, DRIVER_MODESET))
287 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
288
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000290 if (dev->driver->irq_postinstall)
291 ret = dev->driver->irq_postinstall(dev);
292
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700293 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300294 dev->irq_enabled = false;
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000295 if (!drm_core_check_feature(dev, DRIVER_MODESET))
296 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100297 free_irq(irq, dev);
298 } else {
299 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700302 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700304EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/**
307 * Uninstall the IRQ handler.
308 *
309 * \param dev DRM device.
310 *
Sascha Hauer884a53e2012-02-29 09:06:21 +0100311 * Calls the driver's \c irq_uninstall() function, and stops the irq.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200313int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800315 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300316 bool irq_enabled;
317 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
320 return -EINVAL;
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300323 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800325 /*
326 * Wake up any waiters so they don't hang.
327 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000328 if (dev->num_crtcs) {
329 spin_lock_irqsave(&dev->vbl_lock, irqflags);
330 for (i = 0; i < dev->num_crtcs; i++) {
Daniel Vetter57ed0f72013-12-11 11:34:43 +0100331 wake_up(&dev->vblank[i].queue);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300332 dev->vblank[i].enabled = false;
333 dev->vblank[i].last =
Ben Skeggsbde48892011-07-04 12:52:27 +1000334 dev->driver->get_vblank_counter(dev, i);
335 }
336 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800337 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800338
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return -EINVAL;
341
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100342 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Dave Airlie28d52042009-09-21 14:33:58 +1000344 if (!drm_core_check_feature(dev, DRIVER_MODESET))
345 vga_client_register(dev->pdev, NULL, NULL, NULL);
346
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000347 if (dev->driver->irq_uninstall)
348 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100350 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 return 0;
353}
354EXPORT_SYMBOL(drm_irq_uninstall);
355
356/**
357 * IRQ control ioctl.
358 *
359 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000360 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 * \param cmd command.
362 * \param arg user argument, pointing to a drm_control structure.
363 * \return zero on success or a negative number on failure.
364 *
365 * Calls irq_install() or irq_uninstall() according to \p arg.
366 */
Eric Anholtc153f452007-09-03 12:06:45 +1000367int drm_control(struct drm_device *dev, void *data,
368 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Eric Anholtc153f452007-09-03 12:06:45 +1000370 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100371 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372
Mario Kleiner27641c32010-10-23 04:20:23 +0200373 /* if we haven't irq we fallback for compatibility reasons -
374 * this used to be a separate function in drm_dma.h
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Daniel Vetter22471cf2013-11-03 20:02:50 +0100377 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
378 return 0;
379 if (drm_core_check_feature(dev, DRIVER_MODESET))
380 return 0;
381 /* UMS was only ever support on pci devices. */
382 if (WARN_ON(!dev->pdev))
383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Eric Anholtc153f452007-09-03 12:06:45 +1000385 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100387 irq = dev->pdev->irq;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100390 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100392 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100393 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100394 mutex_unlock(&dev->struct_mutex);
395
396 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100398 mutex_lock(&dev->struct_mutex);
399 ret = drm_irq_uninstall(dev);
400 mutex_unlock(&dev->struct_mutex);
401
402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 default:
404 return -EINVAL;
405 }
406}
407
408/**
Ville Syrjälä21b21562013-10-26 17:22:52 +0300409 * drm_calc_timestamping_constants - Calculate vblank timestamp constants
Mario Kleiner27641c32010-10-23 04:20:23 +0200410 *
411 * @crtc drm_crtc whose timestamp constants should be updated.
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300412 * @mode display mode containing the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200413 *
Ville Syrjälä21b21562013-10-26 17:22:52 +0300414 * Calculate and store various constants which are later
415 * needed by vblank and swap-completion timestamping, e.g,
416 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
417 * derived from crtc's true scanout timing, so they take
418 * things like panel scaling or other adjustments into account.
Mario Kleiner27641c32010-10-23 04:20:23 +0200419 */
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300420void drm_calc_timestamping_constants(struct drm_crtc *crtc,
421 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200422{
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300423 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
Ville Syrjälä77666b72013-10-27 21:22:58 +0200424 int dotclock = mode->crtc_clock;
Mario Kleiner27641c32010-10-23 04:20:23 +0200425
426 /* Valid dotclock? */
427 if (dotclock > 0) {
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300428 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
429
430 /*
431 * Convert scanline length in pixels and video
432 * dot clock to line duration, frame duration
433 * and pixel duration in nanoseconds:
Mario Kleiner27641c32010-10-23 04:20:23 +0200434 */
Ville Syrjälä0dae35a2013-10-26 17:11:01 +0300435 pixeldur_ns = 1000000 / dotclock;
436 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
437 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
Ville Syrjäläc0ae24c2013-10-28 19:53:25 +0200438
439 /*
440 * Fields of interlaced scanout modes are only half a frame duration.
441 */
442 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
443 framedur_ns /= 2;
Mario Kleiner27641c32010-10-23 04:20:23 +0200444 } else
445 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
446 crtc->base.id);
447
448 crtc->pixeldur_ns = pixeldur_ns;
449 crtc->linedur_ns = linedur_ns;
450 crtc->framedur_ns = framedur_ns;
451
452 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
Ville Syrjälä545cdd52013-10-26 17:16:30 +0300453 crtc->base.id, mode->crtc_htotal,
454 mode->crtc_vtotal, mode->crtc_vdisplay);
Mario Kleiner27641c32010-10-23 04:20:23 +0200455 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300456 crtc->base.id, dotclock, framedur_ns,
457 linedur_ns, pixeldur_ns);
Mario Kleiner27641c32010-10-23 04:20:23 +0200458}
459EXPORT_SYMBOL(drm_calc_timestamping_constants);
460
461/**
462 * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
463 * drivers. Implements calculation of exact vblank timestamps from
464 * given drm_display_mode timings and current video scanout position
465 * of a crtc. This can be called from within get_vblank_timestamp()
466 * implementation of a kms driver to implement the actual timestamping.
467 *
468 * Should return timestamps conforming to the OML_sync_control OpenML
469 * extension specification. The timestamp corresponds to the end of
470 * the vblank interval, aka start of scanout of topmost-leftmost display
471 * pixel in the following video frame.
472 *
473 * Requires support for optional dev->driver->get_scanout_position()
474 * in kms driver, plus a bit of setup code to provide a drm_display_mode
475 * that corresponds to the true scanout timing.
476 *
477 * The current implementation only handles standard video modes. It
478 * returns as no operation if a doublescan or interlaced video mode is
479 * active. Higher level code is expected to handle this.
480 *
481 * @dev: DRM device.
482 * @crtc: Which crtc's vblank timestamp to retrieve.
483 * @max_error: Desired maximum allowable error in timestamps (nanosecs).
484 * On return contains true maximum error of timestamp.
485 * @vblank_time: Pointer to struct timeval which should receive the timestamp.
486 * @flags: Flags to pass to driver:
487 * 0 = Default.
488 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
489 * @refcrtc: drm_crtc* of crtc which defines scanout timing.
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300490 * @mode: mode which defines the scanout timings
Mario Kleiner27641c32010-10-23 04:20:23 +0200491 *
492 * Returns negative value on error, failure or if not supported in current
493 * video mode:
494 *
495 * -EINVAL - Invalid crtc.
496 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
497 * -ENOTSUPP - Function not supported in current display mode.
498 * -EIO - Failed, e.g., due to failed scanout position query.
499 *
500 * Returns or'ed positive status flags on success:
501 *
502 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
503 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
504 *
505 */
506int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
507 int *max_error,
508 struct timeval *vblank_time,
509 unsigned flags,
Ville Syrjälä7da903e2013-10-26 17:57:31 +0300510 const struct drm_crtc *refcrtc,
511 const struct drm_display_mode *mode)
Mario Kleiner27641c32010-10-23 04:20:23 +0200512{
Imre Deake62f2f52012-10-23 18:53:25 +0000513 ktime_t stime, etime, mono_time_offset;
514 struct timeval tv_etime;
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200515 int vbl_status;
Mario Kleiner27641c32010-10-23 04:20:23 +0200516 int vpos, hpos, i;
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300517 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200518 bool invbl;
519
520 if (crtc < 0 || crtc >= dev->num_crtcs) {
521 DRM_ERROR("Invalid crtc %d\n", crtc);
522 return -EINVAL;
523 }
524
525 /* Scanout position query not supported? Should not happen. */
526 if (!dev->driver->get_scanout_position) {
527 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
528 return -EIO;
529 }
530
Mario Kleiner27641c32010-10-23 04:20:23 +0200531 /* Durations of frames, lines, pixels in nanoseconds. */
532 framedur_ns = refcrtc->framedur_ns;
533 linedur_ns = refcrtc->linedur_ns;
534 pixeldur_ns = refcrtc->pixeldur_ns;
535
536 /* If mode timing undefined, just return as no-op:
537 * Happens during initial modesetting of a crtc.
538 */
Ville Syrjälä8072bfa2013-10-28 21:22:52 +0200539 if (framedur_ns == 0) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200540 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
541 return -EAGAIN;
542 }
543
Mario Kleiner27641c32010-10-23 04:20:23 +0200544 /* Get current scanout position with system timestamp.
545 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
546 * if single query takes longer than max_error nanoseconds.
547 *
548 * This guarantees a tight bound on maximum error if
549 * code gets preempted or delayed for some reason.
550 */
551 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100552 /*
553 * Get vertical and horizontal scanout position vpos, hpos,
554 * and bounding timestamps stime, etime, pre/post query.
555 */
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200556 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100557 &hpos, &stime, &etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200558
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100559 /*
560 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
561 * CLOCK_REALTIME is requested.
562 */
Imre Deakc61eef72012-10-23 18:53:26 +0000563 if (!drm_timestamp_monotonic)
564 mono_time_offset = ktime_get_monotonic_offset();
Mario Kleiner27641c32010-10-23 04:20:23 +0200565
Mario Kleiner27641c32010-10-23 04:20:23 +0200566 /* Return as no-op if scanout query unsupported or failed. */
567 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
568 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
569 crtc, vbl_status);
570 return -EIO;
571 }
572
Mario Kleiner8f6fce02013-10-30 05:13:06 +0100573 /* Compute uncertainty in timestamp of scanout position query. */
Imre Deake62f2f52012-10-23 18:53:25 +0000574 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200575
576 /* Accept result with < max_error nsecs timing uncertainty. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300577 if (duration_ns <= *max_error)
Mario Kleiner27641c32010-10-23 04:20:23 +0200578 break;
579 }
580
581 /* Noisy system timing? */
582 if (i == DRM_TIMESTAMP_MAXRETRIES) {
583 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300584 crtc, duration_ns/1000, *max_error/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200585 }
586
587 /* Return upper bound of timestamp precision error. */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300588 *max_error = duration_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200589
590 /* Check if in vblank area:
591 * vpos is >=0 in video scanout area, but negative
592 * within vblank area, counting down the number of lines until
593 * start of scanout.
594 */
595 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
596
597 /* Convert scanout position into elapsed time at raw_time query
598 * since start of scanout at first display scanline. delta_ns
599 * can be negative if start of scanout hasn't happened yet.
600 */
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300601 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
Mario Kleiner27641c32010-10-23 04:20:23 +0200602
Imre Deakc61eef72012-10-23 18:53:26 +0000603 if (!drm_timestamp_monotonic)
604 etime = ktime_sub(etime, mono_time_offset);
605
Imre Deake62f2f52012-10-23 18:53:25 +0000606 /* save this only for debugging purposes */
607 tv_etime = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200608 /* Subtract time delta from raw timestamp to get final
609 * vblank_time timestamp for end of vblank.
610 */
Michel Dänzere91abf82013-06-12 11:58:44 +0200611 if (delta_ns < 0)
612 etime = ktime_add_ns(etime, -delta_ns);
613 else
614 etime = ktime_sub_ns(etime, delta_ns);
Imre Deake62f2f52012-10-23 18:53:25 +0000615 *vblank_time = ktime_to_timeval(etime);
Mario Kleiner27641c32010-10-23 04:20:23 +0200616
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700617 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
618 crtc, (int)vbl_status, hpos, vpos,
Imre Deake62f2f52012-10-23 18:53:25 +0000619 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
Joe Perchesbbb0aef2011-04-17 20:35:52 -0700620 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
Ville Syrjälä3c184f62013-10-26 17:38:52 +0300621 duration_ns/1000, i);
Mario Kleiner27641c32010-10-23 04:20:23 +0200622
623 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
624 if (invbl)
625 vbl_status |= DRM_VBLANKTIME_INVBL;
626
627 return vbl_status;
628}
629EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
630
Imre Deakc61eef72012-10-23 18:53:26 +0000631static struct timeval get_drm_timestamp(void)
632{
633 ktime_t now;
634
635 now = ktime_get();
636 if (!drm_timestamp_monotonic)
637 now = ktime_sub(now, ktime_get_monotonic_offset());
638
639 return ktime_to_timeval(now);
640}
641
Mario Kleiner27641c32010-10-23 04:20:23 +0200642/**
643 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
644 * vblank interval.
645 *
646 * @dev: DRM device
647 * @crtc: which crtc's vblank timestamp to retrieve
648 * @tvblank: Pointer to target struct timeval which should receive the timestamp
649 * @flags: Flags to pass to driver:
650 * 0 = Default.
651 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
652 *
653 * Fetches the system timestamp corresponding to the time of the most recent
654 * vblank interval on specified crtc. May call into kms-driver to
655 * compute the timestamp with a high-precision GPU specific method.
656 *
657 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
658 * call, i.e., it isn't very precisely locked to the true vblank.
659 *
660 * Returns non-zero if timestamp is considered to be very precise.
661 */
662u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
663 struct timeval *tvblank, unsigned flags)
664{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200665 int ret;
Mario Kleiner27641c32010-10-23 04:20:23 +0200666
667 /* Define requested maximum error on timestamps (nanoseconds). */
668 int max_error = (int) drm_timestamp_precision * 1000;
669
670 /* Query driver if possible and precision timestamping enabled. */
671 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
672 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
673 tvblank, flags);
674 if (ret > 0)
675 return (u32) ret;
676 }
677
678 /* GPU high precision timestamp query unsupported or failed.
Imre Deakc61eef72012-10-23 18:53:26 +0000679 * Return current monotonic/gettimeofday timestamp as best estimate.
Mario Kleiner27641c32010-10-23 04:20:23 +0200680 */
Imre Deakc61eef72012-10-23 18:53:26 +0000681 *tvblank = get_drm_timestamp();
Mario Kleiner27641c32010-10-23 04:20:23 +0200682
683 return 0;
684}
685EXPORT_SYMBOL(drm_get_last_vbltimestamp);
686
687/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700688 * drm_vblank_count - retrieve "cooked" vblank counter value
689 * @dev: DRM device
690 * @crtc: which counter to retrieve
691 *
692 * Fetches the "cooked" vblank count value that represents the number of
693 * vblank events since the system was booted, including lost events due to
694 * modesetting activity.
695 */
696u32 drm_vblank_count(struct drm_device *dev, int crtc)
697{
Ville Syrjälä5380e922013-10-04 14:53:36 +0300698 return atomic_read(&dev->vblank[crtc].count);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700699}
700EXPORT_SYMBOL(drm_vblank_count);
701
702/**
Mario Kleiner27641c32010-10-23 04:20:23 +0200703 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
704 * and the system timestamp corresponding to that vblank counter value.
705 *
706 * @dev: DRM device
707 * @crtc: which counter to retrieve
708 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
709 *
710 * Fetches the "cooked" vblank count value that represents the number of
711 * vblank events since the system was booted, including lost events due to
712 * modesetting activity. Returns corresponding system timestamp of the time
713 * of the vblank interval that corresponds to the current value vblank counter
714 * value.
715 */
716u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
717 struct timeval *vblanktime)
718{
719 u32 cur_vblank;
720
721 /* Read timestamp from slot of _vblank_time ringbuffer
722 * that corresponds to current vblank count. Retry if
723 * count has incremented during readout. This works like
724 * a seqlock.
725 */
726 do {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300727 cur_vblank = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +0200728 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
729 smp_rmb();
Ville Syrjälä5380e922013-10-04 14:53:36 +0300730 } while (cur_vblank != atomic_read(&dev->vblank[crtc].count));
Mario Kleiner27641c32010-10-23 04:20:23 +0200731
732 return cur_vblank;
733}
734EXPORT_SYMBOL(drm_vblank_count_and_time);
735
Rob Clarkc6eefa12012-10-16 22:48:40 +0000736static void send_vblank_event(struct drm_device *dev,
737 struct drm_pending_vblank_event *e,
738 unsigned long seq, struct timeval *now)
739{
740 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
741 e->event.sequence = seq;
742 e->event.tv_sec = now->tv_sec;
743 e->event.tv_usec = now->tv_usec;
744
745 list_add_tail(&e->base.link,
746 &e->base.file_priv->event_list);
747 wake_up_interruptible(&e->base.file_priv->event_wait);
748 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
749 e->event.sequence);
750}
751
752/**
753 * drm_send_vblank_event - helper to send vblank event after pageflip
754 * @dev: DRM device
755 * @crtc: CRTC in question
756 * @e: the event to send
757 *
758 * Updates sequence # and timestamp on event, and sends it to userspace.
759 * Caller must hold event lock.
760 */
761void drm_send_vblank_event(struct drm_device *dev, int crtc,
762 struct drm_pending_vblank_event *e)
763{
764 struct timeval now;
765 unsigned int seq;
766 if (crtc >= 0) {
767 seq = drm_vblank_count_and_time(dev, crtc, &now);
768 } else {
769 seq = 0;
Imre Deakc61eef72012-10-23 18:53:26 +0000770
771 now = get_drm_timestamp();
Rob Clarkc6eefa12012-10-16 22:48:40 +0000772 }
Rob Clark21a245d2012-12-10 10:49:46 -0600773 e->pipe = crtc;
Rob Clarkc6eefa12012-10-16 22:48:40 +0000774 send_vblank_event(dev, e, seq, &now);
775}
776EXPORT_SYMBOL(drm_send_vblank_event);
777
Mario Kleiner27641c32010-10-23 04:20:23 +0200778/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700779 * drm_update_vblank_count - update the master vblank counter
780 * @dev: DRM device
781 * @crtc: counter to update
782 *
783 * Call back into the driver to update the appropriate vblank counter
784 * (specified by @crtc). Deal with wraparound, if it occurred, and
785 * update the last read value so we can deal with wraparound on the next
786 * call if necessary.
787 *
788 * Only necessary when going from off->on, to account for frames we
789 * didn't get an interrupt for.
790 *
791 * Note: caller must hold dev->vbl_lock since this reads & writes
792 * device vblank fields.
793 */
794static void drm_update_vblank_count(struct drm_device *dev, int crtc)
795{
Mario Kleiner27641c32010-10-23 04:20:23 +0200796 u32 cur_vblank, diff, tslot, rc;
797 struct timeval t_vblank;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700798
799 /*
800 * Interrupts were disabled prior to this call, so deal with counter
801 * wrap if needed.
802 * NOTE! It's possible we lost a full dev->max_vblank_count events
803 * here if the register is small or we had vblank interrupts off for
804 * a long time.
Mario Kleiner27641c32010-10-23 04:20:23 +0200805 *
806 * We repeat the hardware vblank counter & timestamp query until
807 * we get consistent results. This to prevent races between gpu
808 * updating its hardware counter while we are retrieving the
809 * corresponding vblank timestamp.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700810 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200811 do {
812 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
813 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
814 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
815
816 /* Deal with counter wrap */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300817 diff = cur_vblank - dev->vblank[crtc].last;
818 if (cur_vblank < dev->vblank[crtc].last) {
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700819 diff += dev->max_vblank_count;
820
821 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
Ville Syrjälä5380e922013-10-04 14:53:36 +0300822 crtc, dev->vblank[crtc].last, cur_vblank, diff);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700823 }
824
825 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
826 crtc, diff);
827
Mario Kleiner27641c32010-10-23 04:20:23 +0200828 /* Reinitialize corresponding vblank timestamp if high-precision query
829 * available. Skip this step if query unsupported or failed. Will
830 * reinitialize delayed at next vblank interrupt in that case.
831 */
832 if (rc) {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300833 tslot = atomic_read(&dev->vblank[crtc].count) + diff;
Mario Kleiner27641c32010-10-23 04:20:23 +0200834 vblanktimestamp(dev, crtc, tslot) = t_vblank;
Mario Kleiner27641c32010-10-23 04:20:23 +0200835 }
836
Mario Kleinerbc215122011-02-21 05:42:01 +0100837 smp_mb__before_atomic_inc();
Ville Syrjälä5380e922013-10-04 14:53:36 +0300838 atomic_add(diff, &dev->vblank[crtc].count);
Mario Kleinerbc215122011-02-21 05:42:01 +0100839 smp_mb__after_atomic_inc();
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700840}
841
842/**
843 * drm_vblank_get - get a reference count on vblank events
844 * @dev: DRM device
845 * @crtc: which CRTC to own
846 *
847 * Acquire a reference count on vblank events to avoid having them disabled
848 * while in use.
849 *
850 * RETURNS
851 * Zero on success, nonzero on failure.
852 */
853int drm_vblank_get(struct drm_device *dev, int crtc)
854{
Peter Hurley97cbc882013-08-20 21:51:12 -0400855 unsigned long irqflags;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700856 int ret = 0;
857
858 spin_lock_irqsave(&dev->vbl_lock, irqflags);
859 /* Going from 0->1 means we have to enable interrupts again */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300860 if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) {
Peter Hurley97cbc882013-08-20 21:51:12 -0400861 spin_lock(&dev->vblank_time_lock);
Ville Syrjälä5380e922013-10-04 14:53:36 +0300862 if (!dev->vblank[crtc].enabled) {
Mario Kleiner27641c32010-10-23 04:20:23 +0200863 /* Enable vblank irqs under vblank_time_lock protection.
864 * All vblank count & timestamp updates are held off
865 * until we are done reinitializing master counter and
866 * timestamps. Filtercode in drm_handle_vblank() will
867 * prevent double-accounting of same vblank interval.
868 */
Li Peng778c9022009-11-09 12:51:22 +0800869 ret = dev->driver->enable_vblank(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +0200870 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
871 crtc, ret);
Li Peng778c9022009-11-09 12:51:22 +0800872 if (ret)
Ville Syrjälä5380e922013-10-04 14:53:36 +0300873 atomic_dec(&dev->vblank[crtc].refcount);
Li Peng778c9022009-11-09 12:51:22 +0800874 else {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300875 dev->vblank[crtc].enabled = true;
Li Peng778c9022009-11-09 12:51:22 +0800876 drm_update_vblank_count(dev, crtc);
877 }
878 }
Peter Hurley97cbc882013-08-20 21:51:12 -0400879 spin_unlock(&dev->vblank_time_lock);
Li Peng778c9022009-11-09 12:51:22 +0800880 } else {
Ville Syrjälä5380e922013-10-04 14:53:36 +0300881 if (!dev->vblank[crtc].enabled) {
882 atomic_dec(&dev->vblank[crtc].refcount);
Li Peng778c9022009-11-09 12:51:22 +0800883 ret = -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700884 }
885 }
886 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
887
888 return ret;
889}
890EXPORT_SYMBOL(drm_vblank_get);
891
892/**
893 * drm_vblank_put - give up ownership of vblank events
894 * @dev: DRM device
895 * @crtc: which counter to give up
896 *
897 * Release ownership of a given vblank counter, turning off interrupts
Mario Kleiner27641c32010-10-23 04:20:23 +0200898 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700899 */
900void drm_vblank_put(struct drm_device *dev, int crtc)
901{
Ville Syrjälä5380e922013-10-04 14:53:36 +0300902 BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0);
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000903
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700904 /* Last user schedules interrupt disable */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300905 if (atomic_dec_and_test(&dev->vblank[crtc].refcount) &&
Mario Kleiner27641c32010-10-23 04:20:23 +0200906 (drm_vblank_offdelay > 0))
Ville Syrjäläe69595c2014-02-19 19:36:08 +0200907 mod_timer(&dev->vblank[crtc].disable_timer,
Daniel Vetterbfd83032013-12-11 11:34:41 +0100908 jiffies + ((drm_vblank_offdelay * HZ)/1000));
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700909}
910EXPORT_SYMBOL(drm_vblank_put);
911
Rob Clarkc6eefa12012-10-16 22:48:40 +0000912/**
913 * drm_vblank_off - disable vblank events on a CRTC
914 * @dev: DRM device
915 * @crtc: CRTC in question
Rob Clarkc6eefa12012-10-16 22:48:40 +0000916 */
Li Peng778c9022009-11-09 12:51:22 +0800917void drm_vblank_off(struct drm_device *dev, int crtc)
918{
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000919 struct drm_pending_vblank_event *e, *t;
920 struct timeval now;
Li Peng778c9022009-11-09 12:51:22 +0800921 unsigned long irqflags;
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000922 unsigned int seq;
Li Peng778c9022009-11-09 12:51:22 +0800923
924 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Mario Kleiner27641c32010-10-23 04:20:23 +0200925 vblank_disable_and_save(dev, crtc);
Daniel Vetter57ed0f72013-12-11 11:34:43 +0100926 wake_up(&dev->vblank[crtc].queue);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000927
928 /* Send any queued vblank events, lest the natives grow disquiet */
929 seq = drm_vblank_count_and_time(dev, crtc, &now);
Imre Deake7783ba2012-11-02 13:30:50 +0200930
931 spin_lock(&dev->event_lock);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000932 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
933 if (e->pipe != crtc)
934 continue;
935 DRM_DEBUG("Sending premature vblank event on disable: \
936 wanted %d, current %d\n",
937 e->event.sequence, seq);
Rob Clarkc6eefa12012-10-16 22:48:40 +0000938 list_del(&e->base.link);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000939 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +0000940 send_vblank_event(dev, e, seq, &now);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000941 }
Imre Deake7783ba2012-11-02 13:30:50 +0200942 spin_unlock(&dev->event_lock);
Christopher James Halse Rogers498548e2011-04-27 16:10:57 +1000943
Li Peng778c9022009-11-09 12:51:22 +0800944 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
945}
946EXPORT_SYMBOL(drm_vblank_off);
947
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700948/**
Dave Airlief453ba02008-11-07 14:05:41 -0800949 * drm_vblank_pre_modeset - account for vblanks across mode sets
950 * @dev: DRM device
951 * @crtc: CRTC in question
Dave Airlief453ba02008-11-07 14:05:41 -0800952 *
953 * Account for vblank events across mode setting events, which will likely
954 * reset the hardware frame counter.
955 */
956void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
957{
Huacai Chenb7ea85a42013-05-21 06:23:43 +0000958 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Jerome Glissee77cef92010-01-07 15:39:13 +0100959 if (!dev->num_crtcs)
960 return;
Dave Airlief453ba02008-11-07 14:05:41 -0800961 /*
962 * To avoid all the problems that might happen if interrupts
963 * were enabled/disabled around or between these calls, we just
964 * have the kernel take a reference on the CRTC (just once though
965 * to avoid corrupting the count if multiple, mismatch calls occur),
966 * so that interrupts remain enabled in the interim.
967 */
Ville Syrjälä5380e922013-10-04 14:53:36 +0300968 if (!dev->vblank[crtc].inmodeset) {
969 dev->vblank[crtc].inmodeset = 0x1;
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000970 if (drm_vblank_get(dev, crtc) == 0)
Ville Syrjälä5380e922013-10-04 14:53:36 +0300971 dev->vblank[crtc].inmodeset |= 0x2;
Dave Airlief453ba02008-11-07 14:05:41 -0800972 }
973}
974EXPORT_SYMBOL(drm_vblank_pre_modeset);
975
976void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
977{
978 unsigned long irqflags;
979
Huacai Chenb7ea85a42013-05-21 06:23:43 +0000980 /* vblank is not initialized (IRQ not installed ?), or has been freed */
981 if (!dev->num_crtcs)
982 return;
983
Ville Syrjälä5380e922013-10-04 14:53:36 +0300984 if (dev->vblank[crtc].inmodeset) {
Dave Airlief453ba02008-11-07 14:05:41 -0800985 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Ville Syrjäläba0bf122013-10-04 14:53:33 +0300986 dev->vblank_disable_allowed = true;
Dave Airlief453ba02008-11-07 14:05:41 -0800987 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000988
Ville Syrjälä5380e922013-10-04 14:53:36 +0300989 if (dev->vblank[crtc].inmodeset & 0x2)
Chris Wilsonb3f5e732009-02-19 14:48:22 +0000990 drm_vblank_put(dev, crtc);
991
Ville Syrjälä5380e922013-10-04 14:53:36 +0300992 dev->vblank[crtc].inmodeset = 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800993 }
994}
995EXPORT_SYMBOL(drm_vblank_post_modeset);
996
997/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700998 * drm_modeset_ctl - handle vblank event counter changes across mode switch
999 * @DRM_IOCTL_ARGS: standard ioctl arguments
1000 *
1001 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1002 * ioctls around modesetting so that any lost vblank events are accounted for.
1003 *
1004 * Generally the counter will reset across mode sets. If interrupts are
1005 * enabled around this call, we don't have to do anything since the counter
1006 * will have already been incremented.
1007 */
1008int drm_modeset_ctl(struct drm_device *dev, void *data,
1009 struct drm_file *file_priv)
1010{
1011 struct drm_modeset_ctl *modeset = data;
Dave Airlie19227562011-02-24 08:35:06 +10001012 unsigned int crtc;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001013
1014 /* If drm_vblank_init() hasn't been called yet, just no-op */
1015 if (!dev->num_crtcs)
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001016 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001017
Laurent Pinchart29935552012-05-30 00:58:09 +02001018 /* KMS drivers handle this internally */
1019 if (drm_core_check_feature(dev, DRIVER_MODESET))
1020 return 0;
1021
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001022 crtc = modeset->crtc;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001023 if (crtc >= dev->num_crtcs)
1024 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001025
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001026 switch (modeset->cmd) {
1027 case _DRM_PRE_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001028 drm_vblank_pre_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001029 break;
1030 case _DRM_POST_MODESET:
Dave Airlief453ba02008-11-07 14:05:41 -08001031 drm_vblank_post_modeset(dev, crtc);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001032 break;
1033 default:
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001034 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001035 }
1036
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001037 return 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001038}
1039
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001040static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1041 union drm_wait_vblank *vblwait,
1042 struct drm_file *file_priv)
1043{
1044 struct drm_pending_vblank_event *e;
1045 struct timeval now;
1046 unsigned long flags;
1047 unsigned int seq;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001048 int ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001049
1050 e = kzalloc(sizeof *e, GFP_KERNEL);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001051 if (e == NULL) {
1052 ret = -ENOMEM;
1053 goto err_put;
1054 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001055
1056 e->pipe = pipe;
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001057 e->base.pid = current->pid;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001058 e->event.base.type = DRM_EVENT_VBLANK;
1059 e->event.base.length = sizeof e->event;
1060 e->event.user_data = vblwait->request.signal;
1061 e->base.event = &e->event.base;
1062 e->base.file_priv = file_priv;
1063 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1064
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001065 spin_lock_irqsave(&dev->event_lock, flags);
1066
1067 if (file_priv->event_space < sizeof e->event) {
Chris Wilsonea5d5522010-12-01 19:41:31 +00001068 ret = -EBUSY;
1069 goto err_unlock;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001070 }
1071
1072 file_priv->event_space -= sizeof e->event;
Mario Kleiner27641c32010-10-23 04:20:23 +02001073 seq = drm_vblank_count_and_time(dev, pipe, &now);
1074
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001075 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1076 (seq - vblwait->request.sequence) <= (1 << 23)) {
1077 vblwait->request.sequence = seq + 1;
Jesse Barnes4a921642009-11-10 08:21:25 +00001078 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001079 }
1080
1081 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1082 vblwait->request.sequence, seq, pipe);
1083
Jesse Barnesb9c2c9a2010-07-01 16:48:09 -07001084 trace_drm_vblank_event_queued(current->pid, pipe,
1085 vblwait->request.sequence);
1086
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001087 e->event.sequence = vblwait->request.sequence;
1088 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
Dan Carpenter6f331622010-12-09 08:35:40 +03001089 drm_vblank_put(dev, pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001090 send_vblank_event(dev, e, seq, &now);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001091 vblwait->reply.sequence = seq;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001092 } else {
Ilija Hadzica6778e92011-10-31 13:11:57 -04001093 /* drm_handle_vblank_events will call drm_vblank_put */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001094 list_add_tail(&e->base.link, &dev->vblank_event_list);
Mario Kleiner000fa7c2010-12-10 16:00:19 +01001095 vblwait->reply.sequence = vblwait->request.sequence;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001096 }
1097
1098 spin_unlock_irqrestore(&dev->event_lock, flags);
1099
1100 return 0;
Chris Wilsonea5d5522010-12-01 19:41:31 +00001101
1102err_unlock:
1103 spin_unlock_irqrestore(&dev->event_lock, flags);
1104 kfree(e);
1105err_put:
Dan Carpenter6f331622010-12-09 08:35:40 +03001106 drm_vblank_put(dev, pipe);
Chris Wilsonea5d5522010-12-01 19:41:31 +00001107 return ret;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001108}
1109
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001110/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 * Wait for VBLANK.
1112 *
1113 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +10001114 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 * \param cmd command.
1116 * \param data user argument, pointing to a drm_wait_vblank structure.
1117 * \return zero on success or a negative number on failure.
1118 *
Eric Anholt30b23632009-01-27 21:19:41 -08001119 * This function enables the vblank interrupt on the pipe requested, then
1120 * sleeps waiting for the requested sequence number to occur, and drops
1121 * the vblank interrupt refcount afterwards. (vblank irq disable follows that
1122 * after a timeout with no further vblank waits scheduled).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001124int drm_wait_vblank(struct drm_device *dev, void *data,
1125 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Eric Anholtc153f452007-09-03 12:06:45 +10001127 union drm_wait_vblank *vblwait = data;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02001128 int ret;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001129 unsigned int flags, seq, crtc, high_crtc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Daniel Vetter49849792013-08-28 15:02:37 +02001131 if (!dev->irq_enabled)
1132 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Eric Anholt30b23632009-01-27 21:19:41 -08001134 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1135 return -EINVAL;
1136
Eric Anholtc153f452007-09-03 12:06:45 +10001137 if (vblwait->request.type &
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001138 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1139 _DRM_VBLANK_HIGH_CRTC_MASK)) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001140 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001141 vblwait->request.type,
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001142 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1143 _DRM_VBLANK_HIGH_CRTC_MASK));
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001144 return -EINVAL;
1145 }
1146
Eric Anholtc153f452007-09-03 12:06:45 +10001147 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
Ilija Hadzic19b01b52011-03-18 16:58:04 -05001148 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1149 if (high_crtc)
1150 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1151 else
1152 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001153 if (crtc >= dev->num_crtcs)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001154 return -EINVAL;
1155
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001156 ret = drm_vblank_get(dev, crtc);
1157 if (ret) {
Paul Rolland8d3457e2009-08-09 12:24:01 +10001158 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001159 return ret;
1160 }
1161 seq = drm_vblank_count(dev, crtc);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +10001162
Eric Anholtc153f452007-09-03 12:06:45 +10001163 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +10001165 vblwait->request.sequence += seq;
1166 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 case _DRM_VBLANK_ABSOLUTE:
1168 break;
1169 default:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001170 ret = -EINVAL;
1171 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Ilija Hadzica6778e92011-10-31 13:11:57 -04001174 if (flags & _DRM_VBLANK_EVENT) {
1175 /* must hold on to the vblank ref until the event fires
1176 * drm_vblank_put will be called asynchronously
1177 */
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001178 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
Ilija Hadzica6778e92011-10-31 13:11:57 -04001179 }
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001180
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001181 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +10001182 (seq - vblwait->request.sequence) <= (1<<23)) {
1183 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +10001184 }
1185
Eric Anholt30b23632009-01-27 21:19:41 -08001186 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1187 vblwait->request.sequence, crtc);
Ville Syrjälä5380e922013-10-04 14:53:36 +03001188 dev->vblank[crtc].last_wait = vblwait->request.sequence;
Daniel Vetterbfd83032013-12-11 11:34:41 +01001189 DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * HZ,
Eric Anholt30b23632009-01-27 21:19:41 -08001190 (((drm_vblank_count(dev, crtc) -
1191 vblwait->request.sequence) <= (1 << 23)) ||
Ville Syrjälä3212a222014-03-06 17:27:39 +02001192 !dev->vblank[crtc].enabled ||
Eric Anholt30b23632009-01-27 21:19:41 -08001193 !dev->irq_enabled));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Eric Anholt30b23632009-01-27 21:19:41 -08001195 if (ret != -EINTR) {
1196 struct timeval now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Mario Kleiner27641c32010-10-23 04:20:23 +02001198 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
Eric Anholt30b23632009-01-27 21:19:41 -08001199 vblwait->reply.tval_sec = now.tv_sec;
1200 vblwait->reply.tval_usec = now.tv_usec;
Mario Kleiner27641c32010-10-23 04:20:23 +02001201
Eric Anholt30b23632009-01-27 21:19:41 -08001202 DRM_DEBUG("returning %d to client\n",
1203 vblwait->reply.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 } else {
Eric Anholt30b23632009-01-27 21:19:41 -08001205 DRM_DEBUG("vblank wait interrupted by signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001208done:
1209 drm_vblank_put(dev, crtc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return ret;
1211}
1212
Sachin Kamatea7f7ab2012-08-01 17:15:31 +05301213static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001214{
1215 struct drm_pending_vblank_event *e, *t;
1216 struct timeval now;
1217 unsigned long flags;
1218 unsigned int seq;
1219
Mario Kleiner27641c32010-10-23 04:20:23 +02001220 seq = drm_vblank_count_and_time(dev, crtc, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001221
1222 spin_lock_irqsave(&dev->event_lock, flags);
1223
1224 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1225 if (e->pipe != crtc)
1226 continue;
1227 if ((seq - e->event.sequence) > (1<<23))
1228 continue;
1229
1230 DRM_DEBUG("vblank event on %d, current %d\n",
1231 e->event.sequence, seq);
1232
Rob Clarkc6eefa12012-10-16 22:48:40 +00001233 list_del(&e->base.link);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001234 drm_vblank_put(dev, e->pipe);
Rob Clarkc6eefa12012-10-16 22:48:40 +00001235 send_vblank_event(dev, e, seq, &now);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001236 }
1237
1238 spin_unlock_irqrestore(&dev->event_lock, flags);
Jesse Barnesac2874b2010-07-01 16:47:31 -07001239
1240 trace_drm_vblank_event(crtc, seq);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001241}
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/**
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001244 * drm_handle_vblank - handle a vblank event
1245 * @dev: DRM device
1246 * @crtc: where this event occurred
1247 *
1248 * Drivers should call this routine in their vblank interrupt handlers to
1249 * update the vblank counter and send any signals that may be pending.
1250 */
Chris Wilson78c6e172011-01-31 10:48:04 +00001251bool drm_handle_vblank(struct drm_device *dev, int crtc)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001252{
Mario Kleiner27641c32010-10-23 04:20:23 +02001253 u32 vblcount;
1254 s64 diff_ns;
1255 struct timeval tvblank;
1256 unsigned long irqflags;
1257
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001258 if (!dev->num_crtcs)
Chris Wilson78c6e172011-01-31 10:48:04 +00001259 return false;
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001260
Mario Kleiner27641c32010-10-23 04:20:23 +02001261 /* Need timestamp lock to prevent concurrent execution with
1262 * vblank enable/disable, as this would cause inconsistent
1263 * or corrupted timestamps and vblank counts.
1264 */
1265 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
1266
1267 /* Vblank irq handling disabled. Nothing to do. */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001268 if (!dev->vblank[crtc].enabled) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001269 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001270 return false;
Mario Kleiner27641c32010-10-23 04:20:23 +02001271 }
1272
1273 /* Fetch corresponding timestamp for this vblank interval from
1274 * driver and store it in proper slot of timestamp ringbuffer.
1275 */
1276
1277 /* Get current timestamp and count. */
Ville Syrjälä5380e922013-10-04 14:53:36 +03001278 vblcount = atomic_read(&dev->vblank[crtc].count);
Mario Kleiner27641c32010-10-23 04:20:23 +02001279 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1280
1281 /* Compute time difference to timestamp of last vblank */
1282 diff_ns = timeval_to_ns(&tvblank) -
1283 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1284
1285 /* Update vblank timestamp and count if at least
1286 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1287 * difference between last stored timestamp and current
1288 * timestamp. A smaller difference means basically
1289 * identical timestamps. Happens if this vblank has
1290 * been already processed and this is a redundant call,
1291 * e.g., due to spurious vblank interrupts. We need to
1292 * ignore those for accounting.
1293 */
Mario Kleinerc4cc3832011-02-21 05:42:00 +01001294 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
Mario Kleiner27641c32010-10-23 04:20:23 +02001295 /* Store new timestamp in ringbuffer. */
1296 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
Mario Kleiner27641c32010-10-23 04:20:23 +02001297
1298 /* Increment cooked vblank count. This also atomically commits
1299 * the timestamp computed above.
1300 */
Mario Kleinerbc215122011-02-21 05:42:01 +01001301 smp_mb__before_atomic_inc();
Ville Syrjälä5380e922013-10-04 14:53:36 +03001302 atomic_inc(&dev->vblank[crtc].count);
Mario Kleinerbc215122011-02-21 05:42:01 +01001303 smp_mb__after_atomic_inc();
Mario Kleiner27641c32010-10-23 04:20:23 +02001304 } else {
1305 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1306 crtc, (int) diff_ns);
1307 }
1308
Daniel Vetter57ed0f72013-12-11 11:34:43 +01001309 wake_up(&dev->vblank[crtc].queue);
Kristian Høgsbergc9a9c5e2009-09-12 04:33:34 +10001310 drm_handle_vblank_events(dev, crtc);
Mario Kleiner27641c32010-10-23 04:20:23 +02001311
1312 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
Chris Wilson78c6e172011-01-31 10:48:04 +00001313 return true;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001314}
1315EXPORT_SYMBOL(drm_handle_vblank);