1 /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
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.
34 #define USER_INT_FLAG (1<<1)
35 #define VSYNC_PIPEB_FLAG (1<<5)
36 #define VSYNC_PIPEA_FLAG (1<<7)
38 #define MAX_NOPID ((u32)~0)
41 * Emit blits for scheduled buffer swaps.
43 * This function will be called with the HW lock held.
45 static void i915_vblank_tasklet(drm_device_t *dev)
47 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
48 unsigned long irqflags;
49 struct list_head *list, *tmp;
53 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
55 list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
56 drm_i915_vbl_swap_t *vbl_swap =
57 list_entry(list, drm_i915_vbl_swap_t, head);
58 atomic_t *counter = vbl_swap->pipe ? &dev->vbl_received2 :
61 if ((atomic_read(counter) - vbl_swap->sequence) <= (1<<23)) {
62 drm_drawable_info_t *drw;
64 spin_unlock(&dev_priv->swaps_lock);
66 spin_lock(&dev->drw_lock);
68 drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
71 int i, num_rects = drw->num_rects;
72 drm_clip_rect_t *rect = drw->rects;
73 drm_i915_sarea_t *sarea_priv =
75 u32 cpp = dev_priv->cpp;
76 u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
77 XY_SRC_COPY_BLT_WRITE_ALPHA |
78 XY_SRC_COPY_BLT_WRITE_RGB)
79 : XY_SRC_COPY_BLT_CMD;
80 u32 pitchropcpp = (sarea_priv->pitch * cpp) |
81 (0xcc << 16) | (cpp << 23) |
85 i915_kernel_lost_context(dev);
89 OUT_RING(GFX_OP_DRAWRECT_INFO);
92 OUT_RING(sarea_priv->width |
93 sarea_priv->height << 16);
94 OUT_RING(sarea_priv->width |
95 sarea_priv->height << 16);
100 sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
102 for (i = 0; i < num_rects; i++, rect++) {
106 OUT_RING(pitchropcpp);
107 OUT_RING((rect->y1 << 16) | rect->x1);
108 OUT_RING((rect->y2 << 16) | rect->x2);
109 OUT_RING(sarea_priv->front_offset);
110 OUT_RING((rect->y1 << 16) | rect->x1);
111 OUT_RING(pitchropcpp & 0xffff);
112 OUT_RING(sarea_priv->back_offset);
118 spin_unlock(&dev->drw_lock);
120 spin_lock(&dev_priv->swaps_lock);
124 drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
126 dev_priv->swaps_pending--;
130 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
133 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
135 drm_device_t *dev = (drm_device_t *) arg;
136 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
139 temp = I915_READ16(I915REG_INT_IDENTITY_R);
141 temp &= (USER_INT_FLAG | VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG);
143 DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
148 I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
150 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
152 if (temp & USER_INT_FLAG)
153 DRM_WAKEUP(&dev_priv->irq_queue);
155 if (temp & (VSYNC_PIPEA_FLAG | VSYNC_PIPEB_FLAG)) {
156 int vblank_pipe = dev_priv->vblank_pipe;
159 (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B))
160 == (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) {
161 if (temp & VSYNC_PIPEA_FLAG)
162 atomic_inc(&dev->vbl_received);
163 if (temp & VSYNC_PIPEB_FLAG)
164 atomic_inc(&dev->vbl_received2);
165 } else if (((temp & VSYNC_PIPEA_FLAG) &&
166 (vblank_pipe & DRM_I915_VBLANK_PIPE_A)) ||
167 ((temp & VSYNC_PIPEB_FLAG) &&
168 (vblank_pipe & DRM_I915_VBLANK_PIPE_B)))
169 atomic_inc(&dev->vbl_received);
171 DRM_WAKEUP(&dev->vbl_queue);
172 drm_vbl_send_signals(dev);
174 if (dev_priv->swaps_pending > 0)
175 drm_locked_tasklet(dev, i915_vblank_tasklet);
181 static int i915_emit_irq(drm_device_t * dev)
183 drm_i915_private_t *dev_priv = dev->dev_private;
186 i915_kernel_lost_context(dev);
188 DRM_DEBUG("%s\n", __FUNCTION__);
190 dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
192 if (dev_priv->counter > 0x7FFFFFFFUL)
193 dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
196 OUT_RING(CMD_STORE_DWORD_IDX);
198 OUT_RING(dev_priv->counter);
201 OUT_RING(GFX_OP_USER_INTERRUPT);
204 return dev_priv->counter;
207 static int i915_wait_irq(drm_device_t * dev, int irq_nr)
209 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
212 DRM_DEBUG("%s irq_nr=%d breadcrumb=%d\n", __FUNCTION__, irq_nr,
213 READ_BREADCRUMB(dev_priv));
215 if (READ_BREADCRUMB(dev_priv) >= irq_nr)
218 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
220 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
221 READ_BREADCRUMB(dev_priv) >= irq_nr);
223 if (ret == DRM_ERR(EBUSY)) {
224 DRM_ERROR("%s: EBUSY -- rec: %d emitted: %d\n",
226 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
229 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
233 static int i915_driver_vblank_do_wait(drm_device_t *dev, unsigned int *sequence,
236 drm_i915_private_t *dev_priv = dev->dev_private;
237 unsigned int cur_vblank;
241 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
242 return DRM_ERR(EINVAL);
245 DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
246 (((cur_vblank = atomic_read(counter))
247 - *sequence) <= (1<<23)));
249 *sequence = cur_vblank;
255 int i915_driver_vblank_wait(drm_device_t *dev, unsigned int *sequence)
257 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received);
260 int i915_driver_vblank_wait2(drm_device_t *dev, unsigned int *sequence)
262 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received2);
265 /* Needs the lock as it touches the ring.
267 int i915_irq_emit(DRM_IOCTL_ARGS)
270 drm_i915_private_t *dev_priv = dev->dev_private;
271 drm_i915_irq_emit_t emit;
274 LOCK_TEST_WITH_RETURN(dev, filp);
277 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
278 return DRM_ERR(EINVAL);
281 DRM_COPY_FROM_USER_IOCTL(emit, (drm_i915_irq_emit_t __user *) data,
284 result = i915_emit_irq(dev);
286 if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
287 DRM_ERROR("copy_to_user\n");
288 return DRM_ERR(EFAULT);
294 /* Doesn't need the hardware lock.
296 int i915_irq_wait(DRM_IOCTL_ARGS)
299 drm_i915_private_t *dev_priv = dev->dev_private;
300 drm_i915_irq_wait_t irqwait;
303 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
304 return DRM_ERR(EINVAL);
307 DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_i915_irq_wait_t __user *) data,
310 return i915_wait_irq(dev, irqwait.irq_seq);
313 static void i915_enable_interrupt (drm_device_t *dev)
315 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
319 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
320 flag |= VSYNC_PIPEA_FLAG;
321 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
322 flag |= VSYNC_PIPEB_FLAG;
324 I915_WRITE16(I915REG_INT_ENABLE_R, USER_INT_FLAG | flag);
327 /* Set the vblank monitor pipe
329 int i915_vblank_pipe_set(DRM_IOCTL_ARGS)
332 drm_i915_private_t *dev_priv = dev->dev_private;
333 drm_i915_vblank_pipe_t pipe;
336 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
337 return DRM_ERR(EINVAL);
340 DRM_COPY_FROM_USER_IOCTL(pipe, (drm_i915_vblank_pipe_t __user *) data,
343 if (pipe.pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
344 DRM_ERROR("%s called with invalid pipe 0x%x\n",
345 __FUNCTION__, pipe.pipe);
346 return DRM_ERR(EINVAL);
349 dev_priv->vblank_pipe = pipe.pipe;
351 i915_enable_interrupt (dev);
356 int i915_vblank_pipe_get(DRM_IOCTL_ARGS)
359 drm_i915_private_t *dev_priv = dev->dev_private;
360 drm_i915_vblank_pipe_t pipe;
364 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
365 return DRM_ERR(EINVAL);
368 flag = I915_READ(I915REG_INT_ENABLE_R);
370 if (flag & VSYNC_PIPEA_FLAG)
371 pipe.pipe |= DRM_I915_VBLANK_PIPE_A;
372 if (flag & VSYNC_PIPEB_FLAG)
373 pipe.pipe |= DRM_I915_VBLANK_PIPE_B;
374 DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_pipe_t __user *) data, pipe,
380 * Schedule buffer swap at given vertical blank.
382 int i915_vblank_swap(DRM_IOCTL_ARGS)
385 drm_i915_private_t *dev_priv = dev->dev_private;
386 drm_i915_vblank_swap_t swap;
387 drm_i915_vbl_swap_t *vbl_swap;
388 unsigned int pipe, seqtype, curseq;
389 unsigned long irqflags;
390 struct list_head *list;
393 DRM_ERROR("%s called with no initialization\n", __func__);
394 return DRM_ERR(EINVAL);
397 if (dev_priv->sarea_priv->rotation) {
398 DRM_DEBUG("Rotation not supported\n");
399 return DRM_ERR(EINVAL);
402 DRM_COPY_FROM_USER_IOCTL(swap, (drm_i915_vblank_swap_t __user *) data,
405 if (swap.seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
406 _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) {
407 DRM_ERROR("Invalid sequence type 0x%x\n", swap.seqtype);
408 return DRM_ERR(EINVAL);
411 pipe = (swap.seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
413 seqtype = swap.seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
415 if (!(dev_priv->vblank_pipe & (1 << pipe))) {
416 DRM_ERROR("Invalid pipe %d\n", pipe);
417 return DRM_ERR(EINVAL);
420 spin_lock_irqsave(&dev->drw_lock, irqflags);
422 if (!drm_get_drawable_info(dev, swap.drawable)) {
423 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
424 DRM_ERROR("Invalid drawable ID %d\n", swap.drawable);
425 return DRM_ERR(EINVAL);
428 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
430 curseq = atomic_read(pipe ? &dev->vbl_received2 : &dev->vbl_received);
432 if (seqtype == _DRM_VBLANK_RELATIVE)
433 swap.sequence += curseq;
435 if ((curseq - swap.sequence) <= (1<<23)) {
436 if (swap.seqtype & _DRM_VBLANK_NEXTONMISS) {
437 swap.sequence = curseq + 1;
439 DRM_DEBUG("Missed target sequence\n");
440 return DRM_ERR(EINVAL);
444 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
446 list_for_each(list, &dev_priv->vbl_swaps.head) {
447 vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head);
449 if (vbl_swap->drw_id == swap.drawable &&
450 vbl_swap->pipe == pipe &&
451 vbl_swap->sequence == swap.sequence) {
452 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
453 DRM_DEBUG("Already scheduled\n");
458 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
460 if (dev_priv->swaps_pending >= 100) {
461 DRM_DEBUG("Too many swaps queued\n");
462 return DRM_ERR(EBUSY);
465 vbl_swap = drm_calloc(1, sizeof(vbl_swap), DRM_MEM_DRIVER);
468 DRM_ERROR("Failed to allocate memory to queue swap\n");
469 return DRM_ERR(ENOMEM);
474 vbl_swap->drw_id = swap.drawable;
475 vbl_swap->pipe = pipe;
476 vbl_swap->sequence = swap.sequence;
478 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
480 list_add_tail((struct list_head *)vbl_swap, &dev_priv->vbl_swaps.head);
481 dev_priv->swaps_pending++;
483 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
485 DRM_COPY_TO_USER_IOCTL((drm_i915_vblank_swap_t __user *) data, swap,
493 void i915_driver_irq_preinstall(drm_device_t * dev)
495 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
497 I915_WRITE16(I915REG_HWSTAM, 0xfffe);
498 I915_WRITE16(I915REG_INT_MASK_R, 0x0);
499 I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
502 void i915_driver_irq_postinstall(drm_device_t * dev)
504 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
506 dev_priv->swaps_lock = SPIN_LOCK_UNLOCKED;
507 INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
508 dev_priv->swaps_pending = 0;
510 if (!dev_priv->vblank_pipe)
511 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A;
512 i915_enable_interrupt(dev);
513 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
516 void i915_driver_irq_uninstall(drm_device_t * dev)
518 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
524 I915_WRITE16(I915REG_HWSTAM, 0xffff);
525 I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
526 I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
528 temp = I915_READ16(I915REG_INT_IDENTITY_R);
529 I915_WRITE16(I915REG_INT_IDENTITY_R, temp);