]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/sys/dev/drm/i915_irq.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / sys / dev / 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 <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "dev/drm/drmP.h"
33 #include "dev/drm/drm.h"
34 #include "dev/drm/i915_drm.h"
35 #include "dev/drm/i915_drv.h"
36
37 #define USER_INT_FLAG (1<<1)
38 #define VSYNC_PIPEB_FLAG (1<<5)
39 #define VSYNC_PIPEA_FLAG (1<<7)
40
41 #define MAX_NOPID ((u32)~0)
42
43 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
44 {
45         drm_device_t *dev = (drm_device_t *) arg;
46         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
47         u16 temp;
48
49         temp = I915_READ16(I915REG_INT_IDENTITY_R);
50
51         temp &= (USER_INT_FLAG | VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG);
52
53         DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
54
55         if (temp == 0)
56                 return IRQ_NONE;
57
58         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
59
60         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
61
62         if (temp & USER_INT_FLAG)
63                 DRM_WAKEUP(&dev_priv->irq_queue);
64
65         if (temp & (VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG)) {
66                 atomic_inc(&dev->vbl_received);
67                 DRM_WAKEUP(&dev->vbl_queue);
68                 drm_vbl_send_signals(dev);
69         }
70
71         return IRQ_HANDLED;
72 }
73
74 static int i915_emit_irq(drm_device_t * dev)
75 {
76         
77         drm_i915_private_t *dev_priv = dev->dev_private;
78         RING_LOCALS;
79
80         i915_kernel_lost_context(dev);
81
82         DRM_DEBUG("%s\n", __FUNCTION__);
83
84         dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
85
86         if (dev_priv->counter > 0x7FFFFFFFUL)
87                  dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
88
89         BEGIN_LP_RING(6);
90         OUT_RING(CMD_STORE_DWORD_IDX);
91         OUT_RING(20);
92         OUT_RING(dev_priv->counter);
93
94         OUT_RING(0);
95         OUT_RING(0);
96         OUT_RING(GFX_OP_USER_INTERRUPT);
97         ADVANCE_LP_RING();
98
99         return dev_priv->counter;
100
101
102 }
103
104 static int i915_wait_irq(drm_device_t * dev, int irq_nr)
105 {
106         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
107         int ret = 0;
108
109         DRM_DEBUG("%s irq_nr=%d breadcrumb=%d\n", __FUNCTION__, irq_nr,
110                   READ_BREADCRUMB(dev_priv));
111
112         if (READ_BREADCRUMB(dev_priv) >= irq_nr)
113                 return 0;
114
115         dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
116
117         DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
118                     READ_BREADCRUMB(dev_priv) >= irq_nr);
119
120         if (ret == DRM_ERR(EBUSY)) {
121                 DRM_ERROR("%s: EBUSY -- rec: %d emitted: %d\n",
122                           __FUNCTION__,
123                           READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
124         }
125
126         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
127         return ret;
128 }
129
130 int i915_driver_vblank_wait(drm_device_t *dev, unsigned int *sequence)
131 {
132         drm_i915_private_t *dev_priv = dev->dev_private;
133         unsigned int cur_vblank;
134         int ret = 0;
135
136         if (!dev_priv) {
137                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
138                 return DRM_ERR(EINVAL);
139         }
140
141         DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
142                     (((cur_vblank = atomic_read(&dev->vbl_received))
143                         - *sequence) <= (1<<23)));
144         
145         *sequence = cur_vblank;
146
147         return ret;
148 }
149
150 /* Needs the lock as it touches the ring.
151  */
152 int i915_irq_emit(DRM_IOCTL_ARGS)
153 {
154         DRM_DEVICE;
155         drm_i915_private_t *dev_priv = dev->dev_private;
156         drm_i915_irq_emit_t emit;
157         int result;
158
159         LOCK_TEST_WITH_RETURN(dev, filp);
160
161         if (!dev_priv) {
162                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
163                 return DRM_ERR(EINVAL);
164         }
165
166         DRM_COPY_FROM_USER_IOCTL(emit, (drm_i915_irq_emit_t __user *) data,
167                                  sizeof(emit));
168
169         result = i915_emit_irq(dev);
170
171         if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
172                 DRM_ERROR("copy_to_user\n");
173                 return DRM_ERR(EFAULT);
174         }
175
176         return 0;
177 }
178
179 /* Doesn't need the hardware lock.
180  */
181 int i915_irq_wait(DRM_IOCTL_ARGS)
182 {
183         DRM_DEVICE;
184         drm_i915_private_t *dev_priv = dev->dev_private;
185         drm_i915_irq_wait_t irqwait;
186
187         if (!dev_priv) {
188                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
189                 return DRM_ERR(EINVAL);
190         }
191
192         DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_i915_irq_wait_t __user *) data,
193                                  sizeof(irqwait));
194
195         return i915_wait_irq(dev, irqwait.irq_seq);
196 }
197
198 static int i915_enable_interrupt (drm_device_t *dev)
199 {
200         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
201         u16 flag;
202         
203         flag = 0;
204         if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
205                 flag |= VSYNC_PIPEA_FLAG;
206         if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
207                 flag |= VSYNC_PIPEB_FLAG;
208         if (dev_priv->vblank_pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
209                 DRM_ERROR("%s called with invalid pipe 0x%x\n", 
210                           __FUNCTION__, dev_priv->vblank_pipe);
211                 return DRM_ERR(EINVAL);
212         }
213         I915_WRITE16(I915REG_INT_ENABLE_R, USER_INT_FLAG | flag);
214         return 0;
215 }
216
217 /* Set the vblank monitor pipe
218  */
219 int i915_vblank_pipe_set(DRM_IOCTL_ARGS)
220 {
221         DRM_DEVICE;
222         drm_i915_private_t *dev_priv = dev->dev_private;
223         drm_i915_vblank_pipe_t pipe;
224
225         if (!dev_priv) {
226                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
227                 return DRM_ERR(EINVAL);
228         }
229
230         DRM_COPY_FROM_USER_IOCTL(pipe, (drm_i915_vblank_pipe_t __user *) data,
231                                  sizeof(pipe));
232
233         dev_priv->vblank_pipe = pipe.pipe;
234         return i915_enable_interrupt (dev);
235 }
236
237 int i915_vblank_pipe_get(DRM_IOCTL_ARGS)
238 {
239         DRM_DEVICE;
240         drm_i915_private_t *dev_priv = dev->dev_private;
241         drm_i915_vblank_pipe_t pipe;
242         u16 flag;
243
244         if (!dev_priv) {
245                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
246                 return DRM_ERR(EINVAL);
247         }
248
249         flag = I915_READ(I915REG_INT_ENABLE_R);
250         pipe.pipe = 0;
251         if (flag & VSYNC_PIPEA_FLAG)
252                 pipe.pipe |= DRM_I915_VBLANK_PIPE_A;
253         if (flag & VSYNC_PIPEB_FLAG)
254                 pipe.pipe |= DRM_I915_VBLANK_PIPE_B;
255         DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_pipe_t __user *) data, pipe,
256                                  sizeof(pipe));
257         return 0;
258 }
259
260 /* drm_dma.h hooks
261 */
262 void i915_driver_irq_preinstall(drm_device_t * dev)
263 {
264         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
265
266         I915_WRITE16(I915REG_HWSTAM, 0xfffe);
267         I915_WRITE16(I915REG_INT_MASK_R, 0x0);
268         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
269 }
270
271 void i915_driver_irq_postinstall(drm_device_t * dev)
272 {
273         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
274
275         i915_enable_interrupt(dev);
276         DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
277 }
278
279 void i915_driver_irq_uninstall(drm_device_t * dev)
280 {
281         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
282         u16 temp;
283         if (!dev_priv)
284                 return;
285
286         I915_WRITE16(I915REG_HWSTAM, 0xffff);
287         I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
288         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
289
290         temp = I915_READ16(I915REG_INT_IDENTITY_R);
291         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
292 }