Merge master.kernel.org:/pub/scm/linux/kernel/git/chrisw/lsm-2.6
[linux-2.6] / drivers / char / drm / i830_irq.c
1 /* i830_dma.c -- DMA support for the I830 -*- linux-c -*-
2  *
3  * Copyright 2002 Tungsten Graphics, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  * 
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  * 
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors: Keith Whitwell <keith@tungstengraphics.com>
26  *
27  */
28
29 #include "drmP.h"
30 #include "drm.h"
31 #include "i830_drm.h"
32 #include "i830_drv.h"
33 #include <linux/interrupt.h>    /* For task queue support */
34 #include <linux/delay.h>
35
36
37 irqreturn_t i830_driver_irq_handler( DRM_IRQ_ARGS )
38 {
39         drm_device_t     *dev = (drm_device_t *)arg;
40         drm_i830_private_t *dev_priv = (drm_i830_private_t *)dev->dev_private;
41         u16 temp;
42
43         temp = I830_READ16(I830REG_INT_IDENTITY_R);
44         DRM_DEBUG("%x\n", temp);
45
46         if ( !( temp & 2 ) ) 
47                 return IRQ_NONE;
48
49         I830_WRITE16(I830REG_INT_IDENTITY_R, temp); 
50
51         atomic_inc(&dev_priv->irq_received);
52         wake_up_interruptible(&dev_priv->irq_queue); 
53
54         return IRQ_HANDLED;
55 }
56
57 static int i830_emit_irq(drm_device_t *dev)
58 {
59         drm_i830_private_t *dev_priv = dev->dev_private;
60         RING_LOCALS;
61
62         DRM_DEBUG("%s\n", __FUNCTION__);
63
64         atomic_inc(&dev_priv->irq_emitted);
65
66         BEGIN_LP_RING(2);
67         OUT_RING( 0 );
68         OUT_RING( GFX_OP_USER_INTERRUPT );
69         ADVANCE_LP_RING();
70
71         return atomic_read(&dev_priv->irq_emitted);
72 }
73
74
75 static int i830_wait_irq(drm_device_t *dev, int irq_nr)
76 {
77         drm_i830_private_t *dev_priv = 
78            (drm_i830_private_t *)dev->dev_private;
79         DECLARE_WAITQUEUE(entry, current);
80         unsigned long end = jiffies + HZ*3;
81         int ret = 0;
82
83         DRM_DEBUG("%s\n", __FUNCTION__);
84
85         if (atomic_read(&dev_priv->irq_received) >= irq_nr)  
86                 return 0; 
87
88         dev_priv->sarea_priv->perf_boxes |= I830_BOX_WAIT;
89
90         add_wait_queue(&dev_priv->irq_queue, &entry);
91
92         for (;;) {
93                 __set_current_state(TASK_INTERRUPTIBLE);
94                 if (atomic_read(&dev_priv->irq_received) >= irq_nr) 
95                    break;
96                 if((signed)(end - jiffies) <= 0) {
97                         DRM_ERROR("timeout iir %x imr %x ier %x hwstam %x\n",
98                                   I830_READ16( I830REG_INT_IDENTITY_R ),
99                                   I830_READ16( I830REG_INT_MASK_R ),
100                                   I830_READ16( I830REG_INT_ENABLE_R ),
101                                   I830_READ16( I830REG_HWSTAM ));
102
103                         ret = -EBUSY;   /* Lockup?  Missed irq? */
104                         break;
105                 }
106                 schedule_timeout(HZ*3);
107                 if (signal_pending(current)) {
108                         ret = -EINTR;
109                         break;
110                 }
111         }
112
113         __set_current_state(TASK_RUNNING);
114         remove_wait_queue(&dev_priv->irq_queue, &entry);
115         return ret;
116 }
117
118
119 /* Needs the lock as it touches the ring.
120  */
121 int i830_irq_emit( struct inode *inode, struct file *filp, unsigned int cmd,
122                    unsigned long arg )
123 {
124         drm_file_t        *priv     = filp->private_data;
125         drm_device_t      *dev      = priv->head->dev;
126         drm_i830_private_t *dev_priv = dev->dev_private;
127         drm_i830_irq_emit_t emit;
128         int result;
129
130         LOCK_TEST_WITH_RETURN(dev, filp);
131
132         if ( !dev_priv ) {
133                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
134                 return -EINVAL;
135         }
136
137         if (copy_from_user( &emit, (drm_i830_irq_emit_t __user *)arg, sizeof(emit) ))
138                 return -EFAULT;
139
140         result = i830_emit_irq( dev );
141
142         if ( copy_to_user( emit.irq_seq, &result, sizeof(int) ) ) {
143                 DRM_ERROR( "copy_to_user\n" );
144                 return -EFAULT;
145         }
146
147         return 0;
148 }
149
150
151 /* Doesn't need the hardware lock.
152  */
153 int i830_irq_wait( struct inode *inode, struct file *filp, unsigned int cmd,
154                    unsigned long arg )
155 {
156         drm_file_t        *priv     = filp->private_data;
157         drm_device_t      *dev      = priv->head->dev;
158         drm_i830_private_t *dev_priv = dev->dev_private;
159         drm_i830_irq_wait_t irqwait;
160
161         if ( !dev_priv ) {
162                 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
163                 return -EINVAL;
164         }
165
166         if (copy_from_user( &irqwait, (drm_i830_irq_wait_t __user *)arg, 
167                             sizeof(irqwait) ))
168                 return -EFAULT;
169
170         return i830_wait_irq( dev, irqwait.irq_seq );
171 }
172
173
174 /* drm_dma.h hooks
175 */
176 void i830_driver_irq_preinstall( drm_device_t *dev ) {
177         drm_i830_private_t *dev_priv =
178                 (drm_i830_private_t *)dev->dev_private;
179
180         I830_WRITE16( I830REG_HWSTAM, 0xffff );
181         I830_WRITE16( I830REG_INT_MASK_R, 0x0 );
182         I830_WRITE16( I830REG_INT_ENABLE_R, 0x0 );
183         atomic_set(&dev_priv->irq_received, 0);
184         atomic_set(&dev_priv->irq_emitted, 0);
185         init_waitqueue_head(&dev_priv->irq_queue);
186 }
187
188 void i830_driver_irq_postinstall( drm_device_t *dev ) {
189         drm_i830_private_t *dev_priv =
190                 (drm_i830_private_t *)dev->dev_private;
191
192         I830_WRITE16( I830REG_INT_ENABLE_R, 0x2 );
193 }
194
195 void i830_driver_irq_uninstall( drm_device_t *dev ) {
196         drm_i830_private_t *dev_priv =
197                 (drm_i830_private_t *)dev->dev_private;
198         if (!dev_priv)
199                 return;
200
201         I830_WRITE16( I830REG_INT_MASK_R, 0xffff );
202         I830_WRITE16( I830REG_INT_ENABLE_R, 0x0 );
203 }