vserver 1.9.3
[linux-2.6.git] / drivers / char / drm / drm_irq.h
1 /**
2  * \file drm_irq.h 
3  * 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.
42  * 
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.
48  * 
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.
52  */
53 int DRM(irq_by_busid)(struct inode *inode, struct file *filp,
54                    unsigned int cmd, unsigned long arg)
55 {
56         drm_file_t *priv = filp->private_data;
57         drm_device_t *dev = priv->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
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;
72
73         p.irq = dev->irq;
74
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;
80 }
81
82 /**
83  * Install IRQ handler.
84  *
85  * \param dev DRM device.
86  * \param irq IRQ number.
87  *
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.
91  */
92 int DRM(irq_install)( drm_device_t *dev )
93 {
94         int ret;
95         unsigned long sh_flags=0;
96
97         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
98                 return -EINVAL;
99
100         if ( dev->irq == 0 )
101                 return -EINVAL;
102
103         down( &dev->struct_sem );
104
105         /* Driver must have been initialized */
106         if ( !dev->dev_private ) {
107                 up( &dev->struct_sem );
108                 return -EINVAL;
109         }
110
111         if ( dev->irq_enabled ) {
112                 up( &dev->struct_sem );
113                 return -EBUSY;
114         }
115         dev->irq_enabled = 1;
116         up( &dev->struct_sem );
117
118         DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
119
120         if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
121                 init_waitqueue_head(&dev->vbl_queue);
122                 
123                 spin_lock_init( &dev->vbl_lock );
124                 
125                 INIT_LIST_HEAD( &dev->vbl_sigs.head );
126                 
127                 dev->vbl_pending = 0;
128         }
129
130                                 /* Before installing handler */
131         dev->fn_tbl.irq_preinstall(dev);
132
133                                 /* Install handler */
134         if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
135                 sh_flags = SA_SHIRQ;
136         
137         ret = request_irq( dev->irq, dev->fn_tbl.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;
144         }
145
146                                 /* After installing handler */
147         dev->fn_tbl.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  */
159 int DRM(irq_uninstall)( drm_device_t *dev )
160 {
161         int irq_enabled;
162
163         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
164                 return -EINVAL;
165
166         down( &dev->struct_sem );
167         irq_enabled = dev->irq_enabled;
168         dev->irq_enabled = 0;
169         up( &dev->struct_sem );
170
171         if ( !irq_enabled )
172                 return -EINVAL;
173
174         DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
175
176         dev->fn_tbl.irq_uninstall(dev);
177
178         free_irq( dev->irq, dev );
179
180         return 0;
181 }
182
183 /**
184  * IRQ control ioctl.
185  *
186  * \param inode device inode.
187  * \param filp file pointer.
188  * \param cmd command.
189  * \param arg user argument, pointing to a drm_control structure.
190  * \return zero on success or a negative number on failure.
191  *
192  * Calls irq_install() or irq_uninstall() according to \p arg.
193  */
194 int DRM(control)( struct inode *inode, struct file *filp,
195                   unsigned int cmd, unsigned long arg )
196 {
197         drm_file_t *priv = filp->private_data;
198         drm_device_t *dev = priv->dev;
199         drm_control_t ctl;
200         
201         /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
202
203         if ( copy_from_user( &ctl, (drm_control_t __user *)arg, sizeof(ctl) ) )
204                 return -EFAULT;
205
206         switch ( ctl.func ) {
207         case DRM_INST_HANDLER:
208                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
209                         return 0;
210                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
211                     ctl.irq != dev->irq)
212                         return -EINVAL;
213                 return DRM(irq_install)( dev );
214         case DRM_UNINST_HANDLER:
215                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
216                         return 0;
217                 return DRM(irq_uninstall)( dev );
218         default:
219                 return -EINVAL;
220         }
221 }
222
223 /**
224  * Wait for VBLANK.
225  *
226  * \param inode device inode.
227  * \param filp file pointer.
228  * \param cmd command.
229  * \param data user argument, pointing to a drm_wait_vblank structure.
230  * \return zero on success or a negative number on failure.
231  *
232  * Verifies the IRQ is installed. 
233  *
234  * If a signal is requested checks if this task has already scheduled the same signal
235  * for the same vblank sequence number - nothing to be done in
236  * that case. If the number of tasks waiting for the interrupt exceeds 100 the
237  * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
238  * task.
239  *
240  * If a signal is not requested, then calls vblank_wait().
241  */
242 int DRM(wait_vblank)( DRM_IOCTL_ARGS )
243 {
244         drm_file_t *priv = filp->private_data;
245         drm_device_t *dev = priv->dev;
246         drm_wait_vblank_t __user *argp = (void __user *)data;
247         drm_wait_vblank_t vblwait;
248         struct timeval now;
249         int ret = 0;
250         unsigned int flags;
251
252         if (!drm_core_check_feature(dev, DRIVER_IRQ_VBL))
253                 return -EINVAL;
254
255         if (!dev->irq)
256                 return -EINVAL;
257
258         DRM_COPY_FROM_USER_IOCTL( vblwait, argp, sizeof(vblwait) );
259
260         switch ( vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK ) {
261         case _DRM_VBLANK_RELATIVE:
262                 vblwait.request.sequence += atomic_read( &dev->vbl_received );
263                 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
264         case _DRM_VBLANK_ABSOLUTE:
265                 break;
266         default:
267                 return -EINVAL;
268         }
269
270         flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
271         
272         if ( flags & _DRM_VBLANK_SIGNAL ) {
273                 unsigned long irqflags;
274                 drm_vbl_sig_t *vbl_sig;
275                 
276                 vblwait.reply.sequence = atomic_read( &dev->vbl_received );
277
278                 spin_lock_irqsave( &dev->vbl_lock, irqflags );
279
280                 /* Check if this task has already scheduled the same signal
281                  * for the same vblank sequence number; nothing to be done in
282                  * that case
283                  */
284                 list_for_each_entry( vbl_sig, &dev->vbl_sigs.head, head ) {
285                         if (vbl_sig->sequence == vblwait.request.sequence
286                             && vbl_sig->info.si_signo == vblwait.request.signal
287                             && vbl_sig->task == current)
288                         {
289                                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
290                                 goto done;
291                         }
292                 }
293
294                 if ( dev->vbl_pending >= 100 ) {
295                         spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
296                         return -EBUSY;
297                 }
298
299                 dev->vbl_pending++;
300
301                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
302
303                 if ( !( vbl_sig = DRM_MALLOC( sizeof( drm_vbl_sig_t ) ) ) ) {
304                         return -ENOMEM;
305                 }
306
307                 memset( (void *)vbl_sig, 0, sizeof(*vbl_sig) );
308
309                 vbl_sig->sequence = vblwait.request.sequence;
310                 vbl_sig->info.si_signo = vblwait.request.signal;
311                 vbl_sig->task = current;
312
313                 spin_lock_irqsave( &dev->vbl_lock, irqflags );
314
315                 list_add_tail( (struct list_head *) vbl_sig, &dev->vbl_sigs.head );
316
317                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
318         } else {
319                 if (dev->fn_tbl.vblank_wait)
320                         ret = dev->fn_tbl.vblank_wait( dev, &vblwait.request.sequence );
321
322                 do_gettimeofday( &now );
323                 vblwait.reply.tval_sec = now.tv_sec;
324                 vblwait.reply.tval_usec = now.tv_usec;
325         }
326
327 done:
328         DRM_COPY_TO_USER_IOCTL( argp, vblwait, sizeof(vblwait) );
329
330         return ret;
331 }
332
333 /**
334  * Send the VBLANK signals.
335  *
336  * \param dev DRM device.
337  *
338  * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
339  *
340  * If a signal is not requested, then calls vblank_wait().
341  */
342 void DRM(vbl_send_signals)( drm_device_t *dev )
343 {
344         struct list_head *list, *tmp;
345         drm_vbl_sig_t *vbl_sig;
346         unsigned int vbl_seq = atomic_read( &dev->vbl_received );
347         unsigned long flags;
348
349         spin_lock_irqsave( &dev->vbl_lock, flags );
350
351         list_for_each_safe( list, tmp, &dev->vbl_sigs.head ) {
352                 vbl_sig = list_entry( list, drm_vbl_sig_t, head );
353                 if ( ( vbl_seq - vbl_sig->sequence ) <= (1<<23) ) {
354                         vbl_sig->info.si_code = vbl_seq;
355                         send_sig_info( vbl_sig->info.si_signo, &vbl_sig->info, vbl_sig->task );
356
357                         list_del( list );
358
359                         DRM_FREE( vbl_sig, sizeof(*vbl_sig) );
360
361                         dev->vbl_pending--;
362                 }
363         }
364
365         spin_unlock_irqrestore( &dev->vbl_lock, flags );
366 }
367
368