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.
33 #include "intel_drv.h"
35 #define MAX_NOPID ((u32)~0)
38 * Interrupts that are always left unmasked.
40 * Since pipe events are edge-triggered from the PIPESTAT register to IIR,
41 * we leave them always unmasked in IMR and then control enabling them through
44 #define I915_INTERRUPT_ENABLE_FIX (I915_ASLE_INTERRUPT | \
45 I915_DISPLAY_PIPE_A_EVENT_INTERRUPT | \
46 I915_DISPLAY_PIPE_B_EVENT_INTERRUPT)
48 /** Interrupts that we mask and unmask at runtime. */
49 #define I915_INTERRUPT_ENABLE_VAR (I915_USER_INTERRUPT)
51 #define I915_PIPE_VBLANK_STATUS (PIPE_START_VBLANK_INTERRUPT_STATUS |\
52 PIPE_VBLANK_INTERRUPT_STATUS)
54 #define I915_PIPE_VBLANK_ENABLE (PIPE_START_VBLANK_INTERRUPT_ENABLE |\
55 PIPE_VBLANK_INTERRUPT_ENABLE)
57 #define DRM_I915_VBLANK_PIPE_ALL (DRM_I915_VBLANK_PIPE_A | \
58 DRM_I915_VBLANK_PIPE_B)
61 i915_enable_irq(drm_i915_private_t *dev_priv, u32 mask)
63 if ((dev_priv->irq_mask_reg & mask) != 0) {
64 dev_priv->irq_mask_reg &= ~mask;
65 I915_WRITE(IMR, dev_priv->irq_mask_reg);
66 (void) I915_READ(IMR);
71 i915_disable_irq(drm_i915_private_t *dev_priv, u32 mask)
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);
187 * Handle hotplug events outside the interrupt handler proper.
189 static void i915_hotplug_work_func(struct work_struct *work)
191 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
193 struct drm_device *dev = dev_priv->dev;
195 /* Just fire off a uevent and let userspace tell us what to do */
196 drm_sysfs_hotplug_event(dev);
199 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
201 struct drm_device *dev = (struct drm_device *) arg;
202 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
203 struct drm_i915_master_private *master_priv;
205 u32 pipea_stats, pipeb_stats;
209 unsigned long irqflags;
213 atomic_inc(&dev_priv->irq_received);
215 iir = I915_READ(IIR);
218 vblank_status = I915_START_VBLANK_INTERRUPT_STATUS;
219 vblank_enable = PIPE_START_VBLANK_INTERRUPT_ENABLE;
221 vblank_status = I915_VBLANK_INTERRUPT_STATUS;
222 vblank_enable = I915_VBLANK_INTERRUPT_ENABLE;
226 irq_received = iir != 0;
228 /* Can't rely on pipestat interrupt bit in iir as it might
229 * have been cleared after the pipestat interrupt was received.
230 * It doesn't set the bit in iir again, but it still produces
231 * interrupts (for non-MSI).
233 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
234 pipea_stats = I915_READ(PIPEASTAT);
235 pipeb_stats = I915_READ(PIPEBSTAT);
238 * Clear the PIPE(A|B)STAT regs before the IIR
240 if (pipea_stats & 0x8000ffff) {
241 I915_WRITE(PIPEASTAT, pipea_stats);
245 if (pipeb_stats & 0x8000ffff) {
246 I915_WRITE(PIPEBSTAT, pipeb_stats);
249 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
256 /* Consume port. Then clear IIR or we'll miss events */
257 if ((I915_HAS_HOTPLUG(dev)) &&
258 (iir & I915_DISPLAY_PORT_INTERRUPT)) {
259 u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
261 DRM_DEBUG("hotplug event received, stat 0x%08x\n",
263 if (hotplug_status & dev_priv->hotplug_supported_mask)
264 schedule_work(&dev_priv->hotplug_work);
266 I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
267 I915_READ(PORT_HOTPLUG_STAT);
270 I915_WRITE(IIR, iir);
271 new_iir = I915_READ(IIR); /* Flush posted writes */
273 if (dev->primary->master) {
274 master_priv = dev->primary->master->driver_priv;
275 if (master_priv->sarea_priv)
276 master_priv->sarea_priv->last_dispatch =
277 READ_BREADCRUMB(dev_priv);
280 if (iir & I915_USER_INTERRUPT) {
281 dev_priv->mm.irq_gem_seqno = i915_get_gem_seqno(dev);
282 DRM_WAKEUP(&dev_priv->irq_queue);
285 if (pipea_stats & vblank_status) {
287 drm_handle_vblank(dev, 0);
290 if (pipeb_stats & vblank_status) {
292 drm_handle_vblank(dev, 1);
295 if ((pipeb_stats & I915_LEGACY_BLC_EVENT_STATUS) ||
296 (iir & I915_ASLE_INTERRUPT))
297 opregion_asle_intr(dev);
299 /* With MSI, interrupts are only generated when iir
300 * transitions from zero to nonzero. If another bit got
301 * set while we were handling the existing iir bits, then
302 * we would never get another interrupt.
304 * This is fine on non-MSI as well, as if we hit this path
305 * we avoid exiting the interrupt handler only to generate
308 * Note that for MSI this could cause a stray interrupt report
309 * if an interrupt landed in the time between writing IIR and
310 * the posting read. This should be rare enough to never
311 * trigger the 99% of 100,000 interrupts test for disabling
320 static int i915_emit_irq(struct drm_device * dev)
322 drm_i915_private_t *dev_priv = dev->dev_private;
323 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
326 i915_kernel_lost_context(dev);
331 if (dev_priv->counter > 0x7FFFFFFFUL)
332 dev_priv->counter = 1;
333 if (master_priv->sarea_priv)
334 master_priv->sarea_priv->last_enqueue = dev_priv->counter;
337 OUT_RING(MI_STORE_DWORD_INDEX);
338 OUT_RING(I915_BREADCRUMB_INDEX << MI_STORE_DWORD_INDEX_SHIFT);
339 OUT_RING(dev_priv->counter);
340 OUT_RING(MI_USER_INTERRUPT);
343 return dev_priv->counter;
346 void i915_user_irq_get(struct drm_device *dev)
348 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
349 unsigned long irqflags;
351 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
352 if (dev->irq_enabled && (++dev_priv->user_irq_refcount == 1))
353 i915_enable_irq(dev_priv, I915_USER_INTERRUPT);
354 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
357 void i915_user_irq_put(struct drm_device *dev)
359 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
360 unsigned long irqflags;
362 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
363 BUG_ON(dev->irq_enabled && dev_priv->user_irq_refcount <= 0);
364 if (dev->irq_enabled && (--dev_priv->user_irq_refcount == 0))
365 i915_disable_irq(dev_priv, I915_USER_INTERRUPT);
366 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
369 static int i915_wait_irq(struct drm_device * dev, int irq_nr)
371 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
372 struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
375 DRM_DEBUG("irq_nr=%d breadcrumb=%d\n", irq_nr,
376 READ_BREADCRUMB(dev_priv));
378 if (READ_BREADCRUMB(dev_priv) >= irq_nr) {
379 if (master_priv->sarea_priv)
380 master_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
384 if (master_priv->sarea_priv)
385 master_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
387 i915_user_irq_get(dev);
388 DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
389 READ_BREADCRUMB(dev_priv) >= irq_nr);
390 i915_user_irq_put(dev);
393 DRM_ERROR("EBUSY -- rec: %d emitted: %d\n",
394 READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
400 /* Needs the lock as it touches the ring.
402 int i915_irq_emit(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_emit_t *emit = data;
409 if (!dev_priv || !dev_priv->ring.virtual_start) {
410 DRM_ERROR("called with no initialization\n");
414 RING_LOCK_TEST_WITH_RETURN(dev, file_priv);
416 mutex_lock(&dev->struct_mutex);
417 result = i915_emit_irq(dev);
418 mutex_unlock(&dev->struct_mutex);
420 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
421 DRM_ERROR("copy_to_user\n");
428 /* Doesn't need the hardware lock.
430 int i915_irq_wait(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_irq_wait_t *irqwait = data;
437 DRM_ERROR("called with no initialization\n");
441 return i915_wait_irq(dev, irqwait->irq_seq);
444 /* Called from drm generic code, passed 'crtc' which
445 * we use as a pipe index
447 int i915_enable_vblank(struct drm_device *dev, int pipe)
449 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
450 unsigned long irqflags;
451 int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
454 pipeconf = I915_READ(pipeconf_reg);
455 if (!(pipeconf & PIPEACONF_ENABLE))
458 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
460 i915_enable_pipestat(dev_priv, pipe,
461 PIPE_START_VBLANK_INTERRUPT_ENABLE);
463 i915_enable_pipestat(dev_priv, pipe,
464 PIPE_VBLANK_INTERRUPT_ENABLE);
465 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
469 /* Called from drm generic code, passed 'crtc' which
470 * we use as a pipe index
472 void i915_disable_vblank(struct drm_device *dev, int pipe)
474 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
475 unsigned long irqflags;
477 spin_lock_irqsave(&dev_priv->user_irq_lock, irqflags);
478 i915_disable_pipestat(dev_priv, pipe,
479 PIPE_VBLANK_INTERRUPT_ENABLE |
480 PIPE_START_VBLANK_INTERRUPT_ENABLE);
481 spin_unlock_irqrestore(&dev_priv->user_irq_lock, irqflags);
484 void i915_enable_interrupt (struct drm_device *dev)
486 struct drm_i915_private *dev_priv = dev->dev_private;
487 opregion_enable_asle(dev);
488 dev_priv->irq_enabled = 1;
492 /* Set the vblank monitor pipe
494 int i915_vblank_pipe_set(struct drm_device *dev, void *data,
495 struct drm_file *file_priv)
497 drm_i915_private_t *dev_priv = dev->dev_private;
500 DRM_ERROR("called with no initialization\n");
507 int i915_vblank_pipe_get(struct drm_device *dev, void *data,
508 struct drm_file *file_priv)
510 drm_i915_private_t *dev_priv = dev->dev_private;
511 drm_i915_vblank_pipe_t *pipe = data;
514 DRM_ERROR("called with no initialization\n");
518 pipe->pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
524 * Schedule buffer swap at given vertical blank.
526 int i915_vblank_swap(struct drm_device *dev, void *data,
527 struct drm_file *file_priv)
529 /* The delayed swap mechanism was fundamentally racy, and has been
530 * removed. The model was that the client requested a delayed flip/swap
531 * from the kernel, then waited for vblank before continuing to perform
532 * rendering. The problem was that the kernel might wake the client
533 * up before it dispatched the vblank swap (since the lock has to be
534 * held while touching the ringbuffer), in which case the client would
535 * clear and start the next frame before the swap occurred, and
536 * flicker would occur in addition to likely missing the vblank.
538 * In the absence of this ioctl, userland falls back to a correct path
539 * of waiting for a vblank, then dispatching the swap on its own.
540 * Context switching to userland and back is plenty fast enough for
541 * meeting the requirements of vblank swapping.
548 void i915_driver_irq_preinstall(struct drm_device * dev)
550 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
552 atomic_set(&dev_priv->irq_received, 0);
554 if (I915_HAS_HOTPLUG(dev)) {
555 I915_WRITE(PORT_HOTPLUG_EN, 0);
556 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
559 I915_WRITE(HWSTAM, 0xeffe);
560 I915_WRITE(PIPEASTAT, 0);
561 I915_WRITE(PIPEBSTAT, 0);
562 I915_WRITE(IMR, 0xffffffff);
563 I915_WRITE(IER, 0x0);
564 (void) I915_READ(IER);
565 INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
568 int i915_driver_irq_postinstall(struct drm_device *dev)
570 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
571 u32 enable_mask = I915_INTERRUPT_ENABLE_FIX | I915_INTERRUPT_ENABLE_VAR;
573 dev_priv->vblank_pipe = DRM_I915_VBLANK_PIPE_A | DRM_I915_VBLANK_PIPE_B;
575 dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
577 /* Unmask the interrupts that we always want on. */
578 dev_priv->irq_mask_reg = ~I915_INTERRUPT_ENABLE_FIX;
580 dev_priv->pipestat[0] = 0;
581 dev_priv->pipestat[1] = 0;
583 if (I915_HAS_HOTPLUG(dev)) {
584 u32 hotplug_en = I915_READ(PORT_HOTPLUG_EN);
586 /* Leave other bits alone */
587 hotplug_en |= HOTPLUG_EN_MASK;
588 I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
590 dev_priv->hotplug_supported_mask = CRT_HOTPLUG_INT_STATUS |
591 TV_HOTPLUG_INT_STATUS | SDVOC_HOTPLUG_INT_STATUS |
592 SDVOB_HOTPLUG_INT_STATUS;
594 dev_priv->hotplug_supported_mask |=
595 HDMIB_HOTPLUG_INT_STATUS |
596 HDMIC_HOTPLUG_INT_STATUS |
597 HDMID_HOTPLUG_INT_STATUS;
599 /* Enable in IER... */
600 enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
601 /* and unmask in IMR */
602 i915_enable_irq(dev_priv, I915_DISPLAY_PORT_INTERRUPT);
605 /* Disable pipe interrupt enables, clear pending pipe status */
606 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
607 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
608 /* Clear pending interrupt status */
609 I915_WRITE(IIR, I915_READ(IIR));
611 I915_WRITE(IER, enable_mask);
612 I915_WRITE(IMR, dev_priv->irq_mask_reg);
613 (void) I915_READ(IER);
615 opregion_enable_asle(dev);
616 DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
621 void i915_driver_irq_uninstall(struct drm_device * dev)
623 drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
628 dev_priv->vblank_pipe = 0;
630 if (I915_HAS_HOTPLUG(dev)) {
631 I915_WRITE(PORT_HOTPLUG_EN, 0);
632 I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
635 I915_WRITE(HWSTAM, 0xffffffff);
636 I915_WRITE(PIPEASTAT, 0);
637 I915_WRITE(PIPEBSTAT, 0);
638 I915_WRITE(IMR, 0xffffffff);
639 I915_WRITE(IER, 0x0);
641 I915_WRITE(PIPEASTAT, I915_READ(PIPEASTAT) & 0x8000ffff);
642 I915_WRITE(PIPEBSTAT, I915_READ(PIPEBSTAT) & 0x8000ffff);
643 I915_WRITE(IIR, I915_READ(IIR));