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 | |
| 36 | #include "drmP.h" |
| 37 | |
| 38 | #include <linux/interrupt.h> /* For task queue support */ |
| 39 | |
| 40 | /** |
| 41 | * Get interrupt from bus id. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 42 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 44 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * \param cmd command. |
| 46 | * \param arg user argument, pointing to a drm_irq_busid structure. |
| 47 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 48 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | * Finds the PCI device with the specified bus id and gets its IRQ number. |
| 50 | * This IOCTL is deprecated, and will now return EINVAL for any busid not equal |
| 51 | * to that of the device that this DRM instance attached to. |
| 52 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 53 | int drm_irq_by_busid(struct drm_device *dev, void *data, |
| 54 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 56 | struct drm_irq_busid *p = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 59 | return -EINVAL; |
| 60 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 61 | if ((p->busnum >> 8) != drm_get_pci_domain(dev) || |
| 62 | (p->busnum & 0xff) != dev->pdev->bus->number || |
| 63 | p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | return -EINVAL; |
| 65 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 66 | p->irq = dev->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 68 | DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum, |
| 69 | p->irq); |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 74 | static void vblank_disable_fn(unsigned long arg) |
| 75 | { |
| 76 | struct drm_device *dev = (struct drm_device *)arg; |
| 77 | unsigned long irqflags; |
| 78 | int i; |
| 79 | |
| 80 | for (i = 0; i < dev->num_crtcs; i++) { |
| 81 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 82 | if (atomic_read(&dev->vblank_refcount[i]) == 0 && |
| 83 | dev->vblank_enabled[i]) { |
| 84 | dev->driver->disable_vblank(dev, i); |
| 85 | dev->vblank_enabled[i] = 0; |
| 86 | } |
| 87 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | static void drm_vblank_cleanup(struct drm_device *dev) |
| 92 | { |
| 93 | /* Bail if the driver didn't call drm_vblank_init() */ |
| 94 | if (dev->num_crtcs == 0) |
| 95 | return; |
| 96 | |
| 97 | del_timer(&dev->vblank_disable_timer); |
| 98 | |
| 99 | vblank_disable_fn((unsigned long)dev); |
| 100 | |
| 101 | drm_free(dev->vbl_queue, sizeof(*dev->vbl_queue) * dev->num_crtcs, |
| 102 | DRM_MEM_DRIVER); |
| 103 | drm_free(dev->vbl_sigs, sizeof(*dev->vbl_sigs) * dev->num_crtcs, |
| 104 | DRM_MEM_DRIVER); |
| 105 | drm_free(dev->_vblank_count, sizeof(*dev->_vblank_count) * |
| 106 | dev->num_crtcs, DRM_MEM_DRIVER); |
| 107 | drm_free(dev->vblank_refcount, sizeof(*dev->vblank_refcount) * |
| 108 | dev->num_crtcs, DRM_MEM_DRIVER); |
| 109 | drm_free(dev->vblank_enabled, sizeof(*dev->vblank_enabled) * |
| 110 | dev->num_crtcs, DRM_MEM_DRIVER); |
| 111 | drm_free(dev->last_vblank, sizeof(*dev->last_vblank) * dev->num_crtcs, |
| 112 | DRM_MEM_DRIVER); |
| 113 | drm_free(dev->vblank_premodeset, sizeof(*dev->vblank_premodeset) * |
| 114 | dev->num_crtcs, DRM_MEM_DRIVER); |
| 115 | drm_free(dev->vblank_offset, sizeof(*dev->vblank_offset) * dev->num_crtcs, |
| 116 | DRM_MEM_DRIVER); |
| 117 | |
| 118 | dev->num_crtcs = 0; |
| 119 | } |
| 120 | |
| 121 | int drm_vblank_init(struct drm_device *dev, int num_crtcs) |
| 122 | { |
| 123 | int i, ret = -ENOMEM; |
| 124 | |
| 125 | setup_timer(&dev->vblank_disable_timer, vblank_disable_fn, |
| 126 | (unsigned long)dev); |
| 127 | spin_lock_init(&dev->vbl_lock); |
| 128 | atomic_set(&dev->vbl_signal_pending, 0); |
| 129 | dev->num_crtcs = num_crtcs; |
| 130 | |
| 131 | dev->vbl_queue = drm_alloc(sizeof(wait_queue_head_t) * num_crtcs, |
| 132 | DRM_MEM_DRIVER); |
| 133 | if (!dev->vbl_queue) |
| 134 | goto err; |
| 135 | |
| 136 | dev->vbl_sigs = drm_alloc(sizeof(struct list_head) * num_crtcs, |
| 137 | DRM_MEM_DRIVER); |
| 138 | if (!dev->vbl_sigs) |
| 139 | goto err; |
| 140 | |
| 141 | dev->_vblank_count = drm_alloc(sizeof(atomic_t) * num_crtcs, |
| 142 | DRM_MEM_DRIVER); |
| 143 | if (!dev->_vblank_count) |
| 144 | goto err; |
| 145 | |
| 146 | dev->vblank_refcount = drm_alloc(sizeof(atomic_t) * num_crtcs, |
| 147 | DRM_MEM_DRIVER); |
| 148 | if (!dev->vblank_refcount) |
| 149 | goto err; |
| 150 | |
| 151 | dev->vblank_enabled = drm_calloc(num_crtcs, sizeof(int), |
| 152 | DRM_MEM_DRIVER); |
| 153 | if (!dev->vblank_enabled) |
| 154 | goto err; |
| 155 | |
| 156 | dev->last_vblank = drm_calloc(num_crtcs, sizeof(u32), DRM_MEM_DRIVER); |
| 157 | if (!dev->last_vblank) |
| 158 | goto err; |
| 159 | |
| 160 | dev->vblank_premodeset = drm_calloc(num_crtcs, sizeof(u32), |
| 161 | DRM_MEM_DRIVER); |
| 162 | if (!dev->vblank_premodeset) |
| 163 | goto err; |
| 164 | |
| 165 | dev->vblank_offset = drm_calloc(num_crtcs, sizeof(u32), DRM_MEM_DRIVER); |
| 166 | if (!dev->vblank_offset) |
| 167 | goto err; |
| 168 | |
| 169 | /* Zero per-crtc vblank stuff */ |
| 170 | for (i = 0; i < num_crtcs; i++) { |
| 171 | init_waitqueue_head(&dev->vbl_queue[i]); |
| 172 | INIT_LIST_HEAD(&dev->vbl_sigs[i]); |
| 173 | atomic_set(&dev->_vblank_count[i], 0); |
| 174 | atomic_set(&dev->vblank_refcount[i], 0); |
| 175 | } |
| 176 | |
| 177 | return 0; |
| 178 | |
| 179 | err: |
| 180 | drm_vblank_cleanup(dev); |
| 181 | return ret; |
| 182 | } |
| 183 | EXPORT_SYMBOL(drm_vblank_init); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | /** |
| 186 | * Install IRQ handler. |
| 187 | * |
| 188 | * \param dev DRM device. |
| 189 | * \param irq IRQ number. |
| 190 | * |
| 191 | * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver |
| 192 | * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions |
| 193 | * before and after the installation. |
| 194 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 195 | static int drm_irq_install(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 198 | unsigned long sh_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 201 | return -EINVAL; |
| 202 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 203 | if (dev->irq == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 206 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | /* Driver must have been initialized */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 209 | if (!dev->dev_private) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 210 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return -EINVAL; |
| 212 | } |
| 213 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 214 | if (dev->irq_enabled) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 215 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return -EBUSY; |
| 217 | } |
| 218 | dev->irq_enabled = 1; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 219 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 221 | DRM_DEBUG("irq=%d\n", dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 223 | /* Before installing handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | dev->driver->irq_preinstall(dev); |
| 225 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 226 | /* Install handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) |
Thomas Gleixner | 935f6e3 | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 228 | sh_flags = IRQF_SHARED; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 229 | |
| 230 | ret = request_irq(dev->irq, dev->driver->irq_handler, |
| 231 | sh_flags, dev->devname, dev); |
| 232 | if (ret < 0) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 233 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | dev->irq_enabled = 0; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 235 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return ret; |
| 237 | } |
| 238 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 239 | /* After installing handler */ |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 240 | ret = dev->driver->irq_postinstall(dev); |
| 241 | if (ret < 0) { |
| 242 | mutex_lock(&dev->struct_mutex); |
| 243 | dev->irq_enabled = 0; |
| 244 | mutex_unlock(&dev->struct_mutex); |
| 245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 247 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Uninstall the IRQ handler. |
| 252 | * |
| 253 | * \param dev DRM device. |
| 254 | * |
| 255 | * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq. |
| 256 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 257 | int drm_irq_uninstall(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | int irq_enabled; |
| 260 | |
| 261 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 262 | return -EINVAL; |
| 263 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 264 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | irq_enabled = dev->irq_enabled; |
| 266 | dev->irq_enabled = 0; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 267 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 269 | if (!irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | |
Márton Németh | 3e684ea | 2008-01-24 15:58:57 +1000 | [diff] [blame] | 272 | DRM_DEBUG("irq=%d\n", dev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | dev->driver->irq_uninstall(dev); |
| 275 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 276 | free_irq(dev->irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 278 | drm_vblank_cleanup(dev); |
| 279 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 280 | dev->locked_tasklet_func = NULL; |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | EXPORT_SYMBOL(drm_irq_uninstall); |
| 286 | |
| 287 | /** |
| 288 | * IRQ control ioctl. |
| 289 | * |
| 290 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 291 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | * \param cmd command. |
| 293 | * \param arg user argument, pointing to a drm_control structure. |
| 294 | * \return zero on success or a negative number on failure. |
| 295 | * |
| 296 | * Calls irq_install() or irq_uninstall() according to \p arg. |
| 297 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 298 | int drm_control(struct drm_device *dev, void *data, |
| 299 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 301 | struct drm_control *ctl = data; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */ |
| 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 306 | switch (ctl->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | case DRM_INST_HANDLER: |
| 308 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 309 | return 0; |
| 310 | if (dev->if_version < DRM_IF_VERSION(1, 2) && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 311 | ctl->irq != dev->irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 313 | return drm_irq_install(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | case DRM_UNINST_HANDLER: |
| 315 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 316 | return 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 317 | return drm_irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | default: |
| 319 | return -EINVAL; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 324 | * drm_vblank_count - retrieve "cooked" vblank counter value |
| 325 | * @dev: DRM device |
| 326 | * @crtc: which counter to retrieve |
| 327 | * |
| 328 | * Fetches the "cooked" vblank count value that represents the number of |
| 329 | * vblank events since the system was booted, including lost events due to |
| 330 | * modesetting activity. |
| 331 | */ |
| 332 | u32 drm_vblank_count(struct drm_device *dev, int crtc) |
| 333 | { |
| 334 | return atomic_read(&dev->_vblank_count[crtc]) + |
| 335 | dev->vblank_offset[crtc]; |
| 336 | } |
| 337 | EXPORT_SYMBOL(drm_vblank_count); |
| 338 | |
| 339 | /** |
| 340 | * drm_update_vblank_count - update the master vblank counter |
| 341 | * @dev: DRM device |
| 342 | * @crtc: counter to update |
| 343 | * |
| 344 | * Call back into the driver to update the appropriate vblank counter |
| 345 | * (specified by @crtc). Deal with wraparound, if it occurred, and |
| 346 | * update the last read value so we can deal with wraparound on the next |
| 347 | * call if necessary. |
| 348 | */ |
| 349 | void drm_update_vblank_count(struct drm_device *dev, int crtc) |
| 350 | { |
| 351 | unsigned long irqflags; |
| 352 | u32 cur_vblank, diff; |
| 353 | |
| 354 | /* |
| 355 | * Interrupts were disabled prior to this call, so deal with counter |
| 356 | * wrap if needed. |
| 357 | * NOTE! It's possible we lost a full dev->max_vblank_count events |
| 358 | * here if the register is small or we had vblank interrupts off for |
| 359 | * a long time. |
| 360 | */ |
| 361 | cur_vblank = dev->driver->get_vblank_counter(dev, crtc); |
| 362 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 363 | if (cur_vblank < dev->last_vblank[crtc]) { |
| 364 | diff = dev->max_vblank_count - |
| 365 | dev->last_vblank[crtc]; |
| 366 | diff += cur_vblank; |
| 367 | } else { |
| 368 | diff = cur_vblank - dev->last_vblank[crtc]; |
| 369 | } |
| 370 | dev->last_vblank[crtc] = cur_vblank; |
| 371 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 372 | |
| 373 | atomic_add(diff, &dev->_vblank_count[crtc]); |
| 374 | } |
| 375 | EXPORT_SYMBOL(drm_update_vblank_count); |
| 376 | |
| 377 | /** |
| 378 | * drm_vblank_get - get a reference count on vblank events |
| 379 | * @dev: DRM device |
| 380 | * @crtc: which CRTC to own |
| 381 | * |
| 382 | * Acquire a reference count on vblank events to avoid having them disabled |
| 383 | * while in use. Note callers will probably want to update the master counter |
| 384 | * using drm_update_vblank_count() above before calling this routine so that |
| 385 | * wakeups occur on the right vblank event. |
| 386 | * |
| 387 | * RETURNS |
| 388 | * Zero on success, nonzero on failure. |
| 389 | */ |
| 390 | int drm_vblank_get(struct drm_device *dev, int crtc) |
| 391 | { |
| 392 | unsigned long irqflags; |
| 393 | int ret = 0; |
| 394 | |
| 395 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 396 | /* Going from 0->1 means we have to enable interrupts again */ |
| 397 | if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1 && |
| 398 | !dev->vblank_enabled[crtc]) { |
| 399 | ret = dev->driver->enable_vblank(dev, crtc); |
| 400 | if (ret) |
| 401 | atomic_dec(&dev->vblank_refcount[crtc]); |
| 402 | else |
| 403 | dev->vblank_enabled[crtc] = 1; |
| 404 | } |
| 405 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 406 | |
| 407 | return ret; |
| 408 | } |
| 409 | EXPORT_SYMBOL(drm_vblank_get); |
| 410 | |
| 411 | /** |
| 412 | * drm_vblank_put - give up ownership of vblank events |
| 413 | * @dev: DRM device |
| 414 | * @crtc: which counter to give up |
| 415 | * |
| 416 | * Release ownership of a given vblank counter, turning off interrupts |
| 417 | * if possible. |
| 418 | */ |
| 419 | void drm_vblank_put(struct drm_device *dev, int crtc) |
| 420 | { |
| 421 | /* Last user schedules interrupt disable */ |
| 422 | if (atomic_dec_and_test(&dev->vblank_refcount[crtc])) |
| 423 | mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ); |
| 424 | } |
| 425 | EXPORT_SYMBOL(drm_vblank_put); |
| 426 | |
| 427 | /** |
| 428 | * drm_modeset_ctl - handle vblank event counter changes across mode switch |
| 429 | * @DRM_IOCTL_ARGS: standard ioctl arguments |
| 430 | * |
| 431 | * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET |
| 432 | * ioctls around modesetting so that any lost vblank events are accounted for. |
| 433 | */ |
| 434 | int drm_modeset_ctl(struct drm_device *dev, void *data, |
| 435 | struct drm_file *file_priv) |
| 436 | { |
| 437 | struct drm_modeset_ctl *modeset = data; |
| 438 | int crtc, ret = 0; |
| 439 | u32 new; |
| 440 | |
| 441 | crtc = modeset->arg; |
| 442 | if (crtc >= dev->num_crtcs) { |
| 443 | ret = -EINVAL; |
| 444 | goto out; |
| 445 | } |
| 446 | |
| 447 | switch (modeset->cmd) { |
| 448 | case _DRM_PRE_MODESET: |
| 449 | dev->vblank_premodeset[crtc] = |
| 450 | dev->driver->get_vblank_counter(dev, crtc); |
| 451 | break; |
| 452 | case _DRM_POST_MODESET: |
| 453 | new = dev->driver->get_vblank_counter(dev, crtc); |
| 454 | dev->vblank_offset[crtc] = dev->vblank_premodeset[crtc] - new; |
| 455 | break; |
| 456 | default: |
| 457 | ret = -EINVAL; |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | out: |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | * Wait for VBLANK. |
| 467 | * |
| 468 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 469 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | * \param cmd command. |
| 471 | * \param data user argument, pointing to a drm_wait_vblank structure. |
| 472 | * \return zero on success or a negative number on failure. |
| 473 | * |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 474 | * Verifies the IRQ is installed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | * |
| 476 | * If a signal is requested checks if this task has already scheduled the same signal |
| 477 | * for the same vblank sequence number - nothing to be done in |
| 478 | * that case. If the number of tasks waiting for the interrupt exceeds 100 the |
| 479 | * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this |
| 480 | * task. |
| 481 | * |
| 482 | * If a signal is not requested, then calls vblank_wait(). |
| 483 | */ |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 484 | int drm_wait_vblank(struct drm_device *dev, void *data, |
| 485 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 487 | union drm_wait_vblank *vblwait = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | struct timeval now; |
| 489 | int ret = 0; |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 490 | unsigned int flags, seq, crtc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 492 | if ((!dev->irq) || (!dev->irq_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | return -EINVAL; |
| 494 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 495 | if (vblwait->request.type & |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 496 | ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) { |
| 497 | DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 498 | vblwait->request.type, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 499 | (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)); |
| 500 | return -EINVAL; |
| 501 | } |
| 502 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 503 | flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 504 | crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 505 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 506 | if (crtc >= dev->num_crtcs) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 507 | return -EINVAL; |
| 508 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 509 | drm_update_vblank_count(dev, crtc); |
| 510 | seq = drm_vblank_count(dev, crtc); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 511 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 512 | switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | case _DRM_VBLANK_RELATIVE: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 514 | vblwait->request.sequence += seq; |
| 515 | vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | case _DRM_VBLANK_ABSOLUTE: |
| 517 | break; |
| 518 | default: |
| 519 | return -EINVAL; |
| 520 | } |
| 521 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 522 | if ((flags & _DRM_VBLANK_NEXTONMISS) && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 523 | (seq - vblwait->request.sequence) <= (1<<23)) { |
| 524 | vblwait->request.sequence = seq + 1; |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 525 | } |
| 526 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 527 | if (flags & _DRM_VBLANK_SIGNAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | unsigned long irqflags; |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 529 | struct list_head *vbl_sigs = &dev->vbl_sigs[crtc]; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 530 | struct drm_vbl_sig *vbl_sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 532 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | /* Check if this task has already scheduled the same signal |
| 535 | * for the same vblank sequence number; nothing to be done in |
| 536 | * that case |
| 537 | */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 538 | list_for_each_entry(vbl_sig, vbl_sigs, head) { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 539 | if (vbl_sig->sequence == vblwait->request.sequence |
| 540 | && vbl_sig->info.si_signo == |
| 541 | vblwait->request.signal |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 542 | && vbl_sig->task == current) { |
| 543 | spin_unlock_irqrestore(&dev->vbl_lock, |
| 544 | irqflags); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 545 | vblwait->reply.sequence = seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | goto done; |
| 547 | } |
| 548 | } |
| 549 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 550 | if (atomic_read(&dev->vbl_signal_pending) >= 100) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 551 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | return -EBUSY; |
| 553 | } |
| 554 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 555 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 557 | vbl_sig = drm_calloc(1, sizeof(struct drm_vbl_sig), |
| 558 | DRM_MEM_DRIVER); |
| 559 | if (!vbl_sig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | return -ENOMEM; |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 561 | |
| 562 | ret = drm_vblank_get(dev, crtc); |
| 563 | if (ret) { |
| 564 | drm_free(vbl_sig, sizeof(struct drm_vbl_sig), |
| 565 | DRM_MEM_DRIVER); |
| 566 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 569 | atomic_inc(&dev->vbl_signal_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 571 | vbl_sig->sequence = vblwait->request.sequence; |
| 572 | vbl_sig->info.si_signo = vblwait->request.signal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | vbl_sig->task = current; |
| 574 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 575 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 577 | list_add_tail(&vbl_sig->head, vbl_sigs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 579 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 049b323 | 2006-10-24 23:34:58 +1000 | [diff] [blame] | 580 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 581 | vblwait->reply.sequence = seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } else { |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 583 | unsigned long cur_vblank; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 585 | ret = drm_vblank_get(dev, crtc); |
| 586 | if (ret) |
| 587 | return ret; |
| 588 | DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ, |
| 589 | (((cur_vblank = drm_vblank_count(dev, crtc)) |
| 590 | - vblwait->request.sequence) <= (1 << 23))); |
| 591 | drm_vblank_put(dev, crtc); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 592 | do_gettimeofday(&now); |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 593 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 594 | vblwait->reply.tval_sec = now.tv_sec; |
| 595 | vblwait->reply.tval_usec = now.tv_usec; |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 596 | vblwait->reply.sequence = cur_vblank; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 599 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Send the VBLANK signals. |
| 605 | * |
| 606 | * \param dev DRM device. |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 607 | * \param crtc CRTC where the vblank event occurred |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | * |
| 609 | * Sends a signal for each task in drm_device::vbl_sigs and empties the list. |
| 610 | * |
| 611 | * If a signal is not requested, then calls vblank_wait(). |
| 612 | */ |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 613 | static void drm_vbl_send_signals(struct drm_device * dev, int crtc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | { |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 615 | struct drm_vbl_sig *vbl_sig, *tmp; |
| 616 | struct list_head *vbl_sigs; |
| 617 | unsigned int vbl_seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | unsigned long flags; |
| 619 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 620 | spin_lock_irqsave(&dev->vbl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 622 | vbl_sigs = &dev->vbl_sigs[crtc]; |
| 623 | vbl_seq = drm_vblank_count(dev, crtc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 625 | list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) { |
| 626 | if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) { |
| 627 | vbl_sig->info.si_code = vbl_seq; |
| 628 | send_sig_info(vbl_sig->info.si_signo, |
| 629 | &vbl_sig->info, vbl_sig->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 631 | list_del(&vbl_sig->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 633 | drm_free(vbl_sig, sizeof(*vbl_sig), |
| 634 | DRM_MEM_DRIVER); |
| 635 | atomic_dec(&dev->vbl_signal_pending); |
| 636 | drm_vblank_put(dev, crtc); |
| 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 640 | spin_unlock_irqrestore(&dev->vbl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 642 | |
Jesse Barnes | ac741ab | 2008-04-22 16:03:07 +1000 | [diff] [blame] | 643 | /** |
| 644 | * drm_handle_vblank - handle a vblank event |
| 645 | * @dev: DRM device |
| 646 | * @crtc: where this event occurred |
| 647 | * |
| 648 | * Drivers should call this routine in their vblank interrupt handlers to |
| 649 | * update the vblank counter and send any signals that may be pending. |
| 650 | */ |
| 651 | void drm_handle_vblank(struct drm_device *dev, int crtc) |
| 652 | { |
| 653 | drm_update_vblank_count(dev, crtc); |
| 654 | DRM_WAKEUP(&dev->vbl_queue[crtc]); |
| 655 | drm_vbl_send_signals(dev, crtc); |
| 656 | } |
| 657 | EXPORT_SYMBOL(drm_handle_vblank); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 658 | |
| 659 | /** |
| 660 | * Tasklet wrapper function. |
| 661 | * |
| 662 | * \param data DRM device in disguise. |
| 663 | * |
| 664 | * Attempts to grab the HW lock and calls the driver callback on success. On |
| 665 | * failure, leave the lock marked as contended so the callback can be called |
| 666 | * from drm_unlock(). |
| 667 | */ |
| 668 | static void drm_locked_tasklet_func(unsigned long data) |
| 669 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 670 | struct drm_device *dev = (struct drm_device *)data; |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 671 | unsigned long irqflags; |
| 672 | |
| 673 | spin_lock_irqsave(&dev->tasklet_lock, irqflags); |
| 674 | |
| 675 | if (!dev->locked_tasklet_func || |
Thomas Hellstrom | 040ac32 | 2007-03-23 13:28:33 +1100 | [diff] [blame] | 676 | !drm_lock_take(&dev->lock, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 677 | DRM_KERNEL_CONTEXT)) { |
| 678 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 679 | return; |
| 680 | } |
| 681 | |
| 682 | dev->lock.lock_time = jiffies; |
| 683 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); |
| 684 | |
| 685 | dev->locked_tasklet_func(dev); |
| 686 | |
Thomas Hellstrom | 040ac32 | 2007-03-23 13:28:33 +1100 | [diff] [blame] | 687 | drm_lock_free(&dev->lock, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 688 | DRM_KERNEL_CONTEXT); |
| 689 | |
| 690 | dev->locked_tasklet_func = NULL; |
| 691 | |
| 692 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Schedule a tasklet to call back a driver hook with the HW lock held. |
| 697 | * |
| 698 | * \param dev DRM device. |
| 699 | * \param func Driver callback. |
| 700 | * |
| 701 | * This is intended for triggering actions that require the HW lock from an |
| 702 | * interrupt handler. The lock will be grabbed ASAP after the interrupt handler |
| 703 | * completes. Note that the callback may be called from interrupt or process |
| 704 | * context, it must not make any assumptions about this. Also, the HW lock will |
| 705 | * be held with the kernel context or any client context. |
| 706 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 707 | void drm_locked_tasklet(struct drm_device *dev, void (*func)(struct drm_device *)) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 708 | { |
| 709 | unsigned long irqflags; |
| 710 | static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0); |
| 711 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | 8163e41 | 2006-10-24 23:30:01 +1000 | [diff] [blame] | 712 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) || |
| 713 | test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state)) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 714 | return; |
| 715 | |
| 716 | spin_lock_irqsave(&dev->tasklet_lock, irqflags); |
| 717 | |
| 718 | if (dev->locked_tasklet_func) { |
| 719 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | dev->locked_tasklet_func = func; |
| 724 | |
| 725 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 726 | |
| 727 | drm_tasklet.data = (unsigned long)dev; |
| 728 | |
| 729 | tasklet_hi_schedule(&drm_tasklet); |
| 730 | } |
| 731 | EXPORT_SYMBOL(drm_locked_tasklet); |