blob: 9fdc2953c73f4e2a5e610f194f17733389747514 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_irq.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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 Airlieb5e89ed2005-09-25 14:28:13 +100042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +100044 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * \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 Airlieb5e89ed2005-09-25 14:28:13 +100048 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * 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 Anholt6c340ea2007-08-25 20:23:09 +100053int drm_irq_by_busid(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100054 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Eric Anholt6c340ea2007-08-25 20:23:09 +100056 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +100057 struct drm_irq_busid __user *argp = (void __user *)arg;
58 struct drm_irq_busid p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
61 return -EINVAL;
62
63 if (copy_from_user(&p, argp, sizeof(p)))
64 return -EFAULT;
65
Dave Airlie332296012006-08-07 20:23:42 +100066 if ((p.busnum >> 8) != drm_get_pci_domain(dev) ||
Dave242ef0e2006-07-18 04:01:01 +100067 (p.busnum & 0xff) != dev->pdev->bus->number ||
68 p.devnum != PCI_SLOT(dev->pdev->devfn) || p.funcnum != PCI_FUNC(dev->pdev->devfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -EINVAL;
70
71 p.irq = dev->irq;
72
Dave Airlieb5e89ed2005-09-25 14:28:13 +100073 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p.busnum, p.devnum, p.funcnum, p.irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (copy_to_user(argp, &p, sizeof(p)))
75 return -EFAULT;
76 return 0;
77}
78
79/**
80 * Install IRQ handler.
81 *
82 * \param dev DRM device.
83 * \param irq IRQ number.
84 *
85 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
86 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
87 * before and after the installation.
88 */
Dave Airlie84b1fd12007-07-11 15:53:27 +100089static int drm_irq_install(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
95 return -EINVAL;
96
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097 if (dev->irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EINVAL;
99
Dave Airlie30e2fb12006-02-02 19:37:46 +1100100 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* Driver must have been initialized */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 if (!dev->dev_private) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100104 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EINVAL;
106 }
107
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000108 if (dev->irq_enabled) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100109 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return -EBUSY;
111 }
112 dev->irq_enabled = 1;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100113 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000115 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
118 init_waitqueue_head(&dev->vbl_queue);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119
120 spin_lock_init(&dev->vbl_lock);
121
Dave Airliebd1b3312007-05-26 05:01:51 +1000122 INIT_LIST_HEAD(&dev->vbl_sigs);
123 INIT_LIST_HEAD(&dev->vbl_sigs2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 dev->vbl_pending = 0;
126 }
127
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 /* Before installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 dev->driver->irq_preinstall(dev);
130
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700133 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134
135 ret = request_irq(dev->irq, dev->driver->irq_handler,
136 sh_flags, dev->devname, dev);
137 if (ret < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100138 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100140 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return ret;
142 }
143
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000144 /* After installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 dev->driver->irq_postinstall(dev);
146
147 return 0;
148}
149
150/**
151 * Uninstall the IRQ handler.
152 *
153 * \param dev DRM device.
154 *
155 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
156 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000157int drm_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int irq_enabled;
160
161 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
162 return -EINVAL;
163
Dave Airlie30e2fb12006-02-02 19:37:46 +1100164 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 irq_enabled = dev->irq_enabled;
166 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100167 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000169 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -EINVAL;
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 dev->driver->irq_uninstall(dev);
175
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000178 dev->locked_tasklet_func = NULL;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183EXPORT_SYMBOL(drm_irq_uninstall);
184
185/**
186 * IRQ control ioctl.
187 *
188 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000189 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 * \param cmd command.
191 * \param arg user argument, pointing to a drm_control structure.
192 * \return zero on success or a negative number on failure.
193 *
194 * Calls irq_install() or irq_uninstall() according to \p arg.
195 */
Eric Anholt6c340ea2007-08-25 20:23:09 +1000196int drm_control(struct inode *inode, struct drm_file *file_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000199 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000200 struct drm_control ctl;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
203
Dave Airliec60ce622007-07-11 15:27:12 +1000204 if (copy_from_user(&ctl, (struct drm_control __user *) arg, sizeof(ctl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -EFAULT;
206
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 switch (ctl.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 case DRM_INST_HANDLER:
209 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
210 return 0;
211 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
212 ctl.irq != dev->irq)
213 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 return drm_irq_install(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 case DRM_UNINST_HANDLER:
216 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
217 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 return drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 default:
220 return -EINVAL;
221 }
222}
223
224/**
225 * Wait for VBLANK.
226 *
227 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000228 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * \param cmd command.
230 * \param data user argument, pointing to a drm_wait_vblank structure.
231 * \return zero on success or a negative number on failure.
232 *
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233 * Verifies the IRQ is installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
235 * If a signal is requested checks if this task has already scheduled the same signal
236 * for the same vblank sequence number - nothing to be done in
237 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
238 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
239 * task.
240 *
241 * If a signal is not requested, then calls vblank_wait().
242 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243int drm_wait_vblank(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Eric Anholt6c340ea2007-08-25 20:23:09 +1000245 struct drm_device *dev = file_priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000246 union drm_wait_vblank __user *argp = (void __user *)data;
247 union drm_wait_vblank vblwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct timeval now;
249 int ret = 0;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000250 unsigned int flags, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (!dev->irq)
253 return -EINVAL;
254
Dave Airlie3d774612006-08-07 20:07:43 +1000255 if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
256 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000258 if (vblwait.request.type &
259 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
260 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
261 vblwait.request.type,
262 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
263 return -EINVAL;
264 }
265
266 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
267
268 if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
269 DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
270 return -EINVAL;
271
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000272 seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
273 : &dev->vbl_received);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000274
275 switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case _DRM_VBLANK_RELATIVE:
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000277 vblwait.request.sequence += seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
279 case _DRM_VBLANK_ABSOLUTE:
280 break;
281 default:
282 return -EINVAL;
283 }
284
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000285 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
286 (seq - vblwait.request.sequence) <= (1<<23)) {
287 vblwait.request.sequence = seq + 1;
288 }
289
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 if (flags & _DRM_VBLANK_SIGNAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unsigned long irqflags;
Dave Airliebd1b3312007-05-26 05:01:51 +1000292 struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000293 ? &dev->vbl_sigs2 : &dev->vbl_sigs;
Dave Airlie55910512007-07-11 16:53:40 +1000294 struct drm_vbl_sig *vbl_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* Check if this task has already scheduled the same signal
299 * for the same vblank sequence number; nothing to be done in
300 * that case
301 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000302 list_for_each_entry(vbl_sig, vbl_sigs, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (vbl_sig->sequence == vblwait.request.sequence
304 && vbl_sig->info.si_signo == vblwait.request.signal
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 && vbl_sig->task == current) {
306 spin_unlock_irqrestore(&dev->vbl_lock,
307 irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=049b3232006-10-24 23:34:58 +1000308 vblwait.reply.sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 goto done;
310 }
311 }
312
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 if (dev->vbl_pending >= 100) {
314 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return -EBUSY;
316 }
317
318 dev->vbl_pending++;
319
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 if (!
323 (vbl_sig =
Dave Airlie55910512007-07-11 16:53:40 +1000324 drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return -ENOMEM;
326 }
327
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 vbl_sig->sequence = vblwait.request.sequence;
331 vbl_sig->info.si_signo = vblwait.request.signal;
332 vbl_sig->task = current;
333
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Dave Airliebd1b3312007-05-26 05:01:51 +1000336 list_add_tail(&vbl_sig->head, vbl_sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=049b3232006-10-24 23:34:58 +1000339
340 vblwait.reply.sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 } else {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000342 if (flags & _DRM_VBLANK_SECONDARY) {
343 if (dev->driver->vblank_wait2)
344 ret = dev->driver->vblank_wait2(dev, &vblwait.request.sequence);
345 } else if (dev->driver->vblank_wait)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 ret =
347 dev->driver->vblank_wait(dev,
348 &vblwait.request.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 do_gettimeofday(&now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 vblwait.reply.tval_sec = now.tv_sec;
352 vblwait.reply.tval_usec = now.tv_usec;
353 }
354
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 done:
Dave Airlie3d774612006-08-07 20:07:43 +1000356 if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
357 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 return ret;
360}
361
362/**
363 * Send the VBLANK signals.
364 *
365 * \param dev DRM device.
366 *
367 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
368 *
369 * If a signal is not requested, then calls vblank_wait().
370 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000371void drm_vbl_send_signals(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 unsigned long flags;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000374 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 spin_lock_irqsave(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000378 for (i = 0; i < 2; i++) {
Dave Airlie55910512007-07-11 16:53:40 +1000379 struct drm_vbl_sig *vbl_sig, *tmp;
Dave Airliebd1b3312007-05-26 05:01:51 +1000380 struct list_head *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000381 unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
382 &dev->vbl_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Dave Airliebd1b3312007-05-26 05:01:51 +1000384 list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000385 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
386 vbl_sig->info.si_code = vbl_seq;
387 send_sig_info(vbl_sig->info.si_signo,
388 &vbl_sig->info, vbl_sig->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Dave Airliebd1b3312007-05-26 05:01:51 +1000390 list_del(&vbl_sig->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000392 drm_free(vbl_sig, sizeof(*vbl_sig),
393 DRM_MEM_DRIVER);
394
395 dev->vbl_pending--;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
399
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000400 spin_unlock_irqrestore(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403EXPORT_SYMBOL(drm_vbl_send_signals);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000404
405/**
406 * Tasklet wrapper function.
407 *
408 * \param data DRM device in disguise.
409 *
410 * Attempts to grab the HW lock and calls the driver callback on success. On
411 * failure, leave the lock marked as contended so the callback can be called
412 * from drm_unlock().
413 */
414static void drm_locked_tasklet_func(unsigned long data)
415{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000416 struct drm_device *dev = (struct drm_device *)data;
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000417 unsigned long irqflags;
418
419 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
420
421 if (!dev->locked_tasklet_func ||
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100422 !drm_lock_take(&dev->lock,
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000423 DRM_KERNEL_CONTEXT)) {
424 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
425 return;
426 }
427
428 dev->lock.lock_time = jiffies;
429 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
430
431 dev->locked_tasklet_func(dev);
432
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100433 drm_lock_free(&dev->lock,
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000434 DRM_KERNEL_CONTEXT);
435
436 dev->locked_tasklet_func = NULL;
437
438 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
439}
440
441/**
442 * Schedule a tasklet to call back a driver hook with the HW lock held.
443 *
444 * \param dev DRM device.
445 * \param func Driver callback.
446 *
447 * This is intended for triggering actions that require the HW lock from an
448 * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
449 * completes. Note that the callback may be called from interrupt or process
450 * context, it must not make any assumptions about this. Also, the HW lock will
451 * be held with the kernel context or any client context.
452 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000453void drm_locked_tasklet(struct drm_device *dev, void (*func)(struct drm_device *))
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000454{
455 unsigned long irqflags;
456 static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
457
=?utf-8?q?Michel_D=C3=A4nzer?=8163e412006-10-24 23:30:01 +1000458 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
459 test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000460 return;
461
462 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
463
464 if (dev->locked_tasklet_func) {
465 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
466 return;
467 }
468
469 dev->locked_tasklet_func = func;
470
471 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
472
473 drm_tasklet.data = (unsigned long)dev;
474
475 tasklet_hi_schedule(&drm_tasklet);
476}
477EXPORT_SYMBOL(drm_locked_tasklet);