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 | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 66 | p->irq = dev->pdev->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 | |
| 74 | /** |
| 75 | * Install IRQ handler. |
| 76 | * |
| 77 | * \param dev DRM device. |
| 78 | * \param irq IRQ number. |
| 79 | * |
| 80 | * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver |
| 81 | * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions |
| 82 | * before and after the installation. |
| 83 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 84 | static int drm_irq_install(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | int ret; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 87 | unsigned long sh_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
| 89 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 90 | return -EINVAL; |
| 91 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 92 | if (dev->pdev->irq == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return -EINVAL; |
| 94 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 95 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | /* Driver must have been initialized */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 98 | if (!dev->dev_private) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 99 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EINVAL; |
| 101 | } |
| 102 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 103 | if (dev->irq_enabled) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 104 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return -EBUSY; |
| 106 | } |
| 107 | dev->irq_enabled = 1; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 108 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 110 | DRM_DEBUG("irq=%d\n", dev->pdev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 112 | if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) { |
| 113 | init_waitqueue_head(&dev->vbl_queue); |
| 114 | |
| 115 | spin_lock_init(&dev->vbl_lock); |
| 116 | |
| 117 | INIT_LIST_HEAD(&dev->vbl_sigs); |
| 118 | INIT_LIST_HEAD(&dev->vbl_sigs2); |
| 119 | |
| 120 | dev->vbl_pending = 0; |
| 121 | } |
| 122 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 123 | /* Before installing handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | dev->driver->irq_preinstall(dev); |
| 125 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 126 | /* Install handler */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED)) |
Thomas Gleixner | 935f6e3 | 2006-07-01 19:29:34 -0700 | [diff] [blame] | 128 | sh_flags = IRQF_SHARED; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 129 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 130 | ret = request_irq(dev->pdev->irq, dev->driver->irq_handler, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 131 | sh_flags, dev->devname, dev); |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 132 | /* Expose the device irq number to drivers that want to export it for |
| 133 | * whatever reason. |
| 134 | */ |
| 135 | dev->irq = dev->pdev->irq; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 136 | if (ret < 0) { |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 137 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | dev->irq_enabled = 0; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 139 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return ret; |
| 141 | } |
| 142 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 143 | /* After installing handler */ |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 144 | dev->driver->irq_postinstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 146 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Uninstall the IRQ handler. |
| 151 | * |
| 152 | * \param dev DRM device. |
| 153 | * |
| 154 | * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq. |
| 155 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 156 | int drm_irq_uninstall(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
| 158 | int irq_enabled; |
| 159 | |
| 160 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 161 | return -EINVAL; |
| 162 | |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 163 | mutex_lock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | irq_enabled = dev->irq_enabled; |
| 165 | dev->irq_enabled = 0; |
Dave Airlie | 30e2fb1 | 2006-02-02 19:37:46 +1100 | [diff] [blame] | 166 | mutex_unlock(&dev->struct_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 168 | if (!irq_enabled) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return -EINVAL; |
| 170 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 171 | DRM_DEBUG("irq=%d\n", dev->pdev->irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | dev->driver->irq_uninstall(dev); |
| 174 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 175 | free_irq(dev->pdev->irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 177 | dev->locked_tasklet_func = NULL; |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | EXPORT_SYMBOL(drm_irq_uninstall); |
| 183 | |
| 184 | /** |
| 185 | * IRQ control ioctl. |
| 186 | * |
| 187 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 188 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | * \param cmd command. |
| 190 | * \param arg user argument, pointing to a drm_control structure. |
| 191 | * \return zero on success or a negative number on failure. |
| 192 | * |
| 193 | * Calls irq_install() or irq_uninstall() according to \p arg. |
| 194 | */ |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 195 | int drm_control(struct drm_device *dev, void *data, |
| 196 | struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 198 | struct drm_control *ctl = data; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */ |
| 201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 203 | switch (ctl->func) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | case DRM_INST_HANDLER: |
| 205 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 206 | return 0; |
| 207 | if (dev->if_version < DRM_IF_VERSION(1, 2) && |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 208 | ctl->irq != dev->pdev->irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return -EINVAL; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 210 | return drm_irq_install(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | case DRM_UNINST_HANDLER: |
| 212 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ)) |
| 213 | return 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 214 | return drm_irq_uninstall(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | default: |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Wait for VBLANK. |
| 222 | * |
| 223 | * \param inode device inode. |
Eric Anholt | 6c340ea | 2007-08-25 20:23:09 +1000 | [diff] [blame] | 224 | * \param file_priv DRM file private. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | * \param cmd command. |
| 226 | * \param data user argument, pointing to a drm_wait_vblank structure. |
| 227 | * \return zero on success or a negative number on failure. |
| 228 | * |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 229 | * Verifies the IRQ is installed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * |
| 231 | * If a signal is requested checks if this task has already scheduled the same signal |
| 232 | * for the same vblank sequence number - nothing to be done in |
| 233 | * that case. If the number of tasks waiting for the interrupt exceeds 100 the |
| 234 | * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this |
| 235 | * task. |
| 236 | * |
| 237 | * If a signal is not requested, then calls vblank_wait(). |
| 238 | */ |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 239 | int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 241 | union drm_wait_vblank *vblwait = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | struct timeval now; |
| 243 | int ret = 0; |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 244 | unsigned int flags, seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Eric Anholt | ed4cb41 | 2008-07-29 12:10:39 -0700 | [diff] [blame] | 246 | if ((!dev->pdev->irq) || (!dev->irq_enabled)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 249 | if (vblwait->request.type & |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 250 | ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) { |
| 251 | DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n", |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 252 | vblwait->request.type, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 253 | (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)); |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 257 | flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK; |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 258 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 259 | if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ? |
| 260 | DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL)) |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 261 | return -EINVAL; |
| 262 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 263 | seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2 |
| 264 | : &dev->vbl_received); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 776c944 | 2006-10-24 22:24:38 +1000 | [diff] [blame] | 265 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 266 | switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | case _DRM_VBLANK_RELATIVE: |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 268 | vblwait->request.sequence += seq; |
| 269 | vblwait->request.type &= ~_DRM_VBLANK_RELATIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | case _DRM_VBLANK_ABSOLUTE: |
| 271 | break; |
| 272 | default: |
| 273 | return -EINVAL; |
| 274 | } |
| 275 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 276 | if ((flags & _DRM_VBLANK_NEXTONMISS) && |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 277 | (seq - vblwait->request.sequence) <= (1<<23)) { |
| 278 | vblwait->request.sequence = seq + 1; |
=?utf-8?q?Michel_D=C3=A4nzer?= | ab285d7 | 2006-10-24 23:34:18 +1000 | [diff] [blame] | 279 | } |
| 280 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 281 | if (flags & _DRM_VBLANK_SIGNAL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | unsigned long irqflags; |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 283 | struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY) |
| 284 | ? &dev->vbl_sigs2 : &dev->vbl_sigs; |
Dave Airlie | 5591051 | 2007-07-11 16:53:40 +1000 | [diff] [blame] | 285 | struct drm_vbl_sig *vbl_sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 287 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | /* Check if this task has already scheduled the same signal |
| 290 | * for the same vblank sequence number; nothing to be done in |
| 291 | * that case |
| 292 | */ |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 293 | list_for_each_entry(vbl_sig, vbl_sigs, head) { |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 294 | if (vbl_sig->sequence == vblwait->request.sequence |
| 295 | && vbl_sig->info.si_signo == |
| 296 | vblwait->request.signal |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 297 | && vbl_sig->task == current) { |
| 298 | spin_unlock_irqrestore(&dev->vbl_lock, |
| 299 | irqflags); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 300 | vblwait->reply.sequence = seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | goto done; |
| 302 | } |
| 303 | } |
| 304 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 305 | if (dev->vbl_pending >= 100) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 306 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return -EBUSY; |
| 308 | } |
| 309 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 310 | dev->vbl_pending++; |
| 311 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 312 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 314 | if (! |
| 315 | (vbl_sig = |
| 316 | drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return -ENOMEM; |
| 318 | } |
| 319 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 320 | memset((void *)vbl_sig, 0, sizeof(*vbl_sig)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 322 | vbl_sig->sequence = vblwait->request.sequence; |
| 323 | vbl_sig->info.si_signo = vblwait->request.signal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | vbl_sig->task = current; |
| 325 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 326 | spin_lock_irqsave(&dev->vbl_lock, irqflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Dave Airlie | bd1b331 | 2007-05-26 05:01:51 +1000 | [diff] [blame] | 328 | list_add_tail(&vbl_sig->head, vbl_sigs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 330 | spin_unlock_irqrestore(&dev->vbl_lock, irqflags); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 049b323 | 2006-10-24 23:34:58 +1000 | [diff] [blame] | 331 | |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 332 | vblwait->reply.sequence = seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } else { |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 334 | if (flags & _DRM_VBLANK_SECONDARY) { |
| 335 | if (dev->driver->vblank_wait2) |
| 336 | ret = dev->driver->vblank_wait2(dev, &vblwait->request.sequence); |
| 337 | } else if (dev->driver->vblank_wait) |
| 338 | ret = |
| 339 | dev->driver->vblank_wait(dev, |
| 340 | &vblwait->request.sequence); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 342 | do_gettimeofday(&now); |
Eric Anholt | c153f45 | 2007-09-03 12:06:45 +1000 | [diff] [blame] | 343 | vblwait->reply.tval_sec = now.tv_sec; |
| 344 | vblwait->reply.tval_usec = now.tv_usec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 347 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Send the VBLANK signals. |
| 353 | * |
| 354 | * \param dev DRM device. |
| 355 | * |
| 356 | * Sends a signal for each task in drm_device::vbl_sigs and empties the list. |
| 357 | * |
| 358 | * If a signal is not requested, then calls vblank_wait(). |
| 359 | */ |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 360 | void drm_vbl_send_signals(struct drm_device * dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | unsigned long flags; |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 363 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 365 | spin_lock_irqsave(&dev->vbl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 367 | for (i = 0; i < 2; i++) { |
| 368 | struct drm_vbl_sig *vbl_sig, *tmp; |
| 369 | struct list_head *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs; |
| 370 | unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 : |
| 371 | &dev->vbl_received); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 373 | list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) { |
| 374 | if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) { |
| 375 | vbl_sig->info.si_code = vbl_seq; |
| 376 | send_sig_info(vbl_sig->info.si_signo, |
| 377 | &vbl_sig->info, vbl_sig->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 379 | list_del(&vbl_sig->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 381 | drm_free(vbl_sig, sizeof(*vbl_sig), |
| 382 | DRM_MEM_DRIVER); |
| 383 | |
| 384 | dev->vbl_pending--; |
| 385 | } |
| 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 389 | spin_unlock_irqrestore(&dev->vbl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 391 | |
Dave Airlie | af6061a | 2008-05-07 12:15:39 +1000 | [diff] [blame] | 392 | EXPORT_SYMBOL(drm_vbl_send_signals); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 393 | |
| 394 | /** |
| 395 | * Tasklet wrapper function. |
| 396 | * |
| 397 | * \param data DRM device in disguise. |
| 398 | * |
| 399 | * Attempts to grab the HW lock and calls the driver callback on success. On |
| 400 | * failure, leave the lock marked as contended so the callback can be called |
| 401 | * from drm_unlock(). |
| 402 | */ |
| 403 | static void drm_locked_tasklet_func(unsigned long data) |
| 404 | { |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 405 | 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] | 406 | unsigned long irqflags; |
Thomas Hellstrom | e5b4f19 | 2008-08-24 17:00:00 +1000 | [diff] [blame] | 407 | void (*tasklet_func)(struct drm_device *); |
| 408 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 409 | spin_lock_irqsave(&dev->tasklet_lock, irqflags); |
Thomas Hellstrom | e5b4f19 | 2008-08-24 17:00:00 +1000 | [diff] [blame] | 410 | tasklet_func = dev->locked_tasklet_func; |
| 411 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 412 | |
Thomas Hellstrom | e5b4f19 | 2008-08-24 17:00:00 +1000 | [diff] [blame] | 413 | if (!tasklet_func || |
Thomas Hellstrom | 040ac32 | 2007-03-23 13:28:33 +1100 | [diff] [blame] | 414 | !drm_lock_take(&dev->lock, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 415 | DRM_KERNEL_CONTEXT)) { |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | |
| 419 | dev->lock.lock_time = jiffies; |
| 420 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); |
| 421 | |
Thomas Hellstrom | e5b4f19 | 2008-08-24 17:00:00 +1000 | [diff] [blame] | 422 | spin_lock_irqsave(&dev->tasklet_lock, irqflags); |
| 423 | tasklet_func = dev->locked_tasklet_func; |
| 424 | dev->locked_tasklet_func = NULL; |
| 425 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 426 | |
| 427 | if (tasklet_func != NULL) |
| 428 | tasklet_func(dev); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 429 | |
Thomas Hellstrom | 040ac32 | 2007-03-23 13:28:33 +1100 | [diff] [blame] | 430 | drm_lock_free(&dev->lock, |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 431 | DRM_KERNEL_CONTEXT); |
=?utf-8?q?Michel_D=C3=A4nzer?= | 2e54a00 | 2006-10-24 23:08:16 +1000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
| 435 | * Schedule a tasklet to call back a driver hook with the HW lock held. |
| 436 | * |
| 437 | * \param dev DRM device. |
| 438 | * \param func Driver callback. |
| 439 | * |
| 440 | * This is intended for triggering actions that require the HW lock from an |
| 441 | * interrupt handler. The lock will be grabbed ASAP after the interrupt handler |
| 442 | * completes. Note that the callback may be called from interrupt or process |
| 443 | * context, it must not make any assumptions about this. Also, the HW lock will |
| 444 | * be held with the kernel context or any client context. |
| 445 | */ |
Dave Airlie | 84b1fd1 | 2007-07-11 15:53:27 +1000 | [diff] [blame] | 446 | 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] | 447 | { |
| 448 | unsigned long irqflags; |
| 449 | static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0); |
| 450 | |
=?utf-8?q?Michel_D=C3=A4nzer?= | 8163e41 | 2006-10-24 23:30:01 +1000 | [diff] [blame] | 451 | if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) || |
| 452 | 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] | 453 | return; |
| 454 | |
| 455 | spin_lock_irqsave(&dev->tasklet_lock, irqflags); |
| 456 | |
| 457 | if (dev->locked_tasklet_func) { |
| 458 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | dev->locked_tasklet_func = func; |
| 463 | |
| 464 | spin_unlock_irqrestore(&dev->tasklet_lock, irqflags); |
| 465 | |
| 466 | drm_tasklet.data = (unsigned long)dev; |
| 467 | |
| 468 | tasklet_hi_schedule(&drm_tasklet); |
| 469 | } |
| 470 | EXPORT_SYMBOL(drm_locked_tasklet); |