blob: 871d2fde09b33ee3d1c8b48b5b6f5d74af0cdffa [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{
Dave Airlie84b1fd12007-07-11 15:53:27 +100056 struct drm_file *priv = filp->private_data;
57 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +100058 struct drm_irq_busid __user *argp = (void __user *)arg;
59 struct drm_irq_busid p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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 Airlie84b1fd12007-07-11 15:53:27 +100090static int drm_irq_install(struct drm_device * 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);
122
Dave Airliebd1b3312007-05-26 05:01:51 +1000123 INIT_LIST_HEAD(&dev->vbl_sigs);
124 INIT_LIST_HEAD(&dev->vbl_sigs2);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 dev->vbl_pending = 0;
127 }
128
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 /* Before installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 dev->driver->irq_preinstall(dev);
131
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700134 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135
136 ret = request_irq(dev->irq, dev->driver->irq_handler,
137 sh_flags, dev->devname, dev);
138 if (ret < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100139 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100141 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return ret;
143 }
144
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000145 /* After installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 dev->driver->irq_postinstall(dev);
147
148 return 0;
149}
150
151/**
152 * Uninstall the IRQ handler.
153 *
154 * \param dev DRM device.
155 *
156 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
157 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000158int drm_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 int irq_enabled;
161
162 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
163 return -EINVAL;
164
Dave Airlie30e2fb12006-02-02 19:37:46 +1100165 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 irq_enabled = dev->irq_enabled;
167 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100168 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -EINVAL;
172
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 dev->driver->irq_uninstall(dev);
176
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000179 dev->locked_tasklet_func = NULL;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return 0;
182}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184EXPORT_SYMBOL(drm_irq_uninstall);
185
186/**
187 * IRQ control ioctl.
188 *
189 * \param inode device inode.
190 * \param filp file pointer.
191 * \param cmd command.
192 * \param arg user argument, pointing to a drm_control structure.
193 * \return zero on success or a negative number on failure.
194 *
195 * Calls irq_install() or irq_uninstall() according to \p arg.
196 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000197int drm_control(struct inode *inode, struct file *filp,
198 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000200 struct drm_file *priv = filp->private_data;
201 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000202 struct drm_control ctl;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
205
Dave Airliec60ce622007-07-11 15:27:12 +1000206 if (copy_from_user(&ctl, (struct drm_control __user *) arg, sizeof(ctl)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -EFAULT;
208
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209 switch (ctl.func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 case DRM_INST_HANDLER:
211 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
212 return 0;
213 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
214 ctl.irq != dev->irq)
215 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 return drm_irq_install(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 case DRM_UNINST_HANDLER:
218 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
219 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220 return drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 default:
222 return -EINVAL;
223 }
224}
225
226/**
227 * Wait for VBLANK.
228 *
229 * \param inode device inode.
230 * \param filp file pointer.
231 * \param cmd command.
232 * \param data user argument, pointing to a drm_wait_vblank structure.
233 * \return zero on success or a negative number on failure.
234 *
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235 * Verifies the IRQ is installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 *
237 * If a signal is requested checks if this task has already scheduled the same signal
238 * for the same vblank sequence number - nothing to be done in
239 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
240 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
241 * task.
242 *
243 * If a signal is not requested, then calls vblank_wait().
244 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245int drm_wait_vblank(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000247 struct drm_file *priv = filp->private_data;
248 struct drm_device *dev = priv->head->dev;
Dave Airliec60ce622007-07-11 15:27:12 +1000249 union drm_wait_vblank __user *argp = (void __user *)data;
250 union drm_wait_vblank vblwait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 struct timeval now;
252 int ret = 0;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000253 unsigned int flags, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 if (!dev->irq)
256 return -EINVAL;
257
Dave Airlie3d774612006-08-07 20:07:43 +1000258 if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
259 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000261 if (vblwait.request.type &
262 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
263 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
264 vblwait.request.type,
265 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
266 return -EINVAL;
267 }
268
269 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
270
271 if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
272 DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
273 return -EINVAL;
274
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000275 seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
276 : &dev->vbl_received);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000277
278 switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 case _DRM_VBLANK_RELATIVE:
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000280 vblwait.request.sequence += seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
282 case _DRM_VBLANK_ABSOLUTE:
283 break;
284 default:
285 return -EINVAL;
286 }
287
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000288 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
289 (seq - vblwait.request.sequence) <= (1<<23)) {
290 vblwait.request.sequence = seq + 1;
291 }
292
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 if (flags & _DRM_VBLANK_SIGNAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 unsigned long irqflags;
Dave Airliebd1b3312007-05-26 05:01:51 +1000295 struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000296 ? &dev->vbl_sigs2 : &dev->vbl_sigs;
Dave Airlie55910512007-07-11 16:53:40 +1000297 struct drm_vbl_sig *vbl_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Check if this task has already scheduled the same signal
302 * for the same vblank sequence number; nothing to be done in
303 * that case
304 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000305 list_for_each_entry(vbl_sig, vbl_sigs, head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (vbl_sig->sequence == vblwait.request.sequence
307 && vbl_sig->info.si_signo == vblwait.request.signal
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 && vbl_sig->task == current) {
309 spin_unlock_irqrestore(&dev->vbl_lock,
310 irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=049b3232006-10-24 23:34:58 +1000311 vblwait.reply.sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 goto done;
313 }
314 }
315
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 if (dev->vbl_pending >= 100) {
317 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return -EBUSY;
319 }
320
321 dev->vbl_pending++;
322
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 if (!
326 (vbl_sig =
Dave Airlie55910512007-07-11 16:53:40 +1000327 drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return -ENOMEM;
329 }
330
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 vbl_sig->sequence = vblwait.request.sequence;
334 vbl_sig->info.si_signo = vblwait.request.signal;
335 vbl_sig->task = current;
336
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Dave Airliebd1b3312007-05-26 05:01:51 +1000339 list_add_tail(&vbl_sig->head, vbl_sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000341 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=049b3232006-10-24 23:34:58 +1000342
343 vblwait.reply.sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 } else {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000345 if (flags & _DRM_VBLANK_SECONDARY) {
346 if (dev->driver->vblank_wait2)
347 ret = dev->driver->vblank_wait2(dev, &vblwait.request.sequence);
348 } else if (dev->driver->vblank_wait)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000349 ret =
350 dev->driver->vblank_wait(dev,
351 &vblwait.request.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000353 do_gettimeofday(&now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 vblwait.reply.tval_sec = now.tv_sec;
355 vblwait.reply.tval_usec = now.tv_usec;
356 }
357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 done:
Dave Airlie3d774612006-08-07 20:07:43 +1000359 if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
360 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 return ret;
363}
364
365/**
366 * Send the VBLANK signals.
367 *
368 * \param dev DRM device.
369 *
370 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
371 *
372 * If a signal is not requested, then calls vblank_wait().
373 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000374void drm_vbl_send_signals(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned long flags;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000377 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 spin_lock_irqsave(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000381 for (i = 0; i < 2; i++) {
Dave Airlie55910512007-07-11 16:53:40 +1000382 struct drm_vbl_sig *vbl_sig, *tmp;
Dave Airliebd1b3312007-05-26 05:01:51 +1000383 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 +1000384 unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
385 &dev->vbl_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dave Airliebd1b3312007-05-26 05:01:51 +1000387 list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000388 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
389 vbl_sig->info.si_code = vbl_seq;
390 send_sig_info(vbl_sig->info.si_signo,
391 &vbl_sig->info, vbl_sig->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Dave Airliebd1b3312007-05-26 05:01:51 +1000393 list_del(&vbl_sig->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000395 drm_free(vbl_sig, sizeof(*vbl_sig),
396 DRM_MEM_DRIVER);
397
398 dev->vbl_pending--;
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401 }
402
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000403 spin_unlock_irqrestore(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406EXPORT_SYMBOL(drm_vbl_send_signals);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000407
408/**
409 * Tasklet wrapper function.
410 *
411 * \param data DRM device in disguise.
412 *
413 * Attempts to grab the HW lock and calls the driver callback on success. On
414 * failure, leave the lock marked as contended so the callback can be called
415 * from drm_unlock().
416 */
417static void drm_locked_tasklet_func(unsigned long data)
418{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000419 struct drm_device *dev = (struct drm_device *)data;
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000420 unsigned long irqflags;
421
422 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
423
424 if (!dev->locked_tasklet_func ||
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100425 !drm_lock_take(&dev->lock,
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000426 DRM_KERNEL_CONTEXT)) {
427 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
428 return;
429 }
430
431 dev->lock.lock_time = jiffies;
432 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
433
434 dev->locked_tasklet_func(dev);
435
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100436 drm_lock_free(&dev->lock,
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000437 DRM_KERNEL_CONTEXT);
438
439 dev->locked_tasklet_func = NULL;
440
441 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
442}
443
444/**
445 * Schedule a tasklet to call back a driver hook with the HW lock held.
446 *
447 * \param dev DRM device.
448 * \param func Driver callback.
449 *
450 * This is intended for triggering actions that require the HW lock from an
451 * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
452 * completes. Note that the callback may be called from interrupt or process
453 * context, it must not make any assumptions about this. Also, the HW lock will
454 * be held with the kernel context or any client context.
455 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000456void 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 +1000457{
458 unsigned long irqflags;
459 static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
460
=?utf-8?q?Michel_D=C3=A4nzer?=8163e412006-10-24 23:30:01 +1000461 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
462 test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000463 return;
464
465 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
466
467 if (dev->locked_tasklet_func) {
468 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
469 return;
470 }
471
472 dev->locked_tasklet_func = func;
473
474 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
475
476 drm_tasklet.data = (unsigned long)dev;
477
478 tasklet_hi_schedule(&drm_tasklet);
479}
480EXPORT_SYMBOL(drm_locked_tasklet);