blob: 33d40187696a765dc4a4697fdafeaf7ecd4015de [file] [log] [blame]
Dave Airlie0d6aa602006-01-02 20:14:23 +11001/* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 */
Dave Airlie0d6aa602006-01-02 20:14:23 +11003/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
Dave Airliebc54fd12005-06-23 22:46:46 +10006 *
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 *
Dave Airlie0d6aa602006-01-02 20:14:23 +110027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include "drmP.h"
30#include "drm.h"
31#include "i915_drm.h"
32#include "i915_drv.h"
33
Dave Airlie0d6aa602006-01-02 20:14:23 +110034#define USER_INT_FLAG (1<<1)
35#define VSYNC_PIPEB_FLAG (1<<5)
36#define VSYNC_PIPEA_FLAG (1<<7)
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define MAX_NOPID ((u32)~0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40irqreturn_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);
Dave Airlie702880f2006-06-24 17:07:34 +100047
48 temp &= (USER_INT_FLAG | VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
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);
Dave Airlie0d6aa602006-01-02 20:14:23 +110056
Dave Airlie6e5fca52006-03-20 18:34:29 +110057 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
58
Dave Airlie0d6aa602006-01-02 20:14:23 +110059 if (temp & USER_INT_FLAG)
60 DRM_WAKEUP(&dev_priv->irq_queue);
61
Dave Airlie702880f2006-06-24 17:07:34 +100062 if (temp & (VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG)) {
=?utf-8?q?Michel_D=C3=A4nzer?=68815ba2006-10-24 22:28:51 +100063 if ((dev_priv->vblank_pipe &
64 (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B))
65 == (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) {
66 if (temp & VSYNC_PIPEA_FLAG)
67 atomic_inc(&dev->vbl_received);
68 if (temp & VSYNC_PIPEB_FLAG)
69 atomic_inc(&dev->vbl_received2);
70 } else
71 atomic_inc(&dev->vbl_received);
72
Dave Airlie0d6aa602006-01-02 20:14:23 +110073 DRM_WAKEUP(&dev->vbl_queue);
74 drm_vbl_send_signals(dev);
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 return IRQ_HANDLED;
78}
79
Dave Airliec94f7022005-07-07 21:03:38 +100080static int i915_emit_irq(drm_device_t * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 drm_i915_private_t *dev_priv = dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 RING_LOCALS;
84
85 i915_kernel_lost_context(dev);
86
87 DRM_DEBUG("%s\n", __FUNCTION__);
88
Alan Hourihanec29b6692006-08-12 16:29:24 +100089 dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Alan Hourihanec29b6692006-08-12 16:29:24 +100091 if (dev_priv->counter > 0x7FFFFFFFUL)
92 dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
93
94 BEGIN_LP_RING(6);
95 OUT_RING(CMD_STORE_DWORD_IDX);
96 OUT_RING(20);
97 OUT_RING(dev_priv->counter);
98 OUT_RING(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 OUT_RING(0);
100 OUT_RING(GFX_OP_USER_INTERRUPT);
101 ADVANCE_LP_RING();
Alan Hourihanec29b6692006-08-12 16:29:24 +1000102
103 return dev_priv->counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Dave Airliec94f7022005-07-07 21:03:38 +1000106static int i915_wait_irq(drm_device_t * dev, int irq_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
109 int ret = 0;
110
111 DRM_DEBUG("%s irq_nr=%d breadcrumb=%d\n", __FUNCTION__, irq_nr,
112 READ_BREADCRUMB(dev_priv));
113
114 if (READ_BREADCRUMB(dev_priv) >= irq_nr)
115 return 0;
116
117 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
118
119 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
120 READ_BREADCRUMB(dev_priv) >= irq_nr);
121
122 if (ret == DRM_ERR(EBUSY)) {
123 DRM_ERROR("%s: EBUSY -- rec: %d emitted: %d\n",
124 __FUNCTION__,
125 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
126 }
127
128 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
129 return ret;
130}
131
=?utf-8?q?Michel_D=C3=A4nzer?=68815ba2006-10-24 22:28:51 +1000132static int i915_driver_vblank_do_wait(drm_device_t *dev, unsigned int *sequence,
133 atomic_t *counter)
Dave Airlie0d6aa602006-01-02 20:14:23 +1100134{
135 drm_i915_private_t *dev_priv = dev->dev_private;
136 unsigned int cur_vblank;
137 int ret = 0;
138
139 if (!dev_priv) {
140 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
141 return DRM_ERR(EINVAL);
142 }
143
144 DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
=?utf-8?q?Michel_D=C3=A4nzer?=68815ba2006-10-24 22:28:51 +1000145 (((cur_vblank = atomic_read(counter))
Dave Airlie0d6aa602006-01-02 20:14:23 +1100146 - *sequence) <= (1<<23)));
147
148 *sequence = cur_vblank;
149
150 return ret;
151}
152
153
=?utf-8?q?Michel_D=C3=A4nzer?=68815ba2006-10-24 22:28:51 +1000154int i915_driver_vblank_wait(drm_device_t *dev, unsigned int *sequence)
155{
156 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received);
157}
158
159int i915_driver_vblank_wait2(drm_device_t *dev, unsigned int *sequence)
160{
161 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received2);
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/* Needs the lock as it touches the ring.
165 */
166int i915_irq_emit(DRM_IOCTL_ARGS)
167{
168 DRM_DEVICE;
169 drm_i915_private_t *dev_priv = dev->dev_private;
170 drm_i915_irq_emit_t emit;
171 int result;
172
173 LOCK_TEST_WITH_RETURN(dev, filp);
174
175 if (!dev_priv) {
176 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
177 return DRM_ERR(EINVAL);
178 }
179
180 DRM_COPY_FROM_USER_IOCTL(emit, (drm_i915_irq_emit_t __user *) data,
181 sizeof(emit));
182
183 result = i915_emit_irq(dev);
184
185 if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
186 DRM_ERROR("copy_to_user\n");
187 return DRM_ERR(EFAULT);
188 }
189
190 return 0;
191}
192
193/* Doesn't need the hardware lock.
194 */
195int i915_irq_wait(DRM_IOCTL_ARGS)
196{
197 DRM_DEVICE;
198 drm_i915_private_t *dev_priv = dev->dev_private;
199 drm_i915_irq_wait_t irqwait;
200
201 if (!dev_priv) {
202 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
203 return DRM_ERR(EINVAL);
204 }
205
206 DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_i915_irq_wait_t __user *) data,
207 sizeof(irqwait));
208
209 return i915_wait_irq(dev, irqwait.irq_seq);
210}
211
Dave Airlie702880f2006-06-24 17:07:34 +1000212static int i915_enable_interrupt (drm_device_t *dev)
213{
214 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
215 u16 flag;
216
217 flag = 0;
218 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
219 flag |= VSYNC_PIPEA_FLAG;
220 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
221 flag |= VSYNC_PIPEB_FLAG;
222 if (dev_priv->vblank_pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
223 DRM_ERROR("%s called with invalid pipe 0x%x\n",
224 __FUNCTION__, dev_priv->vblank_pipe);
225 return DRM_ERR(EINVAL);
226 }
227 I915_WRITE16(I915REG_INT_ENABLE_R, USER_INT_FLAG | flag);
228 return 0;
229}
230
231/* Set the vblank monitor pipe
232 */
233int i915_vblank_pipe_set(DRM_IOCTL_ARGS)
234{
235 DRM_DEVICE;
236 drm_i915_private_t *dev_priv = dev->dev_private;
237 drm_i915_vblank_pipe_t pipe;
238
239 if (!dev_priv) {
240 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
241 return DRM_ERR(EINVAL);
242 }
243
244 DRM_COPY_FROM_USER_IOCTL(pipe, (drm_i915_vblank_pipe_t __user *) data,
245 sizeof(pipe));
246
247 dev_priv->vblank_pipe = pipe.pipe;
248 return i915_enable_interrupt (dev);
249}
250
251int i915_vblank_pipe_get(DRM_IOCTL_ARGS)
252{
253 DRM_DEVICE;
254 drm_i915_private_t *dev_priv = dev->dev_private;
255 drm_i915_vblank_pipe_t pipe;
256 u16 flag;
257
258 if (!dev_priv) {
259 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
260 return DRM_ERR(EINVAL);
261 }
262
263 flag = I915_READ(I915REG_INT_ENABLE_R);
264 pipe.pipe = 0;
265 if (flag & VSYNC_PIPEA_FLAG)
266 pipe.pipe |= DRM_I915_VBLANK_PIPE_A;
267 if (flag & VSYNC_PIPEB_FLAG)
268 pipe.pipe |= DRM_I915_VBLANK_PIPE_B;
269 DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_pipe_t __user *) data, pipe,
270 sizeof(pipe));
271 return 0;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/* drm_dma.h hooks
275*/
276void i915_driver_irq_preinstall(drm_device_t * dev)
277{
278 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
279
280 I915_WRITE16(I915REG_HWSTAM, 0xfffe);
281 I915_WRITE16(I915REG_INT_MASK_R, 0x0);
282 I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
283}
284
285void i915_driver_irq_postinstall(drm_device_t * dev)
286{
287 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
288
Dave Airlie702880f2006-06-24 17:07:34 +1000289 i915_enable_interrupt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
291}
292
293void i915_driver_irq_uninstall(drm_device_t * dev)
294{
295 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
Dave Airlie91e37382006-02-18 15:17:04 +1100296 u16 temp;
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!dev_priv)
299 return;
300
301 I915_WRITE16(I915REG_HWSTAM, 0xffff);
302 I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
303 I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
Dave Airlie91e37382006-02-18 15:17:04 +1100304
305 temp = I915_READ16(I915REG_INT_IDENTITY_R);
306 I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}