blob: 53f0e5af1cc82fd2379b8a48db2afe0ea8d60298 [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 Anholtc153f452007-09-03 12:06:45 +100053int drm_irq_by_busid(struct drm_device *dev, void *data,
54 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Eric Anholtc153f452007-09-03 12:06:45 +100056 struct drm_irq_busid *p = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
59 return -EINVAL;
60
Eric Anholtc153f452007-09-03 12:06:45 +100061 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 Torvalds1da177e2005-04-16 15:20:36 -070064 return -EINVAL;
65
Eric Anholtc153f452007-09-03 12:06:45 +100066 p->irq = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric Anholtc153f452007-09-03 12:06:45 +100068 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
69 p->irq);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 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 Airlie84b1fd12007-07-11 15:53:27 +100084static int drm_irq_install(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100087 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
90 return -EINVAL;
91
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 if (dev->irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -EINVAL;
94
Dave Airlie30e2fb12006-02-02 19:37:46 +110095 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 /* Driver must have been initialized */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100098 if (!dev->dev_private) {
Dave Airlie30e2fb12006-02-02 19:37:46 +110099 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return -EINVAL;
101 }
102
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000103 if (dev->irq_enabled) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100104 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return -EBUSY;
106 }
107 dev->irq_enabled = 1;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100108 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Márton Németh3e684ea2008-01-24 15:58:57 +1000110 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Dave Airlieaf6061a2008-05-07 12:15:39 +1000112 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 Airlieb5e89ed2005-09-25 14:28:13 +1000123 /* Before installing handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dev->driver->irq_preinstall(dev);
125
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126 /* Install handler */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700128 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129
130 ret = request_irq(dev->irq, dev->driver->irq_handler,
131 sh_flags, dev->devname, dev);
132 if (ret < 0) {
Dave Airlie30e2fb12006-02-02 19:37:46 +1100133 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100135 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return ret;
137 }
138
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 /* After installing handler */
Dave Airlieaf6061a2008-05-07 12:15:39 +1000140 dev->driver->irq_postinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Dave Airlieaf6061a2008-05-07 12:15:39 +1000142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145/**
146 * Uninstall the IRQ handler.
147 *
148 * \param dev DRM device.
149 *
150 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
151 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000152int drm_irq_uninstall(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int irq_enabled;
155
156 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
157 return -EINVAL;
158
Dave Airlie30e2fb12006-02-02 19:37:46 +1100159 mutex_lock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 irq_enabled = dev->irq_enabled;
161 dev->irq_enabled = 0;
Dave Airlie30e2fb12006-02-02 19:37:46 +1100162 mutex_unlock(&dev->struct_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return -EINVAL;
166
Márton Németh3e684ea2008-01-24 15:58:57 +1000167 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 dev->driver->irq_uninstall(dev);
170
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000173 dev->locked_tasklet_func = NULL;
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
176}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178EXPORT_SYMBOL(drm_irq_uninstall);
179
180/**
181 * IRQ control ioctl.
182 *
183 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000184 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * \param cmd command.
186 * \param arg user argument, pointing to a drm_control structure.
187 * \return zero on success or a negative number on failure.
188 *
189 * Calls irq_install() or irq_uninstall() according to \p arg.
190 */
Eric Anholtc153f452007-09-03 12:06:45 +1000191int drm_control(struct drm_device *dev, void *data,
192 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Eric Anholtc153f452007-09-03 12:06:45 +1000194 struct drm_control *ctl = data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Eric Anholtc153f452007-09-03 12:06:45 +1000199 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 case DRM_INST_HANDLER:
201 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
202 return 0;
203 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Eric Anholtc153f452007-09-03 12:06:45 +1000204 ctl->irq != dev->irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return -EINVAL;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 return drm_irq_install(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 case DRM_UNINST_HANDLER:
208 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
209 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 return drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 default:
212 return -EINVAL;
213 }
214}
215
216/**
217 * Wait for VBLANK.
218 *
219 * \param inode device inode.
Eric Anholt6c340ea2007-08-25 20:23:09 +1000220 * \param file_priv DRM file private.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * \param cmd command.
222 * \param data user argument, pointing to a drm_wait_vblank structure.
223 * \return zero on success or a negative number on failure.
224 *
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225 * Verifies the IRQ is installed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * If a signal is requested checks if this task has already scheduled the same signal
228 * for the same vblank sequence number - nothing to be done in
229 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
230 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
231 * task.
232 *
233 * If a signal is not requested, then calls vblank_wait().
234 */
Dave Airlieaf6061a2008-05-07 12:15:39 +1000235int drm_wait_vblank(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Eric Anholtc153f452007-09-03 12:06:45 +1000237 union drm_wait_vblank *vblwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 struct timeval now;
239 int ret = 0;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000240 unsigned int flags, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Eric Anholtc153f452007-09-03 12:06:45 +1000242 if ((!dev->irq) || (!dev->irq_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EINVAL;
244
Eric Anholtc153f452007-09-03 12:06:45 +1000245 if (vblwait->request.type &
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000246 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
247 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000248 vblwait->request.type,
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000249 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
250 return -EINVAL;
251 }
252
Eric Anholtc153f452007-09-03 12:06:45 +1000253 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000254
Dave Airlieaf6061a2008-05-07 12:15:39 +1000255 if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
256 DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000257 return -EINVAL;
258
Dave Airlieaf6061a2008-05-07 12:15:39 +1000259 seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
260 : &dev->vbl_received);
=?utf-8?q?Michel_D=C3=A4nzer?=776c9442006-10-24 22:24:38 +1000261
Eric Anholtc153f452007-09-03 12:06:45 +1000262 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case _DRM_VBLANK_RELATIVE:
Eric Anholtc153f452007-09-03 12:06:45 +1000264 vblwait->request.sequence += seq;
265 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 case _DRM_VBLANK_ABSOLUTE:
267 break;
268 default:
269 return -EINVAL;
270 }
271
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000272 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Eric Anholtc153f452007-09-03 12:06:45 +1000273 (seq - vblwait->request.sequence) <= (1<<23)) {
274 vblwait->request.sequence = seq + 1;
=?utf-8?q?Michel_D=C3=A4nzer?=ab285d72006-10-24 23:34:18 +1000275 }
276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 if (flags & _DRM_VBLANK_SIGNAL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 unsigned long irqflags;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000279 struct list_head *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
280 ? &dev->vbl_sigs2 : &dev->vbl_sigs;
Dave Airlie55910512007-07-11 16:53:40 +1000281 struct drm_vbl_sig *vbl_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 /* Check if this task has already scheduled the same signal
286 * for the same vblank sequence number; nothing to be done in
287 * that case
288 */
Dave Airliebd1b3312007-05-26 05:01:51 +1000289 list_for_each_entry(vbl_sig, vbl_sigs, head) {
Eric Anholtc153f452007-09-03 12:06:45 +1000290 if (vbl_sig->sequence == vblwait->request.sequence
291 && vbl_sig->info.si_signo ==
292 vblwait->request.signal
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 && vbl_sig->task == current) {
294 spin_unlock_irqrestore(&dev->vbl_lock,
295 irqflags);
Eric Anholtc153f452007-09-03 12:06:45 +1000296 vblwait->reply.sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 goto done;
298 }
299 }
300
Dave Airlieaf6061a2008-05-07 12:15:39 +1000301 if (dev->vbl_pending >= 100) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return -EBUSY;
304 }
305
Dave Airlieaf6061a2008-05-07 12:15:39 +1000306 dev->vbl_pending++;
307
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Dave Airlieaf6061a2008-05-07 12:15:39 +1000310 if (!
311 (vbl_sig =
312 drm_alloc(sizeof(struct drm_vbl_sig), DRM_MEM_DRIVER))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return -ENOMEM;
314 }
315
Dave Airlieaf6061a2008-05-07 12:15:39 +1000316 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Eric Anholtc153f452007-09-03 12:06:45 +1000318 vbl_sig->sequence = vblwait->request.sequence;
319 vbl_sig->info.si_signo = vblwait->request.signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 vbl_sig->task = current;
321
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 spin_lock_irqsave(&dev->vbl_lock, irqflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dave Airliebd1b3312007-05-26 05:01:51 +1000324 list_add_tail(&vbl_sig->head, vbl_sigs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=049b3232006-10-24 23:34:58 +1000327
Eric Anholtc153f452007-09-03 12:06:45 +1000328 vblwait->reply.sequence = seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 } else {
Dave Airlieaf6061a2008-05-07 12:15:39 +1000330 if (flags & _DRM_VBLANK_SECONDARY) {
331 if (dev->driver->vblank_wait2)
332 ret = dev->driver->vblank_wait2(dev, &vblwait->request.sequence);
333 } else if (dev->driver->vblank_wait)
334 ret =
335 dev->driver->vblank_wait(dev,
336 &vblwait->request.sequence);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 do_gettimeofday(&now);
Eric Anholtc153f452007-09-03 12:06:45 +1000339 vblwait->reply.tval_sec = now.tv_sec;
340 vblwait->reply.tval_usec = now.tv_usec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000343 done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return ret;
345}
346
347/**
348 * Send the VBLANK signals.
349 *
350 * \param dev DRM device.
351 *
352 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
353 *
354 * If a signal is not requested, then calls vblank_wait().
355 */
Dave Airlieaf6061a2008-05-07 12:15:39 +1000356void drm_vbl_send_signals(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 unsigned long flags;
Dave Airlieaf6061a2008-05-07 12:15:39 +1000359 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 spin_lock_irqsave(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Dave Airlieaf6061a2008-05-07 12:15:39 +1000363 for (i = 0; i < 2; i++) {
364 struct drm_vbl_sig *vbl_sig, *tmp;
365 struct list_head *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
366 unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
367 &dev->vbl_received);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Dave Airlieaf6061a2008-05-07 12:15:39 +1000369 list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
370 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
371 vbl_sig->info.si_code = vbl_seq;
372 send_sig_info(vbl_sig->info.si_signo,
373 &vbl_sig->info, vbl_sig->task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Airlieaf6061a2008-05-07 12:15:39 +1000375 list_del(&vbl_sig->head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dave Airlieaf6061a2008-05-07 12:15:39 +1000377 drm_free(vbl_sig, sizeof(*vbl_sig),
378 DRM_MEM_DRIVER);
379
380 dev->vbl_pending--;
381 }
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 spin_unlock_irqrestore(&dev->vbl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387
Dave Airlieaf6061a2008-05-07 12:15:39 +1000388EXPORT_SYMBOL(drm_vbl_send_signals);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000389
390/**
391 * Tasklet wrapper function.
392 *
393 * \param data DRM device in disguise.
394 *
395 * Attempts to grab the HW lock and calls the driver callback on success. On
396 * failure, leave the lock marked as contended so the callback can be called
397 * from drm_unlock().
398 */
399static void drm_locked_tasklet_func(unsigned long data)
400{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000401 struct drm_device *dev = (struct drm_device *)data;
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000402 unsigned long irqflags;
Thomas Hellstrome5b4f192008-08-24 17:00:00 +1000403 void (*tasklet_func)(struct drm_device *);
404
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000405 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
Thomas Hellstrome5b4f192008-08-24 17:00:00 +1000406 tasklet_func = dev->locked_tasklet_func;
407 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000408
Thomas Hellstrome5b4f192008-08-24 17:00:00 +1000409 if (!tasklet_func ||
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100410 !drm_lock_take(&dev->lock,
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000411 DRM_KERNEL_CONTEXT)) {
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000412 return;
413 }
414
415 dev->lock.lock_time = jiffies;
416 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
417
Thomas Hellstrome5b4f192008-08-24 17:00:00 +1000418 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
419 tasklet_func = dev->locked_tasklet_func;
420 dev->locked_tasklet_func = NULL;
421 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
422
423 if (tasklet_func != NULL)
424 tasklet_func(dev);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000425
Thomas Hellstrom040ac322007-03-23 13:28:33 +1100426 drm_lock_free(&dev->lock,
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000427 DRM_KERNEL_CONTEXT);
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000428}
429
430/**
431 * Schedule a tasklet to call back a driver hook with the HW lock held.
432 *
433 * \param dev DRM device.
434 * \param func Driver callback.
435 *
436 * This is intended for triggering actions that require the HW lock from an
437 * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
438 * completes. Note that the callback may be called from interrupt or process
439 * context, it must not make any assumptions about this. Also, the HW lock will
440 * be held with the kernel context or any client context.
441 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000442void 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 +1000443{
444 unsigned long irqflags;
445 static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
446
=?utf-8?q?Michel_D=C3=A4nzer?=8163e412006-10-24 23:30:01 +1000447 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
448 test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
=?utf-8?q?Michel_D=C3=A4nzer?=2e54a002006-10-24 23:08:16 +1000449 return;
450
451 spin_lock_irqsave(&dev->tasklet_lock, irqflags);
452
453 if (dev->locked_tasklet_func) {
454 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
455 return;
456 }
457
458 dev->locked_tasklet_func = func;
459
460 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
461
462 drm_tasklet.data = (unsigned long)dev;
463
464 tasklet_hi_schedule(&drm_tasklet);
465}
466EXPORT_SYMBOL(drm_locked_tasklet);