This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 p;
69
70         if (copy_from_user(&p, (drm_irq_busid_t *)arg, sizeof(p)))
71                 return -EFAULT;
72
73         if ((p.busnum >> 8) != dev->pci_domain ||
74             (p.busnum & 0xff) != dev->pci_bus ||
75             p.devnum != dev->pci_slot ||
76             p.funcnum != dev->pci_func)
77                 return -EINVAL;
78
79         p.irq = dev->irq;
80
81         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
82                   p.busnum, p.devnum, p.funcnum, p.irq);
83         if (copy_to_user((drm_irq_busid_t *)arg, &p, sizeof(p)))
84                 return -EFAULT;
85         return 0;
86 }
87
88 #if __HAVE_IRQ
89
90 /**
91  * Install IRQ handler.
92  *
93  * \param dev DRM device.
94  * \param irq IRQ number.
95  *
96  * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
97  * \c DRM(driver_irq_preinstall)() and \c DRM(driver_irq_postinstall)() functions
98  * before and after the installation.
99  */
100 int DRM(irq_install)( drm_device_t *dev )
101 {
102         int ret;
103  
104         if ( dev->irq == 0 )
105                 return -EINVAL;
106
107         down( &dev->struct_sem );
108
109         /* Driver must have been initialized */
110         if ( !dev->dev_private ) {
111                 up( &dev->struct_sem );
112                 return -EINVAL;
113         }
114
115         if ( dev->irq_enabled ) {
116                 up( &dev->struct_sem );
117                 return -EBUSY;
118         }
119         dev->irq_enabled = 1;
120         up( &dev->struct_sem );
121
122         DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
123
124 #if __HAVE_DMA
125         dev->dma->next_buffer = NULL;
126         dev->dma->next_queue = NULL;
127         dev->dma->this_buffer = NULL;
128 #endif
129
130 #if __HAVE_IRQ_BH
131         INIT_WORK(&dev->work, DRM(irq_immediate_bh), dev);
132 #endif
133
134 #if __HAVE_VBL_IRQ
135         init_waitqueue_head(&dev->vbl_queue);
136
137         spin_lock_init( &dev->vbl_lock );
138
139         INIT_LIST_HEAD( &dev->vbl_sigs.head );
140
141         dev->vbl_pending = 0;
142 #endif
143
144                                 /* Before installing handler */
145         DRM(driver_irq_preinstall)(dev);
146
147                                 /* Install handler */
148         ret = request_irq( dev->irq, DRM(irq_handler),
149                            DRM_IRQ_TYPE, dev->devname, dev );
150         if ( ret < 0 ) {
151                 down( &dev->struct_sem );
152                 dev->irq_enabled = 0;
153                 up( &dev->struct_sem );
154                 return ret;
155         }
156
157                                 /* After installing handler */
158         DRM(driver_irq_postinstall)(dev);
159
160         return 0;
161 }
162
163 /**
164  * Uninstall the IRQ handler.
165  *
166  * \param dev DRM device.
167  *
168  * Calls the driver's \c DRM(driver_irq_uninstall)() function, and stops the irq.
169  */
170 int DRM(irq_uninstall)( drm_device_t *dev )
171 {
172         int irq_enabled;
173
174         down( &dev->struct_sem );
175         irq_enabled = dev->irq_enabled;
176         dev->irq_enabled = 0;
177         up( &dev->struct_sem );
178
179         if ( !irq_enabled )
180                 return -EINVAL;
181
182         DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
183
184         DRM(driver_irq_uninstall)( dev );
185
186         free_irq( dev->irq, dev );
187
188         return 0;
189 }
190
191 /**
192  * IRQ control ioctl.
193  *
194  * \param inode device inode.
195  * \param filp file pointer.
196  * \param cmd command.
197  * \param arg user argument, pointing to a drm_control structure.
198  * \return zero on success or a negative number on failure.
199  *
200  * Calls irq_install() or irq_uninstall() according to \p arg.
201  */
202 int DRM(control)( struct inode *inode, struct file *filp,
203                   unsigned int cmd, unsigned long arg )
204 {
205         drm_file_t *priv = filp->private_data;
206         drm_device_t *dev = priv->dev;
207         drm_control_t ctl;
208
209         if ( copy_from_user( &ctl, (drm_control_t *)arg, sizeof(ctl) ) )
210                 return -EFAULT;
211
212         switch ( ctl.func ) {
213         case DRM_INST_HANDLER:
214                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
215                     ctl.irq != dev->irq)
216                         return -EINVAL;
217                 return DRM(irq_install)( dev );
218         case DRM_UNINST_HANDLER:
219                 return DRM(irq_uninstall)( dev );
220         default:
221                 return -EINVAL;
222         }
223 }
224
225 #if __HAVE_VBL_IRQ
226
227 /**
228  * Wait for VBLANK.
229  *
230  * \param inode device inode.
231  * \param filp file pointer.
232  * \param cmd command.
233  * \param data user argument, pointing to a drm_wait_vblank structure.
234  * \return zero on success or a negative number on failure.
235  *
236  * Verifies the IRQ is installed. 
237  *
238  * If a signal is requested checks if this task has already scheduled the same signal
239  * for the same vblank sequence number - nothing to be done in
240  * that case. If the number of tasks waiting for the interrupt exceeds 100 the
241  * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
242  * task.
243  *
244  * If a signal is not requested, then calls vblank_wait().
245  */
246 int DRM(wait_vblank)( DRM_IOCTL_ARGS )
247 {
248         drm_file_t *priv = filp->private_data;
249         drm_device_t *dev = priv->dev;
250         drm_wait_vblank_t vblwait;
251         struct timeval now;
252         int ret = 0;
253         unsigned int flags;
254
255         if (!dev->irq)
256                 return -EINVAL;
257
258         DRM_COPY_FROM_USER_IOCTL( vblwait, (drm_wait_vblank_t *)data,
259                                   sizeof(vblwait) );
260
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;
269         }
270
271         flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
272         
273         if ( flags & _DRM_VBLANK_SIGNAL ) {
274                 unsigned long irqflags;
275                 drm_vbl_sig_t *vbl_sig;
276                 
277                 vblwait.reply.sequence = atomic_read( &dev->vbl_received );
278
279                 spin_lock_irqsave( &dev->vbl_lock, irqflags );
280
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
284                  */
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)
289                         {
290                                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
291                                 goto done;
292                         }
293                 }
294
295                 if ( dev->vbl_pending >= 100 ) {
296                         spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
297                         return -EBUSY;
298                 }
299
300                 dev->vbl_pending++;
301
302                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
303
304                 if ( !( vbl_sig = DRM_MALLOC( sizeof( drm_vbl_sig_t ) ) ) ) {
305                         return -ENOMEM;
306                 }
307
308                 memset( (void *)vbl_sig, 0, sizeof(*vbl_sig) );
309
310                 vbl_sig->sequence = vblwait.request.sequence;
311                 vbl_sig->info.si_signo = vblwait.request.signal;
312                 vbl_sig->task = current;
313
314                 spin_lock_irqsave( &dev->vbl_lock, irqflags );
315
316                 list_add_tail( (struct list_head *) vbl_sig, &dev->vbl_sigs.head );
317
318                 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
319         } else {
320                 ret = DRM(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( (drm_wait_vblank_t *)data, vblwait,
329                                 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 */