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 <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
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"
37 #define MAX_NOPID ((u32)~0)
40 * Interrupts that are always left unmasked.
42 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
43 * we leave them always unmasked in IMR and then control enabling them through
46 #define I915_INTERRUPT_ENABLE_FIX (I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
47 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
49 /** Interrupts that we mask and unmask at runtime. */
50 #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
52 /** These are all of the interrupts used by the driver */
53 #define I915_INTERRUPT_ENABLE_MASK (I915_INTERRUPT_ENABLE_FIX | \
54 I915_INTERRUPT_ENABLE_VAR)
57 i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
59 DRM_DEBUG("irq_enable_reg = 0x%08x, mask = 0x%08x\n",
60 dev_priv->irq_mask_reg, mask);
61 mask &= I915_INTERRUPT_ENABLE_VAR;
62 if ((dev_priv->irq_mask_reg & mask) != 0) {
63 dev_priv->irq_mask_reg &= ~mask;
64 I915_WRITE(IMR, dev_priv->irq_mask_reg);
65 (void) I915_READ(IMR);
70 i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
72 mask &= I915_INTERRUPT_ENABLE_VAR;
73 if ((dev_priv->irq_mask_reg & mask) != mask) {
74 dev_priv->irq_mask_reg |= mask;
75 I915_WRITE(IMR, dev_priv->irq_mask_reg);
76 (void) I915_READ(IMR);
81 i915_pipestat(int pipe)
91 i915_enable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
93 if ((dev_priv->pipestat[pipe] & mask) != mask) {
94 u32 reg = i915_pipestat(pipe);
96 dev_priv->pipestat[pipe] |= mask;
97 /* Enable the interrupt, clear any pending status */
98 I915_WRITE(reg, dev_priv->pipestat[pipe] | (mask >> 16));
99 (void) I915_READ(reg);
104 i915_disable_pipestat(drm_i915_private_t *dev_priv, int pipe, u32 mask)
106 if ((dev_priv->pipestat[pipe] & mask) != 0) {
107 u32 reg = i915_pipestat(pipe);
109 dev_priv->pipestat[pipe] &= ~mask;
110 I915_WRITE(reg, dev_priv->pipestat[pipe]);
111 (void) I915_READ(reg);
116 * i915_pipe_enabled - check if a pipe is enabled
118 * @pipe: pipe to check
120 * Reading certain registers when the pipe is disabled can hang the chip.
121 * Use this routine to make sure the PLL is running and the pipe is active
122 * before reading such registers if unsure.
125 i915_pipe_enabled(struct drm_device *dev, int pipe)
127 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
128 unsigned long pipeconf = pipe ? PIPEBCONF : PIPEACONF;
130 if (I915_READ(pipeconf) & PIPEACONF_ENABLE)
136 /* Called from drm generic code, passed a 'crtc', which
137 * we use as a pipe index
139 u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
141 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
142 unsigned long high_frame;
143 unsigned long low_frame;
144 u32 high1, high2, low, count;
146 high_frame = pipe ? PIPEBFRAMEHIGH : PIPEAFRAMEHIGH;
147 low_frame = pipe ? PIPEBFRAMEPIXEL : PIPEAFRAMEPIXEL;
149 if (!i915_pipe_enabled(dev, pipe)) {
150 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
155 * High & low register fields aren't synchronized, so make sure
156 * we get a low value that's stable across two reads of the high
160 high1 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
161 PIPE_FRAME_HIGH_SHIFT);
162 low = ((I915_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
163 PIPE_FRAME_LOW_SHIFT);
164 high2 = ((I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
165 PIPE_FRAME_HIGH_SHIFT);
166 } while (high1 != high2);
168 count = (high1 << 8) | low;
173 u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
175 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
176 int reg = pipe ? PIPEB_FRMCOUNT_GM45 : PIPEA_FRMCOUNT_GM45;
178 if (!i915_pipe_enabled(dev, pipe)) {
179 DRM_ERROR("trying to get vblank count for disabled pipe %d\n", pipe);
183 return I915_READ(reg);
186 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
188 struct drm_device *dev = (struct drm_device *) arg;
189 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
191 u32 pipea_stats, pipeb_stats;
193 atomic_inc(&dev_priv->irq_received);
195 for (iir = I915_READ(IIR) ; iir != 0 ; iir = new_iir) {
197 pipea_stats = pipeb_stats = 0;
200 * Clear the PIPE(A|B)STAT regs before the IIR
202 if (iir & I915_DISPLAY_PIPE_A_EVENT_INTERRUPT) {
203 DRM_SPINLOCK(&dev_priv->user_irq_lock);
204 pipea_stats = I915_READ(PIPEASTAT);
205 I915_WRITE(PIPEASTAT, pipea_stats);
206 DRM_SPINUNLOCK(&dev_priv->user_irq_lock);
209 if (iir & I915_DISPLAY_PIPE_B_EVENT_INTERRUPT) {
210 DRM_SPINLOCK(&dev_priv->user_irq_lock);
211 pipeb_stats = I915_READ(PIPEBSTAT);
212 I915_WRITE(PIPEBSTAT, pipeb_stats);
213 DRM_SPINUNLOCK(&dev_priv->user_irq_lock);
216 I915_WRITE(IIR, iir);
217 new_iir = I915_READ(IIR);
219 DRM_DEBUG("iir = 0x%08x, pipestats a = 0x%08x, b = 0x%08x\n",
220 iir, pipea_stats, pipeb_stats);
222 if (dev_priv->sarea_priv)
223 dev_priv->sarea_priv->last_dispatch =
224 READ_BREADCRUMB(dev_priv);
226 if (iir & I915_USER_INTERRUPT) {
228 dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
230 DRM_WAKEUP(&dev_priv->irq_queue);
233 if (pipea_stats & (PIPE_START_VBLANK_INTERRUPT_STATUS |
234 PIPE_VBLANK_INTERRUPT_STATUS))
235 drm_handle_vblank(dev, 0);
237 if (pipeb_stats & (PIPE_START_VBLANK_INTERRUPT_STATUS |
238 PIPE_VBLANK_INTERRUPT_STATUS))
239 drm_handle_vblank(dev, 1);
241 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
242 (iir & I915_ASLE_INTERRUPT))
243 opregion_asle_intr(dev);
248 static int i915_emit_irq(struct drm_device * dev)
250 drm_i915_private_t *dev_priv = dev->dev_private;
253 i915_kernel_lost_context(dev);
258 if (dev_priv->counter > 0x7FFFFFFFUL)
259 dev_priv->counter = 1;
260 if (dev_priv->sarea_priv)
261 dev_priv->sarea_priv->last_enqueue = dev_priv->counter;
264 OUT_RING(MI_STORE_DWORD_INDEX);
265 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
266 OUT_RING(dev_priv->counter);
267 OUT_RING(MI_USER_INTERRUPT);
270 return dev_priv->counter;
273 void i915_user_irq_get(struct drm_device *dev)
275 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
276 unsigned long irqflags;
279 DRM_SPINLOCK_IRQSAVE(&dev_priv->user_irq_lock, irqflags);
280 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
281 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
282 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->user_irq_lock, irqflags);
285 void i915_user_irq_put(struct drm_device *dev)
287 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
288 unsigned long irqflags;
290 DRM_SPINLOCK_IRQSAVE(&dev_priv->user_irq_lock, irqflags);
292 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
294 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
295 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
296 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->user_irq_lock, irqflags);
299 static int i915_wait_irq(struct drm_device * dev, int irq_nr)
301 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
304 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
305 READ_BREADCRUMB(dev_priv));
307 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
308 if (dev_priv->sarea_priv) {
309 dev_priv->sarea_priv->last_dispatch =
310 READ_BREADCRUMB(dev_priv);
315 if (dev_priv->sarea_priv)
316 dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
318 i915_user_irq_get(dev);
319 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
320 READ_BREADCRUMB(dev_priv) >= irq_nr);
321 i915_user_irq_put(dev);
324 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
325 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
328 if (dev_priv->sarea_priv)
329 dev_priv->sarea_priv->last_dispatch =
330 READ_BREADCRUMB(dev_priv);
335 /* Needs the lock as it touches the ring.
337 int i915_irq_emit(struct drm_device *dev, void *data,
338 struct drm_file *file_priv)
340 drm_i915_private_t *dev_priv = dev->dev_private;
341 drm_i915_irq_emit_t *emit = data;
344 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
347 DRM_ERROR("called with no initialization\n");
351 result = i915_emit_irq(dev);
353 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
354 DRM_ERROR("copy_to_user\n");
361 /* Doesn't need the hardware lock.
363 int i915_irq_wait(struct drm_device *dev, void *data,
364 struct drm_file *file_priv)
366 drm_i915_private_t *dev_priv = dev->dev_private;
367 drm_i915_irq_wait_t *irqwait = data;
370 DRM_ERROR("called with no initialization\n");
374 return i915_wait_irq(dev, irqwait->irq_seq);
377 /* Called from drm generic code, passed 'crtc' which
378 * we use as a pipe index
380 int i915_enable_vblank(struct drm_device *dev, int pipe)
382 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
383 unsigned long irqflags;
387 * Older chips didn't have the start vblank interrupt,
391 pipestat = PIPE_START_VBLANK_INTERRUPT_ENABLE;
393 pipestat = PIPE_VBLANK_INTERRUPT_ENABLE;
395 DRM_SPINLOCK_IRQSAVE(&dev_priv->user_irq_lock, irqflags);
396 i915_enable_pipestat(dev_priv, pipe, pipestat);
397 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->user_irq_lock, irqflags);
401 /* Called from drm generic code, passed 'crtc' which
402 * we use as a pipe index
404 void i915_disable_vblank(struct drm_device *dev, int pipe)
406 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
407 unsigned long irqflags;
409 DRM_SPINLOCK_IRQSAVE(&dev_priv->user_irq_lock, irqflags);
410 i915_disable_pipestat(dev_priv, pipe,
411 PIPE_START_VBLANK_INTERRUPT_ENABLE | PIPE_VBLANK_INTERRUPT_ENABLE);
412 DRM_SPINUNLOCK_IRQRESTORE(&dev_priv->user_irq_lock, irqflags);
415 /* Set the vblank monitor pipe
417 int i915_vblank_pipe_set(struct drm_device *dev, void *data,
418 struct drm_file *file_priv)
420 drm_i915_private_t *dev_priv = dev->dev_private;
423 DRM_ERROR("called with no initialization\n");
430 int i915_vblank_pipe_get(struct drm_device *dev, void *data,
431 struct drm_file *file_priv)
433 drm_i915_private_t *dev_priv = dev->dev_private;
434 drm_i915_vblank_pipe_t *pipe = data;
437 DRM_ERROR("called with no initialization\n");
441 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
447 * Schedule buffer swap at given vertical blank.
449 int i915_vblank_swap(struct drm_device *dev, void *data,
450 struct drm_file *file_priv)
452 /* The delayed swap mechanism was fundamentally racy, and has been
453 * removed. The model was that the client requested a delayed flip/swap
454 * from the kernel, then waited for vblank before continuing to perform
455 * rendering. The problem was that the kernel might wake the client
456 * up before it dispatched the vblank swap (since the lock has to be
457 * held while touching the ringbuffer), in which case the client would
458 * clear and start the next frame before the swap occurred, and
459 * flicker would occur in addition to likely missing the vblank.
461 * In the absence of this ioctl, userland falls back to a correct path
462 * of waiting for a vblank, then dispatching the swap on its own.
463 * Context switching to userland and back is plenty fast enough for
464 * meeting the requirements of vblank swapping.
472 void i915_driver_irq_preinstall(struct drm_device * dev)
474 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
476 I915_WRITE(HWSTAM, 0xeffe);
477 I915_WRITE(PIPEASTAT, 0);
478 I915_WRITE(PIPEBSTAT, 0);
479 I915_WRITE(IMR, 0xffffffff);
480 I915_WRITE(IER, 0x0);
481 (void) I915_READ(IER);
484 int i915_driver_irq_postinstall(struct drm_device *dev)
486 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
488 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
490 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
492 /* Unmask the interrupts that we always want on. */
493 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
495 dev_priv->pipestat[0] = 0;
496 dev_priv->pipestat[1] = 0;
498 /* Disable pipe interrupt enables, clear pending pipe status */
499 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
500 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
502 /* Clear pending interrupt status */
503 I915_WRITE(IIR, I915_READ(IIR));
505 I915_WRITE(IER, I915_INTERRUPT_ENABLE_MASK);
506 I915_WRITE(IMR, dev_priv->irq_mask_reg);
507 (void) I915_READ(IER);
509 opregion_enable_asle(dev);
511 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
513 i915_enable_vblank(dev, 0);
514 i915_enable_vblank(dev, 1);
519 void i915_driver_irq_uninstall(struct drm_device * dev)
521 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
526 dev_priv->vblank_pipe = 0;
528 I915_WRITE(HWSTAM, 0xffffffff);
529 I915_WRITE(PIPEASTAT, 0);
530 I915_WRITE(PIPEBSTAT, 0);
531 I915_WRITE(IMR, 0xffffffff);
532 I915_WRITE(IER, 0x0);
534 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
535 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
536 I915_WRITE(IIR, I915_READ(IIR));