Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /** |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * \file drm_irq.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * 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 Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 36 | #include <drm/drmP.h> |
Jesse Barnes | ac2874b | 2010-07-01 16:47:31 -0700 | [diff] [blame] | 37 | #include "drm_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/interrupt.h> /* For task queue support */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 42 | #include <linux/vgaarb.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 43 | #include <linux/export.h> |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 44 | |
| 45 | /* Access macro for slots in vblank timestamp ringbuffer. */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 46 | #define vblanktimestamp(dev, crtc, count) \ |
| 47 | ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE]) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 48 | |
| 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 59 | /* |
| 60 | * Clear vblank timestamp buffer for a crtc. |
| 61 | */ |
| 62 | static void clear_vblank_timestamps(struct drm_device *dev, int crtc) |
| 63 | { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 64 | memset(dev->vblank[crtc].time, 0, sizeof(dev->vblank[crtc].time)); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 65 | } |
| 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 | */ |
| 73 | static 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 Eich | 0355cf3 | 2012-10-13 11:36:14 +0000 | [diff] [blame] | 80 | int count = DRM_TIMESTAMP_MAXRETRIES; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 81 | |
| 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 85 | */ |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 86 | spin_lock_irqsave(&dev->vblank_time_lock, irqflags); |
| 87 | |
| 88 | dev->driver->disable_vblank(dev, crtc); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 89 | dev->vblank[crtc].enabled = false; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 90 | |
| 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 104 | dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 105 | vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 106 | } while (dev->vblank[crtc].last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc); |
Egbert Eich | 0355cf3 | 2012-10-13 11:36:14 +0000 | [diff] [blame] | 107 | |
| 108 | if (!count) |
| 109 | vblrc = 0; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 110 | |
| 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 114 | vblcount = atomic_read(&dev->vblank[crtc].count); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 115 | 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 Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 130 | if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 131 | atomic_inc(&dev->vblank[crtc].count); |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 132 | smp_mb__after_atomic_inc(); |
| 133 | } |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 134 | |
| 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 141 | static void vblank_disable_fn(unsigned long arg) |
| 142 | { |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 143 | struct drm_vblank_crtc *vblank = (void *)arg; |
| 144 | struct drm_device *dev = vblank->dev; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 145 | unsigned long irqflags; |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 146 | int crtc = vblank->crtc; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 147 | |
| 148 | if (!dev->vblank_disable_allowed) |
| 149 | return; |
| 150 | |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 151 | 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 Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 155 | } |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 156 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Keith Packard | 5244021 | 2008-11-18 09:30:25 -0800 | [diff] [blame] | 159 | void drm_vblank_cleanup(struct drm_device *dev) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 160 | { |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 161 | int crtc; |
| 162 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 163 | /* Bail if the driver didn't call drm_vblank_init() */ |
| 164 | if (dev->num_crtcs == 0) |
| 165 | return; |
| 166 | |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 167 | 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 Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 171 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 172 | kfree(dev->vblank); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 173 | |
| 174 | dev->num_crtcs = 0; |
| 175 | } |
Jerome Glisse | e77cef9 | 2010-01-07 15:39:13 +0100 | [diff] [blame] | 176 | EXPORT_SYMBOL(drm_vblank_cleanup); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 177 | |
| 178 | int drm_vblank_init(struct drm_device *dev, int num_crtcs) |
| 179 | { |
| 180 | int i, ret = -ENOMEM; |
| 181 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 182 | spin_lock_init(&dev->vbl_lock); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 183 | spin_lock_init(&dev->vblank_time_lock); |
| 184 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 185 | dev->num_crtcs = num_crtcs; |
| 186 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 187 | dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL); |
| 188 | if (!dev->vblank) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 189 | goto err; |
| 190 | |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 191 | for (i = 0; i < num_crtcs; i++) { |
| 192 | dev->vblank[i].dev = dev; |
| 193 | dev->vblank[i].crtc = i; |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 194 | init_waitqueue_head(&dev->vblank[i].queue); |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 195 | setup_timer(&dev->vblank[i].disable_timer, vblank_disable_fn, |
| 196 | (unsigned long)&dev->vblank[i]); |
| 197 | } |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 198 | |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 199 | DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n"); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 200 | |
| 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ä | ba0bf12 | 2013-10-04 14:53:33 +0300 | [diff] [blame] | 207 | dev->vblank_disable_allowed = false; |
| 208 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 209 | return 0; |
| 210 | |
| 211 | err: |
| 212 | drm_vblank_cleanup(dev); |
| 213 | return ret; |
| 214 | } |
| 215 | EXPORT_SYMBOL(drm_vblank_init); |
| 216 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 217 | static 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 Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 229 | 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 Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Install IRQ handler. |
| 242 | * |
| 243 | * \param dev DRM device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | * |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 245 | * Initializes the IRQ related data. Installs the handler, calling the driver |
Sascha Hauer | 884a53e | 2012-02-29 09:06:21 +0100 | [diff] [blame] | 246 | * \c irq_preinstall() and \c irq_postinstall() functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | * before and after the installation. |
| 248 | */ |
Daniel Vetter | bb0f1b5 | 2013-11-03 21:09:27 +0100 | [diff] [blame] | 249 | int drm_irq_install(struct drm_device *dev, int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Daniel Vetter | bb0f1b5 | 2013-11-03 21:09:27 +0100 | [diff] [blame] | 251 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 252 | unsigned long sh_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 255 | return -EINVAL; |
| 256 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 257 | if (irq == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | return -EINVAL; |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* Driver must have been initialized */ |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 261 | if (!dev->dev_private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 264 | if (dev->irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return -EBUSY; |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 266 | dev->irq_enabled = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 268 | DRM_DEBUG("irq=%d\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 270 | /* Before installing handler */ |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 271 | if (dev->driver->irq_preinstall) |
| 272 | dev->driver->irq_preinstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 274 | /* Install handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) |
Thomas Gleixner | 935f6e3 | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 276 | sh_flags = IRQF_SHARED; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 277 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 278 | ret = request_irq(irq, dev->driver->irq_handler, |
Daniel Vetter | 5829d18 | 2013-11-03 21:48:48 +0100 | [diff] [blame] | 279 | sh_flags, dev->driver->name, dev); |
Jesse Barnes | 9bfbd5c | 2008-09-15 15:00:33 -0700 | [diff] [blame] | 280 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 281 | if (ret < 0) { |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 282 | dev->irq_enabled = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return ret; |
| 284 | } |
| 285 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 286 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 287 | vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL); |
| 288 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 289 | /* After installing handler */ |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 290 | if (dev->driver->irq_postinstall) |
| 291 | ret = dev->driver->irq_postinstall(dev); |
| 292 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 293 | if (ret < 0) { |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 294 | dev->irq_enabled = false; |
Joonyoung Shim | e1c44ac | 2011-08-04 05:41:00 +0000 | [diff] [blame] | 295 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 296 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 297 | free_irq(irq, dev); |
| 298 | } else { |
| 299 | dev->irq = irq; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 302 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 304 | EXPORT_SYMBOL(drm_irq_install); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
| 307 | * Uninstall the IRQ handler. |
| 308 | * |
| 309 | * \param dev DRM device. |
| 310 | * |
Sascha Hauer | 884a53e | 2012-02-29 09:06:21 +0100 | [diff] [blame] | 311 | * Calls the driver's \c irq_uninstall() function, and stops the irq. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | */ |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 313 | int drm_irq_uninstall(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 315 | unsigned long irqflags; |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 316 | bool irq_enabled; |
| 317 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 320 | return -EINVAL; |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | irq_enabled = dev->irq_enabled; |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 323 | dev->irq_enabled = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 325 | /* |
| 326 | * Wake up any waiters so they don't hang. |
| 327 | */ |
Ben Skeggs | bde4889 | 2011-07-04 12:52:27 +1000 | [diff] [blame] | 328 | if (dev->num_crtcs) { |
| 329 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 330 | for (i = 0; i < dev->num_crtcs; i++) { |
Daniel Vetter | 57ed0f7 | 2013-12-11 11:34:43 +0100 | [diff] [blame] | 331 | wake_up(&dev->vblank[i].queue); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 332 | dev->vblank[i].enabled = false; |
| 333 | dev->vblank[i].last = |
Ben Skeggs | bde4889 | 2011-07-04 12:52:27 +1000 | [diff] [blame] | 334 | dev->driver->get_vblank_counter(dev, i); |
| 335 | } |
| 336 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 337 | } |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 338 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 339 | if (!irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return -EINVAL; |
| 341 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 342 | DRM_DEBUG("irq=%d\n", dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 344 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 345 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
| 346 | |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 347 | if (dev->driver->irq_uninstall) |
| 348 | dev->driver->irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 350 | free_irq(dev->irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | EXPORT_SYMBOL(drm_irq_uninstall); |
| 355 | |
| 356 | /** |
| 357 | * IRQ control ioctl. |
| 358 | * |
| 359 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 360 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | * \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 Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 367 | int drm_control(struct drm_device *dev, void *data, |
| 368 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 370 | struct drm_control *ctl = data; |
Daniel Vetter | a319c1a | 2013-11-03 21:09:15 +0100 | [diff] [blame] | 371 | int ret = 0, irq; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 372 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 373 | /* if we haven't irq we fallback for compatibility reasons - |
| 374 | * this used to be a separate function in drm_dma.h |
| 375 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
Daniel Vetter | 22471cf | 2013-11-03 20:02:50 +0100 | [diff] [blame] | 377 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 385 | switch (ctl->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | case DRM_INST_HANDLER: |
Daniel Vetter | a319c1a | 2013-11-03 21:09:15 +0100 | [diff] [blame] | 387 | irq = dev->pdev->irq; |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | if (dev->if_version < DRM_IF_VERSION(1, 2) && |
Daniel Vetter | a319c1a | 2013-11-03 21:09:15 +0100 | [diff] [blame] | 390 | ctl->irq != irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | return -EINVAL; |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 392 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | bb0f1b5 | 2013-11-03 21:09:27 +0100 | [diff] [blame] | 393 | ret = drm_irq_install(dev, irq); |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 394 | mutex_unlock(&dev->struct_mutex); |
| 395 | |
| 396 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | case DRM_UNINST_HANDLER: |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 398 | mutex_lock(&dev->struct_mutex); |
| 399 | ret = drm_irq_uninstall(dev); |
| 400 | mutex_unlock(&dev->struct_mutex); |
| 401 | |
| 402 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | default: |
| 404 | return -EINVAL; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
Ville Syrjälä | 21b2156 | 2013-10-26 17:22:52 +0300 | [diff] [blame] | 409 | * drm_calc_timestamping_constants - Calculate vblank timestamp constants |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 410 | * |
| 411 | * @crtc drm_crtc whose timestamp constants should be updated. |
Ville Syrjälä | 545cdd5 | 2013-10-26 17:16:30 +0300 | [diff] [blame] | 412 | * @mode display mode containing the scanout timings |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 413 | * |
Ville Syrjälä | 21b2156 | 2013-10-26 17:22:52 +0300 | [diff] [blame] | 414 | * 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 419 | */ |
Ville Syrjälä | 545cdd5 | 2013-10-26 17:16:30 +0300 | [diff] [blame] | 420 | void drm_calc_timestamping_constants(struct drm_crtc *crtc, |
| 421 | const struct drm_display_mode *mode) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 422 | { |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 423 | int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0; |
Ville Syrjälä | 77666b7 | 2013-10-27 21:22:58 +0200 | [diff] [blame] | 424 | int dotclock = mode->crtc_clock; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 425 | |
| 426 | /* Valid dotclock? */ |
| 427 | if (dotclock > 0) { |
Ville Syrjälä | 0dae35a | 2013-10-26 17:11:01 +0300 | [diff] [blame] | 428 | 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 434 | */ |
Ville Syrjälä | 0dae35a | 2013-10-26 17:11:01 +0300 | [diff] [blame] | 435 | 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ä | c0ae24c | 2013-10-28 19:53:25 +0200 | [diff] [blame] | 438 | |
| 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 444 | } 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ä | 545cdd5 | 2013-10-26 17:16:30 +0300 | [diff] [blame] | 453 | crtc->base.id, mode->crtc_htotal, |
| 454 | mode->crtc_vtotal, mode->crtc_vdisplay); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 455 | DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n", |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 456 | crtc->base.id, dotclock, framedur_ns, |
| 457 | linedur_ns, pixeldur_ns); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 458 | } |
| 459 | EXPORT_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ä | 7da903e | 2013-10-26 17:57:31 +0300 | [diff] [blame] | 490 | * @mode: mode which defines the scanout timings |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 491 | * |
| 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 | */ |
| 506 | int 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ä | 7da903e | 2013-10-26 17:57:31 +0300 | [diff] [blame] | 510 | const struct drm_crtc *refcrtc, |
| 511 | const struct drm_display_mode *mode) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 512 | { |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 513 | ktime_t stime, etime, mono_time_offset; |
| 514 | struct timeval tv_etime; |
Ville Syrjälä | 8072bfa | 2013-10-28 21:22:52 +0200 | [diff] [blame] | 515 | int vbl_status; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 516 | int vpos, hpos, i; |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 517 | int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 518 | 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 531 | /* 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ä | 8072bfa | 2013-10-28 21:22:52 +0200 | [diff] [blame] | 539 | if (framedur_ns == 0) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 540 | DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc); |
| 541 | return -EAGAIN; |
| 542 | } |
| 543 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 544 | /* 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 Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 552 | /* |
| 553 | * Get vertical and horizontal scanout position vpos, hpos, |
| 554 | * and bounding timestamps stime, etime, pre/post query. |
| 555 | */ |
Ville Syrjälä | abca9e4 | 2013-10-28 20:50:48 +0200 | [diff] [blame] | 556 | vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos, |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 557 | &hpos, &stime, &etime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 558 | |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 559 | /* |
| 560 | * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if |
| 561 | * CLOCK_REALTIME is requested. |
| 562 | */ |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 563 | if (!drm_timestamp_monotonic) |
| 564 | mono_time_offset = ktime_get_monotonic_offset(); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 565 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 566 | /* 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 Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 573 | /* Compute uncertainty in timestamp of scanout position query. */ |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 574 | duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 575 | |
| 576 | /* Accept result with < max_error nsecs timing uncertainty. */ |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 577 | if (duration_ns <= *max_error) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 578 | 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ä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 584 | crtc, duration_ns/1000, *max_error/1000, i); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | /* Return upper bound of timestamp precision error. */ |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 588 | *max_error = duration_ns; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 589 | |
| 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ä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 601 | delta_ns = vpos * linedur_ns + hpos * pixeldur_ns; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 602 | |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 603 | if (!drm_timestamp_monotonic) |
| 604 | etime = ktime_sub(etime, mono_time_offset); |
| 605 | |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 606 | /* save this only for debugging purposes */ |
| 607 | tv_etime = ktime_to_timeval(etime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 608 | /* Subtract time delta from raw timestamp to get final |
| 609 | * vblank_time timestamp for end of vblank. |
| 610 | */ |
Michel Dänzer | e91abf8 | 2013-06-12 11:58:44 +0200 | [diff] [blame] | 611 | if (delta_ns < 0) |
| 612 | etime = ktime_add_ns(etime, -delta_ns); |
| 613 | else |
| 614 | etime = ktime_sub_ns(etime, delta_ns); |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 615 | *vblank_time = ktime_to_timeval(etime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 616 | |
Joe Perches | bbb0aef | 2011-04-17 20:35:52 -0700 | [diff] [blame] | 617 | 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 Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 619 | (long)tv_etime.tv_sec, (long)tv_etime.tv_usec, |
Joe Perches | bbb0aef | 2011-04-17 20:35:52 -0700 | [diff] [blame] | 620 | (long)vblank_time->tv_sec, (long)vblank_time->tv_usec, |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 621 | duration_ns/1000, i); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 622 | |
| 623 | vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD; |
| 624 | if (invbl) |
| 625 | vbl_status |= DRM_VBLANKTIME_INVBL; |
| 626 | |
| 627 | return vbl_status; |
| 628 | } |
| 629 | EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); |
| 630 | |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 631 | static 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 642 | /** |
| 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 | */ |
| 662 | u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc, |
| 663 | struct timeval *tvblank, unsigned flags) |
| 664 | { |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 665 | int ret; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 666 | |
| 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 Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 679 | * Return current monotonic/gettimeofday timestamp as best estimate. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 680 | */ |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 681 | *tvblank = get_drm_timestamp(); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | EXPORT_SYMBOL(drm_get_last_vbltimestamp); |
| 686 | |
| 687 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 688 | * 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 | */ |
| 696 | u32 drm_vblank_count(struct drm_device *dev, int crtc) |
| 697 | { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 698 | return atomic_read(&dev->vblank[crtc].count); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 699 | } |
| 700 | EXPORT_SYMBOL(drm_vblank_count); |
| 701 | |
| 702 | /** |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 703 | * 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 | */ |
| 716 | u32 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 727 | cur_vblank = atomic_read(&dev->vblank[crtc].count); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 728 | *vblanktime = vblanktimestamp(dev, crtc, cur_vblank); |
| 729 | smp_rmb(); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 730 | } while (cur_vblank != atomic_read(&dev->vblank[crtc].count)); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 731 | |
| 732 | return cur_vblank; |
| 733 | } |
| 734 | EXPORT_SYMBOL(drm_vblank_count_and_time); |
| 735 | |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 736 | static 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 | */ |
| 761 | void 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 Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 770 | |
| 771 | now = get_drm_timestamp(); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 772 | } |
Rob Clark | 21a245d | 2012-12-10 10:49:46 -0600 | [diff] [blame] | 773 | e->pipe = crtc; |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 774 | send_vblank_event(dev, e, seq, &now); |
| 775 | } |
| 776 | EXPORT_SYMBOL(drm_send_vblank_event); |
| 777 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 778 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 779 | * 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 | */ |
| 794 | static void drm_update_vblank_count(struct drm_device *dev, int crtc) |
| 795 | { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 796 | u32 cur_vblank, diff, tslot, rc; |
| 797 | struct timeval t_vblank; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 798 | |
| 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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 805 | * |
| 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 Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 810 | */ |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 811 | 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 817 | diff = cur_vblank - dev->vblank[crtc].last; |
| 818 | if (cur_vblank < dev->vblank[crtc].last) { |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 819 | 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 822 | crtc, dev->vblank[crtc].last, cur_vblank, diff); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n", |
| 826 | crtc, diff); |
| 827 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 828 | /* 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 833 | tslot = atomic_read(&dev->vblank[crtc].count) + diff; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 834 | vblanktimestamp(dev, crtc, tslot) = t_vblank; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 835 | } |
| 836 | |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 837 | smp_mb__before_atomic_inc(); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 838 | atomic_add(diff, &dev->vblank[crtc].count); |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 839 | smp_mb__after_atomic_inc(); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 840 | } |
| 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 | */ |
| 853 | int drm_vblank_get(struct drm_device *dev, int crtc) |
| 854 | { |
Peter Hurley | 97cbc88 | 2013-08-20 21:51:12 -0400 | [diff] [blame] | 855 | unsigned long irqflags; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 856 | 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 860 | if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) { |
Peter Hurley | 97cbc88 | 2013-08-20 21:51:12 -0400 | [diff] [blame] | 861 | spin_lock(&dev->vblank_time_lock); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 862 | if (!dev->vblank[crtc].enabled) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 863 | /* 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 Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 869 | ret = dev->driver->enable_vblank(dev, crtc); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 870 | DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", |
| 871 | crtc, ret); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 872 | if (ret) |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 873 | atomic_dec(&dev->vblank[crtc].refcount); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 874 | else { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 875 | dev->vblank[crtc].enabled = true; |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 876 | drm_update_vblank_count(dev, crtc); |
| 877 | } |
| 878 | } |
Peter Hurley | 97cbc88 | 2013-08-20 21:51:12 -0400 | [diff] [blame] | 879 | spin_unlock(&dev->vblank_time_lock); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 880 | } else { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 881 | if (!dev->vblank[crtc].enabled) { |
| 882 | atomic_dec(&dev->vblank[crtc].refcount); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 883 | ret = -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 887 | |
| 888 | return ret; |
| 889 | } |
| 890 | EXPORT_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 Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 898 | * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 899 | */ |
| 900 | void drm_vblank_put(struct drm_device *dev, int crtc) |
| 901 | { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 902 | BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0); |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 903 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 904 | /* Last user schedules interrupt disable */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 905 | if (atomic_dec_and_test(&dev->vblank[crtc].refcount) && |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 906 | (drm_vblank_offdelay > 0)) |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame^] | 907 | mod_timer(&dev->vblank[crtc].disable_timer, |
Daniel Vetter | bfd8303 | 2013-12-11 11:34:41 +0100 | [diff] [blame] | 908 | jiffies + ((drm_vblank_offdelay * HZ)/1000)); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 909 | } |
| 910 | EXPORT_SYMBOL(drm_vblank_put); |
| 911 | |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 912 | /** |
| 913 | * drm_vblank_off - disable vblank events on a CRTC |
| 914 | * @dev: DRM device |
| 915 | * @crtc: CRTC in question |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 916 | */ |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 917 | void drm_vblank_off(struct drm_device *dev, int crtc) |
| 918 | { |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 919 | struct drm_pending_vblank_event *e, *t; |
| 920 | struct timeval now; |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 921 | unsigned long irqflags; |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 922 | unsigned int seq; |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 923 | |
| 924 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 925 | vblank_disable_and_save(dev, crtc); |
Daniel Vetter | 57ed0f7 | 2013-12-11 11:34:43 +0100 | [diff] [blame] | 926 | wake_up(&dev->vblank[crtc].queue); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 927 | |
| 928 | /* Send any queued vblank events, lest the natives grow disquiet */ |
| 929 | seq = drm_vblank_count_and_time(dev, crtc, &now); |
Imre Deak | e7783ba | 2012-11-02 13:30:50 +0200 | [diff] [blame] | 930 | |
| 931 | spin_lock(&dev->event_lock); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 932 | 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 Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 938 | list_del(&e->base.link); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 939 | drm_vblank_put(dev, e->pipe); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 940 | send_vblank_event(dev, e, seq, &now); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 941 | } |
Imre Deak | e7783ba | 2012-11-02 13:30:50 +0200 | [diff] [blame] | 942 | spin_unlock(&dev->event_lock); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 943 | |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 944 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 945 | } |
| 946 | EXPORT_SYMBOL(drm_vblank_off); |
| 947 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 948 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 949 | * drm_vblank_pre_modeset - account for vblanks across mode sets |
| 950 | * @dev: DRM device |
| 951 | * @crtc: CRTC in question |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 952 | * |
| 953 | * Account for vblank events across mode setting events, which will likely |
| 954 | * reset the hardware frame counter. |
| 955 | */ |
| 956 | void drm_vblank_pre_modeset(struct drm_device *dev, int crtc) |
| 957 | { |
Huacai Chen | b7ea85a4 | 2013-05-21 06:23:43 +0000 | [diff] [blame] | 958 | /* vblank is not initialized (IRQ not installed ?), or has been freed */ |
Jerome Glisse | e77cef9 | 2010-01-07 15:39:13 +0100 | [diff] [blame] | 959 | if (!dev->num_crtcs) |
| 960 | return; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 961 | /* |
| 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ä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 968 | if (!dev->vblank[crtc].inmodeset) { |
| 969 | dev->vblank[crtc].inmodeset = 0x1; |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 970 | if (drm_vblank_get(dev, crtc) == 0) |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 971 | dev->vblank[crtc].inmodeset |= 0x2; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | EXPORT_SYMBOL(drm_vblank_pre_modeset); |
| 975 | |
| 976 | void drm_vblank_post_modeset(struct drm_device *dev, int crtc) |
| 977 | { |
| 978 | unsigned long irqflags; |
| 979 | |
Huacai Chen | b7ea85a4 | 2013-05-21 06:23:43 +0000 | [diff] [blame] | 980 | /* vblank is not initialized (IRQ not installed ?), or has been freed */ |
| 981 | if (!dev->num_crtcs) |
| 982 | return; |
| 983 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 984 | if (dev->vblank[crtc].inmodeset) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 985 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Ville Syrjälä | ba0bf12 | 2013-10-04 14:53:33 +0300 | [diff] [blame] | 986 | dev->vblank_disable_allowed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 987 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 988 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 989 | if (dev->vblank[crtc].inmodeset & 0x2) |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 990 | drm_vblank_put(dev, crtc); |
| 991 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 992 | dev->vblank[crtc].inmodeset = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | EXPORT_SYMBOL(drm_vblank_post_modeset); |
| 996 | |
| 997 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 998 | * 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 | */ |
| 1008 | int drm_modeset_ctl(struct drm_device *dev, void *data, |
| 1009 | struct drm_file *file_priv) |
| 1010 | { |
| 1011 | struct drm_modeset_ctl *modeset = data; |
Dave Airlie | 1922756 | 2011-02-24 08:35:06 +1000 | [diff] [blame] | 1012 | unsigned int crtc; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1013 | |
| 1014 | /* If drm_vblank_init() hasn't been called yet, just no-op */ |
| 1015 | if (!dev->num_crtcs) |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1016 | return 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1017 | |
Laurent Pinchart | 2993555 | 2012-05-30 00:58:09 +0200 | [diff] [blame] | 1018 | /* KMS drivers handle this internally */ |
| 1019 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1020 | return 0; |
| 1021 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1022 | crtc = modeset->crtc; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1023 | if (crtc >= dev->num_crtcs) |
| 1024 | return -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1025 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1026 | switch (modeset->cmd) { |
| 1027 | case _DRM_PRE_MODESET: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1028 | drm_vblank_pre_modeset(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1029 | break; |
| 1030 | case _DRM_POST_MODESET: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1031 | drm_vblank_post_modeset(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1032 | break; |
| 1033 | default: |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1034 | return -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1037 | return 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1040 | static 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 Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1048 | int ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1049 | |
| 1050 | e = kzalloc(sizeof *e, GFP_KERNEL); |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1051 | if (e == NULL) { |
| 1052 | ret = -ENOMEM; |
| 1053 | goto err_put; |
| 1054 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1055 | |
| 1056 | e->pipe = pipe; |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 1057 | e->base.pid = current->pid; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1058 | 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øgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1065 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1066 | |
| 1067 | if (file_priv->event_space < sizeof e->event) { |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1068 | ret = -EBUSY; |
| 1069 | goto err_unlock; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | file_priv->event_space -= sizeof e->event; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1073 | seq = drm_vblank_count_and_time(dev, pipe, &now); |
| 1074 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1075 | if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) && |
| 1076 | (seq - vblwait->request.sequence) <= (1 << 23)) { |
| 1077 | vblwait->request.sequence = seq + 1; |
Jesse Barnes | 4a92164 | 2009-11-10 08:21:25 +0000 | [diff] [blame] | 1078 | vblwait->reply.sequence = vblwait->request.sequence; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n", |
| 1082 | vblwait->request.sequence, seq, pipe); |
| 1083 | |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 1084 | trace_drm_vblank_event_queued(current->pid, pipe, |
| 1085 | vblwait->request.sequence); |
| 1086 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1087 | e->event.sequence = vblwait->request.sequence; |
| 1088 | if ((seq - vblwait->request.sequence) <= (1 << 23)) { |
Dan Carpenter | 6f33162 | 2010-12-09 08:35:40 +0300 | [diff] [blame] | 1089 | drm_vblank_put(dev, pipe); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1090 | send_vblank_event(dev, e, seq, &now); |
Mario Kleiner | 000fa7c | 2010-12-10 16:00:19 +0100 | [diff] [blame] | 1091 | vblwait->reply.sequence = seq; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1092 | } else { |
Ilija Hadzic | a6778e9 | 2011-10-31 13:11:57 -0400 | [diff] [blame] | 1093 | /* drm_handle_vblank_events will call drm_vblank_put */ |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1094 | list_add_tail(&e->base.link, &dev->vblank_event_list); |
Mario Kleiner | 000fa7c | 2010-12-10 16:00:19 +0100 | [diff] [blame] | 1095 | vblwait->reply.sequence = vblwait->request.sequence; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1099 | |
| 1100 | return 0; |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1101 | |
| 1102 | err_unlock: |
| 1103 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1104 | kfree(e); |
| 1105 | err_put: |
Dan Carpenter | 6f33162 | 2010-12-09 08:35:40 +0300 | [diff] [blame] | 1106 | drm_vblank_put(dev, pipe); |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1107 | return ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1108 | } |
| 1109 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1110 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | * Wait for VBLANK. |
| 1112 | * |
| 1113 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1114 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | * \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 Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1119 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | */ |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1124 | int drm_wait_vblank(struct drm_device *dev, void *data, |
| 1125 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1127 | union drm_wait_vblank *vblwait = data; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1128 | int ret; |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1129 | unsigned int flags, seq, crtc, high_crtc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | |
Daniel Vetter | 4984979 | 2013-08-28 15:02:37 +0200 | [diff] [blame] | 1131 | if (!dev->irq_enabled) |
| 1132 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1134 | if (vblwait->request.type & _DRM_VBLANK_SIGNAL) |
| 1135 | return -EINVAL; |
| 1136 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1137 | if (vblwait->request.type & |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1138 | ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | |
| 1139 | _DRM_VBLANK_HIGH_CRTC_MASK)) { |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1140 | DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1141 | vblwait->request.type, |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1142 | (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | |
| 1143 | _DRM_VBLANK_HIGH_CRTC_MASK)); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1144 | return -EINVAL; |
| 1145 | } |
| 1146 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1147 | flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1148 | 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 Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1153 | if (crtc >= dev->num_crtcs) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1154 | return -EINVAL; |
| 1155 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1156 | ret = drm_vblank_get(dev, crtc); |
| 1157 | if (ret) { |
Paul Rolland | 8d3457e | 2009-08-09 12:24:01 +1000 | [diff] [blame] | 1158 | DRM_DEBUG("failed to acquire vblank counter, %d\n", ret); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1159 | return ret; |
| 1160 | } |
| 1161 | seq = drm_vblank_count(dev, crtc); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1162 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1163 | switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | case _DRM_VBLANK_RELATIVE: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1165 | vblwait->request.sequence += seq; |
| 1166 | vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | case _DRM_VBLANK_ABSOLUTE: |
| 1168 | break; |
| 1169 | default: |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1170 | ret = -EINVAL; |
| 1171 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Ilija Hadzic | a6778e9 | 2011-10-31 13:11:57 -0400 | [diff] [blame] | 1174 | 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øgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1178 | return drm_queue_vblank_event(dev, crtc, vblwait, file_priv); |
Ilija Hadzic | a6778e9 | 2011-10-31 13:11:57 -0400 | [diff] [blame] | 1179 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1180 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 1181 | if ((flags & _DRM_VBLANK_NEXTONMISS) && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1182 | (seq - vblwait->request.sequence) <= (1<<23)) { |
| 1183 | vblwait->request.sequence = seq + 1; |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 1184 | } |
| 1185 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1186 | DRM_DEBUG("waiting on vblank count %d, crtc %d\n", |
| 1187 | vblwait->request.sequence, crtc); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1188 | dev->vblank[crtc].last_wait = vblwait->request.sequence; |
Daniel Vetter | bfd8303 | 2013-12-11 11:34:41 +0100 | [diff] [blame] | 1189 | DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * HZ, |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1190 | (((drm_vblank_count(dev, crtc) - |
| 1191 | vblwait->request.sequence) <= (1 << 23)) || |
| 1192 | !dev->irq_enabled)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1194 | if (ret != -EINTR) { |
| 1195 | struct timeval now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1197 | vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now); |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1198 | vblwait->reply.tval_sec = now.tv_sec; |
| 1199 | vblwait->reply.tval_usec = now.tv_usec; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1200 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1201 | DRM_DEBUG("returning %d to client\n", |
| 1202 | vblwait->reply.sequence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } else { |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1204 | DRM_DEBUG("vblank wait interrupted by signal\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
| 1206 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1207 | done: |
| 1208 | drm_vblank_put(dev, crtc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | return ret; |
| 1210 | } |
| 1211 | |
Sachin Kamat | ea7f7ab | 2012-08-01 17:15:31 +0530 | [diff] [blame] | 1212 | static void drm_handle_vblank_events(struct drm_device *dev, int crtc) |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1213 | { |
| 1214 | struct drm_pending_vblank_event *e, *t; |
| 1215 | struct timeval now; |
| 1216 | unsigned long flags; |
| 1217 | unsigned int seq; |
| 1218 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1219 | seq = drm_vblank_count_and_time(dev, crtc, &now); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1220 | |
| 1221 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1222 | |
| 1223 | list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { |
| 1224 | if (e->pipe != crtc) |
| 1225 | continue; |
| 1226 | if ((seq - e->event.sequence) > (1<<23)) |
| 1227 | continue; |
| 1228 | |
| 1229 | DRM_DEBUG("vblank event on %d, current %d\n", |
| 1230 | e->event.sequence, seq); |
| 1231 | |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1232 | list_del(&e->base.link); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1233 | drm_vblank_put(dev, e->pipe); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1234 | send_vblank_event(dev, e, seq, &now); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Jesse Barnes | ac2874b | 2010-07-01 16:47:31 -0700 | [diff] [blame] | 1238 | |
| 1239 | trace_drm_vblank_event(crtc, seq); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1240 | } |
| 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1243 | * drm_handle_vblank - handle a vblank event |
| 1244 | * @dev: DRM device |
| 1245 | * @crtc: where this event occurred |
| 1246 | * |
| 1247 | * Drivers should call this routine in their vblank interrupt handlers to |
| 1248 | * update the vblank counter and send any signals that may be pending. |
| 1249 | */ |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1250 | bool drm_handle_vblank(struct drm_device *dev, int crtc) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1251 | { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1252 | u32 vblcount; |
| 1253 | s64 diff_ns; |
| 1254 | struct timeval tvblank; |
| 1255 | unsigned long irqflags; |
| 1256 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1257 | if (!dev->num_crtcs) |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1258 | return false; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1259 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1260 | /* Need timestamp lock to prevent concurrent execution with |
| 1261 | * vblank enable/disable, as this would cause inconsistent |
| 1262 | * or corrupted timestamps and vblank counts. |
| 1263 | */ |
| 1264 | spin_lock_irqsave(&dev->vblank_time_lock, irqflags); |
| 1265 | |
| 1266 | /* Vblank irq handling disabled. Nothing to do. */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1267 | if (!dev->vblank[crtc].enabled) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1268 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1269 | return false; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /* Fetch corresponding timestamp for this vblank interval from |
| 1273 | * driver and store it in proper slot of timestamp ringbuffer. |
| 1274 | */ |
| 1275 | |
| 1276 | /* Get current timestamp and count. */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1277 | vblcount = atomic_read(&dev->vblank[crtc].count); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1278 | drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ); |
| 1279 | |
| 1280 | /* Compute time difference to timestamp of last vblank */ |
| 1281 | diff_ns = timeval_to_ns(&tvblank) - |
| 1282 | timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount)); |
| 1283 | |
| 1284 | /* Update vblank timestamp and count if at least |
| 1285 | * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds |
| 1286 | * difference between last stored timestamp and current |
| 1287 | * timestamp. A smaller difference means basically |
| 1288 | * identical timestamps. Happens if this vblank has |
| 1289 | * been already processed and this is a redundant call, |
| 1290 | * e.g., due to spurious vblank interrupts. We need to |
| 1291 | * ignore those for accounting. |
| 1292 | */ |
Mario Kleiner | c4cc383 | 2011-02-21 05:42:00 +0100 | [diff] [blame] | 1293 | if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1294 | /* Store new timestamp in ringbuffer. */ |
| 1295 | vblanktimestamp(dev, crtc, vblcount + 1) = tvblank; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1296 | |
| 1297 | /* Increment cooked vblank count. This also atomically commits |
| 1298 | * the timestamp computed above. |
| 1299 | */ |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 1300 | smp_mb__before_atomic_inc(); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1301 | atomic_inc(&dev->vblank[crtc].count); |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 1302 | smp_mb__after_atomic_inc(); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1303 | } else { |
| 1304 | DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n", |
| 1305 | crtc, (int) diff_ns); |
| 1306 | } |
| 1307 | |
Daniel Vetter | 57ed0f7 | 2013-12-11 11:34:43 +0100 | [diff] [blame] | 1308 | wake_up(&dev->vblank[crtc].queue); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1309 | drm_handle_vblank_events(dev, crtc); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1310 | |
| 1311 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1312 | return true; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1313 | } |
| 1314 | EXPORT_SYMBOL(drm_handle_vblank); |