Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drm_irq.c IRQ and vblank support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
| 5 | * \author Gareth Hughes <gareth@valinux.com> |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com |
| 10 | * |
| 11 | * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas. |
| 12 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
| 13 | * All Rights Reserved. |
| 14 | * |
| 15 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 16 | * copy of this software and associated documentation files (the "Software"), |
| 17 | * to deal in the Software without restriction, including without limitation |
| 18 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 19 | * and/or sell copies of the Software, and to permit persons to whom the |
| 20 | * Software is furnished to do so, subject to the following conditions: |
| 21 | * |
| 22 | * The above copyright notice and this permission notice (including the next |
| 23 | * paragraph) shall be included in all copies or substantial portions of the |
| 24 | * Software. |
| 25 | * |
| 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 29 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 30 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 31 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 32 | * OTHER DEALINGS IN THE SOFTWARE. |
| 33 | */ |
| 34 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 35 | #include <drm/drmP.h> |
Jesse Barnes | ac2874b | 2010-07-01 16:47:31 -0700 | [diff] [blame] | 36 | #include "drm_trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <linux/interrupt.h> /* For task queue support */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 41 | #include <linux/vgaarb.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 42 | #include <linux/export.h> |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 43 | |
| 44 | /* Access macro for slots in vblank timestamp ringbuffer. */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 45 | #define vblanktimestamp(dev, crtc, count) \ |
| 46 | ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE]) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 47 | |
| 48 | /* Retry timestamp calculation up to 3 times to satisfy |
| 49 | * drm_timestamp_precision before giving up. |
| 50 | */ |
| 51 | #define DRM_TIMESTAMP_MAXRETRIES 3 |
| 52 | |
| 53 | /* Threshold in nanoseconds for detection of redundant |
| 54 | * vblank irq in drm_handle_vblank(). 1 msec should be ok. |
| 55 | */ |
| 56 | #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000 |
| 57 | |
Ville Syrjälä | 13b030a | 2014-08-06 14:49:47 +0300 | [diff] [blame] | 58 | /** |
| 59 | * drm_update_vblank_count - update the master vblank counter |
| 60 | * @dev: DRM device |
| 61 | * @crtc: counter to update |
| 62 | * |
| 63 | * Call back into the driver to update the appropriate vblank counter |
| 64 | * (specified by @crtc). Deal with wraparound, if it occurred, and |
| 65 | * update the last read value so we can deal with wraparound on the next |
| 66 | * call if necessary. |
| 67 | * |
| 68 | * Only necessary when going from off->on, to account for frames we |
| 69 | * didn't get an interrupt for. |
| 70 | * |
| 71 | * Note: caller must hold dev->vbl_lock since this reads & writes |
| 72 | * device vblank fields. |
| 73 | */ |
| 74 | static void drm_update_vblank_count(struct drm_device *dev, int crtc) |
| 75 | { |
| 76 | u32 cur_vblank, diff, tslot, rc; |
| 77 | struct timeval t_vblank; |
| 78 | |
| 79 | /* |
| 80 | * Interrupts were disabled prior to this call, so deal with counter |
| 81 | * wrap if needed. |
| 82 | * NOTE! It's possible we lost a full dev->max_vblank_count events |
| 83 | * here if the register is small or we had vblank interrupts off for |
| 84 | * a long time. |
| 85 | * |
| 86 | * We repeat the hardware vblank counter & timestamp query until |
| 87 | * we get consistent results. This to prevent races between gpu |
| 88 | * updating its hardware counter while we are retrieving the |
| 89 | * corresponding vblank timestamp. |
| 90 | */ |
| 91 | do { |
| 92 | cur_vblank = dev->driver->get_vblank_counter(dev, crtc); |
| 93 | rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0); |
| 94 | } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc)); |
| 95 | |
| 96 | /* Deal with counter wrap */ |
| 97 | diff = cur_vblank - dev->vblank[crtc].last; |
| 98 | if (cur_vblank < dev->vblank[crtc].last) { |
| 99 | diff += dev->max_vblank_count; |
| 100 | |
| 101 | DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n", |
| 102 | crtc, dev->vblank[crtc].last, cur_vblank, diff); |
| 103 | } |
| 104 | |
| 105 | DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n", |
| 106 | crtc, diff); |
| 107 | |
| 108 | /* Reinitialize corresponding vblank timestamp if high-precision query |
| 109 | * available. Skip this step if query unsupported or failed. Will |
| 110 | * reinitialize delayed at next vblank interrupt in that case. |
| 111 | */ |
| 112 | if (rc) { |
| 113 | tslot = atomic_read(&dev->vblank[crtc].count) + diff; |
| 114 | vblanktimestamp(dev, crtc, tslot) = t_vblank; |
| 115 | } |
| 116 | |
| 117 | smp_mb__before_atomic(); |
| 118 | atomic_add(diff, &dev->vblank[crtc].count); |
| 119 | smp_mb__after_atomic(); |
| 120 | } |
| 121 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 122 | /* |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 123 | * Disable vblank irq's on crtc, make sure that last vblank count |
| 124 | * of hardware and corresponding consistent software vblank counter |
| 125 | * are preserved, even if there are any spurious vblank irq's after |
| 126 | * disable. |
| 127 | */ |
| 128 | static void vblank_disable_and_save(struct drm_device *dev, int crtc) |
| 129 | { |
| 130 | unsigned long irqflags; |
| 131 | u32 vblcount; |
| 132 | s64 diff_ns; |
| 133 | int vblrc; |
| 134 | struct timeval tvblank; |
Egbert Eich | 0355cf3 | 2012-10-13 11:36:14 +0000 | [diff] [blame] | 135 | int count = DRM_TIMESTAMP_MAXRETRIES; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 136 | |
| 137 | /* Prevent vblank irq processing while disabling vblank irqs, |
| 138 | * so no updates of timestamps or count can happen after we've |
| 139 | * disabled. Needed to prevent races in case of delayed irq's. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 140 | */ |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 141 | spin_lock_irqsave(&dev->vblank_time_lock, irqflags); |
| 142 | |
Ville Syrjälä | 812e746 | 2014-08-06 14:49:48 +0300 | [diff] [blame] | 143 | /* |
| 144 | * If the vblank interrupt was already disbled update the count |
| 145 | * and timestamp to maintain the appearance that the counter |
| 146 | * has been ticking all along until this time. This makes the |
| 147 | * count account for the entire time between drm_vblank_on() and |
| 148 | * drm_vblank_off(). |
| 149 | */ |
| 150 | if (!dev->vblank[crtc].enabled) { |
| 151 | drm_update_vblank_count(dev, crtc); |
| 152 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); |
| 153 | return; |
| 154 | } |
| 155 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 156 | dev->driver->disable_vblank(dev, crtc); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 157 | dev->vblank[crtc].enabled = false; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 158 | |
| 159 | /* No further vblank irq's will be processed after |
| 160 | * this point. Get current hardware vblank count and |
| 161 | * vblank timestamp, repeat until they are consistent. |
| 162 | * |
| 163 | * FIXME: There is still a race condition here and in |
| 164 | * drm_update_vblank_count() which can cause off-by-one |
| 165 | * reinitialization of software vblank counter. If gpu |
| 166 | * vblank counter doesn't increment exactly at the leading |
| 167 | * edge of a vblank interval, then we can lose 1 count if |
| 168 | * we happen to execute between start of vblank and the |
| 169 | * delayed gpu counter increment. |
| 170 | */ |
| 171 | do { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 172 | dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 173 | vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 174 | } 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] | 175 | |
| 176 | if (!count) |
| 177 | vblrc = 0; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 178 | |
| 179 | /* Compute time difference to stored timestamp of last vblank |
| 180 | * as updated by last invocation of drm_handle_vblank() in vblank irq. |
| 181 | */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 182 | vblcount = atomic_read(&dev->vblank[crtc].count); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 183 | diff_ns = timeval_to_ns(&tvblank) - |
| 184 | timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount)); |
| 185 | |
| 186 | /* If there is at least 1 msec difference between the last stored |
| 187 | * timestamp and tvblank, then we are currently executing our |
| 188 | * disable inside a new vblank interval, the tvblank timestamp |
| 189 | * corresponds to this new vblank interval and the irq handler |
| 190 | * for this vblank didn't run yet and won't run due to our disable. |
| 191 | * Therefore we need to do the job of drm_handle_vblank() and |
| 192 | * increment the vblank counter by one to account for this vblank. |
| 193 | * |
| 194 | * Skip this step if there isn't any high precision timestamp |
| 195 | * available. In that case we can't account for this and just |
| 196 | * hope for the best. |
| 197 | */ |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 198 | if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 199 | atomic_inc(&dev->vblank[crtc].count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 200 | smp_mb__after_atomic(); |
Mario Kleiner | bc21512 | 2011-02-21 05:42:01 +0100 | [diff] [blame] | 201 | } |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 202 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 203 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 206 | static void vblank_disable_fn(unsigned long arg) |
| 207 | { |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 208 | struct drm_vblank_crtc *vblank = (void *)arg; |
| 209 | struct drm_device *dev = vblank->dev; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 210 | unsigned long irqflags; |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 211 | int crtc = vblank->crtc; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 212 | |
| 213 | if (!dev->vblank_disable_allowed) |
| 214 | return; |
| 215 | |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 216 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 217 | if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) { |
| 218 | DRM_DEBUG("disabling vblank on crtc %d\n", crtc); |
| 219 | vblank_disable_and_save(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 220 | } |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 221 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 224 | /** |
| 225 | * drm_vblank_cleanup - cleanup vblank support |
| 226 | * @dev: DRM device |
| 227 | * |
| 228 | * This function cleans up any resources allocated in drm_vblank_init. |
| 229 | */ |
Keith Packard | 5244021 | 2008-11-18 09:30:25 -0800 | [diff] [blame] | 230 | void drm_vblank_cleanup(struct drm_device *dev) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 231 | { |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 232 | int crtc; |
| 233 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 234 | /* Bail if the driver didn't call drm_vblank_init() */ |
| 235 | if (dev->num_crtcs == 0) |
| 236 | return; |
| 237 | |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 238 | for (crtc = 0; crtc < dev->num_crtcs; crtc++) { |
| 239 | del_timer_sync(&dev->vblank[crtc].disable_timer); |
| 240 | vblank_disable_fn((unsigned long)&dev->vblank[crtc]); |
| 241 | } |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 242 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 243 | kfree(dev->vblank); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 244 | |
| 245 | dev->num_crtcs = 0; |
| 246 | } |
Jerome Glisse | e77cef9 | 2010-01-07 15:39:13 +0100 | [diff] [blame] | 247 | EXPORT_SYMBOL(drm_vblank_cleanup); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 248 | |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 249 | /** |
| 250 | * drm_vblank_init - initialize vblank support |
| 251 | * @dev: drm_device |
| 252 | * @num_crtcs: number of crtcs supported by @dev |
| 253 | * |
| 254 | * This function initializes vblank support for @num_crtcs display pipelines. |
| 255 | * |
| 256 | * Returns: |
| 257 | * Zero on success or a negative error code on failure. |
| 258 | */ |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 259 | int drm_vblank_init(struct drm_device *dev, int num_crtcs) |
| 260 | { |
| 261 | int i, ret = -ENOMEM; |
| 262 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 263 | spin_lock_init(&dev->vbl_lock); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 264 | spin_lock_init(&dev->vblank_time_lock); |
| 265 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 266 | dev->num_crtcs = num_crtcs; |
| 267 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 268 | dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL); |
| 269 | if (!dev->vblank) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 270 | goto err; |
| 271 | |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 272 | for (i = 0; i < num_crtcs; i++) { |
| 273 | dev->vblank[i].dev = dev; |
| 274 | dev->vblank[i].crtc = i; |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 275 | init_waitqueue_head(&dev->vblank[i].queue); |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 276 | setup_timer(&dev->vblank[i].disable_timer, vblank_disable_fn, |
| 277 | (unsigned long)&dev->vblank[i]); |
| 278 | } |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 279 | |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 280 | DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n"); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 281 | |
| 282 | /* Driver specific high-precision vblank timestamping supported? */ |
| 283 | if (dev->driver->get_vblank_timestamp) |
| 284 | DRM_INFO("Driver supports precise vblank timestamp query.\n"); |
| 285 | else |
| 286 | DRM_INFO("No driver support for vblank timestamp query.\n"); |
| 287 | |
Ville Syrjälä | ba0bf12 | 2013-10-04 14:53:33 +0300 | [diff] [blame] | 288 | dev->vblank_disable_allowed = false; |
| 289 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | |
| 292 | err: |
| 293 | drm_vblank_cleanup(dev); |
| 294 | return ret; |
| 295 | } |
| 296 | EXPORT_SYMBOL(drm_vblank_init); |
| 297 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 298 | static void drm_irq_vgaarb_nokms(void *cookie, bool state) |
| 299 | { |
| 300 | struct drm_device *dev = cookie; |
| 301 | |
| 302 | if (dev->driver->vgaarb_irq) { |
| 303 | dev->driver->vgaarb_irq(dev, state); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | if (!dev->irq_enabled) |
| 308 | return; |
| 309 | |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 310 | if (state) { |
| 311 | if (dev->driver->irq_uninstall) |
| 312 | dev->driver->irq_uninstall(dev); |
| 313 | } else { |
| 314 | if (dev->driver->irq_preinstall) |
| 315 | dev->driver->irq_preinstall(dev); |
| 316 | if (dev->driver->irq_postinstall) |
| 317 | dev->driver->irq_postinstall(dev); |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | /** |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 322 | * drm_irq_install - install IRQ handler |
| 323 | * @dev: DRM device |
| 324 | * @irq: IRQ number to install the handler for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | * |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 326 | * Initializes the IRQ related data. Installs the handler, calling the driver |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 327 | * irq_preinstall() and irq_postinstall() functions before and after the |
| 328 | * installation. |
| 329 | * |
| 330 | * This is the simplified helper interface provided for drivers with no special |
| 331 | * needs. Drivers which need to install interrupt handlers for multiple |
| 332 | * interrupts must instead set drm_device->irq_enabled to signal the DRM core |
| 333 | * that vblank interrupts are available. |
| 334 | * |
| 335 | * Returns: |
| 336 | * Zero on success or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | */ |
Daniel Vetter | bb0f1b5 | 2013-11-03 21:09:27 +0100 | [diff] [blame] | 338 | int drm_irq_install(struct drm_device *dev, int irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 340 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 341 | unsigned long sh_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 344 | return -EINVAL; |
| 345 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 346 | if (irq == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | return -EINVAL; |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | /* Driver must have been initialized */ |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 350 | if (!dev->dev_private) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 353 | if (dev->irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return -EBUSY; |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 355 | dev->irq_enabled = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 357 | DRM_DEBUG("irq=%d\n", irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 359 | /* Before installing handler */ |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 360 | if (dev->driver->irq_preinstall) |
| 361 | dev->driver->irq_preinstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 363 | /* Install handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) |
Thomas Gleixner | 935f6e3 | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 365 | sh_flags = IRQF_SHARED; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 366 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 367 | ret = request_irq(irq, dev->driver->irq_handler, |
Daniel Vetter | 5829d18 | 2013-11-03 21:48:48 +0100 | [diff] [blame] | 368 | sh_flags, dev->driver->name, dev); |
Jesse Barnes | 9bfbd5c | 2008-09-15 15:00:33 -0700 | [diff] [blame] | 369 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 370 | if (ret < 0) { |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 371 | dev->irq_enabled = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return ret; |
| 373 | } |
| 374 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 375 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 376 | vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL); |
| 377 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 378 | /* After installing handler */ |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 379 | if (dev->driver->irq_postinstall) |
| 380 | ret = dev->driver->irq_postinstall(dev); |
| 381 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 382 | if (ret < 0) { |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 383 | dev->irq_enabled = false; |
Joonyoung Shim | e1c44ac | 2011-08-04 05:41:00 +0000 | [diff] [blame] | 384 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 385 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 386 | free_irq(irq, dev); |
| 387 | } else { |
| 388 | dev->irq = irq; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 391 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 393 | EXPORT_SYMBOL(drm_irq_install); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | |
| 395 | /** |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 396 | * drm_irq_uninstall - uninstall the IRQ handler |
| 397 | * @dev: DRM device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 399 | * Calls the driver's irq_uninstall() function and unregisters the IRQ handler. |
| 400 | * This should only be called by drivers which used drm_irq_install() to set up |
| 401 | * their interrupt handler. Other drivers must only reset |
| 402 | * drm_device->irq_enabled to false. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 404 | * Note that for kernel modesetting drivers it is a bug if this function fails. |
| 405 | * The sanity checks are only to catch buggy user modesetting drivers which call |
| 406 | * the same function through an ioctl. |
| 407 | * |
| 408 | * Returns: |
| 409 | * Zero on success or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | */ |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 411 | int drm_irq_uninstall(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | { |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 413 | unsigned long irqflags; |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 414 | bool irq_enabled; |
| 415 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
| 417 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 418 | return -EINVAL; |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | irq_enabled = dev->irq_enabled; |
Ville Syrjälä | 4423843 | 2013-10-04 14:53:37 +0300 | [diff] [blame] | 421 | dev->irq_enabled = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 423 | /* |
| 424 | * Wake up any waiters so they don't hang. |
| 425 | */ |
Ben Skeggs | bde4889 | 2011-07-04 12:52:27 +1000 | [diff] [blame] | 426 | if (dev->num_crtcs) { |
| 427 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 428 | for (i = 0; i < dev->num_crtcs; i++) { |
Daniel Vetter | 57ed0f7 | 2013-12-11 11:34:43 +0100 | [diff] [blame] | 429 | wake_up(&dev->vblank[i].queue); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 430 | dev->vblank[i].enabled = false; |
| 431 | dev->vblank[i].last = |
Ben Skeggs | bde4889 | 2011-07-04 12:52:27 +1000 | [diff] [blame] | 432 | dev->driver->get_vblank_counter(dev, i); |
| 433 | } |
| 434 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 435 | } |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 436 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 437 | if (!irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | return -EINVAL; |
| 439 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 440 | DRM_DEBUG("irq=%d\n", dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 442 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 443 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
| 444 | |
Joonyoung Shim | 5037f8a | 2011-08-04 05:41:01 +0000 | [diff] [blame] | 445 | if (dev->driver->irq_uninstall) |
| 446 | dev->driver->irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Daniel Vetter | 7c1a38e | 2013-12-16 11:21:15 +0100 | [diff] [blame] | 448 | free_irq(dev->irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | |
| 450 | return 0; |
| 451 | } |
| 452 | EXPORT_SYMBOL(drm_irq_uninstall); |
| 453 | |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 454 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | * IRQ control ioctl. |
| 456 | * |
| 457 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 458 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | * \param cmd command. |
| 460 | * \param arg user argument, pointing to a drm_control structure. |
| 461 | * \return zero on success or a negative number on failure. |
| 462 | * |
| 463 | * Calls irq_install() or irq_uninstall() according to \p arg. |
| 464 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 465 | int drm_control(struct drm_device *dev, void *data, |
| 466 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 468 | struct drm_control *ctl = data; |
Daniel Vetter | a319c1a | 2013-11-03 21:09:15 +0100 | [diff] [blame] | 469 | int ret = 0, irq; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 470 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 471 | /* if we haven't irq we fallback for compatibility reasons - |
| 472 | * this used to be a separate function in drm_dma.h |
| 473 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Daniel Vetter | 22471cf | 2013-11-03 20:02:50 +0100 | [diff] [blame] | 475 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 476 | return 0; |
| 477 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 478 | return 0; |
| 479 | /* UMS was only ever support on pci devices. */ |
| 480 | if (WARN_ON(!dev->pdev)) |
| 481 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 483 | switch (ctl->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | case DRM_INST_HANDLER: |
Daniel Vetter | a319c1a | 2013-11-03 21:09:15 +0100 | [diff] [blame] | 485 | irq = dev->pdev->irq; |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | if (dev->if_version < DRM_IF_VERSION(1, 2) && |
Daniel Vetter | a319c1a | 2013-11-03 21:09:15 +0100 | [diff] [blame] | 488 | ctl->irq != irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | return -EINVAL; |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 490 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | bb0f1b5 | 2013-11-03 21:09:27 +0100 | [diff] [blame] | 491 | ret = drm_irq_install(dev, irq); |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 492 | mutex_unlock(&dev->struct_mutex); |
| 493 | |
| 494 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | case DRM_UNINST_HANDLER: |
Daniel Vetter | e090c53 | 2013-11-03 20:27:05 +0100 | [diff] [blame] | 496 | mutex_lock(&dev->struct_mutex); |
| 497 | ret = drm_irq_uninstall(dev); |
| 498 | mutex_unlock(&dev->struct_mutex); |
| 499 | |
| 500 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | default: |
| 502 | return -EINVAL; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /** |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 507 | * drm_calc_timestamping_constants - calculate vblank timestamp constants |
| 508 | * @crtc: drm_crtc whose timestamp constants should be updated. |
| 509 | * @mode: display mode containing the scanout timings |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 510 | * |
Ville Syrjälä | 21b2156 | 2013-10-26 17:22:52 +0300 | [diff] [blame] | 511 | * Calculate and store various constants which are later |
| 512 | * needed by vblank and swap-completion timestamping, e.g, |
| 513 | * by drm_calc_vbltimestamp_from_scanoutpos(). They are |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 514 | * derived from CRTC's true scanout timing, so they take |
Ville Syrjälä | 21b2156 | 2013-10-26 17:22:52 +0300 | [diff] [blame] | 515 | * things like panel scaling or other adjustments into account. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 516 | */ |
Ville Syrjälä | 545cdd5 | 2013-10-26 17:16:30 +0300 | [diff] [blame] | 517 | void drm_calc_timestamping_constants(struct drm_crtc *crtc, |
| 518 | const struct drm_display_mode *mode) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 519 | { |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 520 | int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0; |
Ville Syrjälä | 77666b7 | 2013-10-27 21:22:58 +0200 | [diff] [blame] | 521 | int dotclock = mode->crtc_clock; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 522 | |
| 523 | /* Valid dotclock? */ |
| 524 | if (dotclock > 0) { |
Ville Syrjälä | 0dae35a | 2013-10-26 17:11:01 +0300 | [diff] [blame] | 525 | int frame_size = mode->crtc_htotal * mode->crtc_vtotal; |
| 526 | |
| 527 | /* |
| 528 | * Convert scanline length in pixels and video |
| 529 | * dot clock to line duration, frame duration |
| 530 | * and pixel duration in nanoseconds: |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 531 | */ |
Ville Syrjälä | 0dae35a | 2013-10-26 17:11:01 +0300 | [diff] [blame] | 532 | pixeldur_ns = 1000000 / dotclock; |
| 533 | linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock); |
| 534 | framedur_ns = div_u64((u64) frame_size * 1000000, dotclock); |
Ville Syrjälä | c0ae24c | 2013-10-28 19:53:25 +0200 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Fields of interlaced scanout modes are only half a frame duration. |
| 538 | */ |
| 539 | if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 540 | framedur_ns /= 2; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 541 | } else |
| 542 | DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n", |
| 543 | crtc->base.id); |
| 544 | |
| 545 | crtc->pixeldur_ns = pixeldur_ns; |
| 546 | crtc->linedur_ns = linedur_ns; |
| 547 | crtc->framedur_ns = framedur_ns; |
| 548 | |
| 549 | DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n", |
Ville Syrjälä | 545cdd5 | 2013-10-26 17:16:30 +0300 | [diff] [blame] | 550 | crtc->base.id, mode->crtc_htotal, |
| 551 | mode->crtc_vtotal, mode->crtc_vdisplay); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 552 | 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] | 553 | crtc->base.id, dotclock, framedur_ns, |
| 554 | linedur_ns, pixeldur_ns); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 555 | } |
| 556 | EXPORT_SYMBOL(drm_calc_timestamping_constants); |
| 557 | |
| 558 | /** |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 559 | * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper |
| 560 | * @dev: DRM device |
| 561 | * @crtc: Which CRTC's vblank timestamp to retrieve |
| 562 | * @max_error: Desired maximum allowable error in timestamps (nanosecs) |
| 563 | * On return contains true maximum error of timestamp |
| 564 | * @vblank_time: Pointer to struct timeval which should receive the timestamp |
| 565 | * @flags: Flags to pass to driver: |
| 566 | * 0 = Default, |
| 567 | * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler |
| 568 | * @refcrtc: CRTC which defines scanout timing |
| 569 | * @mode: mode which defines the scanout timings |
| 570 | * |
| 571 | * Implements calculation of exact vblank timestamps from given drm_display_mode |
| 572 | * timings and current video scanout position of a CRTC. This can be called from |
| 573 | * within get_vblank_timestamp() implementation of a kms driver to implement the |
| 574 | * actual timestamping. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 575 | * |
| 576 | * Should return timestamps conforming to the OML_sync_control OpenML |
| 577 | * extension specification. The timestamp corresponds to the end of |
| 578 | * the vblank interval, aka start of scanout of topmost-leftmost display |
| 579 | * pixel in the following video frame. |
| 580 | * |
| 581 | * Requires support for optional dev->driver->get_scanout_position() |
| 582 | * in kms driver, plus a bit of setup code to provide a drm_display_mode |
| 583 | * that corresponds to the true scanout timing. |
| 584 | * |
| 585 | * The current implementation only handles standard video modes. It |
| 586 | * returns as no operation if a doublescan or interlaced video mode is |
| 587 | * active. Higher level code is expected to handle this. |
| 588 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 589 | * Returns: |
| 590 | * Negative value on error, failure or if not supported in current |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 591 | * video mode: |
| 592 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 593 | * -EINVAL - Invalid CRTC. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 594 | * -EAGAIN - Temporary unavailable, e.g., called before initial modeset. |
| 595 | * -ENOTSUPP - Function not supported in current display mode. |
| 596 | * -EIO - Failed, e.g., due to failed scanout position query. |
| 597 | * |
| 598 | * Returns or'ed positive status flags on success: |
| 599 | * |
| 600 | * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping. |
| 601 | * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval. |
| 602 | * |
| 603 | */ |
| 604 | int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc, |
| 605 | int *max_error, |
| 606 | struct timeval *vblank_time, |
| 607 | unsigned flags, |
Ville Syrjälä | 7da903e | 2013-10-26 17:57:31 +0300 | [diff] [blame] | 608 | const struct drm_crtc *refcrtc, |
| 609 | const struct drm_display_mode *mode) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 610 | { |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 611 | ktime_t stime, etime, mono_time_offset; |
| 612 | struct timeval tv_etime; |
Ville Syrjälä | 8072bfa | 2013-10-28 21:22:52 +0200 | [diff] [blame] | 613 | int vbl_status; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 614 | int vpos, hpos, i; |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 615 | int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 616 | bool invbl; |
| 617 | |
| 618 | if (crtc < 0 || crtc >= dev->num_crtcs) { |
| 619 | DRM_ERROR("Invalid crtc %d\n", crtc); |
| 620 | return -EINVAL; |
| 621 | } |
| 622 | |
| 623 | /* Scanout position query not supported? Should not happen. */ |
| 624 | if (!dev->driver->get_scanout_position) { |
| 625 | DRM_ERROR("Called from driver w/o get_scanout_position()!?\n"); |
| 626 | return -EIO; |
| 627 | } |
| 628 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 629 | /* Durations of frames, lines, pixels in nanoseconds. */ |
| 630 | framedur_ns = refcrtc->framedur_ns; |
| 631 | linedur_ns = refcrtc->linedur_ns; |
| 632 | pixeldur_ns = refcrtc->pixeldur_ns; |
| 633 | |
| 634 | /* If mode timing undefined, just return as no-op: |
| 635 | * Happens during initial modesetting of a crtc. |
| 636 | */ |
Ville Syrjälä | 8072bfa | 2013-10-28 21:22:52 +0200 | [diff] [blame] | 637 | if (framedur_ns == 0) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 638 | DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc); |
| 639 | return -EAGAIN; |
| 640 | } |
| 641 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 642 | /* Get current scanout position with system timestamp. |
| 643 | * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times |
| 644 | * if single query takes longer than max_error nanoseconds. |
| 645 | * |
| 646 | * This guarantees a tight bound on maximum error if |
| 647 | * code gets preempted or delayed for some reason. |
| 648 | */ |
| 649 | for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) { |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 650 | /* |
| 651 | * Get vertical and horizontal scanout position vpos, hpos, |
| 652 | * and bounding timestamps stime, etime, pre/post query. |
| 653 | */ |
Ville Syrjälä | abca9e4 | 2013-10-28 20:50:48 +0200 | [diff] [blame] | 654 | vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos, |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 655 | &hpos, &stime, &etime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 656 | |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 657 | /* |
| 658 | * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if |
| 659 | * CLOCK_REALTIME is requested. |
| 660 | */ |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 661 | if (!drm_timestamp_monotonic) |
| 662 | mono_time_offset = ktime_get_monotonic_offset(); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 663 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 664 | /* Return as no-op if scanout query unsupported or failed. */ |
| 665 | if (!(vbl_status & DRM_SCANOUTPOS_VALID)) { |
| 666 | DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n", |
| 667 | crtc, vbl_status); |
| 668 | return -EIO; |
| 669 | } |
| 670 | |
Mario Kleiner | 8f6fce0 | 2013-10-30 05:13:06 +0100 | [diff] [blame] | 671 | /* Compute uncertainty in timestamp of scanout position query. */ |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 672 | duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 673 | |
| 674 | /* Accept result with < max_error nsecs timing uncertainty. */ |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 675 | if (duration_ns <= *max_error) |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 676 | break; |
| 677 | } |
| 678 | |
| 679 | /* Noisy system timing? */ |
| 680 | if (i == DRM_TIMESTAMP_MAXRETRIES) { |
| 681 | DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n", |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 682 | crtc, duration_ns/1000, *max_error/1000, i); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* Return upper bound of timestamp precision error. */ |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 686 | *max_error = duration_ns; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 687 | |
| 688 | /* Check if in vblank area: |
| 689 | * vpos is >=0 in video scanout area, but negative |
| 690 | * within vblank area, counting down the number of lines until |
| 691 | * start of scanout. |
| 692 | */ |
| 693 | invbl = vbl_status & DRM_SCANOUTPOS_INVBL; |
| 694 | |
| 695 | /* Convert scanout position into elapsed time at raw_time query |
| 696 | * since start of scanout at first display scanline. delta_ns |
| 697 | * can be negative if start of scanout hasn't happened yet. |
| 698 | */ |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 699 | delta_ns = vpos * linedur_ns + hpos * pixeldur_ns; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 700 | |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 701 | if (!drm_timestamp_monotonic) |
| 702 | etime = ktime_sub(etime, mono_time_offset); |
| 703 | |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 704 | /* save this only for debugging purposes */ |
| 705 | tv_etime = ktime_to_timeval(etime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 706 | /* Subtract time delta from raw timestamp to get final |
| 707 | * vblank_time timestamp for end of vblank. |
| 708 | */ |
Michel Dänzer | e91abf8 | 2013-06-12 11:58:44 +0200 | [diff] [blame] | 709 | if (delta_ns < 0) |
| 710 | etime = ktime_add_ns(etime, -delta_ns); |
| 711 | else |
| 712 | etime = ktime_sub_ns(etime, delta_ns); |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 713 | *vblank_time = ktime_to_timeval(etime); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 714 | |
Joe Perches | bbb0aef | 2011-04-17 20:35:52 -0700 | [diff] [blame] | 715 | DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n", |
| 716 | crtc, (int)vbl_status, hpos, vpos, |
Imre Deak | e62f2f5 | 2012-10-23 18:53:25 +0000 | [diff] [blame] | 717 | (long)tv_etime.tv_sec, (long)tv_etime.tv_usec, |
Joe Perches | bbb0aef | 2011-04-17 20:35:52 -0700 | [diff] [blame] | 718 | (long)vblank_time->tv_sec, (long)vblank_time->tv_usec, |
Ville Syrjälä | 3c184f6 | 2013-10-26 17:38:52 +0300 | [diff] [blame] | 719 | duration_ns/1000, i); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 720 | |
| 721 | vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD; |
| 722 | if (invbl) |
| 723 | vbl_status |= DRM_VBLANKTIME_INVBL; |
| 724 | |
| 725 | return vbl_status; |
| 726 | } |
| 727 | EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); |
| 728 | |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 729 | static struct timeval get_drm_timestamp(void) |
| 730 | { |
| 731 | ktime_t now; |
| 732 | |
| 733 | now = ktime_get(); |
| 734 | if (!drm_timestamp_monotonic) |
| 735 | now = ktime_sub(now, ktime_get_monotonic_offset()); |
| 736 | |
| 737 | return ktime_to_timeval(now); |
| 738 | } |
| 739 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 740 | /** |
| 741 | * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 742 | * vblank interval |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 743 | * @dev: DRM device |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 744 | * @crtc: which CRTC's vblank timestamp to retrieve |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 745 | * @tvblank: Pointer to target struct timeval which should receive the timestamp |
| 746 | * @flags: Flags to pass to driver: |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 747 | * 0 = Default, |
| 748 | * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 749 | * |
| 750 | * Fetches the system timestamp corresponding to the time of the most recent |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 751 | * vblank interval on specified CRTC. May call into kms-driver to |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 752 | * compute the timestamp with a high-precision GPU specific method. |
| 753 | * |
| 754 | * Returns zero if timestamp originates from uncorrected do_gettimeofday() |
| 755 | * call, i.e., it isn't very precisely locked to the true vblank. |
| 756 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 757 | * Returns: |
| 758 | * Non-zero if timestamp is considered to be very precise, zero otherwise. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 759 | */ |
| 760 | u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc, |
| 761 | struct timeval *tvblank, unsigned flags) |
| 762 | { |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 763 | int ret; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 764 | |
| 765 | /* Define requested maximum error on timestamps (nanoseconds). */ |
| 766 | int max_error = (int) drm_timestamp_precision * 1000; |
| 767 | |
| 768 | /* Query driver if possible and precision timestamping enabled. */ |
| 769 | if (dev->driver->get_vblank_timestamp && (max_error > 0)) { |
| 770 | ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error, |
| 771 | tvblank, flags); |
| 772 | if (ret > 0) |
| 773 | return (u32) ret; |
| 774 | } |
| 775 | |
| 776 | /* GPU high precision timestamp query unsupported or failed. |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 777 | * Return current monotonic/gettimeofday timestamp as best estimate. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 778 | */ |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 779 | *tvblank = get_drm_timestamp(); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 780 | |
| 781 | return 0; |
| 782 | } |
| 783 | EXPORT_SYMBOL(drm_get_last_vbltimestamp); |
| 784 | |
| 785 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 786 | * drm_vblank_count - retrieve "cooked" vblank counter value |
| 787 | * @dev: DRM device |
| 788 | * @crtc: which counter to retrieve |
| 789 | * |
| 790 | * Fetches the "cooked" vblank count value that represents the number of |
| 791 | * vblank events since the system was booted, including lost events due to |
| 792 | * modesetting activity. |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 793 | * |
| 794 | * Returns: |
| 795 | * The software vblank counter. |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 796 | */ |
| 797 | u32 drm_vblank_count(struct drm_device *dev, int crtc) |
| 798 | { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 799 | return atomic_read(&dev->vblank[crtc].count); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 800 | } |
| 801 | EXPORT_SYMBOL(drm_vblank_count); |
| 802 | |
| 803 | /** |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 804 | * drm_vblank_count_and_time - retrieve "cooked" vblank counter value |
| 805 | * and the system timestamp corresponding to that vblank counter value. |
| 806 | * |
| 807 | * @dev: DRM device |
| 808 | * @crtc: which counter to retrieve |
| 809 | * @vblanktime: Pointer to struct timeval to receive the vblank timestamp. |
| 810 | * |
| 811 | * Fetches the "cooked" vblank count value that represents the number of |
| 812 | * vblank events since the system was booted, including lost events due to |
| 813 | * modesetting activity. Returns corresponding system timestamp of the time |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 814 | * of the vblank interval that corresponds to the current vblank counter value. |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 815 | */ |
| 816 | u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc, |
| 817 | struct timeval *vblanktime) |
| 818 | { |
| 819 | u32 cur_vblank; |
| 820 | |
| 821 | /* Read timestamp from slot of _vblank_time ringbuffer |
| 822 | * that corresponds to current vblank count. Retry if |
| 823 | * count has incremented during readout. This works like |
| 824 | * a seqlock. |
| 825 | */ |
| 826 | do { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 827 | cur_vblank = atomic_read(&dev->vblank[crtc].count); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 828 | *vblanktime = vblanktimestamp(dev, crtc, cur_vblank); |
| 829 | smp_rmb(); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 830 | } while (cur_vblank != atomic_read(&dev->vblank[crtc].count)); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 831 | |
| 832 | return cur_vblank; |
| 833 | } |
| 834 | EXPORT_SYMBOL(drm_vblank_count_and_time); |
| 835 | |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 836 | static void send_vblank_event(struct drm_device *dev, |
| 837 | struct drm_pending_vblank_event *e, |
| 838 | unsigned long seq, struct timeval *now) |
| 839 | { |
| 840 | WARN_ON_SMP(!spin_is_locked(&dev->event_lock)); |
| 841 | e->event.sequence = seq; |
| 842 | e->event.tv_sec = now->tv_sec; |
| 843 | e->event.tv_usec = now->tv_usec; |
| 844 | |
| 845 | list_add_tail(&e->base.link, |
| 846 | &e->base.file_priv->event_list); |
| 847 | wake_up_interruptible(&e->base.file_priv->event_wait); |
| 848 | trace_drm_vblank_event_delivered(e->base.pid, e->pipe, |
| 849 | e->event.sequence); |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * drm_send_vblank_event - helper to send vblank event after pageflip |
| 854 | * @dev: DRM device |
| 855 | * @crtc: CRTC in question |
| 856 | * @e: the event to send |
| 857 | * |
| 858 | * Updates sequence # and timestamp on event, and sends it to userspace. |
| 859 | * Caller must hold event lock. |
| 860 | */ |
| 861 | void drm_send_vblank_event(struct drm_device *dev, int crtc, |
| 862 | struct drm_pending_vblank_event *e) |
| 863 | { |
| 864 | struct timeval now; |
| 865 | unsigned int seq; |
| 866 | if (crtc >= 0) { |
| 867 | seq = drm_vblank_count_and_time(dev, crtc, &now); |
| 868 | } else { |
| 869 | seq = 0; |
Imre Deak | c61eef7 | 2012-10-23 18:53:26 +0000 | [diff] [blame] | 870 | |
| 871 | now = get_drm_timestamp(); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 872 | } |
Rob Clark | 21a245d | 2012-12-10 10:49:46 -0600 | [diff] [blame] | 873 | e->pipe = crtc; |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 874 | send_vblank_event(dev, e, seq, &now); |
| 875 | } |
| 876 | EXPORT_SYMBOL(drm_send_vblank_event); |
| 877 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 878 | /** |
Ville Syrjälä | f275228 | 2014-02-19 21:29:49 +0200 | [diff] [blame] | 879 | * drm_vblank_enable - enable the vblank interrupt on a CRTC |
| 880 | * @dev: DRM device |
| 881 | * @crtc: CRTC in question |
| 882 | */ |
| 883 | static int drm_vblank_enable(struct drm_device *dev, int crtc) |
| 884 | { |
| 885 | int ret = 0; |
| 886 | |
| 887 | assert_spin_locked(&dev->vbl_lock); |
| 888 | |
| 889 | spin_lock(&dev->vblank_time_lock); |
| 890 | |
| 891 | if (!dev->vblank[crtc].enabled) { |
Daniel Vetter | 1cfb80b | 2014-05-21 15:11:33 +0200 | [diff] [blame] | 892 | /* |
| 893 | * Enable vblank irqs under vblank_time_lock protection. |
Ville Syrjälä | f275228 | 2014-02-19 21:29:49 +0200 | [diff] [blame] | 894 | * All vblank count & timestamp updates are held off |
| 895 | * until we are done reinitializing master counter and |
| 896 | * timestamps. Filtercode in drm_handle_vblank() will |
| 897 | * prevent double-accounting of same vblank interval. |
| 898 | */ |
| 899 | ret = dev->driver->enable_vblank(dev, crtc); |
| 900 | DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret); |
| 901 | if (ret) |
| 902 | atomic_dec(&dev->vblank[crtc].refcount); |
| 903 | else { |
| 904 | dev->vblank[crtc].enabled = true; |
| 905 | drm_update_vblank_count(dev, crtc); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | spin_unlock(&dev->vblank_time_lock); |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
| 914 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 915 | * drm_vblank_get - get a reference count on vblank events |
| 916 | * @dev: DRM device |
| 917 | * @crtc: which CRTC to own |
| 918 | * |
| 919 | * Acquire a reference count on vblank events to avoid having them disabled |
| 920 | * while in use. |
| 921 | * |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 922 | * This is the legacy version of drm_crtc_vblank_get(). |
| 923 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 924 | * Returns: |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 925 | * Zero on success, nonzero on failure. |
| 926 | */ |
| 927 | int drm_vblank_get(struct drm_device *dev, int crtc) |
| 928 | { |
Peter Hurley | 97cbc88 | 2013-08-20 21:51:12 -0400 | [diff] [blame] | 929 | unsigned long irqflags; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 930 | int ret = 0; |
| 931 | |
| 932 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 933 | /* Going from 0->1 means we have to enable interrupts again */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 934 | if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) { |
Ville Syrjälä | f275228 | 2014-02-19 21:29:49 +0200 | [diff] [blame] | 935 | ret = drm_vblank_enable(dev, crtc); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 936 | } else { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 937 | if (!dev->vblank[crtc].enabled) { |
| 938 | atomic_dec(&dev->vblank[crtc].refcount); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 939 | ret = -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 943 | |
| 944 | return ret; |
| 945 | } |
| 946 | EXPORT_SYMBOL(drm_vblank_get); |
| 947 | |
| 948 | /** |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 949 | * drm_crtc_vblank_get - get a reference count on vblank events |
| 950 | * @crtc: which CRTC to own |
| 951 | * |
| 952 | * Acquire a reference count on vblank events to avoid having them disabled |
| 953 | * while in use. |
| 954 | * |
| 955 | * This is the native kms version of drm_vblank_off(). |
| 956 | * |
| 957 | * Returns: |
| 958 | * Zero on success, nonzero on failure. |
| 959 | */ |
| 960 | int drm_crtc_vblank_get(struct drm_crtc *crtc) |
| 961 | { |
| 962 | return drm_vblank_get(crtc->dev, drm_crtc_index(crtc)); |
| 963 | } |
| 964 | EXPORT_SYMBOL(drm_crtc_vblank_get); |
| 965 | |
| 966 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 967 | * drm_vblank_put - give up ownership of vblank events |
| 968 | * @dev: DRM device |
| 969 | * @crtc: which counter to give up |
| 970 | * |
| 971 | * Release ownership of a given vblank counter, turning off interrupts |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 972 | * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 973 | * |
| 974 | * This is the legacy version of drm_crtc_vblank_put(). |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 975 | */ |
| 976 | void drm_vblank_put(struct drm_device *dev, int crtc) |
| 977 | { |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 978 | BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0); |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 979 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 980 | /* Last user schedules interrupt disable */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 981 | if (atomic_dec_and_test(&dev->vblank[crtc].refcount) && |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 982 | (drm_vblank_offdelay > 0)) |
Ville Syrjälä | e69595c | 2014-02-19 19:36:08 +0200 | [diff] [blame] | 983 | mod_timer(&dev->vblank[crtc].disable_timer, |
Daniel Vetter | bfd8303 | 2013-12-11 11:34:41 +0100 | [diff] [blame] | 984 | jiffies + ((drm_vblank_offdelay * HZ)/1000)); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 985 | } |
| 986 | EXPORT_SYMBOL(drm_vblank_put); |
| 987 | |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 988 | /** |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 989 | * drm_crtc_vblank_put - give up ownership of vblank events |
| 990 | * @crtc: which counter to give up |
| 991 | * |
| 992 | * Release ownership of a given vblank counter, turning off interrupts |
| 993 | * if possible. Disable interrupts after drm_vblank_offdelay milliseconds. |
| 994 | * |
| 995 | * This is the native kms version of drm_vblank_put(). |
| 996 | */ |
| 997 | void drm_crtc_vblank_put(struct drm_crtc *crtc) |
| 998 | { |
| 999 | drm_vblank_put(crtc->dev, drm_crtc_index(crtc)); |
| 1000 | } |
| 1001 | EXPORT_SYMBOL(drm_crtc_vblank_put); |
| 1002 | |
| 1003 | /** |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1004 | * drm_vblank_off - disable vblank events on a CRTC |
| 1005 | * @dev: DRM device |
| 1006 | * @crtc: CRTC in question |
| 1007 | * |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1008 | * Drivers can use this function to shut down the vblank interrupt handling when |
| 1009 | * disabling a crtc. This function ensures that the latest vblank frame count is |
| 1010 | * stored so that drm_vblank_on() can restore it again. |
| 1011 | * |
| 1012 | * Drivers must use this function when the hardware vblank counter can get |
| 1013 | * reset, e.g. when suspending. |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 1014 | * |
| 1015 | * This is the legacy version of drm_crtc_vblank_off(). |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1016 | */ |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 1017 | void drm_vblank_off(struct drm_device *dev, int crtc) |
| 1018 | { |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1019 | struct drm_pending_vblank_event *e, *t; |
| 1020 | struct timeval now; |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 1021 | unsigned long irqflags; |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1022 | unsigned int seq; |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 1023 | |
| 1024 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1025 | vblank_disable_and_save(dev, crtc); |
Daniel Vetter | 57ed0f7 | 2013-12-11 11:34:43 +0100 | [diff] [blame] | 1026 | wake_up(&dev->vblank[crtc].queue); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1027 | |
| 1028 | /* Send any queued vblank events, lest the natives grow disquiet */ |
| 1029 | seq = drm_vblank_count_and_time(dev, crtc, &now); |
Imre Deak | e7783ba | 2012-11-02 13:30:50 +0200 | [diff] [blame] | 1030 | |
| 1031 | spin_lock(&dev->event_lock); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1032 | list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { |
| 1033 | if (e->pipe != crtc) |
| 1034 | continue; |
| 1035 | DRM_DEBUG("Sending premature vblank event on disable: \ |
| 1036 | wanted %d, current %d\n", |
| 1037 | e->event.sequence, seq); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1038 | list_del(&e->base.link); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1039 | drm_vblank_put(dev, e->pipe); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1040 | send_vblank_event(dev, e, seq, &now); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1041 | } |
Imre Deak | e7783ba | 2012-11-02 13:30:50 +0200 | [diff] [blame] | 1042 | spin_unlock(&dev->event_lock); |
Christopher James Halse Rogers | 498548e | 2011-04-27 16:10:57 +1000 | [diff] [blame] | 1043 | |
Ville Syrjälä | 7ffd7a6 | 2014-08-06 14:49:44 +0300 | [diff] [blame] | 1044 | /* |
| 1045 | * Prevent subsequent drm_vblank_get() from re-enabling |
| 1046 | * the vblank interrupt by bumping the refcount. |
| 1047 | */ |
| 1048 | if (!dev->vblank[crtc].inmodeset) { |
| 1049 | atomic_inc(&dev->vblank[crtc].refcount); |
| 1050 | dev->vblank[crtc].inmodeset = 1; |
| 1051 | } |
| 1052 | |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 1053 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(drm_vblank_off); |
| 1056 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1057 | /** |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 1058 | * drm_crtc_vblank_off - disable vblank events on a CRTC |
| 1059 | * @crtc: CRTC in question |
| 1060 | * |
| 1061 | * Drivers can use this function to shut down the vblank interrupt handling when |
| 1062 | * disabling a crtc. This function ensures that the latest vblank frame count is |
| 1063 | * stored so that drm_vblank_on can restore it again. |
| 1064 | * |
| 1065 | * Drivers must use this function when the hardware vblank counter can get |
| 1066 | * reset, e.g. when suspending. |
| 1067 | * |
| 1068 | * This is the native kms version of drm_vblank_off(). |
| 1069 | */ |
| 1070 | void drm_crtc_vblank_off(struct drm_crtc *crtc) |
| 1071 | { |
| 1072 | drm_vblank_off(crtc->dev, drm_crtc_index(crtc)); |
| 1073 | } |
| 1074 | EXPORT_SYMBOL(drm_crtc_vblank_off); |
| 1075 | |
| 1076 | /** |
Ville Syrjälä | f275228 | 2014-02-19 21:29:49 +0200 | [diff] [blame] | 1077 | * drm_vblank_on - enable vblank events on a CRTC |
| 1078 | * @dev: DRM device |
| 1079 | * @crtc: CRTC in question |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * This functions restores the vblank interrupt state captured with |
| 1082 | * drm_vblank_off() again. Note that calls to drm_vblank_on() and |
| 1083 | * drm_vblank_off() can be unbalanced and so can also be unconditionaly called |
| 1084 | * in driver load code to reflect the current hardware state of the crtc. |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 1085 | * |
| 1086 | * This is the legacy version of drm_crtc_vblank_on(). |
Ville Syrjälä | f275228 | 2014-02-19 21:29:49 +0200 | [diff] [blame] | 1087 | */ |
| 1088 | void drm_vblank_on(struct drm_device *dev, int crtc) |
| 1089 | { |
| 1090 | unsigned long irqflags; |
| 1091 | |
| 1092 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Ville Syrjälä | 7ffd7a6 | 2014-08-06 14:49:44 +0300 | [diff] [blame] | 1093 | /* Drop our private "prevent drm_vblank_get" refcount */ |
| 1094 | if (dev->vblank[crtc].inmodeset) { |
| 1095 | atomic_dec(&dev->vblank[crtc].refcount); |
| 1096 | dev->vblank[crtc].inmodeset = 0; |
| 1097 | } |
Ville Syrjälä | f8ad028 | 2014-08-06 14:49:49 +0300 | [diff] [blame^] | 1098 | |
| 1099 | /* |
| 1100 | * sample the current counter to avoid random jumps |
| 1101 | * when drm_vblank_enable() applies the diff |
| 1102 | * |
| 1103 | * -1 to make sure user will never see the same |
| 1104 | * vblank counter value before and after a modeset |
| 1105 | */ |
| 1106 | dev->vblank[crtc].last = |
| 1107 | (dev->driver->get_vblank_counter(dev, crtc) - 1) & |
| 1108 | dev->max_vblank_count; |
| 1109 | |
Ville Syrjälä | f275228 | 2014-02-19 21:29:49 +0200 | [diff] [blame] | 1110 | /* re-enable interrupts if there's are users left */ |
| 1111 | if (atomic_read(&dev->vblank[crtc].refcount) != 0) |
| 1112 | WARN_ON(drm_vblank_enable(dev, crtc)); |
| 1113 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 1114 | } |
| 1115 | EXPORT_SYMBOL(drm_vblank_on); |
| 1116 | |
| 1117 | /** |
Daniel Vetter | 89dd6a4 | 2014-05-15 15:32:12 +0200 | [diff] [blame] | 1118 | * drm_crtc_vblank_on - enable vblank events on a CRTC |
| 1119 | * @crtc: CRTC in question |
| 1120 | * |
| 1121 | * This functions restores the vblank interrupt state captured with |
| 1122 | * drm_vblank_off() again. Note that calls to drm_vblank_on() and |
| 1123 | * drm_vblank_off() can be unbalanced and so can also be unconditionaly called |
| 1124 | * in driver load code to reflect the current hardware state of the crtc. |
| 1125 | * |
| 1126 | * This is the native kms version of drm_vblank_on(). |
| 1127 | */ |
| 1128 | void drm_crtc_vblank_on(struct drm_crtc *crtc) |
| 1129 | { |
| 1130 | drm_vblank_on(crtc->dev, drm_crtc_index(crtc)); |
| 1131 | } |
| 1132 | EXPORT_SYMBOL(drm_crtc_vblank_on); |
| 1133 | |
| 1134 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1135 | * drm_vblank_pre_modeset - account for vblanks across mode sets |
| 1136 | * @dev: DRM device |
| 1137 | * @crtc: CRTC in question |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1138 | * |
| 1139 | * Account for vblank events across mode setting events, which will likely |
| 1140 | * reset the hardware frame counter. |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1141 | * |
| 1142 | * This is done by grabbing a temporary vblank reference to ensure that the |
| 1143 | * vblank interrupt keeps running across the modeset sequence. With this the |
| 1144 | * software-side vblank frame counting will ensure that there are no jumps or |
| 1145 | * discontinuities. |
| 1146 | * |
| 1147 | * Unfortunately this approach is racy and also doesn't work when the vblank |
| 1148 | * interrupt stops running, e.g. across system suspend resume. It is therefore |
| 1149 | * highly recommended that drivers use the newer drm_vblank_off() and |
| 1150 | * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when |
| 1151 | * using "cooked" software vblank frame counters and not relying on any hardware |
| 1152 | * counters. |
| 1153 | * |
| 1154 | * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc |
| 1155 | * again. |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1156 | */ |
| 1157 | void drm_vblank_pre_modeset(struct drm_device *dev, int crtc) |
| 1158 | { |
Huacai Chen | b7ea85a4 | 2013-05-21 06:23:43 +0000 | [diff] [blame] | 1159 | /* vblank is not initialized (IRQ not installed ?), or has been freed */ |
Jerome Glisse | e77cef9 | 2010-01-07 15:39:13 +0100 | [diff] [blame] | 1160 | if (!dev->num_crtcs) |
| 1161 | return; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1162 | /* |
| 1163 | * To avoid all the problems that might happen if interrupts |
| 1164 | * were enabled/disabled around or between these calls, we just |
| 1165 | * have the kernel take a reference on the CRTC (just once though |
| 1166 | * to avoid corrupting the count if multiple, mismatch calls occur), |
| 1167 | * so that interrupts remain enabled in the interim. |
| 1168 | */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1169 | if (!dev->vblank[crtc].inmodeset) { |
| 1170 | dev->vblank[crtc].inmodeset = 0x1; |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 1171 | if (drm_vblank_get(dev, crtc) == 0) |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1172 | dev->vblank[crtc].inmodeset |= 0x2; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | EXPORT_SYMBOL(drm_vblank_pre_modeset); |
| 1176 | |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1177 | /** |
| 1178 | * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes |
| 1179 | * @dev: DRM device |
| 1180 | * @crtc: CRTC in question |
| 1181 | * |
| 1182 | * This function again drops the temporary vblank reference acquired in |
| 1183 | * drm_vblank_pre_modeset. |
| 1184 | */ |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1185 | void drm_vblank_post_modeset(struct drm_device *dev, int crtc) |
| 1186 | { |
| 1187 | unsigned long irqflags; |
| 1188 | |
Huacai Chen | b7ea85a4 | 2013-05-21 06:23:43 +0000 | [diff] [blame] | 1189 | /* vblank is not initialized (IRQ not installed ?), or has been freed */ |
| 1190 | if (!dev->num_crtcs) |
| 1191 | return; |
| 1192 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1193 | if (dev->vblank[crtc].inmodeset) { |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1194 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Ville Syrjälä | ba0bf12 | 2013-10-04 14:53:33 +0300 | [diff] [blame] | 1195 | dev->vblank_disable_allowed = true; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1196 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 1197 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1198 | if (dev->vblank[crtc].inmodeset & 0x2) |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 1199 | drm_vblank_put(dev, crtc); |
| 1200 | |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1201 | dev->vblank[crtc].inmodeset = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | EXPORT_SYMBOL(drm_vblank_post_modeset); |
| 1205 | |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1206 | /* |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1207 | * drm_modeset_ctl - handle vblank event counter changes across mode switch |
| 1208 | * @DRM_IOCTL_ARGS: standard ioctl arguments |
| 1209 | * |
| 1210 | * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET |
| 1211 | * ioctls around modesetting so that any lost vblank events are accounted for. |
| 1212 | * |
| 1213 | * Generally the counter will reset across mode sets. If interrupts are |
| 1214 | * enabled around this call, we don't have to do anything since the counter |
| 1215 | * will have already been incremented. |
| 1216 | */ |
| 1217 | int drm_modeset_ctl(struct drm_device *dev, void *data, |
| 1218 | struct drm_file *file_priv) |
| 1219 | { |
| 1220 | struct drm_modeset_ctl *modeset = data; |
Dave Airlie | 1922756 | 2011-02-24 08:35:06 +1000 | [diff] [blame] | 1221 | unsigned int crtc; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1222 | |
| 1223 | /* If drm_vblank_init() hasn't been called yet, just no-op */ |
| 1224 | if (!dev->num_crtcs) |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1225 | return 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1226 | |
Laurent Pinchart | 2993555 | 2012-05-30 00:58:09 +0200 | [diff] [blame] | 1227 | /* KMS drivers handle this internally */ |
| 1228 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 1229 | return 0; |
| 1230 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1231 | crtc = modeset->crtc; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1232 | if (crtc >= dev->num_crtcs) |
| 1233 | return -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1234 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1235 | switch (modeset->cmd) { |
| 1236 | case _DRM_PRE_MODESET: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1237 | drm_vblank_pre_modeset(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1238 | break; |
| 1239 | case _DRM_POST_MODESET: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 1240 | drm_vblank_post_modeset(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1241 | break; |
| 1242 | default: |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1243 | return -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1246 | return 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1249 | static int drm_queue_vblank_event(struct drm_device *dev, int pipe, |
| 1250 | union drm_wait_vblank *vblwait, |
| 1251 | struct drm_file *file_priv) |
| 1252 | { |
| 1253 | struct drm_pending_vblank_event *e; |
| 1254 | struct timeval now; |
| 1255 | unsigned long flags; |
| 1256 | unsigned int seq; |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1257 | int ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1258 | |
| 1259 | e = kzalloc(sizeof *e, GFP_KERNEL); |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1260 | if (e == NULL) { |
| 1261 | ret = -ENOMEM; |
| 1262 | goto err_put; |
| 1263 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1264 | |
| 1265 | e->pipe = pipe; |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 1266 | e->base.pid = current->pid; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1267 | e->event.base.type = DRM_EVENT_VBLANK; |
| 1268 | e->event.base.length = sizeof e->event; |
| 1269 | e->event.user_data = vblwait->request.signal; |
| 1270 | e->base.event = &e->event.base; |
| 1271 | e->base.file_priv = file_priv; |
| 1272 | e->base.destroy = (void (*) (struct drm_pending_event *)) kfree; |
| 1273 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1274 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1275 | |
| 1276 | if (file_priv->event_space < sizeof e->event) { |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1277 | ret = -EBUSY; |
| 1278 | goto err_unlock; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | file_priv->event_space -= sizeof e->event; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1282 | seq = drm_vblank_count_and_time(dev, pipe, &now); |
| 1283 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1284 | if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) && |
| 1285 | (seq - vblwait->request.sequence) <= (1 << 23)) { |
| 1286 | vblwait->request.sequence = seq + 1; |
Jesse Barnes | 4a92164 | 2009-11-10 08:21:25 +0000 | [diff] [blame] | 1287 | vblwait->reply.sequence = vblwait->request.sequence; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n", |
| 1291 | vblwait->request.sequence, seq, pipe); |
| 1292 | |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 1293 | trace_drm_vblank_event_queued(current->pid, pipe, |
| 1294 | vblwait->request.sequence); |
| 1295 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1296 | e->event.sequence = vblwait->request.sequence; |
| 1297 | if ((seq - vblwait->request.sequence) <= (1 << 23)) { |
Dan Carpenter | 6f33162 | 2010-12-09 08:35:40 +0300 | [diff] [blame] | 1298 | drm_vblank_put(dev, pipe); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1299 | send_vblank_event(dev, e, seq, &now); |
Mario Kleiner | 000fa7c | 2010-12-10 16:00:19 +0100 | [diff] [blame] | 1300 | vblwait->reply.sequence = seq; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1301 | } else { |
Ilija Hadzic | a6778e9 | 2011-10-31 13:11:57 -0400 | [diff] [blame] | 1302 | /* drm_handle_vblank_events will call drm_vblank_put */ |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1303 | list_add_tail(&e->base.link, &dev->vblank_event_list); |
Mario Kleiner | 000fa7c | 2010-12-10 16:00:19 +0100 | [diff] [blame] | 1304 | vblwait->reply.sequence = vblwait->request.sequence; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1308 | |
| 1309 | return 0; |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1310 | |
| 1311 | err_unlock: |
| 1312 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 1313 | kfree(e); |
| 1314 | err_put: |
Dan Carpenter | 6f33162 | 2010-12-09 08:35:40 +0300 | [diff] [blame] | 1315 | drm_vblank_put(dev, pipe); |
Chris Wilson | ea5d552 | 2010-12-01 19:41:31 +0000 | [diff] [blame] | 1316 | return ret; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1317 | } |
| 1318 | |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1319 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | * Wait for VBLANK. |
| 1321 | * |
| 1322 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 1323 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | * \param cmd command. |
| 1325 | * \param data user argument, pointing to a drm_wait_vblank structure. |
| 1326 | * \return zero on success or a negative number on failure. |
| 1327 | * |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1328 | * This function enables the vblank interrupt on the pipe requested, then |
| 1329 | * sleeps waiting for the requested sequence number to occur, and drops |
Daniel Vetter | f5752b3 | 2014-05-08 16:41:51 +0200 | [diff] [blame] | 1330 | * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1331 | * after a timeout with no further vblank waits scheduled). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | */ |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1333 | int drm_wait_vblank(struct drm_device *dev, void *data, |
| 1334 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1336 | union drm_wait_vblank *vblwait = data; |
Laurent Pinchart | 4a1b071 | 2012-05-17 13:27:21 +0200 | [diff] [blame] | 1337 | int ret; |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1338 | unsigned int flags, seq, crtc, high_crtc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
Daniel Vetter | 4984979 | 2013-08-28 15:02:37 +0200 | [diff] [blame] | 1340 | if (!dev->irq_enabled) |
| 1341 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1343 | if (vblwait->request.type & _DRM_VBLANK_SIGNAL) |
| 1344 | return -EINVAL; |
| 1345 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1346 | if (vblwait->request.type & |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1347 | ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | |
| 1348 | _DRM_VBLANK_HIGH_CRTC_MASK)) { |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1349 | DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1350 | vblwait->request.type, |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1351 | (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK | |
| 1352 | _DRM_VBLANK_HIGH_CRTC_MASK)); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1353 | return -EINVAL; |
| 1354 | } |
| 1355 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1356 | flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; |
Ilija Hadzic | 19b01b5 | 2011-03-18 16:58:04 -0500 | [diff] [blame] | 1357 | high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK); |
| 1358 | if (high_crtc) |
| 1359 | crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT; |
| 1360 | else |
| 1361 | crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1362 | if (crtc >= dev->num_crtcs) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1363 | return -EINVAL; |
| 1364 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1365 | ret = drm_vblank_get(dev, crtc); |
| 1366 | if (ret) { |
Paul Rolland | 8d3457e | 2009-08-09 12:24:01 +1000 | [diff] [blame] | 1367 | DRM_DEBUG("failed to acquire vblank counter, %d\n", ret); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1368 | return ret; |
| 1369 | } |
| 1370 | seq = drm_vblank_count(dev, crtc); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 1371 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1372 | switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | case _DRM_VBLANK_RELATIVE: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1374 | vblwait->request.sequence += seq; |
| 1375 | vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | case _DRM_VBLANK_ABSOLUTE: |
| 1377 | break; |
| 1378 | default: |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1379 | ret = -EINVAL; |
| 1380 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Ilija Hadzic | a6778e9 | 2011-10-31 13:11:57 -0400 | [diff] [blame] | 1383 | if (flags & _DRM_VBLANK_EVENT) { |
| 1384 | /* must hold on to the vblank ref until the event fires |
| 1385 | * drm_vblank_put will be called asynchronously |
| 1386 | */ |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1387 | return drm_queue_vblank_event(dev, crtc, vblwait, file_priv); |
Ilija Hadzic | a6778e9 | 2011-10-31 13:11:57 -0400 | [diff] [blame] | 1388 | } |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1389 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 1390 | if ((flags & _DRM_VBLANK_NEXTONMISS) && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 1391 | (seq - vblwait->request.sequence) <= (1<<23)) { |
| 1392 | vblwait->request.sequence = seq + 1; |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 1393 | } |
| 1394 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1395 | DRM_DEBUG("waiting on vblank count %d, crtc %d\n", |
| 1396 | vblwait->request.sequence, crtc); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1397 | dev->vblank[crtc].last_wait = vblwait->request.sequence; |
Daniel Vetter | bfd8303 | 2013-12-11 11:34:41 +0100 | [diff] [blame] | 1398 | DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * HZ, |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1399 | (((drm_vblank_count(dev, crtc) - |
| 1400 | vblwait->request.sequence) <= (1 << 23)) || |
Ville Syrjälä | 3212a22 | 2014-03-06 17:27:39 +0200 | [diff] [blame] | 1401 | !dev->vblank[crtc].enabled || |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1402 | !dev->irq_enabled)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1404 | if (ret != -EINTR) { |
| 1405 | struct timeval now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1407 | vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now); |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1408 | vblwait->reply.tval_sec = now.tv_sec; |
| 1409 | vblwait->reply.tval_usec = now.tv_usec; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1410 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1411 | DRM_DEBUG("returning %d to client\n", |
| 1412 | vblwait->reply.sequence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } else { |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 1414 | DRM_DEBUG("vblank wait interrupted by signal\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1417 | done: |
| 1418 | drm_vblank_put(dev, crtc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | return ret; |
| 1420 | } |
| 1421 | |
Sachin Kamat | ea7f7ab | 2012-08-01 17:15:31 +0530 | [diff] [blame] | 1422 | 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] | 1423 | { |
| 1424 | struct drm_pending_vblank_event *e, *t; |
| 1425 | struct timeval now; |
| 1426 | unsigned long flags; |
| 1427 | unsigned int seq; |
| 1428 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1429 | seq = drm_vblank_count_and_time(dev, crtc, &now); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1430 | |
| 1431 | spin_lock_irqsave(&dev->event_lock, flags); |
| 1432 | |
| 1433 | list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { |
| 1434 | if (e->pipe != crtc) |
| 1435 | continue; |
| 1436 | if ((seq - e->event.sequence) > (1<<23)) |
| 1437 | continue; |
| 1438 | |
| 1439 | DRM_DEBUG("vblank event on %d, current %d\n", |
| 1440 | e->event.sequence, seq); |
| 1441 | |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1442 | list_del(&e->base.link); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1443 | drm_vblank_put(dev, e->pipe); |
Rob Clark | c6eefa1 | 2012-10-16 22:48:40 +0000 | [diff] [blame] | 1444 | send_vblank_event(dev, e, seq, &now); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Jesse Barnes | ac2874b | 2010-07-01 16:47:31 -0700 | [diff] [blame] | 1448 | |
| 1449 | trace_drm_vblank_event(crtc, seq); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1450 | } |
| 1451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1453 | * drm_handle_vblank - handle a vblank event |
| 1454 | * @dev: DRM device |
| 1455 | * @crtc: where this event occurred |
| 1456 | * |
| 1457 | * Drivers should call this routine in their vblank interrupt handlers to |
| 1458 | * update the vblank counter and send any signals that may be pending. |
| 1459 | */ |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1460 | bool drm_handle_vblank(struct drm_device *dev, int crtc) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1461 | { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1462 | u32 vblcount; |
| 1463 | s64 diff_ns; |
| 1464 | struct timeval tvblank; |
| 1465 | unsigned long irqflags; |
| 1466 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1467 | if (!dev->num_crtcs) |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1468 | return false; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1469 | |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1470 | /* Need timestamp lock to prevent concurrent execution with |
| 1471 | * vblank enable/disable, as this would cause inconsistent |
| 1472 | * or corrupted timestamps and vblank counts. |
| 1473 | */ |
| 1474 | spin_lock_irqsave(&dev->vblank_time_lock, irqflags); |
| 1475 | |
| 1476 | /* Vblank irq handling disabled. Nothing to do. */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1477 | if (!dev->vblank[crtc].enabled) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1478 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1479 | return false; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | /* Fetch corresponding timestamp for this vblank interval from |
| 1483 | * driver and store it in proper slot of timestamp ringbuffer. |
| 1484 | */ |
| 1485 | |
| 1486 | /* Get current timestamp and count. */ |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1487 | vblcount = atomic_read(&dev->vblank[crtc].count); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1488 | drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ); |
| 1489 | |
| 1490 | /* Compute time difference to timestamp of last vblank */ |
| 1491 | diff_ns = timeval_to_ns(&tvblank) - |
| 1492 | timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount)); |
| 1493 | |
| 1494 | /* Update vblank timestamp and count if at least |
| 1495 | * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds |
| 1496 | * difference between last stored timestamp and current |
| 1497 | * timestamp. A smaller difference means basically |
| 1498 | * identical timestamps. Happens if this vblank has |
| 1499 | * been already processed and this is a redundant call, |
| 1500 | * e.g., due to spurious vblank interrupts. We need to |
| 1501 | * ignore those for accounting. |
| 1502 | */ |
Mario Kleiner | c4cc383 | 2011-02-21 05:42:00 +0100 | [diff] [blame] | 1503 | if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) { |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1504 | /* Store new timestamp in ringbuffer. */ |
| 1505 | vblanktimestamp(dev, crtc, vblcount + 1) = tvblank; |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1506 | |
| 1507 | /* Increment cooked vblank count. This also atomically commits |
| 1508 | * the timestamp computed above. |
| 1509 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1510 | smp_mb__before_atomic(); |
Ville Syrjälä | 5380e92 | 2013-10-04 14:53:36 +0300 | [diff] [blame] | 1511 | atomic_inc(&dev->vblank[crtc].count); |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 1512 | smp_mb__after_atomic(); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1513 | } else { |
| 1514 | DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n", |
| 1515 | crtc, (int) diff_ns); |
| 1516 | } |
| 1517 | |
Daniel Vetter | 57ed0f7 | 2013-12-11 11:34:43 +0100 | [diff] [blame] | 1518 | wake_up(&dev->vblank[crtc].queue); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 1519 | drm_handle_vblank_events(dev, crtc); |
Mario Kleiner | 27641c3 | 2010-10-23 04:20:23 +0200 | [diff] [blame] | 1520 | |
| 1521 | spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags); |
Chris Wilson | 78c6e17 | 2011-01-31 10:48:04 +0000 | [diff] [blame] | 1522 | return true; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 1523 | } |
| 1524 | EXPORT_SYMBOL(drm_handle_vblank); |