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 MAX_NOPID ((u32)~0)
37 * Emit blits for scheduled buffer swaps.
39 * This function will be called with the HW lock held.
41 static void i915_vblank_tasklet(struct drm_device *dev)
43 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
44 unsigned long irqflags;
45 struct list_head *list, *tmp, hits, *hit;
46 int nhits, nrects, slice[2], upper[2], lower[2], i;
47 unsigned counter[2] = { atomic_read(&dev->vbl_received),
48 atomic_read(&dev->vbl_received2) };
49 struct drm_drawable_info *drw;
50 drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
51 u32 cpp = dev_priv->cpp;
52 u32 cmd = (cpp == 4) ? (XY_SRC_COPY_BLT_CMD |
53 XY_SRC_COPY_BLT_WRITE_ALPHA |
54 XY_SRC_COPY_BLT_WRITE_RGB)
55 : XY_SRC_COPY_BLT_CMD;
56 u32 src_pitch = sarea_priv->pitch * cpp;
57 u32 dst_pitch = sarea_priv->pitch * cpp;
58 u32 ropcpp = (0xcc << 16) | ((cpp - 1) << 24);
61 if (IS_I965G(dev) && sarea_priv->front_tiled) {
62 cmd |= XY_SRC_COPY_BLT_DST_TILED;
65 if (IS_I965G(dev) && sarea_priv->back_tiled) {
66 cmd |= XY_SRC_COPY_BLT_SRC_TILED;
72 INIT_LIST_HEAD(&hits);
76 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
78 /* Find buffer swaps scheduled for this vertical blank */
79 list_for_each_safe(list, tmp, &dev_priv->vbl_swaps.head) {
80 drm_i915_vbl_swap_t *vbl_swap =
81 list_entry(list, drm_i915_vbl_swap_t, head);
83 if ((counter[vbl_swap->pipe] - vbl_swap->sequence) > (1<<23))
87 dev_priv->swaps_pending--;
89 spin_unlock(&dev_priv->swaps_lock);
90 spin_lock(&dev->drw_lock);
92 drw = drm_get_drawable_info(dev, vbl_swap->drw_id);
95 spin_unlock(&dev->drw_lock);
96 drm_free(vbl_swap, sizeof(*vbl_swap), DRM_MEM_DRIVER);
97 spin_lock(&dev_priv->swaps_lock);
101 list_for_each(hit, &hits) {
102 drm_i915_vbl_swap_t *swap_cmp =
103 list_entry(hit, drm_i915_vbl_swap_t, head);
104 struct drm_drawable_info *drw_cmp =
105 drm_get_drawable_info(dev, swap_cmp->drw_id);
108 drw_cmp->rects[0].y1 > drw->rects[0].y1) {
109 list_add_tail(list, hit);
114 spin_unlock(&dev->drw_lock);
116 /* List of hits was empty, or we reached the end of it */
118 list_add_tail(list, hits.prev);
122 spin_lock(&dev_priv->swaps_lock);
126 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
130 spin_unlock(&dev_priv->swaps_lock);
132 i915_kernel_lost_context(dev);
137 OUT_RING(GFX_OP_DRAWRECT_INFO_I965);
139 OUT_RING(((sarea_priv->width - 1) & 0xffff) | ((sarea_priv->height - 1) << 16));
145 OUT_RING(GFX_OP_DRAWRECT_INFO);
148 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
149 OUT_RING(sarea_priv->width | sarea_priv->height << 16);
155 sarea_priv->ctxOwner = DRM_KERNEL_CONTEXT;
157 upper[0] = upper[1] = 0;
158 slice[0] = max(sarea_priv->pipeA_h / nhits, 1);
159 slice[1] = max(sarea_priv->pipeB_h / nhits, 1);
160 lower[0] = sarea_priv->pipeA_y + slice[0];
161 lower[1] = sarea_priv->pipeB_y + slice[0];
163 spin_lock(&dev->drw_lock);
165 /* Emit blits for buffer swaps, partitioning both outputs into as many
166 * slices as there are buffer swaps scheduled in order to avoid tearing
167 * (based on the assumption that a single buffer swap would always
168 * complete before scanout starts).
170 for (i = 0; i++ < nhits;
171 upper[0] = lower[0], lower[0] += slice[0],
172 upper[1] = lower[1], lower[1] += slice[1]) {
174 lower[0] = lower[1] = sarea_priv->height;
176 list_for_each(hit, &hits) {
177 drm_i915_vbl_swap_t *swap_hit =
178 list_entry(hit, drm_i915_vbl_swap_t, head);
179 struct drm_clip_rect *rect;
181 unsigned short top, bottom;
183 drw = drm_get_drawable_info(dev, swap_hit->drw_id);
189 pipe = swap_hit->pipe;
191 bottom = lower[pipe];
193 for (num_rects = drw->num_rects; num_rects--; rect++) {
194 int y1 = max(rect->y1, top);
195 int y2 = min(rect->y2, bottom);
203 OUT_RING(ropcpp | dst_pitch);
204 OUT_RING((y1 << 16) | rect->x1);
205 OUT_RING((y2 << 16) | rect->x2);
206 OUT_RING(sarea_priv->front_offset);
207 OUT_RING((y1 << 16) | rect->x1);
209 OUT_RING(sarea_priv->back_offset);
216 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
218 list_for_each_safe(hit, tmp, &hits) {
219 drm_i915_vbl_swap_t *swap_hit =
220 list_entry(hit, drm_i915_vbl_swap_t, head);
224 drm_free(swap_hit, sizeof(*swap_hit), DRM_MEM_DRIVER);
228 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
230 struct drm_device *dev = (struct drm_device *) arg;
231 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
233 u32 pipea_stats, pipeb_stats;
235 pipea_stats = I915_READ(PIPEASTAT);
236 pipeb_stats = I915_READ(PIPEBSTAT);
238 temp = I915_READ16(IIR);
240 temp &= (I915_USER_INTERRUPT |
241 I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
242 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT);
244 DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
249 I915_WRITE16(IIR, temp);
250 (void) I915_READ16(IIR);
251 DRM_READMEMORYBARRIER();
253 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
255 if (temp & I915_USER_INTERRUPT)
256 DRM_WAKEUP(&dev_priv->irq_queue);
258 if (temp & (I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
259 I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)) {
260 int vblank_pipe = dev_priv->vblank_pipe;
263 (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B))
264 == (DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B)) {
265 if (temp & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT)
266 atomic_inc(&dev->vbl_received);
267 if (temp & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)
268 atomic_inc(&dev->vbl_received2);
269 } else if (((temp & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT) &&
270 (vblank_pipe & DRM_I915_VBLANK_PIPE_A)) ||
271 ((temp & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT) &&
272 (vblank_pipe & DRM_I915_VBLANK_PIPE_B)))
273 atomic_inc(&dev->vbl_received);
275 DRM_WAKEUP(&dev->vbl_queue);
276 drm_vbl_send_signals(dev);
278 if (dev_priv->swaps_pending > 0)
279 drm_locked_tasklet(dev, i915_vblank_tasklet);
280 I915_WRITE(PIPEASTAT,
281 pipea_stats|I915_VBLANK_INTERRUPT_ENABLE|
282 PIPE_VBLANK_INTERRUPT_STATUS);
283 I915_WRITE(PIPEBSTAT,
284 pipeb_stats|I915_VBLANK_INTERRUPT_ENABLE|
285 PIPE_VBLANK_INTERRUPT_STATUS);
291 static int i915_emit_irq(struct drm_device * dev)
293 drm_i915_private_t *dev_priv = dev->dev_private;
296 i915_kernel_lost_context(dev);
300 dev_priv->sarea_priv->last_enqueue = ++dev_priv->counter;
302 if (dev_priv->counter > 0x7FFFFFFFUL)
303 dev_priv->sarea_priv->last_enqueue = dev_priv->counter = 1;
306 OUT_RING(MI_STORE_DWORD_INDEX);
307 OUT_RING(5 << MI_STORE_DWORD_INDEX_SHIFT);
308 OUT_RING(dev_priv->counter);
311 OUT_RING(MI_USER_INTERRUPT);
314 return dev_priv->counter;
317 static int i915_wait_irq(struct drm_device * dev, int irq_nr)
319 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
322 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
323 READ_BREADCRUMB(dev_priv));
325 if (READ_BREADCRUMB(dev_priv) >= irq_nr)
328 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
330 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
331 READ_BREADCRUMB(dev_priv) >= irq_nr);
334 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
335 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
338 dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
342 static int i915_driver_vblank_do_wait(struct drm_device *dev, unsigned int *sequence,
345 drm_i915_private_t *dev_priv = dev->dev_private;
346 unsigned int cur_vblank;
350 DRM_ERROR("called with no initialization\n");
354 DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
355 (((cur_vblank = atomic_read(counter))
356 - *sequence) <= (1<<23)));
358 *sequence = cur_vblank;
364 int i915_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence)
366 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received);
369 int i915_driver_vblank_wait2(struct drm_device *dev, unsigned int *sequence)
371 return i915_driver_vblank_do_wait(dev, sequence, &dev->vbl_received2);
374 /* Needs the lock as it touches the ring.
376 int i915_irq_emit(struct drm_device *dev, void *data,
377 struct drm_file *file_priv)
379 drm_i915_private_t *dev_priv = dev->dev_private;
380 drm_i915_irq_emit_t *emit = data;
383 LOCK_TEST_WITH_RETURN(dev, file_priv);
386 DRM_ERROR("called with no initialization\n");
390 result = i915_emit_irq(dev);
392 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
393 DRM_ERROR("copy_to_user\n");
400 /* Doesn't need the hardware lock.
402 int i915_irq_wait(struct drm_device *dev, void *data,
403 struct drm_file *file_priv)
405 drm_i915_private_t *dev_priv = dev->dev_private;
406 drm_i915_irq_wait_t *irqwait = data;
409 DRM_ERROR("called with no initialization\n");
413 return i915_wait_irq(dev, irqwait->irq_seq);
416 static void i915_enable_interrupt (struct drm_device *dev)
418 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
422 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_A)
423 flag |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
424 if (dev_priv->vblank_pipe & DRM_I915_VBLANK_PIPE_B)
425 flag |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
427 I915_WRITE16(IER, I915_USER_INTERRUPT | flag);
430 /* Set the vblank monitor pipe
432 int i915_vblank_pipe_set(struct drm_device *dev, void *data,
433 struct drm_file *file_priv)
435 drm_i915_private_t *dev_priv = dev->dev_private;
436 drm_i915_vblank_pipe_t *pipe = data;
439 DRM_ERROR("called with no initialization\n");
443 if (pipe->pipe & ~(DRM_I915_VBLANK_PIPE_A|DRM_I915_VBLANK_PIPE_B)) {
444 DRM_ERROR("called with invalid pipe 0x%x\n", pipe->pipe);
448 dev_priv->vblank_pipe = pipe->pipe;
450 i915_enable_interrupt (dev);
455 int i915_vblank_pipe_get(struct drm_device *dev, void *data,
456 struct drm_file *file_priv)
458 drm_i915_private_t *dev_priv = dev->dev_private;
459 drm_i915_vblank_pipe_t *pipe = data;
463 DRM_ERROR("called with no initialization\n");
467 flag = I915_READ(IER);
469 if (flag & I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT)
470 pipe->pipe |= DRM_I915_VBLANK_PIPE_A;
471 if (flag & I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT)
472 pipe->pipe |= DRM_I915_VBLANK_PIPE_B;
478 * Schedule buffer swap at given vertical blank.
480 int i915_vblank_swap(struct drm_device *dev, void *data,
481 struct drm_file *file_priv)
483 drm_i915_private_t *dev_priv = dev->dev_private;
484 drm_i915_vblank_swap_t *swap = data;
485 drm_i915_vbl_swap_t *vbl_swap;
486 unsigned int pipe, seqtype, curseq;
487 unsigned long irqflags;
488 struct list_head *list;
491 DRM_ERROR("%s called with no initialization\n", __func__);
495 if (dev_priv->sarea_priv->rotation) {
496 DRM_DEBUG("Rotation not supported\n");
500 if (swap->seqtype & ~(_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE |
501 _DRM_VBLANK_SECONDARY | _DRM_VBLANK_NEXTONMISS)) {
502 DRM_ERROR("Invalid sequence type 0x%x\n", swap->seqtype);
506 pipe = (swap->seqtype & _DRM_VBLANK_SECONDARY) ? 1 : 0;
508 seqtype = swap->seqtype & (_DRM_VBLANK_RELATIVE | _DRM_VBLANK_ABSOLUTE);
510 if (!(dev_priv->vblank_pipe & (1 << pipe))) {
511 DRM_ERROR("Invalid pipe %d\n", pipe);
515 spin_lock_irqsave(&dev->drw_lock, irqflags);
517 if (!drm_get_drawable_info(dev, swap->drawable)) {
518 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
519 DRM_DEBUG("Invalid drawable ID %d\n", swap->drawable);
523 spin_unlock_irqrestore(&dev->drw_lock, irqflags);
525 curseq = atomic_read(pipe ? &dev->vbl_received2 : &dev->vbl_received);
527 if (seqtype == _DRM_VBLANK_RELATIVE)
528 swap->sequence += curseq;
530 if ((curseq - swap->sequence) <= (1<<23)) {
531 if (swap->seqtype & _DRM_VBLANK_NEXTONMISS) {
532 swap->sequence = curseq + 1;
534 DRM_DEBUG("Missed target sequence\n");
539 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
541 list_for_each(list, &dev_priv->vbl_swaps.head) {
542 vbl_swap = list_entry(list, drm_i915_vbl_swap_t, head);
544 if (vbl_swap->drw_id == swap->drawable &&
545 vbl_swap->pipe == pipe &&
546 vbl_swap->sequence == swap->sequence) {
547 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
548 DRM_DEBUG("Already scheduled\n");
553 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
555 if (dev_priv->swaps_pending >= 100) {
556 DRM_DEBUG("Too many swaps queued\n");
560 vbl_swap = drm_calloc(1, sizeof(*vbl_swap), DRM_MEM_DRIVER);
563 DRM_ERROR("Failed to allocate memory to queue swap\n");
569 vbl_swap->drw_id = swap->drawable;
570 vbl_swap->pipe = pipe;
571 vbl_swap->sequence = swap->sequence;
573 spin_lock_irqsave(&dev_priv->swaps_lock, irqflags);
575 list_add_tail(&vbl_swap->head, &dev_priv->vbl_swaps.head);
576 dev_priv->swaps_pending++;
578 spin_unlock_irqrestore(&dev_priv->swaps_lock, irqflags);
585 void i915_driver_irq_preinstall(struct drm_device * dev)
587 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
589 I915_WRITE16(HWSTAM, 0xfffe);
590 I915_WRITE16(IMR, 0x0);
591 I915_WRITE16(IER, 0x0);
594 void i915_driver_irq_postinstall(struct drm_device * dev)
596 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
598 spin_lock_init(&dev_priv->swaps_lock);
599 INIT_LIST_HEAD(&dev_priv->vbl_swaps.head);
600 dev_priv->swaps_pending = 0;
602 if (!dev_priv->vblank_pipe)
603 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A;
604 i915_enable_interrupt(dev);
605 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
608 void i915_driver_irq_uninstall(struct drm_device * dev)
610 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
616 I915_WRITE16(HWSTAM, 0xffff);
617 I915_WRITE16(IMR, 0xffff);
618 I915_WRITE16(IER, 0x0);
620 temp = I915_READ16(IIR);
621 I915_WRITE16(IIR, temp);