Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6] / drivers / char / drm / i915_irq.c
1 /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2  */
3 /*
4  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sub license, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice (including the
16  * next paragraph) shall be included in all copies or substantial portions
17  * of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  */
28
29 #include "drmP.h"
30 #include "drm.h"
31 #include "i915_drm.h"
32 #include "i915_drv.h"
33
34 #define USER_INT_FLAG (1<<1)
35 #define VSYNC_PIPEB_FLAG (1<<5)
36 #define VSYNC_PIPEA_FLAG (1<<7)
37
38 #define MAX_NOPID ((u32)~0)
39
40 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
41 {
42         drm_device_t *dev = (drm_device_t *) arg;
43         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
44         u16 temp;
45
46         temp = I915_READ16(I915REG_INT_IDENTITY_R);
47
48         temp &= (USER_INT_FLAG | VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG);
49
50         DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
51
52         if (temp == 0)
53                 return IRQ_NONE;
54
55         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
56
57         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
58
59         if (temp & USER_INT_FLAG)
60                 DRM_WAKEUP(&dev_priv->irq_queue);
61
62         if (temp & (VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG)) {
63                 atomic_inc(&dev->vbl_received);
64                 DRM_WAKEUP(&dev->vbl_queue);
65                 drm_vbl_send_signals(dev);
66         }
67
68         return IRQ_HANDLED;
69 }
70
71 static int i915_emit_irq(drm_device_t * dev)
72 {
73         drm_i915_private_t *dev_priv = dev->dev_private;
74         RING_LOCALS;
75
76         i915_kernel_lost_context(dev);
77
78         DRM_DEBUG("%s\n", __FUNCTION__);
79
80         dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
81
82         if (dev_priv->counter > 0x7FFFFFFFUL)
83                 dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
84
85         BEGIN_LP_RING(6);
86         OUT_RING(CMD_STORE_DWORD_IDX);
87         OUT_RING(20);
88         OUT_RING(dev_priv->counter);
89         OUT_RING(0);
90         OUT_RING(0);
91         OUT_RING(GFX_OP_USER_INTERRUPT);
92         ADVANCE_LP_RING();
93         
94         return dev_priv->counter;
95 }
96
97 static int i915_wait_irq(drm_device_t * dev, int irq_nr)
98 {
99         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
100         int ret = 0;
101
102         DRM_DEBUG("%s irq_nr=%d breadcrumb=%d\n", __FUNCTION__, irq_nr,
103                   READ_BREADCRUMB(dev_priv));
104
105         if (READ_BREADCRUMB(dev_priv) >= irq_nr)
106                 return 0;
107
108         dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
109
110         DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
111                     READ_BREADCRUMB(dev_priv) >= irq_nr);
112
113         if (ret == DRM_ERR(EBUSY)) {
114                 DRM_ERROR("%s: EBUSY -- rec: %d emitted: %d\n",
115                           __FUNCTION__,
116                           READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
117         }
118
119         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
120         return ret;
121 }
122
123 int i915_driver_vblank_wait(drm_device_t *dev, unsigned int *sequence)
124 {
125         drm_i915_private_t *dev_priv = dev->dev_private;
126         unsigned int cur_vblank;
127         int ret = 0;
128
129         if (!dev_priv) {
130                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
131                 return DRM_ERR(EINVAL);
132         }
133
134         DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
135                     (((cur_vblank = atomic_read(&dev->vbl_received))
136                         - *sequence) <= (1<<23)));
137         
138         *sequence = cur_vblank;
139
140         return ret;
141 }
142
143
144 /* Needs the lock as it touches the ring.
145  */
146 int i915_irq_emit(DRM_IOCTL_ARGS)
147 {
148         DRM_DEVICE;
149         drm_i915_private_t *dev_priv = dev->dev_private;
150         drm_i915_irq_emit_t emit;
151         int result;
152
153         LOCK_TEST_WITH_RETURN(dev, filp);
154
155         if (!dev_priv) {
156                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
157                 return DRM_ERR(EINVAL);
158         }
159
160         DRM_COPY_FROM_USER_IOCTL(emit, (drm_i915_irq_emit_t __user *) data,
161                                  sizeof(emit));
162
163         result = i915_emit_irq(dev);
164
165         if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
166                 DRM_ERROR("copy_to_user\n");
167                 return DRM_ERR(EFAULT);
168         }
169
170         return 0;
171 }
172
173 /* Doesn't need the hardware lock.
174  */
175 int i915_irq_wait(DRM_IOCTL_ARGS)
176 {
177         DRM_DEVICE;
178         drm_i915_private_t *dev_priv = dev->dev_private;
179         drm_i915_irq_wait_t irqwait;
180
181         if (!dev_priv) {
182                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
183                 return DRM_ERR(EINVAL);
184         }
185
186         DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_i915_irq_wait_t __user *) data,
187                                  sizeof(irqwait));
188
189         return i915_wait_irq(dev, irqwait.irq_seq);
190 }
191
192 static int i915_enable_interrupt (drm_device_t *dev)
193 {
194         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
195         u16 flag;
196
197         flag = 0;
198         if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
199                 flag |= VSYNC_PIPEA_FLAG;
200         if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
201                 flag |= VSYNC_PIPEB_FLAG;
202         if (dev_priv->vblank_pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
203                 DRM_ERROR("%s called with invalid pipe 0x%x\n",
204                           __FUNCTION__, dev_priv->vblank_pipe);
205                 return DRM_ERR(EINVAL);
206         }
207         I915_WRITE16(I915REG_INT_ENABLE_R, USER_INT_FLAG | flag);
208         return 0;
209 }
210
211 /* Set the vblank monitor pipe
212  */
213 int i915_vblank_pipe_set(DRM_IOCTL_ARGS)
214 {
215         DRM_DEVICE;
216         drm_i915_private_t *dev_priv = dev->dev_private;
217         drm_i915_vblank_pipe_t pipe;
218
219         if (!dev_priv) {
220                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
221                 return DRM_ERR(EINVAL);
222         }
223
224         DRM_COPY_FROM_USER_IOCTL(pipe, (drm_i915_vblank_pipe_t __user *) data,
225                                  sizeof(pipe));
226
227         dev_priv->vblank_pipe = pipe.pipe;
228         return i915_enable_interrupt (dev);
229 }
230
231 int i915_vblank_pipe_get(DRM_IOCTL_ARGS)
232 {
233         DRM_DEVICE;
234         drm_i915_private_t *dev_priv = dev->dev_private;
235         drm_i915_vblank_pipe_t pipe;
236         u16 flag;
237
238         if (!dev_priv) {
239                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
240                 return DRM_ERR(EINVAL);
241         }
242
243         flag = I915_READ(I915REG_INT_ENABLE_R);
244         pipe.pipe = 0;
245         if (flag & VSYNC_PIPEA_FLAG)
246                 pipe.pipe |= DRM_I915_VBLANK_PIPE_A;
247         if (flag & VSYNC_PIPEB_FLAG)
248                 pipe.pipe |= DRM_I915_VBLANK_PIPE_B;
249         DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_pipe_t __user *) data, pipe,
250                                  sizeof(pipe));
251         return 0;
252 }
253
254 /* drm_dma.h hooks
255 */
256 void i915_driver_irq_preinstall(drm_device_t * dev)
257 {
258         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
259
260         I915_WRITE16(I915REG_HWSTAM, 0xfffe);
261         I915_WRITE16(I915REG_INT_MASK_R, 0x0);
262         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
263 }
264
265 void i915_driver_irq_postinstall(drm_device_t * dev)
266 {
267         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
268
269         i915_enable_interrupt(dev);
270         DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
271 }
272
273 void i915_driver_irq_uninstall(drm_device_t * dev)
274 {
275         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
276         u16 temp;
277
278         if (!dev_priv)
279                 return;
280
281         I915_WRITE16(I915REG_HWSTAM, 0xffff);
282         I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
283         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
284
285         temp = I915_READ16(I915REG_INT_IDENTITY_R);
286         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
287 }