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