VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 #ifndef __HAVE_SHARED_IRQ
41 #define __HAVE_SHARED_IRQ       0
42 #endif
43
44 #if __HAVE_SHARED_IRQ
45 #define DRM_IRQ_TYPE            SA_SHIRQ
46 #else
47 #define DRM_IRQ_TYPE            0
48 #endif
49
50 /**
51  * Get interrupt from bus id.
52  * 
53  * \param inode device inode.
54  * \param filp file pointer.
55  * \param cmd command.
56  * \param arg user argument, pointing to a drm_irq_busid structure.
57  * \return zero on success or a negative number on failure.
58  * 
59  * Finds the PCI device with the specified bus id and gets its IRQ number.
60  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
61  * to that of the device that this DRM instance attached to.
62  */
63 int DRM(irq_by_busid)(struct inode *inode, struct file *filp,
64                    unsigned int cmd, unsigned long arg)
65 {
66         drm_file_t *priv = filp->private_data;
67         drm_device_t *dev = priv->dev;
68         drm_irq_busid_t __user *argp = (void __user *)arg;
69         drm_irq_busid_t p;
70
71         if (copy_from_user(&p, argp, sizeof(p)))
72                 return -EFAULT;
73
74         if ((p.busnum >> 8) != dev->pci_domain ||
75             (p.busnum & 0xff) != dev->pci_bus ||
76             p.devnum != dev->pci_slot ||
77             p.funcnum != dev->pci_func)
78                 return -EINVAL;
79
80         p.irq = dev->irq;
81
82         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
83                   p.busnum, p.devnum, p.funcnum, p.irq);
84         if (copy_to_user(argp, &p, sizeof(p)))
85                 return -EFAULT;
86         return 0;
87 }
88
89 #if __HAVE_IRQ
90
91 /**
92  * Install IRQ handler.
93  *
94  * \param dev DRM device.
95  * \param irq IRQ number.
96  *
97  * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
98  * \c DRM(driver_irq_preinstall)() and \c DRM(driver_irq_postinstall)() functions
99  * before and after the installation.
100  */
101 int DRM(irq_install)( drm_device_t *dev )
102 {
103         int ret;
104  
105         if ( dev->irq == 0 )
106                 return -EINVAL;
107
108         down( &dev->struct_sem );
109
110         /* Driver must have been initialized */
111         if ( !dev->dev_private ) {
112                 up( &dev->struct_sem );
113                 return -EINVAL;
114         }
115
116         if ( dev->irq_enabled ) {
117                 up( &dev->struct_sem );
118                 return -EBUSY;
119         }
120         dev->irq_enabled = 1;
121         up( &dev->struct_sem );
122
123         DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
124
125 #if __HAVE_DMA
126         dev->dma->next_buffer = NULL;
127         dev->dma->next_queue = NULL;
128         dev->dma->this_buffer = NULL;
129 #endif
130
131 #ifdef __HAVE_IRQ_BH
132         INIT_WORK(&dev->work, DRM(irq_immediate_bh), dev);
133 #endif
134
135 #ifdef __HAVE_VBL_IRQ
136         init_waitqueue_head(&dev->vbl_queue);
137
138         spin_lock_init( &dev->vbl_lock );
139
140         INIT_LIST_HEAD( &dev->vbl_sigs.head );
141
142         dev->vbl_pending = 0;
143 #endif
144
145                                 /* Before installing handler */
146         DRM(driver_irq_preinstall)(dev);
147
148                                 /* Install handler */
149         ret = request_irq( dev->irq, DRM(irq_handler),
150                            DRM_IRQ_TYPE, dev->devname, dev );
151         if ( ret < 0 ) {
152                 down( &dev->struct_sem );
153                 dev->irq_enabled = 0;
154                 up( &dev->struct_sem );
155                 return ret;
156         }
157
158                                 /* After installing handler */
159         DRM(driver_irq_postinstall)(dev);
160
161         return 0;
162 }
163
164 /**
165  * Uninstall the IRQ handler.
166  *
167  * \param dev DRM device.
168  *
169  * Calls the driver's \c DRM(driver_irq_uninstall)() function, and stops the irq.
170  */
171 int DRM(irq_uninstall)( drm_device_t *dev )
172 {
173         int irq_enabled;
174
175         down( &dev->struct_sem );
176         irq_enabled = dev->irq_enabled;
177         dev->irq_enabled = 0;
178         up( &dev->struct_sem );
179
180         if ( !irq_enabled )
181                 return -EINVAL;
182
183         DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
184
185         DRM(driver_irq_uninstall)( dev );
186
187         free_irq( dev->irq, dev );
188
189         return 0;
190 }
191
192 /**
193  * IRQ control ioctl.
194  *
195  * \param inode device inode.
196  * \param filp file pointer.
197  * \param cmd command.
198  * \param arg user argument, pointing to a drm_control structure.
199  * \return zero on success or a negative number on failure.
200  *
201  * Calls irq_install() or irq_uninstall() according to \p arg.
202  */
203 int DRM(control)( struct inode *inode, struct file *filp,
204                   unsigned int cmd, unsigned long arg )
205 {
206         drm_file_t *priv = filp->private_data;
207         drm_device_t *dev = priv->dev;
208         drm_control_t ctl;
209
210         if ( copy_from_user( &ctl, (drm_control_t __user *)arg, sizeof(ctl) ) )
211                 return -EFAULT;
212
213         switch ( ctl.func ) {
214         case DRM_INST_HANDLER:
215                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
216                     ctl.irq != dev->irq)
217                         return -EINVAL;
218                 return DRM(irq_install)( dev );
219         case DRM_UNINST_HANDLER:
220                 return DRM(irq_uninstall)( dev );
221         default:
222                 return -EINVAL;
223         }
224 }
225
226 #ifdef __HAVE_VBL_IRQ
227
228 /**
229  * Wait for VBLANK.
230  *
231  * \param inode device inode.
232  * \param filp file pointer.
233  * \param cmd command.
234  * \param data user argument, pointing to a drm_wait_vblank structure.
235  * \return zero on success or a negative number on failure.
236  *
237  * Verifies the IRQ is installed. 
238  *
239  * If a signal is requested checks if this task has already scheduled the same signal
240  * for the same vblank sequence number - nothing to be done in
241  * that case. If the number of tasks waiting for the interrupt exceeds 100 the
242  * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
243  * task.
244  *
245  * If a signal is not requested, then calls vblank_wait().
246  */
247 int DRM(wait_vblank)( DRM_IOCTL_ARGS )
248 {
249         drm_file_t *priv = filp->private_data;
250         drm_device_t *dev = priv->dev;
251         drm_wait_vblank_t __user *argp = (void __user *)data;
252         drm_wait_vblank_t vblwait;
253         struct timeval now;
254         int ret = 0;
255         unsigned int flags;
256
257         if (!dev->irq)
258                 return -EINVAL;
259
260         DRM_COPY_FROM_USER_IOCTL( vblwait, argp, sizeof(vblwait) );
261
262         switch ( vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK ) {
263         case _DRM_VBLANK_RELATIVE:
264                 vblwait.request.sequence += atomic_read( &dev->vbl_received );
265                 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
266         case _DRM_VBLANK_ABSOLUTE:
267                 break;
268         default:
269                 return -EINVAL;
270         }
271
272         flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
273         
274         if ( flags & _DRM_VBLANK_SIGNAL ) {
275                 unsigned long irqflags;
276                 drm_vbl_sig_t *vbl_sig;
277                 
278                 vblwait.reply.sequence = atomic_read( &dev->vbl_received );
279
280                 spin_lock_irqsave( &dev->vbl_lock, irqflags );
281
282                 /* Check if this task has already scheduled the same signal
283                  * for the same vblank sequence number; nothing to be done in
284                  * that case
285                  */
286                 list_for_each_entry( vbl_sig, &dev->vbl_sigs.head, head ) {
287                         if (vbl_sig->sequence == vblwait.request.sequence
288                             && vbl_sig->info.si_signo == vblwait.request.signal
289                             && vbl_sig->task == current)
290                         {
291                                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
292                                 goto done;
293                         }
294                 }
295
296                 if ( dev->vbl_pending >= 100 ) {
297                         spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
298                         return -EBUSY;
299                 }
300
301                 dev->vbl_pending++;
302
303                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
304
305                 if ( !( vbl_sig = DRM_MALLOC( sizeof( drm_vbl_sig_t ) ) ) ) {
306                         return -ENOMEM;
307                 }
308
309                 memset( (void *)vbl_sig, 0, sizeof(*vbl_sig) );
310
311                 vbl_sig->sequence = vblwait.request.sequence;
312                 vbl_sig->info.si_signo = vblwait.request.signal;
313                 vbl_sig->task = current;
314
315                 spin_lock_irqsave( &dev->vbl_lock, irqflags );
316
317                 list_add_tail( (struct list_head *) vbl_sig, &dev->vbl_sigs.head );
318
319                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
320         } else {
321                 ret = DRM(vblank_wait)( dev, &vblwait.request.sequence );
322
323                 do_gettimeofday( &now );
324                 vblwait.reply.tval_sec = now.tv_sec;
325                 vblwait.reply.tval_usec = now.tv_usec;
326         }
327
328 done:
329         DRM_COPY_TO_USER_IOCTL( argp, vblwait, sizeof(vblwait) );
330
331         return ret;
332 }
333
334 /**
335  * Send the VBLANK signals.
336  *
337  * \param dev DRM device.
338  *
339  * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
340  *
341  * If a signal is not requested, then calls vblank_wait().
342  */
343 void DRM(vbl_send_signals)( drm_device_t *dev )
344 {
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;
349
350         spin_lock_irqsave( &dev->vbl_lock, flags );
351
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 );
357
358                         list_del( list );
359
360                         DRM_FREE( vbl_sig, sizeof(*vbl_sig) );
361
362                         dev->vbl_pending--;
363                 }
364         }
365
366         spin_unlock_irqrestore( &dev->vbl_lock, flags );
367 }
368
369 #endif  /* __HAVE_VBL_IRQ */
370
371 #endif /* __HAVE_IRQ */