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;
70 if (copy_from_user(&p, (drm_irq_busid_t *)arg, sizeof(p)))
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)
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)))
91 * Install IRQ handler.
93 * \param dev DRM device.
94 * \param irq IRQ number.
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.
100 int DRM(irq_install)( drm_device_t *dev )
107 down( &dev->struct_sem );
109 /* Driver must have been initialized */
110 if ( !dev->dev_private ) {
111 up( &dev->struct_sem );
115 if ( dev->irq_enabled ) {
116 up( &dev->struct_sem );
119 dev->irq_enabled = 1;
120 up( &dev->struct_sem );
122 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
125 dev->dma->next_buffer = NULL;
126 dev->dma->next_queue = NULL;
127 dev->dma->this_buffer = NULL;
131 INIT_WORK(&dev->work, DRM(irq_immediate_bh), dev);
135 init_waitqueue_head(&dev->vbl_queue);
137 spin_lock_init( &dev->vbl_lock );
139 INIT_LIST_HEAD( &dev->vbl_sigs.head );
141 dev->vbl_pending = 0;
144 /* Before installing handler */
145 DRM(driver_irq_preinstall)(dev);
147 /* Install handler */
148 ret = request_irq( dev->irq, DRM(irq_handler),
149 DRM_IRQ_TYPE, dev->devname, dev );
151 down( &dev->struct_sem );
152 dev->irq_enabled = 0;
153 up( &dev->struct_sem );
157 /* After installing handler */
158 DRM(driver_irq_postinstall)(dev);
164 * Uninstall the IRQ handler.
166 * \param dev DRM device.
168 * Calls the driver's \c DRM(driver_irq_uninstall)() function, and stops the irq.
170 int DRM(irq_uninstall)( drm_device_t *dev )
174 down( &dev->struct_sem );
175 irq_enabled = dev->irq_enabled;
176 dev->irq_enabled = 0;
177 up( &dev->struct_sem );
182 DRM_DEBUG( "%s: irq=%d\n", __FUNCTION__, dev->irq );
184 DRM(driver_irq_uninstall)( dev );
186 free_irq( dev->irq, dev );
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.
200 * Calls irq_install() or irq_uninstall() according to \p arg.
202 int DRM(control)( struct inode *inode, struct file *filp,
203 unsigned int cmd, unsigned long arg )
205 drm_file_t *priv = filp->private_data;
206 drm_device_t *dev = priv->dev;
209 if ( copy_from_user( &ctl, (drm_control_t *)arg, sizeof(ctl) ) )
212 switch ( ctl.func ) {
213 case DRM_INST_HANDLER:
214 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
217 return DRM(irq_install)( dev );
218 case DRM_UNINST_HANDLER:
219 return DRM(irq_uninstall)( dev );
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.
236 * Verifies the IRQ is installed.
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
244 * If a signal is not requested, then calls vblank_wait().
246 int DRM(wait_vblank)( DRM_IOCTL_ARGS )
248 drm_file_t *priv = filp->private_data;
249 drm_device_t *dev = priv->dev;
250 drm_wait_vblank_t vblwait;
258 DRM_COPY_FROM_USER_IOCTL( vblwait, (drm_wait_vblank_t *)data,
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:
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
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 );
295 if ( dev->vbl_pending >= 100 ) {
296 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
302 spin_unlock_irqrestore( &dev->vbl_lock, irqflags );
304 if ( !( vbl_sig = DRM_MALLOC( sizeof( drm_vbl_sig_t ) ) ) ) {
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 );
320 ret = DRM(vblank_wait)( dev, &vblwait.request.sequence );
322 do_gettimeofday( &now );
323 vblwait.reply.tval_sec = now.tv_sec;
324 vblwait.reply.tval_usec = now.tv_usec;
328 DRM_COPY_TO_USER_IOCTL( (drm_wait_vblank_t *)data, 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 */