drm: misc cleanup
[firewire-audio.git] / drivers / char / drm / drm_irq.c
blobcdd4aecd25e2616b8d5f1dda0db1191b58e58ba0
1 /**
2 * \file drm_irq.h
3 * IRQ support
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
9 /*
10 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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.
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.
36 #include "drmP.h"
38 #include <linux/interrupt.h> /* For task queue support */
40 /**
41 * Get interrupt from bus id.
43 * \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.
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.
53 int drm_irq_by_busid(struct inode *inode, struct file *filp,
54 unsigned int cmd, unsigned long arg)
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;
61 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
62 return -EINVAL;
64 if (copy_from_user(&p, argp, sizeof(p)))
65 return -EFAULT;
67 if ((p.busnum >> 8) != dev->pci_domain ||
68 (p.busnum & 0xff) != dev->pci_bus ||
69 p.devnum != dev->pci_slot ||
70 p.funcnum != dev->pci_func)
71 return -EINVAL;
73 p.irq = dev->irq;
75 DRM_DEBUG("%d:%d:%d => IRQ %d\n",
76 p.busnum, p.devnum, p.funcnum, p.irq);
77 if (copy_to_user(argp, &p, sizeof(p)))
78 return -EFAULT;
79 return 0;
82 /**
83 * Install IRQ handler.
85 * \param dev DRM device.
86 * \param irq IRQ number.
88 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
89 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
90 * before and after the installation.
92 static int drm_irq_install( drm_device_t *dev )
94 int ret;
95 unsigned long sh_flags=0;
97 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
98 return -EINVAL;
100 if ( dev->irq == 0 )
101 return -EINVAL;
103 down( &dev->struct_sem );
105 /* Driver must have been initialized */
106 if ( !dev->dev_private ) {
107 up( &dev->struct_sem );
108 return -EINVAL;
111 if ( dev->irq_enabled ) {
112 up( &dev->struct_sem );
113 return -EBUSY;
115 dev->irq_enabled = 1;
116 up( &dev->struct_sem );
118 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
120 if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
121 init_waitqueue_head(&dev->vbl_queue);
123 spin_lock_init( &dev->vbl_lock );
125 INIT_LIST_HEAD( &dev->vbl_sigs.head );
127 dev->vbl_pending = 0;
130 /* Before installing handler */
131 dev->driver->irq_preinstall(dev);
133 /* Install handler */
134 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
135 sh_flags = SA_SHIRQ;
137 ret = request_irq( dev->irq, dev->driver->irq_handler,
138 sh_flags, dev->devname, dev );
139 if ( ret < 0 ) {
140 down( &dev->struct_sem );
141 dev->irq_enabled = 0;
142 up( &dev->struct_sem );
143 return ret;
146 /* After installing handler */
147 dev->driver->irq_postinstall(dev);
149 return 0;
153 * Uninstall the IRQ handler.
155 * \param dev DRM device.
157 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
159 int drm_irq_uninstall( drm_device_t *dev )
161 int irq_enabled;
163 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
164 return -EINVAL;
166 down( &dev->struct_sem );
167 irq_enabled = dev->irq_enabled;
168 dev->irq_enabled = 0;
169 up( &dev->struct_sem );
171 if ( !irq_enabled )
172 return -EINVAL;
174 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
176 dev->driver->irq_uninstall(dev);
178 free_irq( dev->irq, dev );
180 return 0;
182 EXPORT_SYMBOL(drm_irq_uninstall);
185 * IRQ control ioctl.
187 * \param inode device inode.
188 * \param filp file pointer.
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.
193 * Calls irq_install() or irq_uninstall() according to \p arg.
195 int drm_control( struct inode *inode, struct file *filp,
196 unsigned int cmd, unsigned long arg )
198 drm_file_t *priv = filp->private_data;
199 drm_device_t *dev = priv->head->dev;
200 drm_control_t ctl;
202 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
204 if ( copy_from_user( &ctl, (drm_control_t __user *)arg, sizeof(ctl) ) )
205 return -EFAULT;
207 switch ( ctl.func ) {
208 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;
214 return drm_irq_install( dev );
215 case DRM_UNINST_HANDLER:
216 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
217 return 0;
218 return drm_irq_uninstall( dev );
219 default:
220 return -EINVAL;
225 * Wait for VBLANK.
227 * \param inode device inode.
228 * \param filp file pointer.
229 * \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.
233 * Verifies the IRQ is installed.
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.
241 * If a signal is not requested, then calls vblank_wait().
243 int drm_wait_vblank( DRM_IOCTL_ARGS )
245 drm_file_t *priv = filp->private_data;
246 drm_device_t *dev = priv->head->dev;
247 drm_wait_vblank_t __user *argp = (void __user *)data;
248 drm_wait_vblank_t vblwait;
249 struct timeval now;
250 int ret = 0;
251 unsigned int flags;
253 if (!drm_core_check_feature(dev, DRIVER_IRQ_VBL))
254 return -EINVAL;
256 if (!dev->irq)
257 return -EINVAL;
259 DRM_COPY_FROM_USER_IOCTL( vblwait, argp, sizeof(vblwait) );
261 switch ( vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK ) {
262 case _DRM_VBLANK_RELATIVE:
263 vblwait.request.sequence += atomic_read( &dev->vbl_received );
264 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
265 case _DRM_VBLANK_ABSOLUTE:
266 break;
267 default:
268 return -EINVAL;
271 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
273 if ( flags & _DRM_VBLANK_SIGNAL ) {
274 unsigned long irqflags;
275 drm_vbl_sig_t *vbl_sig;
277 vblwait.reply.sequence = atomic_read( &dev->vbl_received );
279 spin_lock_irqsave( &dev->vbl_lock, irqflags );
281 /* Check if this task has already scheduled the same signal
282 * for the same vblank sequence number; nothing to be done in
283 * that case
285 list_for_each_entry( vbl_sig, &dev->vbl_sigs.head, head ) {
286 if (vbl_sig->sequence == vblwait.request.sequence
287 && vbl_sig->info.si_signo == vblwait.request.signal
288 && vbl_sig->task == current)
290 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
291 goto done;
295 if ( dev->vbl_pending >= 100 ) {
296 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
297 return -EBUSY;
300 dev->vbl_pending++;
302 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
304 if ( !( vbl_sig = drm_alloc( sizeof( drm_vbl_sig_t ), DRM_MEM_DRIVER ) ) ) {
305 return -ENOMEM;
308 memset( (void *)vbl_sig, 0, sizeof(*vbl_sig) );
310 vbl_sig->sequence = vblwait.request.sequence;
311 vbl_sig->info.si_signo = vblwait.request.signal;
312 vbl_sig->task = current;
314 spin_lock_irqsave( &dev->vbl_lock, irqflags );
316 list_add_tail( (struct list_head *) vbl_sig, &dev->vbl_sigs.head );
318 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
319 } else {
320 if (dev->driver->vblank_wait)
321 ret = dev->driver->vblank_wait( dev, &vblwait.request.sequence );
323 do_gettimeofday( &now );
324 vblwait.reply.tval_sec = now.tv_sec;
325 vblwait.reply.tval_usec = now.tv_usec;
328 done:
329 DRM_COPY_TO_USER_IOCTL( argp, vblwait, sizeof(vblwait) );
331 return ret;
335 * Send the VBLANK signals.
337 * \param dev DRM device.
339 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
341 * If a signal is not requested, then calls vblank_wait().
343 void drm_vbl_send_signals( drm_device_t *dev )
345 struct list_head *list, *tmp;
346 drm_vbl_sig_t *vbl_sig;
347 unsigned int vbl_seq = atomic_read( &dev->vbl_received );
348 unsigned long flags;
350 spin_lock_irqsave( &dev->vbl_lock, flags );
352 list_for_each_safe( list, tmp, &dev->vbl_sigs.head ) {
353 vbl_sig = list_entry( list, drm_vbl_sig_t, head );
354 if ( ( vbl_seq - vbl_sig->sequence ) <= (1<<23) ) {
355 vbl_sig->info.si_code = vbl_seq;
356 send_sig_info( vbl_sig->info.si_signo, &vbl_sig->info, vbl_sig->task );
358 list_del( list );
360 drm_free( vbl_sig, sizeof(*vbl_sig), DRM_MEM_DRIVER );
362 dev->vbl_pending--;
366 spin_unlock_irqrestore( &dev->vbl_lock, flags );
368 EXPORT_SYMBOL(drm_vbl_send_signals);