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