5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
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
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.
38 #include <linux/interrupt.h> /* For task queue support */
40 #ifndef __HAVE_SHARED_IRQ
41 #define __HAVE_SHARED_IRQ 0
45 #define DRM_IRQ_TYPE SA_SHIRQ
47 #define DRM_IRQ_TYPE 0
51 * Get interrupt from bus id.
53 * \param inode device inode.
54 * \param filp file pointer.
56 * \param arg user argument, pointing to a drm_irq_busid structure.
57 * \return zero on success or a negative number on failure.
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.
63 int DRM(irq_by_busid)(struct inode *inode, struct file *filp,
64 unsigned int cmd, unsigned long arg)
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;
71 if (copy_from_user(&p, argp, sizeof(p)))
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)
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)))
92 * Install IRQ handler.
94 * \param dev DRM device.
95 * \param irq IRQ number.
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.
101 int DRM(irq_install)( drm_device_t *dev )
108 down( &dev->struct_sem );
110 /* Driver must have been initialized */
111 if ( !dev->dev_private ) {
112 up( &dev->struct_sem );
116 if ( dev->irq_enabled ) {
117 up( &dev->struct_sem );
120 dev->irq_enabled = 1;
121 up( &dev->struct_sem );
123 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
126 dev->dma->next_buffer = NULL;
127 dev->dma->next_queue = NULL;
128 dev->dma->this_buffer = NULL;
132 INIT_WORK(&dev->work, DRM(irq_immediate_bh), dev);
135 #ifdef __HAVE_VBL_IRQ
136 init_waitqueue_head(&dev->vbl_queue);
138 spin_lock_init( &dev->vbl_lock );
140 INIT_LIST_HEAD( &dev->vbl_sigs.head );
142 dev->vbl_pending = 0;
145 /* Before installing handler */
146 DRM(driver_irq_preinstall)(dev);
148 /* Install handler */
149 ret = request_irq( dev->irq, DRM(irq_handler),
150 DRM_IRQ_TYPE, dev->devname, dev );
152 down( &dev->struct_sem );
153 dev->irq_enabled = 0;
154 up( &dev->struct_sem );
158 /* After installing handler */
159 DRM(driver_irq_postinstall)(dev);
165 * Uninstall the IRQ handler.
167 * \param dev DRM device.
169 * Calls the driver's \c DRM(driver_irq_uninstall)() function, and stops the irq.
171 int DRM(irq_uninstall)( drm_device_t *dev )
175 down( &dev->struct_sem );
176 irq_enabled = dev->irq_enabled;
177 dev->irq_enabled = 0;
178 up( &dev->struct_sem );
183 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
185 DRM(driver_irq_uninstall)( dev );
187 free_irq( dev->irq, dev );
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.
201 * Calls irq_install() or irq_uninstall() according to \p arg.
203 int DRM(control)( struct inode *inode, struct file *filp,
204 unsigned int cmd, unsigned long arg )
206 drm_file_t *priv = filp->private_data;
207 drm_device_t *dev = priv->dev;
210 if ( copy_from_user( &ctl, (drm_control_t __user *)arg, sizeof(ctl) ) )
213 switch ( ctl.func ) {
214 case DRM_INST_HANDLER:
215 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
218 return DRM(irq_install)( dev );
219 case DRM_UNINST_HANDLER:
220 return DRM(irq_uninstall)( dev );
226 #ifdef __HAVE_VBL_IRQ
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.
237 * Verifies the IRQ is installed.
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
245 * If a signal is not requested, then calls vblank_wait().
247 int DRM(wait_vblank)( DRM_IOCTL_ARGS )
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;
260 DRM_COPY_FROM_USER_IOCTL( vblwait, argp, sizeof(vblwait) );
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:
272 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
274 if ( flags & _DRM_VBLANK_SIGNAL ) {
275 unsigned long irqflags;
276 drm_vbl_sig_t *vbl_sig;
278 vblwait.reply.sequence = atomic_read( &dev->vbl_received );
280 spin_lock_irqsave( &dev->vbl_lock, irqflags );
282 /* Check if this task has already scheduled the same signal
283 * for the same vblank sequence number; nothing to be done in
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)
291 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
296 if ( dev->vbl_pending >= 100 ) {
297 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
303 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
305 if ( !( vbl_sig = DRM_MALLOC( sizeof( drm_vbl_sig_t ) ) ) ) {
309 memset( (void *)vbl_sig, 0, sizeof(*vbl_sig) );
311 vbl_sig->sequence = vblwait.request.sequence;
312 vbl_sig->info.si_signo = vblwait.request.signal;
313 vbl_sig->task = current;
315 spin_lock_irqsave( &dev->vbl_lock, irqflags );
317 list_add_tail( (struct list_head *) vbl_sig, &dev->vbl_sigs.head );
319 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
321 ret = DRM(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;
329 DRM_COPY_TO_USER_IOCTL( argp, vblwait, sizeof(vblwait) );
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 );
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 );
360 DRM_FREE( vbl_sig, sizeof(*vbl_sig) );
366 spin_unlock_irqrestore( &dev->vbl_lock, flags );
369 #endif /* __HAVE_VBL_IRQ */
371 #endif /* __HAVE_IRQ */