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" |
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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Get interrupt from bus id. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 45 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 47 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * \param cmd command. |
| 49 | * \param arg user argument, pointing to a drm_irq_busid structure. |
| 50 | * \return zero on success or a negative number on failure. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 51 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * Finds the PCI device with the specified bus id and gets its IRQ number. |
| 53 | * This IOCTL is deprecated, and will now return EINVAL for any busid not equal |
| 54 | * to that of the device that this DRM instance attached to. |
| 55 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 56 | int drm_irq_by_busid(struct drm_device *dev, void *data, |
| 57 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 59 | struct drm_irq_busid *p = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 61 | if (drm_core_check_feature(dev, DRIVER_USE_PLATFORM_DEVICE)) |
| 62 | return -EINVAL; |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 65 | return -EINVAL; |
| 66 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 67 | if ((p->busnum >> 8) != drm_get_pci_domain(dev) || |
| 68 | (p->busnum & 0xff) != dev->pdev->bus->number || |
| 69 | 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] | 70 | return -EINVAL; |
| 71 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 72 | p->irq = dev->pdev->irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 74 | DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum, |
| 75 | p->irq); |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 80 | static void vblank_disable_fn(unsigned long arg) |
| 81 | { |
| 82 | struct drm_device *dev = (struct drm_device *)arg; |
| 83 | unsigned long irqflags; |
| 84 | int i; |
| 85 | |
| 86 | if (!dev->vblank_disable_allowed) |
| 87 | return; |
| 88 | |
| 89 | for (i = 0; i < dev->num_crtcs; i++) { |
| 90 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 91 | if (atomic_read(&dev->vblank_refcount[i]) == 0 && |
| 92 | dev->vblank_enabled[i]) { |
| 93 | DRM_DEBUG("disabling vblank on crtc %d\n", i); |
| 94 | dev->last_vblank[i] = |
| 95 | dev->driver->get_vblank_counter(dev, i); |
| 96 | dev->driver->disable_vblank(dev, i); |
| 97 | dev->vblank_enabled[i] = 0; |
| 98 | } |
| 99 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 100 | } |
| 101 | } |
| 102 | |
Keith Packard | 5244021 | 2008-11-18 09:30:25 -0800 | [diff] [blame] | 103 | void drm_vblank_cleanup(struct drm_device *dev) |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 104 | { |
| 105 | /* Bail if the driver didn't call drm_vblank_init() */ |
| 106 | if (dev->num_crtcs == 0) |
| 107 | return; |
| 108 | |
| 109 | del_timer(&dev->vblank_disable_timer); |
| 110 | |
| 111 | vblank_disable_fn((unsigned long)dev); |
| 112 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 113 | kfree(dev->vbl_queue); |
| 114 | kfree(dev->_vblank_count); |
| 115 | kfree(dev->vblank_refcount); |
| 116 | kfree(dev->vblank_enabled); |
| 117 | kfree(dev->last_vblank); |
| 118 | kfree(dev->last_vblank_wait); |
| 119 | kfree(dev->vblank_inmodeset); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 120 | |
| 121 | dev->num_crtcs = 0; |
| 122 | } |
Jerome Glisse | e77cef9 | 2010-01-07 15:39:13 +0100 | [diff] [blame] | 123 | EXPORT_SYMBOL(drm_vblank_cleanup); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 124 | |
| 125 | int drm_vblank_init(struct drm_device *dev, int num_crtcs) |
| 126 | { |
| 127 | int i, ret = -ENOMEM; |
| 128 | |
| 129 | setup_timer(&dev->vblank_disable_timer, vblank_disable_fn, |
| 130 | (unsigned long)dev); |
| 131 | spin_lock_init(&dev->vbl_lock); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 132 | dev->num_crtcs = num_crtcs; |
| 133 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 134 | dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs, |
| 135 | GFP_KERNEL); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 136 | if (!dev->vbl_queue) |
| 137 | goto err; |
| 138 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 139 | dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs, GFP_KERNEL); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 140 | if (!dev->_vblank_count) |
| 141 | goto err; |
| 142 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 143 | dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs, |
| 144 | GFP_KERNEL); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 145 | if (!dev->vblank_refcount) |
| 146 | goto err; |
| 147 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 148 | dev->vblank_enabled = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 149 | if (!dev->vblank_enabled) |
| 150 | goto err; |
| 151 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 152 | dev->last_vblank = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 153 | if (!dev->last_vblank) |
| 154 | goto err; |
| 155 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 156 | dev->last_vblank_wait = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL); |
Eric Anholt | fede5c9 | 2008-12-19 17:23:38 -0800 | [diff] [blame] | 157 | if (!dev->last_vblank_wait) |
| 158 | goto err; |
| 159 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 160 | dev->vblank_inmodeset = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 161 | if (!dev->vblank_inmodeset) |
| 162 | goto err; |
| 163 | |
| 164 | /* Zero per-crtc vblank stuff */ |
| 165 | for (i = 0; i < num_crtcs; i++) { |
| 166 | init_waitqueue_head(&dev->vbl_queue[i]); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 167 | atomic_set(&dev->_vblank_count[i], 0); |
| 168 | atomic_set(&dev->vblank_refcount[i], 0); |
| 169 | } |
| 170 | |
| 171 | dev->vblank_disable_allowed = 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 172 | return 0; |
| 173 | |
| 174 | err: |
| 175 | drm_vblank_cleanup(dev); |
| 176 | return ret; |
| 177 | } |
| 178 | EXPORT_SYMBOL(drm_vblank_init); |
| 179 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 180 | static void drm_irq_vgaarb_nokms(void *cookie, bool state) |
| 181 | { |
| 182 | struct drm_device *dev = cookie; |
| 183 | |
| 184 | if (dev->driver->vgaarb_irq) { |
| 185 | dev->driver->vgaarb_irq(dev, state); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (!dev->irq_enabled) |
| 190 | return; |
| 191 | |
| 192 | if (state) |
| 193 | dev->driver->irq_uninstall(dev); |
| 194 | else { |
| 195 | dev->driver->irq_preinstall(dev); |
| 196 | dev->driver->irq_postinstall(dev); |
| 197 | } |
| 198 | } |
| 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Install IRQ handler. |
| 202 | * |
| 203 | * \param dev DRM device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 205 | * Initializes the IRQ related data. Installs the handler, calling the driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions |
| 207 | * before and after the installation. |
| 208 | */ |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 209 | int drm_irq_install(struct drm_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 211 | int ret = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 212 | unsigned long sh_flags = 0; |
Dave Airlie | b8da7de | 2009-06-02 16:50:35 +1000 | [diff] [blame] | 213 | char *irqname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 216 | return -EINVAL; |
| 217 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 218 | if (drm_dev_to_irq(dev) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 221 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | /* Driver must have been initialized */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 224 | if (!dev->dev_private) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 225 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | return -EINVAL; |
| 227 | } |
| 228 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 229 | if (dev->irq_enabled) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 230 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | return -EBUSY; |
| 232 | } |
| 233 | dev->irq_enabled = 1; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 234 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 236 | DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 238 | /* Before installing handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | dev->driver->irq_preinstall(dev); |
| 240 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 241 | /* Install handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) |
Thomas Gleixner | 935f6e3 | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 243 | sh_flags = IRQF_SHARED; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 244 | |
Dave Airlie | b8da7de | 2009-06-02 16:50:35 +1000 | [diff] [blame] | 245 | if (dev->devname) |
| 246 | irqname = dev->devname; |
| 247 | else |
| 248 | irqname = dev->driver->name; |
| 249 | |
Jesse Barnes | 9bfbd5c | 2008-09-15 15:00:33 -0700 | [diff] [blame] | 250 | ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler, |
Dave Airlie | b8da7de | 2009-06-02 16:50:35 +1000 | [diff] [blame] | 251 | sh_flags, irqname, dev); |
Jesse Barnes | 9bfbd5c | 2008-09-15 15:00:33 -0700 | [diff] [blame] | 252 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 253 | if (ret < 0) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 254 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | dev->irq_enabled = 0; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 256 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return ret; |
| 258 | } |
| 259 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 260 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 261 | vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL); |
| 262 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 263 | /* After installing handler */ |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 264 | ret = dev->driver->irq_postinstall(dev); |
| 265 | if (ret < 0) { |
| 266 | mutex_lock(&dev->struct_mutex); |
| 267 | dev->irq_enabled = 0; |
| 268 | mutex_unlock(&dev->struct_mutex); |
| 269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 271 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 273 | EXPORT_SYMBOL(drm_irq_install); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
| 275 | /** |
| 276 | * Uninstall the IRQ handler. |
| 277 | * |
| 278 | * \param dev DRM device. |
| 279 | * |
| 280 | * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq. |
| 281 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 282 | int drm_irq_uninstall(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 284 | unsigned long irqflags; |
| 285 | int irq_enabled, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 288 | return -EINVAL; |
| 289 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 290 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | irq_enabled = dev->irq_enabled; |
| 292 | dev->irq_enabled = 0; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 293 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 295 | /* |
| 296 | * Wake up any waiters so they don't hang. |
| 297 | */ |
| 298 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 299 | for (i = 0; i < dev->num_crtcs; i++) { |
| 300 | DRM_WAKEUP(&dev->vbl_queue[i]); |
| 301 | dev->vblank_enabled[i] = 0; |
Jesse Barnes | 14d200c | 2009-02-06 13:04:49 -0800 | [diff] [blame] | 302 | dev->last_vblank[i] = dev->driver->get_vblank_counter(dev, i); |
Jesse Barnes | dc1336f | 2009-01-06 10:21:24 -0800 | [diff] [blame] | 303 | } |
| 304 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 305 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 306 | if (!irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return -EINVAL; |
| 308 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 309 | DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Dave Airlie | 28d5204 | 2009-09-21 14:33:58 +1000 | [diff] [blame] | 311 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 312 | vga_client_register(dev->pdev, NULL, NULL, NULL); |
| 313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | dev->driver->irq_uninstall(dev); |
| 315 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 316 | free_irq(drm_dev_to_irq(dev), dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | EXPORT_SYMBOL(drm_irq_uninstall); |
| 321 | |
| 322 | /** |
| 323 | * IRQ control ioctl. |
| 324 | * |
| 325 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 326 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | * \param cmd command. |
| 328 | * \param arg user argument, pointing to a drm_control structure. |
| 329 | * \return zero on success or a negative number on failure. |
| 330 | * |
| 331 | * Calls irq_install() or irq_uninstall() according to \p arg. |
| 332 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 333 | int drm_control(struct drm_device *dev, void *data, |
| 334 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 336 | struct drm_control *ctl = data; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 337 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */ |
| 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 341 | switch (ctl->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | case DRM_INST_HANDLER: |
| 343 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 344 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 345 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 346 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | if (dev->if_version < DRM_IF_VERSION(1, 2) && |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 348 | ctl->irq != drm_dev_to_irq(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 350 | return drm_irq_install(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | case DRM_UNINST_HANDLER: |
| 352 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 353 | return 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 354 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 355 | return 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 356 | return drm_irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | default: |
| 358 | return -EINVAL; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 363 | * drm_vblank_count - retrieve "cooked" vblank counter value |
| 364 | * @dev: DRM device |
| 365 | * @crtc: which counter to retrieve |
| 366 | * |
| 367 | * Fetches the "cooked" vblank count value that represents the number of |
| 368 | * vblank events since the system was booted, including lost events due to |
| 369 | * modesetting activity. |
| 370 | */ |
| 371 | u32 drm_vblank_count(struct drm_device *dev, int crtc) |
| 372 | { |
| 373 | return atomic_read(&dev->_vblank_count[crtc]); |
| 374 | } |
| 375 | EXPORT_SYMBOL(drm_vblank_count); |
| 376 | |
| 377 | /** |
| 378 | * drm_update_vblank_count - update the master vblank counter |
| 379 | * @dev: DRM device |
| 380 | * @crtc: counter to update |
| 381 | * |
| 382 | * Call back into the driver to update the appropriate vblank counter |
| 383 | * (specified by @crtc). Deal with wraparound, if it occurred, and |
| 384 | * update the last read value so we can deal with wraparound on the next |
| 385 | * call if necessary. |
| 386 | * |
| 387 | * Only necessary when going from off->on, to account for frames we |
| 388 | * didn't get an interrupt for. |
| 389 | * |
| 390 | * Note: caller must hold dev->vbl_lock since this reads & writes |
| 391 | * device vblank fields. |
| 392 | */ |
| 393 | static void drm_update_vblank_count(struct drm_device *dev, int crtc) |
| 394 | { |
| 395 | u32 cur_vblank, diff; |
| 396 | |
| 397 | /* |
| 398 | * Interrupts were disabled prior to this call, so deal with counter |
| 399 | * wrap if needed. |
| 400 | * NOTE! It's possible we lost a full dev->max_vblank_count events |
| 401 | * here if the register is small or we had vblank interrupts off for |
| 402 | * a long time. |
| 403 | */ |
| 404 | cur_vblank = dev->driver->get_vblank_counter(dev, crtc); |
| 405 | diff = cur_vblank - dev->last_vblank[crtc]; |
| 406 | if (cur_vblank < dev->last_vblank[crtc]) { |
| 407 | diff += dev->max_vblank_count; |
| 408 | |
| 409 | DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n", |
| 410 | crtc, dev->last_vblank[crtc], cur_vblank, diff); |
| 411 | } |
| 412 | |
| 413 | DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n", |
| 414 | crtc, diff); |
| 415 | |
| 416 | atomic_add(diff, &dev->_vblank_count[crtc]); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * drm_vblank_get - get a reference count on vblank events |
| 421 | * @dev: DRM device |
| 422 | * @crtc: which CRTC to own |
| 423 | * |
| 424 | * Acquire a reference count on vblank events to avoid having them disabled |
| 425 | * while in use. |
| 426 | * |
| 427 | * RETURNS |
| 428 | * Zero on success, nonzero on failure. |
| 429 | */ |
| 430 | int drm_vblank_get(struct drm_device *dev, int crtc) |
| 431 | { |
| 432 | unsigned long irqflags; |
| 433 | int ret = 0; |
| 434 | |
| 435 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 436 | /* Going from 0->1 means we have to enable interrupts again */ |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 437 | if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) { |
| 438 | if (!dev->vblank_enabled[crtc]) { |
| 439 | ret = dev->driver->enable_vblank(dev, crtc); |
| 440 | DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret); |
| 441 | if (ret) |
| 442 | atomic_dec(&dev->vblank_refcount[crtc]); |
| 443 | else { |
| 444 | dev->vblank_enabled[crtc] = 1; |
| 445 | drm_update_vblank_count(dev, crtc); |
| 446 | } |
| 447 | } |
| 448 | } else { |
| 449 | if (!dev->vblank_enabled[crtc]) { |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 450 | atomic_dec(&dev->vblank_refcount[crtc]); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 451 | ret = -EINVAL; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | EXPORT_SYMBOL(drm_vblank_get); |
| 459 | |
| 460 | /** |
| 461 | * drm_vblank_put - give up ownership of vblank events |
| 462 | * @dev: DRM device |
| 463 | * @crtc: which counter to give up |
| 464 | * |
| 465 | * Release ownership of a given vblank counter, turning off interrupts |
| 466 | * if possible. |
| 467 | */ |
| 468 | void drm_vblank_put(struct drm_device *dev, int crtc) |
| 469 | { |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 470 | BUG_ON (atomic_read (&dev->vblank_refcount[crtc]) == 0); |
| 471 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 472 | /* Last user schedules interrupt disable */ |
| 473 | if (atomic_dec_and_test(&dev->vblank_refcount[crtc])) |
| 474 | mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ); |
| 475 | } |
| 476 | EXPORT_SYMBOL(drm_vblank_put); |
| 477 | |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 478 | void drm_vblank_off(struct drm_device *dev, int crtc) |
| 479 | { |
| 480 | unsigned long irqflags; |
| 481 | |
| 482 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Jesse Barnes | e32ee7f | 2010-03-26 18:07:15 +0000 | [diff] [blame] | 483 | dev->driver->disable_vblank(dev, crtc); |
Li Peng | 778c902 | 2009-11-09 12:51:22 +0800 | [diff] [blame] | 484 | DRM_WAKEUP(&dev->vbl_queue[crtc]); |
| 485 | dev->vblank_enabled[crtc] = 0; |
| 486 | dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc); |
| 487 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
| 488 | } |
| 489 | EXPORT_SYMBOL(drm_vblank_off); |
| 490 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 491 | /** |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 492 | * drm_vblank_pre_modeset - account for vblanks across mode sets |
| 493 | * @dev: DRM device |
| 494 | * @crtc: CRTC in question |
| 495 | * @post: post or pre mode set? |
| 496 | * |
| 497 | * Account for vblank events across mode setting events, which will likely |
| 498 | * reset the hardware frame counter. |
| 499 | */ |
| 500 | void drm_vblank_pre_modeset(struct drm_device *dev, int crtc) |
| 501 | { |
Jerome Glisse | e77cef9 | 2010-01-07 15:39:13 +0100 | [diff] [blame] | 502 | /* vblank is not initialized (IRQ not installed ?) */ |
| 503 | if (!dev->num_crtcs) |
| 504 | return; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 505 | /* |
| 506 | * To avoid all the problems that might happen if interrupts |
| 507 | * were enabled/disabled around or between these calls, we just |
| 508 | * have the kernel take a reference on the CRTC (just once though |
| 509 | * to avoid corrupting the count if multiple, mismatch calls occur), |
| 510 | * so that interrupts remain enabled in the interim. |
| 511 | */ |
| 512 | if (!dev->vblank_inmodeset[crtc]) { |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 513 | dev->vblank_inmodeset[crtc] = 0x1; |
| 514 | if (drm_vblank_get(dev, crtc) == 0) |
| 515 | dev->vblank_inmodeset[crtc] |= 0x2; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | EXPORT_SYMBOL(drm_vblank_pre_modeset); |
| 519 | |
| 520 | void drm_vblank_post_modeset(struct drm_device *dev, int crtc) |
| 521 | { |
| 522 | unsigned long irqflags; |
| 523 | |
| 524 | if (dev->vblank_inmodeset[crtc]) { |
| 525 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
| 526 | dev->vblank_disable_allowed = 1; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 527 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Chris Wilson | b3f5e73 | 2009-02-19 14:48:22 +0000 | [diff] [blame] | 528 | |
| 529 | if (dev->vblank_inmodeset[crtc] & 0x2) |
| 530 | drm_vblank_put(dev, crtc); |
| 531 | |
| 532 | dev->vblank_inmodeset[crtc] = 0; |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | EXPORT_SYMBOL(drm_vblank_post_modeset); |
| 536 | |
| 537 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 538 | * drm_modeset_ctl - handle vblank event counter changes across mode switch |
| 539 | * @DRM_IOCTL_ARGS: standard ioctl arguments |
| 540 | * |
| 541 | * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET |
| 542 | * ioctls around modesetting so that any lost vblank events are accounted for. |
| 543 | * |
| 544 | * Generally the counter will reset across mode sets. If interrupts are |
| 545 | * enabled around this call, we don't have to do anything since the counter |
| 546 | * will have already been incremented. |
| 547 | */ |
| 548 | int drm_modeset_ctl(struct drm_device *dev, void *data, |
| 549 | struct drm_file *file_priv) |
| 550 | { |
| 551 | struct drm_modeset_ctl *modeset = data; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 552 | int crtc, ret = 0; |
| 553 | |
| 554 | /* If drm_vblank_init() hasn't been called yet, just no-op */ |
| 555 | if (!dev->num_crtcs) |
| 556 | goto out; |
| 557 | |
| 558 | crtc = modeset->crtc; |
| 559 | if (crtc >= dev->num_crtcs) { |
| 560 | ret = -EINVAL; |
| 561 | goto out; |
| 562 | } |
| 563 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 564 | switch (modeset->cmd) { |
| 565 | case _DRM_PRE_MODESET: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 566 | drm_vblank_pre_modeset(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 567 | break; |
| 568 | case _DRM_POST_MODESET: |
Dave Airlie | f453ba0 | 2008-11-07 14:05:41 -0800 | [diff] [blame] | 569 | drm_vblank_post_modeset(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 570 | break; |
| 571 | default: |
| 572 | ret = -EINVAL; |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | out: |
| 577 | return ret; |
| 578 | } |
| 579 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 580 | static int drm_queue_vblank_event(struct drm_device *dev, int pipe, |
| 581 | union drm_wait_vblank *vblwait, |
| 582 | struct drm_file *file_priv) |
| 583 | { |
| 584 | struct drm_pending_vblank_event *e; |
| 585 | struct timeval now; |
| 586 | unsigned long flags; |
| 587 | unsigned int seq; |
| 588 | |
| 589 | e = kzalloc(sizeof *e, GFP_KERNEL); |
| 590 | if (e == NULL) |
| 591 | return -ENOMEM; |
| 592 | |
| 593 | e->pipe = pipe; |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 594 | e->base.pid = current->pid; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 595 | e->event.base.type = DRM_EVENT_VBLANK; |
| 596 | e->event.base.length = sizeof e->event; |
| 597 | e->event.user_data = vblwait->request.signal; |
| 598 | e->base.event = &e->event.base; |
| 599 | e->base.file_priv = file_priv; |
| 600 | e->base.destroy = (void (*) (struct drm_pending_event *)) kfree; |
| 601 | |
| 602 | do_gettimeofday(&now); |
| 603 | spin_lock_irqsave(&dev->event_lock, flags); |
| 604 | |
| 605 | if (file_priv->event_space < sizeof e->event) { |
| 606 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 607 | kfree(e); |
| 608 | return -ENOMEM; |
| 609 | } |
| 610 | |
| 611 | file_priv->event_space -= sizeof e->event; |
| 612 | seq = drm_vblank_count(dev, pipe); |
| 613 | if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) && |
| 614 | (seq - vblwait->request.sequence) <= (1 << 23)) { |
| 615 | vblwait->request.sequence = seq + 1; |
Jesse Barnes | 4a92164 | 2009-11-10 08:21:25 +0000 | [diff] [blame] | 616 | vblwait->reply.sequence = vblwait->request.sequence; |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n", |
| 620 | vblwait->request.sequence, seq, pipe); |
| 621 | |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 622 | trace_drm_vblank_event_queued(current->pid, pipe, |
| 623 | vblwait->request.sequence); |
| 624 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 625 | e->event.sequence = vblwait->request.sequence; |
| 626 | if ((seq - vblwait->request.sequence) <= (1 << 23)) { |
| 627 | e->event.tv_sec = now.tv_sec; |
| 628 | e->event.tv_usec = now.tv_usec; |
| 629 | drm_vblank_put(dev, e->pipe); |
| 630 | list_add_tail(&e->base.link, &e->base.file_priv->event_list); |
| 631 | wake_up_interruptible(&e->base.file_priv->event_wait); |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 632 | trace_drm_vblank_event_delivered(current->pid, pipe, |
| 633 | vblwait->request.sequence); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 634 | } else { |
| 635 | list_add_tail(&e->base.link, &dev->vblank_event_list); |
| 636 | } |
| 637 | |
| 638 | spin_unlock_irqrestore(&dev->event_lock, flags); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 643 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | * Wait for VBLANK. |
| 645 | * |
| 646 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 647 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | * \param cmd command. |
| 649 | * \param data user argument, pointing to a drm_wait_vblank structure. |
| 650 | * \return zero on success or a negative number on failure. |
| 651 | * |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 652 | * This function enables the vblank interrupt on the pipe requested, then |
| 653 | * sleeps waiting for the requested sequence number to occur, and drops |
| 654 | * the vblank interrupt refcount afterwards. (vblank irq disable follows that |
| 655 | * after a timeout with no further vblank waits scheduled). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | */ |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 657 | int drm_wait_vblank(struct drm_device *dev, void *data, |
| 658 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 660 | union drm_wait_vblank *vblwait = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | int ret = 0; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 662 | unsigned int flags, seq, crtc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
Jordan Crouse | dcdb167 | 2010-05-27 13:40:25 -0600 | [diff] [blame] | 664 | if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | return -EINVAL; |
| 666 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 667 | if (vblwait->request.type & _DRM_VBLANK_SIGNAL) |
| 668 | return -EINVAL; |
| 669 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 670 | if (vblwait->request.type & |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 671 | ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) { |
| 672 | DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 673 | vblwait->request.type, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 674 | (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)); |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 678 | flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 679 | crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0; |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 680 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 681 | if (crtc >= dev->num_crtcs) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 682 | return -EINVAL; |
| 683 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 684 | ret = drm_vblank_get(dev, crtc); |
| 685 | if (ret) { |
Paul Rolland | 8d3457e | 2009-08-09 12:24:01 +1000 | [diff] [blame] | 686 | DRM_DEBUG("failed to acquire vblank counter, %d\n", ret); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 687 | return ret; |
| 688 | } |
| 689 | seq = drm_vblank_count(dev, crtc); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 690 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 691 | switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | case _DRM_VBLANK_RELATIVE: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 693 | vblwait->request.sequence += seq; |
| 694 | vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | case _DRM_VBLANK_ABSOLUTE: |
| 696 | break; |
| 697 | default: |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 698 | ret = -EINVAL; |
| 699 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 702 | if (flags & _DRM_VBLANK_EVENT) |
| 703 | return drm_queue_vblank_event(dev, crtc, vblwait, file_priv); |
| 704 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 705 | if ((flags & _DRM_VBLANK_NEXTONMISS) && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 706 | (seq - vblwait->request.sequence) <= (1<<23)) { |
| 707 | vblwait->request.sequence = seq + 1; |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 708 | } |
| 709 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 710 | DRM_DEBUG("waiting on vblank count %d, crtc %d\n", |
| 711 | vblwait->request.sequence, crtc); |
| 712 | dev->last_vblank_wait[crtc] = vblwait->request.sequence; |
| 713 | DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ, |
| 714 | (((drm_vblank_count(dev, crtc) - |
| 715 | vblwait->request.sequence) <= (1 << 23)) || |
| 716 | !dev->irq_enabled)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 718 | if (ret != -EINTR) { |
| 719 | struct timeval now; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 721 | do_gettimeofday(&now); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 723 | vblwait->reply.tval_sec = now.tv_sec; |
| 724 | vblwait->reply.tval_usec = now.tv_usec; |
| 725 | vblwait->reply.sequence = drm_vblank_count(dev, crtc); |
| 726 | DRM_DEBUG("returning %d to client\n", |
| 727 | vblwait->reply.sequence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } else { |
Eric Anholt | 30b2363 | 2009-01-27 21:19:41 -0800 | [diff] [blame] | 729 | DRM_DEBUG("vblank wait interrupted by signal\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 732 | done: |
| 733 | drm_vblank_put(dev, crtc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | return ret; |
| 735 | } |
| 736 | |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 737 | void drm_handle_vblank_events(struct drm_device *dev, int crtc) |
| 738 | { |
| 739 | struct drm_pending_vblank_event *e, *t; |
| 740 | struct timeval now; |
| 741 | unsigned long flags; |
| 742 | unsigned int seq; |
| 743 | |
| 744 | do_gettimeofday(&now); |
| 745 | seq = drm_vblank_count(dev, crtc); |
| 746 | |
| 747 | spin_lock_irqsave(&dev->event_lock, flags); |
| 748 | |
| 749 | list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) { |
| 750 | if (e->pipe != crtc) |
| 751 | continue; |
| 752 | if ((seq - e->event.sequence) > (1<<23)) |
| 753 | continue; |
| 754 | |
| 755 | DRM_DEBUG("vblank event on %d, current %d\n", |
| 756 | e->event.sequence, seq); |
| 757 | |
| 758 | e->event.sequence = seq; |
| 759 | e->event.tv_sec = now.tv_sec; |
| 760 | e->event.tv_usec = now.tv_usec; |
| 761 | drm_vblank_put(dev, e->pipe); |
| 762 | list_move_tail(&e->base.link, &e->base.file_priv->event_list); |
| 763 | wake_up_interruptible(&e->base.file_priv->event_wait); |
Jesse Barnes | b9c2c9a | 2010-07-01 16:48:09 -0700 | [diff] [blame] | 764 | trace_drm_vblank_event_delivered(e->base.pid, e->pipe, |
| 765 | e->event.sequence); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | spin_unlock_irqrestore(&dev->event_lock, flags); |
Jesse Barnes | ac2874b | 2010-07-01 16:47:31 -0700 | [diff] [blame] | 769 | |
| 770 | trace_drm_vblank_event(crtc, seq); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 771 | } |
| 772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | /** |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 774 | * drm_handle_vblank - handle a vblank event |
| 775 | * @dev: DRM device |
| 776 | * @crtc: where this event occurred |
| 777 | * |
| 778 | * Drivers should call this routine in their vblank interrupt handlers to |
| 779 | * update the vblank counter and send any signals that may be pending. |
| 780 | */ |
| 781 | void drm_handle_vblank(struct drm_device *dev, int crtc) |
| 782 | { |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 783 | if (!dev->num_crtcs) |
| 784 | return; |
| 785 | |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 786 | atomic_inc(&dev->_vblank_count[crtc]); |
| 787 | DRM_WAKEUP(&dev->vbl_queue[crtc]); |
Kristian Høgsberg | c9a9c5e | 2009-09-12 04:33:34 +1000 | [diff] [blame] | 788 | drm_handle_vblank_events(dev, crtc); |
Jesse Barnes | 0a3e67a | 2008-09-30 12:14:26 -0700 | [diff] [blame] | 789 | } |
| 790 | EXPORT_SYMBOL(drm_handle_vblank); |