blob: 2b10e5b60cfa98737b616511b7897cdb6b656bf1 [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.
44 * \param filp file pointer.
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 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 */
53int drm_irq_by_busid(struct inode *inode, struct file *filp,
Dave Airlieb5e89ed2005-09-25 14:28:13 +100054 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 drm_file_t *priv = filp->private_data;
57 drm_device_t *dev = priv->head->dev;
58 drm_irq_busid_t __user *argp = (void __user *)arg;
59 drm_irq_busid_t p;
60
61 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
62 return -EINVAL;
63
64 if (copy_from_user(&p, argp, sizeof(p)))
65 return -EFAULT;
66
Dave Airlie332296012006-08-07 20:23:42 +100067 if ((p.busnum >> 8) != drm_get_pci_domain(dev) ||
Dave242ef0e2006-07-18 04:01:01 +100068 (p.busnum & 0xff) != dev->pdev->bus->number ||
69 p.devnum != PCI_SLOT(dev->pdev->devfn) || p.funcnum != PCI_FUNC(dev->pdev->devfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EINVAL;
71
72 p.irq = dev->irq;
73
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p.busnum, p.devnum, p.funcnum, p.irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (copy_to_user(argp, &p, sizeof(p)))
76 return -EFAULT;
77 return 0;
78}
79
80/**
81 * Install IRQ handler.
82 *
83 * \param dev DRM device.
84 * \param irq IRQ number.
85 *
86 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
87 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
88 * before and after the installation.
89 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090static int drm_irq_install(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
96 return -EINVAL;
97
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 if (dev->irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return -EINVAL;
100
Dave Airlie30e2fb12006-02-02 19:37:46 +1100101 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* Driver must have been initialized */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 if (!dev->dev_private) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100105 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return -EINVAL;
107 }
108
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 if (dev->irq_enabled) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100110 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EBUSY;
112 }
113 dev->irq_enabled = 1;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100114 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
119 init_waitqueue_head(&dev->vbl_queue);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120
121 spin_lock_init(&dev->vbl_lock);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000122 spin_lock_init(&dev->tasklet_lock);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123
124 INIT_LIST_HEAD(&dev->vbl_sigs.head);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000125 INIT_LIST_HEAD(&dev->vbl_sigs2.head);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 dev->vbl_pending = 0;
128 }
129
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 /* Before installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 dev->driver->irq_preinstall(dev);
132
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700135 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136
137 ret = request_irq(dev->irq, dev->driver->irq_handler,
138 sh_flags, dev->devname, dev);
139 if (ret < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100140 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100142 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return ret;
144 }
145
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000146 /* After installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 dev->driver->irq_postinstall(dev);
148
149 return 0;
150}
151
152/**
153 * Uninstall the IRQ handler.
154 *
155 * \param dev DRM device.
156 *
157 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
158 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159int drm_irq_uninstall(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int irq_enabled;
162
163 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
164 return -EINVAL;
165
Dave Airlie30e2fb12006-02-02 19:37:46 +1100166 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 irq_enabled = dev->irq_enabled;
168 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100169 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return -EINVAL;
173
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 dev->driver->irq_uninstall(dev);
177
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000180 dev->locked_tasklet_func = NULL;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return 0;
183}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL(drm_irq_uninstall);
186
187/**
188 * IRQ control ioctl.
189 *
190 * \param inode device inode.
191 * \param filp file pointer.
192 * \param cmd command.
193 * \param arg user argument, pointing to a drm_control structure.
194 * \return zero on success or a negative number on failure.
195 *
196 * Calls irq_install() or irq_uninstall() according to \p arg.
197 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198int drm_control(struct inode *inode, struct file *filp,
199 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 drm_file_t *priv = filp->private_data;
202 drm_device_t *dev = priv->head->dev;
203 drm_control_t ctl;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
206
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 if (copy_from_user(&ctl, (drm_control_t __user *) arg, sizeof(ctl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -EFAULT;
209
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 switch (ctl.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 case DRM_INST_HANDLER:
212 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
213 return 0;
214 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
215 ctl.irq != dev->irq)
216 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 return drm_irq_install(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 case DRM_UNINST_HANDLER:
219 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
220 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000221 return drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 default:
223 return -EINVAL;
224 }
225}
226
227/**
228 * Wait for VBLANK.
229 *
230 * \param inode device inode.
231 * \param filp file pointer.
232 * \param cmd command.
233 * \param data user argument, pointing to a drm_wait_vblank structure.
234 * \return zero on success or a negative number on failure.
235 *
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000236 * Verifies the IRQ is installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 * If a signal is requested checks if this task has already scheduled the same signal
239 * for the same vblank sequence number - nothing to be done in
240 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
241 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
242 * task.
243 *
244 * If a signal is not requested, then calls vblank_wait().
245 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246int drm_wait_vblank(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 drm_file_t *priv = filp->private_data;
249 drm_device_t *dev = priv->head->dev;
250 drm_wait_vblank_t __user *argp = (void __user *)data;
251 drm_wait_vblank_t vblwait;
252 struct timeval now;
253 int ret = 0;
254 unsigned int flags;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000255 atomic_t *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 if (!dev->irq)
258 return -EINVAL;
259
Dave Airlie3d774612006-08-07 20:07:43 +1000260 if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
261 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000263 if (vblwait.request.type &
264 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
265 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
266 vblwait.request.type,
267 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
268 return -EINVAL;
269 }
270
271 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
272
273 if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
274 DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
275 return -EINVAL;
276
277 seq = (flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2 :
278 &dev->vbl_received;
279
280 switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 case _DRM_VBLANK_RELATIVE:
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000282 vblwait.request.sequence += atomic_read(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
284 case _DRM_VBLANK_ABSOLUTE:
285 break;
286 default:
287 return -EINVAL;
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;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000292 drm_vbl_sig_t *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
293 ? &dev->vbl_sigs2 : &dev->vbl_sigs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 drm_vbl_sig_t *vbl_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000296 vblwait.reply.sequence = atomic_read(seq);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297
298 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* Check if this task has already scheduled the same signal
301 * for the same vblank sequence number; nothing to be done in
302 * that case
303 */
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000304 list_for_each_entry(vbl_sig, &vbl_sigs->head, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (vbl_sig->sequence == vblwait.request.sequence
306 && vbl_sig->info.si_signo == vblwait.request.signal
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000307 && vbl_sig->task == current) {
308 spin_unlock_irqrestore(&dev->vbl_lock,
309 irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto done;
311 }
312 }
313
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314 if (dev->vbl_pending >= 100) {
315 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return -EBUSY;
317 }
318
319 dev->vbl_pending++;
320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 if (!
324 (vbl_sig =
325 drm_alloc(sizeof(drm_vbl_sig_t), DRM_MEM_DRIVER))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return -ENOMEM;
327 }
328
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 vbl_sig->sequence = vblwait.request.sequence;
332 vbl_sig->info.si_signo = vblwait.request.signal;
333 vbl_sig->task = current;
334
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000337 list_add_tail((struct list_head *)vbl_sig, &vbl_sigs->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 } else {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000341 if (flags & _DRM_VBLANK_SECONDARY) {
342 if (dev->driver->vblank_wait2)
343 ret = dev->driver->vblank_wait2(dev, &vblwait.request.sequence);
344 } else if (dev->driver->vblank_wait)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000345 ret =
346 dev->driver->vblank_wait(dev,
347 &vblwait.request.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 do_gettimeofday(&now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 vblwait.reply.tval_sec = now.tv_sec;
351 vblwait.reply.tval_usec = now.tv_usec;
352 }
353
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 done:
Dave Airlie3d774612006-08-07 20:07:43 +1000355 if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
356 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 return ret;
359}
360
361/**
362 * Send the VBLANK signals.
363 *
364 * \param dev DRM device.
365 *
366 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
367 *
368 * If a signal is not requested, then calls vblank_wait().
369 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370void drm_vbl_send_signals(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 unsigned long flags;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000373 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000375 spin_lock_irqsave(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000377 for (i = 0; i < 2; i++) {
378 struct list_head *list, *tmp;
379 drm_vbl_sig_t *vbl_sig;
380 drm_vbl_sig_t *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
381 unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
382 &dev->vbl_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000384 list_for_each_safe(list, tmp, &vbl_sigs->head) {
385 vbl_sig = list_entry(list, drm_vbl_sig_t, head);
386 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
387 vbl_sig->info.si_code = vbl_seq;
388 send_sig_info(vbl_sig->info.si_signo,
389 &vbl_sig->info, vbl_sig->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000391 list_del(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000393 drm_free(vbl_sig, sizeof(*vbl_sig),
394 DRM_MEM_DRIVER);
395
396 dev->vbl_pending--;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 }
400
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 spin_unlock_irqrestore(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404EXPORT_SYMBOL(drm_vbl_send_signals);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000405
406/**
407 * Tasklet wrapper function.
408 *
409 * \param data DRM device in disguise.
410 *
411 * Attempts to grab the HW lock and calls the driver callback on success. On
412 * failure, leave the lock marked as contended so the callback can be called
413 * from drm_unlock().
414 */
415static void drm_locked_tasklet_func(unsigned long data)
416{
417 drm_device_t *dev = (drm_device_t*)data;
418 unsigned long irqflags;
419
420 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
421
422 if (!dev->locked_tasklet_func ||
423 !drm_lock_take(&dev->lock.hw_lock->lock,
424 DRM_KERNEL_CONTEXT)) {
425 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
426 return;
427 }
428
429 dev->lock.lock_time = jiffies;
430 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
431
432 dev->locked_tasklet_func(dev);
433
434 drm_lock_free(dev, &dev->lock.hw_lock->lock,
435 DRM_KERNEL_CONTEXT);
436
437 dev->locked_tasklet_func = NULL;
438
439 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
440}
441
442/**
443 * Schedule a tasklet to call back a driver hook with the HW lock held.
444 *
445 * \param dev DRM device.
446 * \param func Driver callback.
447 *
448 * This is intended for triggering actions that require the HW lock from an
449 * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
450 * completes. Note that the callback may be called from interrupt or process
451 * context, it must not make any assumptions about this. Also, the HW lock will
452 * be held with the kernel context or any client context.
453 */
454void drm_locked_tasklet(drm_device_t *dev, void (*func)(drm_device_t*))
455{
456 unsigned long irqflags;
457 static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
458
459 if (test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
460 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);