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.
29 #include "dev/drm/drmP.h"
30 #include "dev/drm/drm.h"
31 #include "dev/drm/i915_drm.h"
32 #include "dev/drm/i915_drv.h"
34 #define MAX_NOPID ((u32)~0)
37 * Interrupts that are always left unmasked.
39 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
40 * we leave them always unmasked in IMR and then control enabling them through
43 #define I915_INTERRUPT_ENABLE_FIX (I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
44 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
46 /** Interrupts that we mask and unmask at runtime. */
47 #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
49 /** These are all of the interrupts used by the driver */
50 #define I915_INTERRUPT_ENABLE_MASK (I915_INTERRUPT_ENABLE_FIX | \
51 I915_INTERRUPT_ENABLE_VAR)
53 #define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
54 PIPE_VBLANK_INTERRUPT_STATUS)
56 #define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
57 PIPE_VBLANK_INTERRUPT_ENABLE)
59 #define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
60 DRM_I915_VBLANK_PIPE_B)
63 i915_enable_irq(drm_i915_private_t
*dev_priv
, u32 mask
)
65 mask
&= I915_INTERRUPT_ENABLE_VAR
;
66 if ((dev_priv
->irq_mask_reg
& mask
) != 0) {
67 dev_priv
->irq_mask_reg
&= ~mask
;
68 I915_WRITE(IMR
, dev_priv
->irq_mask_reg
);
69 (void) I915_READ(IMR
);
74 i915_disable_irq(drm_i915_private_t
*dev_priv
, u32 mask
)
76 mask
&= I915_INTERRUPT_ENABLE_VAR
;
77 if ((dev_priv
->irq_mask_reg
& mask
) != mask
) {
78 dev_priv
->irq_mask_reg
|= mask
;
79 I915_WRITE(IMR
, dev_priv
->irq_mask_reg
);
80 (void) I915_READ(IMR
);
85 i915_pipestat(int pipe
)
95 i915_enable_pipestat(drm_i915_private_t
*dev_priv
, int pipe
, u32 mask
)
97 if ((dev_priv
->pipestat
[pipe
] & mask
) != mask
) {
98 u32 reg
= i915_pipestat(pipe
);
100 dev_priv
->pipestat
[pipe
] |= mask
;
101 /* Enable the interrupt, clear any pending status */
102 I915_WRITE(reg
, dev_priv
->pipestat
[pipe
] | (mask
>> 16));
103 (void) I915_READ(reg
);
108 i915_disable_pipestat(drm_i915_private_t
*dev_priv
, int pipe
, u32 mask
)
110 if ((dev_priv
->pipestat
[pipe
] & mask
) != 0) {
111 u32 reg
= i915_pipestat(pipe
);
113 dev_priv
->pipestat
[pipe
] &= ~mask
;
114 I915_WRITE(reg
, dev_priv
->pipestat
[pipe
]);
115 (void) I915_READ(reg
);
120 * i915_pipe_enabled - check if a pipe is enabled
122 * @pipe: pipe to check
124 * Reading certain registers when the pipe is disabled can hang the chip.
125 * Use this routine to make sure the PLL is running and the pipe is active
126 * before reading such registers if unsure.
129 i915_pipe_enabled(struct drm_device
*dev
, int pipe
)
131 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
132 unsigned long pipeconf
= pipe
? PIPEBCONF
: PIPEACONF
;
134 if (I915_READ(pipeconf
) & PIPEACONF_ENABLE
)
140 /* Called from drm generic code, passed a 'crtc', which
141 * we use as a pipe index
143 u32
i915_get_vblank_counter(struct drm_device
*dev
, int pipe
)
145 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
146 unsigned long high_frame
;
147 unsigned long low_frame
;
148 u32 high1
, high2
, low
, count
;
150 high_frame
= pipe
? PIPEBFRAMEHIGH
: PIPEAFRAMEHIGH
;
151 low_frame
= pipe
? PIPEBFRAMEPIXEL
: PIPEAFRAMEPIXEL
;
153 if (!i915_pipe_enabled(dev
, pipe
)) {
154 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe
);
159 * High & low register fields aren't synchronized, so make sure
160 * we get a low value that's stable across two reads of the high
164 high1
= ((I915_READ(high_frame
) & PIPE_FRAME_HIGH_MASK
) >>
165 PIPE_FRAME_HIGH_SHIFT
);
166 low
= ((I915_READ(low_frame
) & PIPE_FRAME_LOW_MASK
) >>
167 PIPE_FRAME_LOW_SHIFT
);
168 high2
= ((I915_READ(high_frame
) & PIPE_FRAME_HIGH_MASK
) >>
169 PIPE_FRAME_HIGH_SHIFT
);
170 } while (high1
!= high2
);
172 count
= (high1
<< 8) | low
;
177 u32
gm45_get_vblank_counter(struct drm_device
*dev
, int pipe
)
179 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
180 int reg
= pipe
? PIPEB_FRMCOUNT_GM45
: PIPEA_FRMCOUNT_GM45
;
182 if (!i915_pipe_enabled(dev
, pipe
)) {
183 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe
);
187 return I915_READ(reg
);
190 irqreturn_t
i915_driver_irq_handler(DRM_IRQ_ARGS
)
192 struct drm_device
*dev
= (struct drm_device
*) arg
;
193 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
195 u32 pipea_stats
, pipeb_stats
;
200 atomic_inc(&dev_priv
->irq_received
);
202 iir
= I915_READ(IIR
);
205 vblank_status
= I915_START_VBLANK_INTERRUPT_STATUS
;
206 vblank_enable
= PIPE_START_VBLANK_INTERRUPT_ENABLE
;
208 vblank_status
= I915_VBLANK_INTERRUPT_STATUS
;
209 vblank_enable
= I915_VBLANK_INTERRUPT_ENABLE
;
213 irq_received
= iir
!= 0;
215 /* Can't rely on pipestat interrupt bit in iir as it might
216 * have been cleared after the pipestat interrupt was received.
217 * It doesn't set the bit in iir again, but it still produces
218 * interrupts (for non-MSI).
220 DRM_SPINLOCK(&dev_priv
->user_irq_lock
);
221 pipea_stats
= I915_READ(PIPEASTAT
);
222 pipeb_stats
= I915_READ(PIPEBSTAT
);
225 * Clear the PIPE(A|B)STAT regs before the IIR
227 if (pipea_stats
& 0x8000ffff) {
228 I915_WRITE(PIPEASTAT
, pipea_stats
);
232 if (pipeb_stats
& 0x8000ffff) {
233 I915_WRITE(PIPEBSTAT
, pipeb_stats
);
236 DRM_SPINUNLOCK(&dev_priv
->user_irq_lock
);
241 I915_WRITE(IIR
, iir
);
242 new_iir
= I915_READ(IIR
); /* Flush posted writes */
244 if (dev_priv
->sarea_priv
)
245 dev_priv
->sarea_priv
->last_dispatch
=
246 READ_BREADCRUMB(dev_priv
);
248 if (iir
& I915_USER_INTERRUPT
) {
249 DRM_WAKEUP(&dev_priv
->irq_queue
);
252 if (pipea_stats
& vblank_status
)
253 drm_handle_vblank(dev
, 0);
255 if (pipeb_stats
& vblank_status
)
256 drm_handle_vblank(dev
, 1);
258 /* With MSI, interrupts are only generated when iir
259 * transitions from zero to nonzero. If another bit got
260 * set while we were handling the existing iir bits, then
261 * we would never get another interrupt.
263 * This is fine on non-MSI as well, as if we hit this path
264 * we avoid exiting the interrupt handler only to generate
267 * Note that for MSI this could cause a stray interrupt report
268 * if an interrupt landed in the time between writing IIR and
269 * the posting read. This should be rare enough to never
270 * trigger the 99% of 100,000 interrupts test for disabling
277 static int i915_emit_irq(struct drm_device
* dev
)
279 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
282 i915_kernel_lost_context(dev
);
284 if (++dev_priv
->counter
> 0x7FFFFFFFUL
)
285 dev_priv
->counter
= 0;
286 if (dev_priv
->sarea_priv
)
287 dev_priv
->sarea_priv
->last_enqueue
= dev_priv
->counter
;
289 DRM_DEBUG("emitting: %d\n", dev_priv
->counter
);
292 OUT_RING(MI_STORE_DWORD_INDEX
);
293 OUT_RING(I915_BREADCRUMB_INDEX
<< MI_STORE_DWORD_INDEX_SHIFT
);
294 OUT_RING(dev_priv
->counter
);
295 OUT_RING(MI_USER_INTERRUPT
);
298 return dev_priv
->counter
;
301 void i915_user_irq_get(struct drm_device
*dev
)
303 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
306 DRM_SPINLOCK(&dev_priv
->user_irq_lock
);
307 if (dev
->irq_enabled
&& (++dev_priv
->user_irq_refcount
== 1))
308 i915_enable_irq(dev_priv
, I915_USER_INTERRUPT
);
309 DRM_SPINUNLOCK(&dev_priv
->user_irq_lock
);
312 void i915_user_irq_put(struct drm_device
*dev
)
314 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
316 DRM_SPINLOCK(&dev_priv
->user_irq_lock
);
317 if (dev
->irq_enabled
) {
318 KASSERT(dev_priv
->user_irq_refcount
> 0, ("invalid refcount"));
319 if (--dev_priv
->user_irq_refcount
== 0)
320 i915_disable_irq(dev_priv
, I915_USER_INTERRUPT
);
322 DRM_SPINUNLOCK(&dev_priv
->user_irq_lock
);
325 static int i915_wait_irq(struct drm_device
* dev
, int irq_nr
)
327 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
330 if (READ_BREADCRUMB(dev_priv
) >= irq_nr
) {
331 if (dev_priv
->sarea_priv
) {
332 dev_priv
->sarea_priv
->last_dispatch
=
333 READ_BREADCRUMB(dev_priv
);
338 if (dev_priv
->sarea_priv
)
339 dev_priv
->sarea_priv
->perf_boxes
|= I915_BOX_WAIT
;
341 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr
,
342 READ_BREADCRUMB(dev_priv
));
344 i915_user_irq_get(dev
);
345 DRM_WAIT_ON(ret
, dev_priv
->irq_queue
, 3 * DRM_HZ
,
346 READ_BREADCRUMB(dev_priv
) >= irq_nr
);
347 i915_user_irq_put(dev
);
349 if (ret
== -ERESTART
)
350 DRM_DEBUG("restarting syscall\n");
353 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
354 READ_BREADCRUMB(dev_priv
), (int)dev_priv
->counter
);
360 /* Needs the lock as it touches the ring.
362 int i915_irq_emit(struct drm_device
*dev
, void *data
,
363 struct drm_file
*file_priv
)
365 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
366 drm_i915_irq_emit_t
*emit
= data
;
370 DRM_ERROR("called with no initialization\n");
374 RING_LOCK_TEST_WITH_RETURN(dev
, file_priv
);
376 result
= i915_emit_irq(dev
);
378 if (DRM_COPY_TO_USER(emit
->irq_seq
, &result
, sizeof(int))) {
379 DRM_ERROR("copy_to_user\n");
386 /* Doesn't need the hardware lock.
388 int i915_irq_wait(struct drm_device
*dev
, void *data
,
389 struct drm_file
*file_priv
)
391 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
392 drm_i915_irq_wait_t
*irqwait
= data
;
395 DRM_ERROR("called with no initialization\n");
399 return i915_wait_irq(dev
, irqwait
->irq_seq
);
402 /* Called from drm generic code, passed 'crtc' which
403 * we use as a pipe index
405 int i915_enable_vblank(struct drm_device
*dev
, int pipe
)
407 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
408 int pipeconf_reg
= (pipe
== 0) ? PIPEACONF
: PIPEBCONF
;
411 pipeconf
= I915_READ(pipeconf_reg
);
412 if (!(pipeconf
& PIPEACONF_ENABLE
))
415 DRM_SPINLOCK(&dev_priv
->user_irq_lock
);
417 i915_enable_pipestat(dev_priv
, pipe
,
418 PIPE_START_VBLANK_INTERRUPT_ENABLE
);
420 i915_enable_pipestat(dev_priv
, pipe
,
421 PIPE_VBLANK_INTERRUPT_ENABLE
);
422 DRM_SPINUNLOCK(&dev_priv
->user_irq_lock
);
426 /* Called from drm generic code, passed 'crtc' which
427 * we use as a pipe index
429 void i915_disable_vblank(struct drm_device
*dev
, int pipe
)
431 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
433 DRM_SPINLOCK(&dev_priv
->user_irq_lock
);
434 i915_disable_pipestat(dev_priv
, pipe
,
435 PIPE_VBLANK_INTERRUPT_ENABLE
|
436 PIPE_START_VBLANK_INTERRUPT_ENABLE
);
437 DRM_SPINUNLOCK(&dev_priv
->user_irq_lock
);
440 /* Set the vblank monitor pipe
442 int i915_vblank_pipe_set(struct drm_device
*dev
, void *data
,
443 struct drm_file
*file_priv
)
445 drm_i915_private_t
*dev_priv
= dev
->dev_private
;
448 DRM_ERROR("called with no initialization\n");
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
;
462 DRM_ERROR("called with no initialization\n");
466 pipe
->pipe
= DRM_I915_VBLANK_PIPE_A
| DRM_I915_VBLANK_PIPE_B
;
472 * Schedule buffer swap at given vertical blank.
474 int i915_vblank_swap(struct drm_device
*dev
, void *data
,
475 struct drm_file
*file_priv
)
477 /* The delayed swap mechanism was fundamentally racy, and has been
478 * removed. The model was that the client requested a delayed flip/swap
479 * from the kernel, then waited for vblank before continuing to perform
480 * rendering. The problem was that the kernel might wake the client
481 * up before it dispatched the vblank swap (since the lock has to be
482 * held while touching the ringbuffer), in which case the client would
483 * clear and start the next frame before the swap occurred, and
484 * flicker would occur in addition to likely missing the vblank.
486 * In the absence of this ioctl, userland falls back to a correct path
487 * of waiting for a vblank, then dispatching the swap on its own.
488 * Context switching to userland and back is plenty fast enough for
489 * meeting the requirements of vblank swapping.
496 void i915_driver_irq_preinstall(struct drm_device
* dev
)
498 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
500 atomic_set_int(&dev_priv
->irq_received
, 0);
502 I915_WRITE(HWSTAM
, 0xeffe);
503 I915_WRITE(PIPEASTAT
, 0);
504 I915_WRITE(PIPEBSTAT
, 0);
505 I915_WRITE(IMR
, 0xffffffff);
506 I915_WRITE(IER
, 0x0);
507 (void) I915_READ(IER
);
510 int i915_driver_irq_postinstall(struct drm_device
*dev
)
512 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
514 dev_priv
->vblank_pipe
= DRM_I915_VBLANK_PIPE_A
| DRM_I915_VBLANK_PIPE_B
;
516 dev
->max_vblank_count
= 0xffffff; /* only 24 bits of frame count */
518 /* Unmask the interrupts that we always want on. */
519 dev_priv
->irq_mask_reg
= ~I915_INTERRUPT_ENABLE_FIX
;
521 dev_priv
->pipestat
[0] = 0;
522 dev_priv
->pipestat
[1] = 0;
524 /* Disable pipe interrupt enables, clear pending pipe status */
525 I915_WRITE(PIPEASTAT
, I915_READ(PIPEASTAT
) & 0x8000ffff);
526 I915_WRITE(PIPEBSTAT
, I915_READ(PIPEBSTAT
) & 0x8000ffff);
528 /* Clear pending interrupt status */
529 I915_WRITE(IIR
, I915_READ(IIR
));
531 I915_WRITE(IER
, I915_INTERRUPT_ENABLE_MASK
);
532 I915_WRITE(IMR
, dev_priv
->irq_mask_reg
);
533 (void) I915_READ(IER
);
538 void i915_driver_irq_uninstall(struct drm_device
* dev
)
540 drm_i915_private_t
*dev_priv
= (drm_i915_private_t
*) dev
->dev_private
;
545 dev_priv
->vblank_pipe
= 0;
547 I915_WRITE(HWSTAM
, 0xffffffff);
548 I915_WRITE(PIPEASTAT
, 0);
549 I915_WRITE(PIPEBSTAT
, 0);
550 I915_WRITE(IMR
, 0xffffffff);
551 I915_WRITE(IER
, 0x0);
553 I915_WRITE(PIPEASTAT
, I915_READ(PIPEASTAT
) & 0x8000ffff);
554 I915_WRITE(PIPEBSTAT
, I915_READ(PIPEBSTAT
) & 0x8000ffff);
555 I915_WRITE(IIR
, I915_READ(IIR
));