xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision b6b0fac04de9ae9b1559eddf8e9490f3c9a01885)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
41e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
45e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
46e5868a31SEgbert Eich };
47e5868a31SEgbert Eich 
48e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
56e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
63e5868a31SEgbert Eich };
64e5868a31SEgbert Eich 
65704cfb87SDaniel Vetter static const u32 hpd_status_g4x[] = {
66e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
72e5868a31SEgbert Eich };
73e5868a31SEgbert Eich 
74e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
75e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
81e5868a31SEgbert Eich };
82e5868a31SEgbert Eich 
83036a4a7dSZhenyu Wang /* For display hotplug interrupt */
84995b6762SChris Wilson static void
85f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
86036a4a7dSZhenyu Wang {
874bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
884bc9d430SDaniel Vetter 
89c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
90c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
91c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr &= ~mask;
92c67a470bSPaulo Zanoni 		return;
93c67a470bSPaulo Zanoni 	}
94c67a470bSPaulo Zanoni 
951ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
961ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
971ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
983143a2bfSChris Wilson 		POSTING_READ(DEIMR);
99036a4a7dSZhenyu Wang 	}
100036a4a7dSZhenyu Wang }
101036a4a7dSZhenyu Wang 
1020ff9800aSPaulo Zanoni static void
103f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
104036a4a7dSZhenyu Wang {
1054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1064bc9d430SDaniel Vetter 
107c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
108c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
109c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr |= mask;
110c67a470bSPaulo Zanoni 		return;
111c67a470bSPaulo Zanoni 	}
112c67a470bSPaulo Zanoni 
1131ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1141ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1151ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1163143a2bfSChris Wilson 		POSTING_READ(DEIMR);
117036a4a7dSZhenyu Wang 	}
118036a4a7dSZhenyu Wang }
119036a4a7dSZhenyu Wang 
12043eaea13SPaulo Zanoni /**
12143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
12243eaea13SPaulo Zanoni  * @dev_priv: driver private
12343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
12443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
12543eaea13SPaulo Zanoni  */
12643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
12743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
12843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
12943eaea13SPaulo Zanoni {
13043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
13143eaea13SPaulo Zanoni 
132c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
133c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
134c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr &= ~interrupt_mask;
135c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr |= (~enabled_irq_mask &
136c67a470bSPaulo Zanoni 						interrupt_mask);
137c67a470bSPaulo Zanoni 		return;
138c67a470bSPaulo Zanoni 	}
139c67a470bSPaulo Zanoni 
14043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
14143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
14243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
14343eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
14443eaea13SPaulo Zanoni }
14543eaea13SPaulo Zanoni 
14643eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
14743eaea13SPaulo Zanoni {
14843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
14943eaea13SPaulo Zanoni }
15043eaea13SPaulo Zanoni 
15143eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
15243eaea13SPaulo Zanoni {
15343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
15443eaea13SPaulo Zanoni }
15543eaea13SPaulo Zanoni 
156edbfdb45SPaulo Zanoni /**
157edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
158edbfdb45SPaulo Zanoni   * @dev_priv: driver private
159edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
160edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
161edbfdb45SPaulo Zanoni   */
162edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
163edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
164edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
165edbfdb45SPaulo Zanoni {
166605cd25bSPaulo Zanoni 	uint32_t new_val;
167edbfdb45SPaulo Zanoni 
168edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
169edbfdb45SPaulo Zanoni 
170c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
171c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
172c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr &= ~interrupt_mask;
173c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr |= (~enabled_irq_mask &
174c67a470bSPaulo Zanoni 						     interrupt_mask);
175c67a470bSPaulo Zanoni 		return;
176c67a470bSPaulo Zanoni 	}
177c67a470bSPaulo Zanoni 
178605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
179f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
180f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
181f52ecbcfSPaulo Zanoni 
182605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
183605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
184605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
185edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
186edbfdb45SPaulo Zanoni 	}
187f52ecbcfSPaulo Zanoni }
188edbfdb45SPaulo Zanoni 
189edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
190edbfdb45SPaulo Zanoni {
191edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
192edbfdb45SPaulo Zanoni }
193edbfdb45SPaulo Zanoni 
194edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
195edbfdb45SPaulo Zanoni {
196edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
197edbfdb45SPaulo Zanoni }
198edbfdb45SPaulo Zanoni 
1998664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2008664281bSPaulo Zanoni {
2018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2028664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2038664281bSPaulo Zanoni 	enum pipe pipe;
2048664281bSPaulo Zanoni 
2054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2064bc9d430SDaniel Vetter 
2078664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2088664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2098664281bSPaulo Zanoni 
2108664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2118664281bSPaulo Zanoni 			return false;
2128664281bSPaulo Zanoni 	}
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni 	return true;
2158664281bSPaulo Zanoni }
2168664281bSPaulo Zanoni 
2178664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2188664281bSPaulo Zanoni {
2198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2208664281bSPaulo Zanoni 	enum pipe pipe;
2218664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2228664281bSPaulo Zanoni 
223fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
224fee884edSDaniel Vetter 
2258664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2268664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2278664281bSPaulo Zanoni 
2288664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
2298664281bSPaulo Zanoni 			return false;
2308664281bSPaulo Zanoni 	}
2318664281bSPaulo Zanoni 
2328664281bSPaulo Zanoni 	return true;
2338664281bSPaulo Zanoni }
2348664281bSPaulo Zanoni 
2352d9d2b0bSVille Syrjälä static void i9xx_clear_fifo_underrun(struct drm_device *dev, enum pipe pipe)
2362d9d2b0bSVille Syrjälä {
2372d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
2382d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
2392d9d2b0bSVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
2402d9d2b0bSVille Syrjälä 
2412d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
2422d9d2b0bSVille Syrjälä 
2432d9d2b0bSVille Syrjälä 	I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
2442d9d2b0bSVille Syrjälä 	POSTING_READ(reg);
2452d9d2b0bSVille Syrjälä }
2462d9d2b0bSVille Syrjälä 
2478664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2488664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2498664281bSPaulo Zanoni {
2508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2518664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
2528664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
2538664281bSPaulo Zanoni 
2548664281bSPaulo Zanoni 	if (enable)
2558664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
2568664281bSPaulo Zanoni 	else
2578664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
2588664281bSPaulo Zanoni }
2598664281bSPaulo Zanoni 
2608664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
2617336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
2628664281bSPaulo Zanoni {
2638664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2648664281bSPaulo Zanoni 	if (enable) {
2657336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
2667336df65SDaniel Vetter 
2678664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
2688664281bSPaulo Zanoni 			return;
2698664281bSPaulo Zanoni 
2708664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
2718664281bSPaulo Zanoni 	} else {
2727336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
2737336df65SDaniel Vetter 
2747336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
2758664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
2767336df65SDaniel Vetter 
2777336df65SDaniel Vetter 		if (!was_enabled &&
2787336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
2797336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
2807336df65SDaniel Vetter 				      pipe_name(pipe));
2817336df65SDaniel Vetter 		}
2828664281bSPaulo Zanoni 	}
2838664281bSPaulo Zanoni }
2848664281bSPaulo Zanoni 
28538d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
28638d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
28738d83c96SDaniel Vetter {
28838d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
28938d83c96SDaniel Vetter 
29038d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
29138d83c96SDaniel Vetter 
29238d83c96SDaniel Vetter 	if (enable)
29338d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
29438d83c96SDaniel Vetter 	else
29538d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
29638d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
29738d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
29838d83c96SDaniel Vetter }
29938d83c96SDaniel Vetter 
300fee884edSDaniel Vetter /**
301fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
302fee884edSDaniel Vetter  * @dev_priv: driver private
303fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
304fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
305fee884edSDaniel Vetter  */
306fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
307fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
308fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
309fee884edSDaniel Vetter {
310fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
311fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
312fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
313fee884edSDaniel Vetter 
314fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
315fee884edSDaniel Vetter 
316c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
317c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
318c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
319c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
320c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
321c67a470bSPaulo Zanoni 						 interrupt_mask);
322c67a470bSPaulo Zanoni 		return;
323c67a470bSPaulo Zanoni 	}
324c67a470bSPaulo Zanoni 
325fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
326fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
327fee884edSDaniel Vetter }
328fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
329fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
330fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
331fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
332fee884edSDaniel Vetter 
333de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
334de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3358664281bSPaulo Zanoni 					    bool enable)
3368664281bSPaulo Zanoni {
3378664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
338de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
339de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3408664281bSPaulo Zanoni 
3418664281bSPaulo Zanoni 	if (enable)
342fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3438664281bSPaulo Zanoni 	else
344fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3458664281bSPaulo Zanoni }
3468664281bSPaulo Zanoni 
3478664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3488664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3498664281bSPaulo Zanoni 					    bool enable)
3508664281bSPaulo Zanoni {
3518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3528664281bSPaulo Zanoni 
3538664281bSPaulo Zanoni 	if (enable) {
3541dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3551dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3561dd246fbSDaniel Vetter 
3578664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3588664281bSPaulo Zanoni 			return;
3598664281bSPaulo Zanoni 
360fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3618664281bSPaulo Zanoni 	} else {
3621dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3631dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3641dd246fbSDaniel Vetter 
3651dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
366fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3671dd246fbSDaniel Vetter 
3681dd246fbSDaniel Vetter 		if (!was_enabled &&
3691dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3701dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3711dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3721dd246fbSDaniel Vetter 		}
3738664281bSPaulo Zanoni 	}
3748664281bSPaulo Zanoni }
3758664281bSPaulo Zanoni 
3768664281bSPaulo Zanoni /**
3778664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3788664281bSPaulo Zanoni  * @dev: drm device
3798664281bSPaulo Zanoni  * @pipe: pipe
3808664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3818664281bSPaulo Zanoni  *
3828664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3838664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3848664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3858664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3868664281bSPaulo Zanoni  * bit for all the pipes.
3878664281bSPaulo Zanoni  *
3888664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3898664281bSPaulo Zanoni  */
3908664281bSPaulo Zanoni bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
3918664281bSPaulo Zanoni 					   enum pipe pipe, bool enable)
3928664281bSPaulo Zanoni {
3938664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3948664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
3958664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3968664281bSPaulo Zanoni 	unsigned long flags;
3978664281bSPaulo Zanoni 	bool ret;
3988664281bSPaulo Zanoni 
3998664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4008664281bSPaulo Zanoni 
4018664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
4028664281bSPaulo Zanoni 
4038664281bSPaulo Zanoni 	if (enable == ret)
4048664281bSPaulo Zanoni 		goto done;
4058664281bSPaulo Zanoni 
4068664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4078664281bSPaulo Zanoni 
4082d9d2b0bSVille Syrjälä 	if (enable && (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev)))
4092d9d2b0bSVille Syrjälä 		i9xx_clear_fifo_underrun(dev, pipe);
4102d9d2b0bSVille Syrjälä 	else if (IS_GEN5(dev) || IS_GEN6(dev))
4118664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
4128664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
4137336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
41438d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
41538d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
4168664281bSPaulo Zanoni 
4178664281bSPaulo Zanoni done:
4188664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4198664281bSPaulo Zanoni 	return ret;
4208664281bSPaulo Zanoni }
4218664281bSPaulo Zanoni 
4228664281bSPaulo Zanoni /**
4238664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
4248664281bSPaulo Zanoni  * @dev: drm device
4258664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
4268664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4278664281bSPaulo Zanoni  *
4288664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
4298664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
4308664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
4318664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4328664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4338664281bSPaulo Zanoni  *
4348664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4358664281bSPaulo Zanoni  */
4368664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4378664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4388664281bSPaulo Zanoni 					   bool enable)
4398664281bSPaulo Zanoni {
4408664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
441de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
442de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4438664281bSPaulo Zanoni 	unsigned long flags;
4448664281bSPaulo Zanoni 	bool ret;
4458664281bSPaulo Zanoni 
446de28075dSDaniel Vetter 	/*
447de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
448de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
449de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
450de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
451de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
452de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
453de28075dSDaniel Vetter 	 */
4548664281bSPaulo Zanoni 
4558664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4568664281bSPaulo Zanoni 
4578664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4588664281bSPaulo Zanoni 
4598664281bSPaulo Zanoni 	if (enable == ret)
4608664281bSPaulo Zanoni 		goto done;
4618664281bSPaulo Zanoni 
4628664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4638664281bSPaulo Zanoni 
4648664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
465de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4668664281bSPaulo Zanoni 	else
4678664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4688664281bSPaulo Zanoni 
4698664281bSPaulo Zanoni done:
4708664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4718664281bSPaulo Zanoni 	return ret;
4728664281bSPaulo Zanoni }
4738664281bSPaulo Zanoni 
4748664281bSPaulo Zanoni 
4757c463586SKeith Packard void
4763b6c42e8SDaniel Vetter i915_enable_pipestat(drm_i915_private_t *dev_priv, enum pipe pipe, u32 mask)
4777c463586SKeith Packard {
4789db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
47946c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4807c463586SKeith Packard 
481b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
482b79480baSDaniel Vetter 
48346c06a30SVille Syrjälä 	if ((pipestat & mask) == mask)
48446c06a30SVille Syrjälä 		return;
48546c06a30SVille Syrjälä 
4867c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
48746c06a30SVille Syrjälä 	pipestat |= mask | (mask >> 16);
48846c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4893143a2bfSChris Wilson 	POSTING_READ(reg);
4907c463586SKeith Packard }
4917c463586SKeith Packard 
4927c463586SKeith Packard void
4933b6c42e8SDaniel Vetter i915_disable_pipestat(drm_i915_private_t *dev_priv, enum pipe pipe, u32 mask)
4947c463586SKeith Packard {
4959db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
49646c06a30SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
4977c463586SKeith Packard 
498b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
499b79480baSDaniel Vetter 
50046c06a30SVille Syrjälä 	if ((pipestat & mask) == 0)
50146c06a30SVille Syrjälä 		return;
50246c06a30SVille Syrjälä 
50346c06a30SVille Syrjälä 	pipestat &= ~mask;
50446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5053143a2bfSChris Wilson 	POSTING_READ(reg);
5067c463586SKeith Packard }
5077c463586SKeith Packard 
508c0e09200SDave Airlie /**
509f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
51001c66889SZhao Yakui  */
511f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
51201c66889SZhao Yakui {
5131ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
5141ec14ad3SChris Wilson 	unsigned long irqflags;
5151ec14ad3SChris Wilson 
516f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
517f49e38ddSJani Nikula 		return;
518f49e38ddSJani Nikula 
5191ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
52001c66889SZhao Yakui 
5213b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_ENABLE);
522a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5233b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
5243b6c42e8SDaniel Vetter 				     PIPE_LEGACY_BLC_EVENT_ENABLE);
5251ec14ad3SChris Wilson 
5261ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
52701c66889SZhao Yakui }
52801c66889SZhao Yakui 
52901c66889SZhao Yakui /**
5300a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
5310a3e67a4SJesse Barnes  * @dev: DRM device
5320a3e67a4SJesse Barnes  * @pipe: pipe to check
5330a3e67a4SJesse Barnes  *
5340a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
5350a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
5360a3e67a4SJesse Barnes  * before reading such registers if unsure.
5370a3e67a4SJesse Barnes  */
5380a3e67a4SJesse Barnes static int
5390a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
5400a3e67a4SJesse Barnes {
5410a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
542702e7a56SPaulo Zanoni 
543a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
544a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
545a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
546a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
54771f8ba6bSPaulo Zanoni 
548a01025afSDaniel Vetter 		return intel_crtc->active;
549a01025afSDaniel Vetter 	} else {
550a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
551a01025afSDaniel Vetter 	}
5520a3e67a4SJesse Barnes }
5530a3e67a4SJesse Barnes 
5544cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5554cdb83ecSVille Syrjälä {
5564cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5574cdb83ecSVille Syrjälä 	return 0;
5584cdb83ecSVille Syrjälä }
5594cdb83ecSVille Syrjälä 
56042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
56142f52ef8SKeith Packard  * we use as a pipe index
56242f52ef8SKeith Packard  */
563f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5640a3e67a4SJesse Barnes {
5650a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
5660a3e67a4SJesse Barnes 	unsigned long high_frame;
5670a3e67a4SJesse Barnes 	unsigned long low_frame;
568391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
5690a3e67a4SJesse Barnes 
5700a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
57144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5729db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5730a3e67a4SJesse Barnes 		return 0;
5740a3e67a4SJesse Barnes 	}
5750a3e67a4SJesse Barnes 
576391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
577391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
578391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
579391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
580391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
581391f75e2SVille Syrjälä 
582391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
583391f75e2SVille Syrjälä 	} else {
584391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
585391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
586391f75e2SVille Syrjälä 		u32 htotal;
587391f75e2SVille Syrjälä 
588391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
589391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
590391f75e2SVille Syrjälä 
591391f75e2SVille Syrjälä 		vbl_start *= htotal;
592391f75e2SVille Syrjälä 	}
593391f75e2SVille Syrjälä 
5949db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5959db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5965eddb70bSChris Wilson 
5970a3e67a4SJesse Barnes 	/*
5980a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5990a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6000a3e67a4SJesse Barnes 	 * register.
6010a3e67a4SJesse Barnes 	 */
6020a3e67a4SJesse Barnes 	do {
6035eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
604391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6055eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6060a3e67a4SJesse Barnes 	} while (high1 != high2);
6070a3e67a4SJesse Barnes 
6085eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
609391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6105eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
611391f75e2SVille Syrjälä 
612391f75e2SVille Syrjälä 	/*
613391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
614391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
615391f75e2SVille Syrjälä 	 * counter against vblank start.
616391f75e2SVille Syrjälä 	 */
617edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6180a3e67a4SJesse Barnes }
6190a3e67a4SJesse Barnes 
620f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6219880b7a5SJesse Barnes {
6229880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6239db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6249880b7a5SJesse Barnes 
6259880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
62644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
6279db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6289880b7a5SJesse Barnes 		return 0;
6299880b7a5SJesse Barnes 	}
6309880b7a5SJesse Barnes 
6319880b7a5SJesse Barnes 	return I915_READ(reg);
6329880b7a5SJesse Barnes }
6339880b7a5SJesse Barnes 
634ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
635ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
636ad3543edSMario Kleiner #define __raw_i915_read16(dev_priv__, reg__) readw((dev_priv__)->regs + (reg__))
637ad3543edSMario Kleiner 
638095163baSVille Syrjälä static bool ilk_pipe_in_vblank_locked(struct drm_device *dev, enum pipe pipe)
63954ddcbd2SVille Syrjälä {
64054ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
64154ddcbd2SVille Syrjälä 	uint32_t status;
64254ddcbd2SVille Syrjälä 
643095163baSVille Syrjälä 	if (INTEL_INFO(dev)->gen < 7) {
64454ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
64554ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
64654ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
64754ddcbd2SVille Syrjälä 	} else {
64854ddcbd2SVille Syrjälä 		switch (pipe) {
64954ddcbd2SVille Syrjälä 		default:
65054ddcbd2SVille Syrjälä 		case PIPE_A:
65154ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
65254ddcbd2SVille Syrjälä 			break;
65354ddcbd2SVille Syrjälä 		case PIPE_B:
65454ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
65554ddcbd2SVille Syrjälä 			break;
65654ddcbd2SVille Syrjälä 		case PIPE_C:
65754ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
65854ddcbd2SVille Syrjälä 			break;
65954ddcbd2SVille Syrjälä 		}
66054ddcbd2SVille Syrjälä 	}
661ad3543edSMario Kleiner 
662095163baSVille Syrjälä 	return __raw_i915_read32(dev_priv, DEISR) & status;
66354ddcbd2SVille Syrjälä }
66454ddcbd2SVille Syrjälä 
665f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
666abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
667abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6680af7e4dfSMario Kleiner {
669c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
670c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
671c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
672c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6733aa18df8SVille Syrjälä 	int position;
6740af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
6750af7e4dfSMario Kleiner 	bool in_vbl = true;
6760af7e4dfSMario Kleiner 	int ret = 0;
677ad3543edSMario Kleiner 	unsigned long irqflags;
6780af7e4dfSMario Kleiner 
679c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6800af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6819db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6820af7e4dfSMario Kleiner 		return 0;
6830af7e4dfSMario Kleiner 	}
6840af7e4dfSMario Kleiner 
685c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
686c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
687c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
688c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6890af7e4dfSMario Kleiner 
690d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
691d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
692d31faf65SVille Syrjälä 		vbl_end /= 2;
693d31faf65SVille Syrjälä 		vtotal /= 2;
694d31faf65SVille Syrjälä 	}
695d31faf65SVille Syrjälä 
696c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
697c2baf4b7SVille Syrjälä 
698ad3543edSMario Kleiner 	/*
699ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
700ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
701ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
702ad3543edSMario Kleiner 	 */
703ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
704ad3543edSMario Kleiner 
705ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
706ad3543edSMario Kleiner 
707ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
708ad3543edSMario Kleiner 	if (stime)
709ad3543edSMario Kleiner 		*stime = ktime_get();
710ad3543edSMario Kleiner 
7117c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7120af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7130af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7140af7e4dfSMario Kleiner 		 */
7157c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
716ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
7177c06b08aSVille Syrjälä 		else
718ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
71954ddcbd2SVille Syrjälä 
720095163baSVille Syrjälä 		if (HAS_PCH_SPLIT(dev)) {
72154ddcbd2SVille Syrjälä 			/*
72254ddcbd2SVille Syrjälä 			 * The scanline counter increments at the leading edge
72354ddcbd2SVille Syrjälä 			 * of hsync, ie. it completely misses the active portion
72454ddcbd2SVille Syrjälä 			 * of the line. Fix up the counter at both edges of vblank
72554ddcbd2SVille Syrjälä 			 * to get a more accurate picture whether we're in vblank
72654ddcbd2SVille Syrjälä 			 * or not.
72754ddcbd2SVille Syrjälä 			 */
728095163baSVille Syrjälä 			in_vbl = ilk_pipe_in_vblank_locked(dev, pipe);
72954ddcbd2SVille Syrjälä 			if ((in_vbl && position == vbl_start - 1) ||
73054ddcbd2SVille Syrjälä 			    (!in_vbl && position == vbl_end - 1))
73154ddcbd2SVille Syrjälä 				position = (position + 1) % vtotal;
7320af7e4dfSMario Kleiner 		} else {
733095163baSVille Syrjälä 			/*
734095163baSVille Syrjälä 			 * ISR vblank status bits don't work the way we'd want
735095163baSVille Syrjälä 			 * them to work on non-PCH platforms (for
736095163baSVille Syrjälä 			 * ilk_pipe_in_vblank_locked()), and there doesn't
737095163baSVille Syrjälä 			 * appear any other way to determine if we're currently
738095163baSVille Syrjälä 			 * in vblank.
739095163baSVille Syrjälä 			 *
740095163baSVille Syrjälä 			 * Instead let's assume that we're already in vblank if
741095163baSVille Syrjälä 			 * we got called from the vblank interrupt and the
742095163baSVille Syrjälä 			 * scanline counter value indicates that we're on the
743095163baSVille Syrjälä 			 * line just prior to vblank start. This should result
744095163baSVille Syrjälä 			 * in the correct answer, unless the vblank interrupt
745095163baSVille Syrjälä 			 * delivery really got delayed for almost exactly one
746095163baSVille Syrjälä 			 * full frame/field.
747095163baSVille Syrjälä 			 */
748095163baSVille Syrjälä 			if (flags & DRM_CALLED_FROM_VBLIRQ &&
749095163baSVille Syrjälä 			    position == vbl_start - 1) {
750095163baSVille Syrjälä 				position = (position + 1) % vtotal;
751095163baSVille Syrjälä 
752095163baSVille Syrjälä 				/* Signal this correction as "applied". */
753095163baSVille Syrjälä 				ret |= 0x8;
754095163baSVille Syrjälä 			}
755095163baSVille Syrjälä 		}
756095163baSVille Syrjälä 	} else {
7570af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
7580af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
7590af7e4dfSMario Kleiner 		 * scanout position.
7600af7e4dfSMario Kleiner 		 */
761ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7620af7e4dfSMario Kleiner 
7633aa18df8SVille Syrjälä 		/* convert to pixel counts */
7643aa18df8SVille Syrjälä 		vbl_start *= htotal;
7653aa18df8SVille Syrjälä 		vbl_end *= htotal;
7663aa18df8SVille Syrjälä 		vtotal *= htotal;
7673aa18df8SVille Syrjälä 	}
7683aa18df8SVille Syrjälä 
769ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
770ad3543edSMario Kleiner 	if (etime)
771ad3543edSMario Kleiner 		*etime = ktime_get();
772ad3543edSMario Kleiner 
773ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
774ad3543edSMario Kleiner 
775ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
776ad3543edSMario Kleiner 
7773aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7783aa18df8SVille Syrjälä 
7793aa18df8SVille Syrjälä 	/*
7803aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7813aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7823aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7833aa18df8SVille Syrjälä 	 * up since vbl_end.
7843aa18df8SVille Syrjälä 	 */
7853aa18df8SVille Syrjälä 	if (position >= vbl_start)
7863aa18df8SVille Syrjälä 		position -= vbl_end;
7873aa18df8SVille Syrjälä 	else
7883aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7893aa18df8SVille Syrjälä 
7907c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7913aa18df8SVille Syrjälä 		*vpos = position;
7923aa18df8SVille Syrjälä 		*hpos = 0;
7933aa18df8SVille Syrjälä 	} else {
7940af7e4dfSMario Kleiner 		*vpos = position / htotal;
7950af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7960af7e4dfSMario Kleiner 	}
7970af7e4dfSMario Kleiner 
7980af7e4dfSMario Kleiner 	/* In vblank? */
7990af7e4dfSMario Kleiner 	if (in_vbl)
8000af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
8010af7e4dfSMario Kleiner 
8020af7e4dfSMario Kleiner 	return ret;
8030af7e4dfSMario Kleiner }
8040af7e4dfSMario Kleiner 
805f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8060af7e4dfSMario Kleiner 			      int *max_error,
8070af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8080af7e4dfSMario Kleiner 			      unsigned flags)
8090af7e4dfSMario Kleiner {
8104041b853SChris Wilson 	struct drm_crtc *crtc;
8110af7e4dfSMario Kleiner 
8127eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8134041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8140af7e4dfSMario Kleiner 		return -EINVAL;
8150af7e4dfSMario Kleiner 	}
8160af7e4dfSMario Kleiner 
8170af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
8184041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
8194041b853SChris Wilson 	if (crtc == NULL) {
8204041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8214041b853SChris Wilson 		return -EINVAL;
8224041b853SChris Wilson 	}
8234041b853SChris Wilson 
8244041b853SChris Wilson 	if (!crtc->enabled) {
8254041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8264041b853SChris Wilson 		return -EBUSY;
8274041b853SChris Wilson 	}
8280af7e4dfSMario Kleiner 
8290af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8304041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8314041b853SChris Wilson 						     vblank_time, flags,
8327da903efSVille Syrjälä 						     crtc,
8337da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
8340af7e4dfSMario Kleiner }
8350af7e4dfSMario Kleiner 
83667c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
83767c347ffSJani Nikula 				struct drm_connector *connector)
838321a1b30SEgbert Eich {
839321a1b30SEgbert Eich 	enum drm_connector_status old_status;
840321a1b30SEgbert Eich 
841321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
842321a1b30SEgbert Eich 	old_status = connector->status;
843321a1b30SEgbert Eich 
844321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
84567c347ffSJani Nikula 	if (old_status == connector->status)
84667c347ffSJani Nikula 		return false;
84767c347ffSJani Nikula 
84867c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
849321a1b30SEgbert Eich 		      connector->base.id,
850321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
85167c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
85267c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
85367c347ffSJani Nikula 
85467c347ffSJani Nikula 	return true;
855321a1b30SEgbert Eich }
856321a1b30SEgbert Eich 
8575ca58282SJesse Barnes /*
8585ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8595ca58282SJesse Barnes  */
860ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
861ac4c16c5SEgbert Eich 
8625ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8635ca58282SJesse Barnes {
8645ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
8655ca58282SJesse Barnes 						    hotplug_work);
8665ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
867c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
868cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
869cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
870cd569aedSEgbert Eich 	struct drm_connector *connector;
871cd569aedSEgbert Eich 	unsigned long irqflags;
872cd569aedSEgbert Eich 	bool hpd_disabled = false;
873321a1b30SEgbert Eich 	bool changed = false;
874142e2398SEgbert Eich 	u32 hpd_event_bits;
8755ca58282SJesse Barnes 
87652d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
87752d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
87852d7ecedSDaniel Vetter 		return;
87952d7ecedSDaniel Vetter 
880a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
881e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
882e67189abSJesse Barnes 
883cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
884142e2398SEgbert Eich 
885142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
886142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
887cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
888cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
889cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
890cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
891cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
892cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
893cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
894cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
895cd569aedSEgbert Eich 				drm_get_connector_name(connector));
896cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
897cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
898cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
899cd569aedSEgbert Eich 			hpd_disabled = true;
900cd569aedSEgbert Eich 		}
901142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
902142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
903142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
904142e2398SEgbert Eich 		}
905cd569aedSEgbert Eich 	}
906cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
907cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
908cd569aedSEgbert Eich 	  * some connectors */
909ac4c16c5SEgbert Eich 	if (hpd_disabled) {
910cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
911ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
912ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
913ac4c16c5SEgbert Eich 	}
914cd569aedSEgbert Eich 
915cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
916cd569aedSEgbert Eich 
917321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
918321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
919321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
920321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
921cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
922cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
923321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
924321a1b30SEgbert Eich 				changed = true;
925321a1b30SEgbert Eich 		}
926321a1b30SEgbert Eich 	}
92740ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
92840ee3381SKeith Packard 
929321a1b30SEgbert Eich 	if (changed)
930321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9315ca58282SJesse Barnes }
9325ca58282SJesse Barnes 
9333ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
9343ca1ccedSVille Syrjälä {
9353ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
9363ca1ccedSVille Syrjälä }
9373ca1ccedSVille Syrjälä 
938d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
939f97108d1SJesse Barnes {
940f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
941b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9429270388eSDaniel Vetter 	u8 new_delay;
9439270388eSDaniel Vetter 
944d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
945f97108d1SJesse Barnes 
94673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94773edd18fSDaniel Vetter 
94820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9499270388eSDaniel Vetter 
9507648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
951b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
952b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
953f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
954f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
955f97108d1SJesse Barnes 
956f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
957b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
95920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
96020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
96120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
962b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
967f97108d1SJesse Barnes 	}
968f97108d1SJesse Barnes 
9697648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
97020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
971f97108d1SJesse Barnes 
972d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9739270388eSDaniel Vetter 
974f97108d1SJesse Barnes 	return;
975f97108d1SJesse Barnes }
976f97108d1SJesse Barnes 
977549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
978549f7365SChris Wilson 			struct intel_ring_buffer *ring)
979549f7365SChris Wilson {
980475553deSChris Wilson 	if (ring->obj == NULL)
981475553deSChris Wilson 		return;
982475553deSChris Wilson 
983814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9849862e600SChris Wilson 
985549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
98610cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
987549f7365SChris Wilson }
988549f7365SChris Wilson 
98927544369SDeepak S static void gen6_set_pm_mask(struct drm_i915_private *dev_priv,
99027544369SDeepak S 			     u32 pm_iir, int new_delay)
99127544369SDeepak S {
99227544369SDeepak S 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
99327544369SDeepak S 		if (new_delay >= dev_priv->rps.max_delay) {
99427544369SDeepak S 			/* Mask UP THRESHOLD Interrupts */
99527544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
99627544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
99727544369SDeepak S 				   GEN6_PM_RP_UP_THRESHOLD);
99827544369SDeepak S 			dev_priv->rps.rp_up_masked = true;
99927544369SDeepak S 		}
100027544369SDeepak S 		if (dev_priv->rps.rp_down_masked) {
100127544369SDeepak S 			/* UnMask DOWN THRESHOLD Interrupts */
100227544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
100327544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
100427544369SDeepak S 				   ~GEN6_PM_RP_DOWN_THRESHOLD);
100527544369SDeepak S 			dev_priv->rps.rp_down_masked = false;
100627544369SDeepak S 		}
100727544369SDeepak S 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
100827544369SDeepak S 		if (new_delay <= dev_priv->rps.min_delay) {
100927544369SDeepak S 			/* Mask DOWN THRESHOLD Interrupts */
101027544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
101127544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
101227544369SDeepak S 				   GEN6_PM_RP_DOWN_THRESHOLD);
101327544369SDeepak S 			dev_priv->rps.rp_down_masked = true;
101427544369SDeepak S 		}
101527544369SDeepak S 
101627544369SDeepak S 		if (dev_priv->rps.rp_up_masked) {
101727544369SDeepak S 			/* UnMask UP THRESHOLD Interrupts */
101827544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
101927544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
102027544369SDeepak S 				   ~GEN6_PM_RP_UP_THRESHOLD);
102127544369SDeepak S 			dev_priv->rps.rp_up_masked = false;
102227544369SDeepak S 		}
102327544369SDeepak S 	}
102427544369SDeepak S }
102527544369SDeepak S 
10264912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10273b8d8d91SJesse Barnes {
10284912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1029c6a828d3SDaniel Vetter 						    rps.work);
1030edbfdb45SPaulo Zanoni 	u32 pm_iir;
1031dd75fdc8SChris Wilson 	int new_delay, adj;
10323b8d8d91SJesse Barnes 
103359cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1034c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1035c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
10364848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
1037edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
103859cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10394912d041SBen Widawsky 
104060611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
104160611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
104260611c13SPaulo Zanoni 
10434848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
10443b8d8d91SJesse Barnes 		return;
10453b8d8d91SJesse Barnes 
10464fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10477b9e0ae6SChris Wilson 
1048dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
10497425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1050dd75fdc8SChris Wilson 		if (adj > 0)
1051dd75fdc8SChris Wilson 			adj *= 2;
1052dd75fdc8SChris Wilson 		else
1053dd75fdc8SChris Wilson 			adj = 1;
1054dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
10557425034aSVille Syrjälä 
10567425034aSVille Syrjälä 		/*
10577425034aSVille Syrjälä 		 * For better performance, jump directly
10587425034aSVille Syrjälä 		 * to RPe if we're below it.
10597425034aSVille Syrjälä 		 */
1060dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
10617425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
1062dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1063dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
1064dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
1065dd75fdc8SChris Wilson 		else
1066dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
1067dd75fdc8SChris Wilson 		adj = 0;
1068dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1069dd75fdc8SChris Wilson 		if (adj < 0)
1070dd75fdc8SChris Wilson 			adj *= 2;
1071dd75fdc8SChris Wilson 		else
1072dd75fdc8SChris Wilson 			adj = -1;
1073dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
1074dd75fdc8SChris Wilson 	} else { /* unknown event */
1075dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
1076dd75fdc8SChris Wilson 	}
10773b8d8d91SJesse Barnes 
107879249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
107979249636SBen Widawsky 	 * interrupt
108079249636SBen Widawsky 	 */
10811272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
10821272e7b8SVille Syrjälä 			    dev_priv->rps.min_delay, dev_priv->rps.max_delay);
108327544369SDeepak S 
108427544369SDeepak S 	gen6_set_pm_mask(dev_priv, pm_iir, new_delay);
1085dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
1086dd75fdc8SChris Wilson 
10870a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
10880a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
10890a073b84SJesse Barnes 	else
10904912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
10913b8d8d91SJesse Barnes 
10924fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
10933b8d8d91SJesse Barnes }
10943b8d8d91SJesse Barnes 
1095e3689190SBen Widawsky 
1096e3689190SBen Widawsky /**
1097e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1098e3689190SBen Widawsky  * occurred.
1099e3689190SBen Widawsky  * @work: workqueue struct
1100e3689190SBen Widawsky  *
1101e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1102e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1103e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1104e3689190SBen Widawsky  */
1105e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1106e3689190SBen Widawsky {
1107e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1108a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
1109e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
111035a85ac6SBen Widawsky 	char *parity_event[6];
1111e3689190SBen Widawsky 	uint32_t misccpctl;
1112e3689190SBen Widawsky 	unsigned long flags;
111335a85ac6SBen Widawsky 	uint8_t slice = 0;
1114e3689190SBen Widawsky 
1115e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1116e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1117e3689190SBen Widawsky 	 * any time we access those registers.
1118e3689190SBen Widawsky 	 */
1119e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1120e3689190SBen Widawsky 
112135a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
112235a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
112335a85ac6SBen Widawsky 		goto out;
112435a85ac6SBen Widawsky 
1125e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1126e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1127e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1128e3689190SBen Widawsky 
112935a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
113035a85ac6SBen Widawsky 		u32 reg;
113135a85ac6SBen Widawsky 
113235a85ac6SBen Widawsky 		slice--;
113335a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
113435a85ac6SBen Widawsky 			break;
113535a85ac6SBen Widawsky 
113635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
113735a85ac6SBen Widawsky 
113835a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
113935a85ac6SBen Widawsky 
114035a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1141e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1142e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1143e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1144e3689190SBen Widawsky 
114535a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
114635a85ac6SBen Widawsky 		POSTING_READ(reg);
1147e3689190SBen Widawsky 
1148cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1149e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1150e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1151e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
115235a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
115335a85ac6SBen Widawsky 		parity_event[5] = NULL;
1154e3689190SBen Widawsky 
11555bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1156e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1157e3689190SBen Widawsky 
115835a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
115935a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1160e3689190SBen Widawsky 
116135a85ac6SBen Widawsky 		kfree(parity_event[4]);
1162e3689190SBen Widawsky 		kfree(parity_event[3]);
1163e3689190SBen Widawsky 		kfree(parity_event[2]);
1164e3689190SBen Widawsky 		kfree(parity_event[1]);
1165e3689190SBen Widawsky 	}
1166e3689190SBen Widawsky 
116735a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
116835a85ac6SBen Widawsky 
116935a85ac6SBen Widawsky out:
117035a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
117135a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
117235a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
117335a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
117435a85ac6SBen Widawsky 
117535a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
117635a85ac6SBen Widawsky }
117735a85ac6SBen Widawsky 
117835a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1179e3689190SBen Widawsky {
1180e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1181e3689190SBen Widawsky 
1182040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1183e3689190SBen Widawsky 		return;
1184e3689190SBen Widawsky 
1185d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
118635a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1187d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1188e3689190SBen Widawsky 
118935a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
119035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
119135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
119235a85ac6SBen Widawsky 
119335a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
119435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
119535a85ac6SBen Widawsky 
1196a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1197e3689190SBen Widawsky }
1198e3689190SBen Widawsky 
1199f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1200f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1201f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1202f1af8fc1SPaulo Zanoni {
1203f1af8fc1SPaulo Zanoni 	if (gt_iir &
1204f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1205f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1206f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1207f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1208f1af8fc1SPaulo Zanoni }
1209f1af8fc1SPaulo Zanoni 
1210e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1211e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1212e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1213e7b4c6b1SDaniel Vetter {
1214e7b4c6b1SDaniel Vetter 
1215cc609d5dSBen Widawsky 	if (gt_iir &
1216cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1217e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1218cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1219e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1220cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1221e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1222e7b4c6b1SDaniel Vetter 
1223cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1224cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1225cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
1226e7b4c6b1SDaniel Vetter 		DRM_ERROR("GT error interrupt 0x%08x\n", gt_iir);
1227e7b4c6b1SDaniel Vetter 		i915_handle_error(dev, false);
1228e7b4c6b1SDaniel Vetter 	}
1229e3689190SBen Widawsky 
123035a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
123135a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1232e7b4c6b1SDaniel Vetter }
1233e7b4c6b1SDaniel Vetter 
1234abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1235abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1236abd58f01SBen Widawsky 				       u32 master_ctl)
1237abd58f01SBen Widawsky {
1238abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1239abd58f01SBen Widawsky 	uint32_t tmp = 0;
1240abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1241abd58f01SBen Widawsky 
1242abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1243abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1244abd58f01SBen Widawsky 		if (tmp) {
1245abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1246abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1247abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1248abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1249abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1250abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1251abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1252abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1253abd58f01SBen Widawsky 		} else
1254abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1255abd58f01SBen Widawsky 	}
1256abd58f01SBen Widawsky 
1257abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VCS1_IRQ) {
1258abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1259abd58f01SBen Widawsky 		if (tmp) {
1260abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1261abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1262abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1263abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
1264abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1265abd58f01SBen Widawsky 		} else
1266abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1267abd58f01SBen Widawsky 	}
1268abd58f01SBen Widawsky 
1269abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1270abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1271abd58f01SBen Widawsky 		if (tmp) {
1272abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1273abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1274abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1275abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1276abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1277abd58f01SBen Widawsky 		} else
1278abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1279abd58f01SBen Widawsky 	}
1280abd58f01SBen Widawsky 
1281abd58f01SBen Widawsky 	return ret;
1282abd58f01SBen Widawsky }
1283abd58f01SBen Widawsky 
1284b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1285b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1286b543fb04SEgbert Eich 
128710a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1288b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1289b543fb04SEgbert Eich 					 const u32 *hpd)
1290b543fb04SEgbert Eich {
1291b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1292b543fb04SEgbert Eich 	int i;
129310a504deSDaniel Vetter 	bool storm_detected = false;
1294b543fb04SEgbert Eich 
129591d131d2SDaniel Vetter 	if (!hotplug_trigger)
129691d131d2SDaniel Vetter 		return;
129791d131d2SDaniel Vetter 
1298cc9bd499SImre Deak 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1299cc9bd499SImre Deak 			  hotplug_trigger);
1300cc9bd499SImre Deak 
1301b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1302b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1303821450c6SEgbert Eich 
13043432087eSChris Wilson 		WARN_ONCE(hpd[i] & hotplug_trigger &&
13058b5565b8SChris Wilson 			  dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED,
1306cba1c073SChris Wilson 			  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1307cba1c073SChris Wilson 			  hotplug_trigger, i, hpd[i]);
1308b8f102e8SEgbert Eich 
1309b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1310b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1311b543fb04SEgbert Eich 			continue;
1312b543fb04SEgbert Eich 
1313bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1314b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1315b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1316b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1317b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1318b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1319b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1320b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1321b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1322142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1323b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
132410a504deSDaniel Vetter 			storm_detected = true;
1325b543fb04SEgbert Eich 		} else {
1326b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1327b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1328b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1329b543fb04SEgbert Eich 		}
1330b543fb04SEgbert Eich 	}
1331b543fb04SEgbert Eich 
133210a504deSDaniel Vetter 	if (storm_detected)
133310a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1334b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
13355876fa0dSDaniel Vetter 
1336645416f5SDaniel Vetter 	/*
1337645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1338645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1339645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1340645416f5SDaniel Vetter 	 * deadlock.
1341645416f5SDaniel Vetter 	 */
1342645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1343b543fb04SEgbert Eich }
1344b543fb04SEgbert Eich 
1345515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1346515ac2bbSDaniel Vetter {
134728c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
134828c70f16SDaniel Vetter 
134928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1350515ac2bbSDaniel Vetter }
1351515ac2bbSDaniel Vetter 
1352ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1353ce99c256SDaniel Vetter {
13549ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
13559ee32feaSDaniel Vetter 
13569ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1357ce99c256SDaniel Vetter }
1358ce99c256SDaniel Vetter 
13598bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1360277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1361eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1362eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
13638bc5e955SDaniel Vetter 					 uint32_t crc4)
13648bf1e9f1SShuang He {
13658bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
13668bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
13678bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1368ac2300d4SDamien Lespiau 	int head, tail;
1369b2c88f5bSDamien Lespiau 
1370d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1371d538bbdfSDamien Lespiau 
13720c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1373d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
13740c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
13750c912c79SDamien Lespiau 		return;
13760c912c79SDamien Lespiau 	}
13770c912c79SDamien Lespiau 
1378d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1379d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1380b2c88f5bSDamien Lespiau 
1381b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1382d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1383b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1384b2c88f5bSDamien Lespiau 		return;
1385b2c88f5bSDamien Lespiau 	}
1386b2c88f5bSDamien Lespiau 
1387b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
13888bf1e9f1SShuang He 
13898bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1390eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1391eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1392eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1393eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1394eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1395b2c88f5bSDamien Lespiau 
1396b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1397d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1398d538bbdfSDamien Lespiau 
1399d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
140007144428SDamien Lespiau 
140107144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
14028bf1e9f1SShuang He }
1403277de95eSDaniel Vetter #else
1404277de95eSDaniel Vetter static inline void
1405277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1406277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1407277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1408277de95eSDaniel Vetter 			     uint32_t crc4) {}
1409277de95eSDaniel Vetter #endif
1410eba94eb9SDaniel Vetter 
1411277de95eSDaniel Vetter 
1412277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14135a69b89fSDaniel Vetter {
14145a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14155a69b89fSDaniel Vetter 
1416277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14175a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
14185a69b89fSDaniel Vetter 				     0, 0, 0, 0);
14195a69b89fSDaniel Vetter }
14205a69b89fSDaniel Vetter 
1421277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1422eba94eb9SDaniel Vetter {
1423eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1424eba94eb9SDaniel Vetter 
1425277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1426eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1427eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1428eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1429eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
14308bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1431eba94eb9SDaniel Vetter }
14325b3a856bSDaniel Vetter 
1433277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14345b3a856bSDaniel Vetter {
14355b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14360b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
14370b5c5ed0SDaniel Vetter 
14380b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
14390b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
14400b5c5ed0SDaniel Vetter 	else
14410b5c5ed0SDaniel Vetter 		res1 = 0;
14420b5c5ed0SDaniel Vetter 
14430b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14440b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
14450b5c5ed0SDaniel Vetter 	else
14460b5c5ed0SDaniel Vetter 		res2 = 0;
14475b3a856bSDaniel Vetter 
1448277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14490b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
14500b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
14510b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
14520b5c5ed0SDaniel Vetter 				     res1, res2);
14535b3a856bSDaniel Vetter }
14548bf1e9f1SShuang He 
14551403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
14561403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
14571403c0d4SPaulo Zanoni  * the work queue. */
14581403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1459baf02a1fSBen Widawsky {
146041a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
146159cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
14624848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
14634d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_PM_RPS_EVENTS);
146459cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
14652adbee62SDaniel Vetter 
14662adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
146741a05a3aSDaniel Vetter 	}
1468baf02a1fSBen Widawsky 
14691403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
147012638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
147112638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
147212638c57SBen Widawsky 
147312638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
147412638c57SBen Widawsky 			DRM_ERROR("VEBOX CS error interrupt 0x%08x\n", pm_iir);
147512638c57SBen Widawsky 			i915_handle_error(dev_priv->dev, false);
147612638c57SBen Widawsky 		}
147712638c57SBen Widawsky 	}
14781403c0d4SPaulo Zanoni }
1479baf02a1fSBen Widawsky 
1480ff1f525eSDaniel Vetter static irqreturn_t valleyview_irq_handler(int irq, void *arg)
14817e231dbeSJesse Barnes {
14827e231dbeSJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
14837e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
14847e231dbeSJesse Barnes 	u32 iir, gt_iir, pm_iir;
14857e231dbeSJesse Barnes 	irqreturn_t ret = IRQ_NONE;
14867e231dbeSJesse Barnes 	unsigned long irqflags;
14877e231dbeSJesse Barnes 	int pipe;
14887e231dbeSJesse Barnes 	u32 pipe_stats[I915_MAX_PIPES];
14897e231dbeSJesse Barnes 
14907e231dbeSJesse Barnes 	while (true) {
14917e231dbeSJesse Barnes 		iir = I915_READ(VLV_IIR);
14927e231dbeSJesse Barnes 		gt_iir = I915_READ(GTIIR);
14937e231dbeSJesse Barnes 		pm_iir = I915_READ(GEN6_PMIIR);
14947e231dbeSJesse Barnes 
14957e231dbeSJesse Barnes 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
14967e231dbeSJesse Barnes 			goto out;
14977e231dbeSJesse Barnes 
14987e231dbeSJesse Barnes 		ret = IRQ_HANDLED;
14997e231dbeSJesse Barnes 
1500e7b4c6b1SDaniel Vetter 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
15017e231dbeSJesse Barnes 
15027e231dbeSJesse Barnes 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
15037e231dbeSJesse Barnes 		for_each_pipe(pipe) {
15047e231dbeSJesse Barnes 			int reg = PIPESTAT(pipe);
15057e231dbeSJesse Barnes 			pipe_stats[pipe] = I915_READ(reg);
15067e231dbeSJesse Barnes 
15077e231dbeSJesse Barnes 			/*
15087e231dbeSJesse Barnes 			 * Clear the PIPE*STAT regs before the IIR
15097e231dbeSJesse Barnes 			 */
15102d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
15117e231dbeSJesse Barnes 				I915_WRITE(reg, pipe_stats[pipe]);
15127e231dbeSJesse Barnes 		}
15137e231dbeSJesse Barnes 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
15147e231dbeSJesse Barnes 
151531acc7f5SJesse Barnes 		for_each_pipe(pipe) {
15167b5562d4SJesse Barnes 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
151731acc7f5SJesse Barnes 				drm_handle_vblank(dev, pipe);
151831acc7f5SJesse Barnes 
151931acc7f5SJesse Barnes 			if (pipe_stats[pipe] & PLANE_FLIPDONE_INT_STATUS_VLV) {
152031acc7f5SJesse Barnes 				intel_prepare_page_flip(dev, pipe);
152131acc7f5SJesse Barnes 				intel_finish_page_flip(dev, pipe);
152231acc7f5SJesse Barnes 			}
15234356d586SDaniel Vetter 
15244356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1525277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
15262d9d2b0bSVille Syrjälä 
15272d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
15282d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1529fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
153031acc7f5SJesse Barnes 		}
153131acc7f5SJesse Barnes 
15327e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
15337e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
15347e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1535b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
15367e231dbeSJesse Barnes 
153710a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
153891d131d2SDaniel Vetter 
15394aeebd74SDaniel Vetter 			if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
15404aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
15414aeebd74SDaniel Vetter 
15427e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
15437e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
15447e231dbeSJesse Barnes 		}
15457e231dbeSJesse Barnes 
1546515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1547515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
15487e231dbeSJesse Barnes 
154960611c13SPaulo Zanoni 		if (pm_iir)
1550d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
15517e231dbeSJesse Barnes 
15527e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
15537e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
15547e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
15557e231dbeSJesse Barnes 	}
15567e231dbeSJesse Barnes 
15577e231dbeSJesse Barnes out:
15587e231dbeSJesse Barnes 	return ret;
15597e231dbeSJesse Barnes }
15607e231dbeSJesse Barnes 
156123e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1562776ad806SJesse Barnes {
1563776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
15649db4a9c7SJesse Barnes 	int pipe;
1565b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1566776ad806SJesse Barnes 
156710a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
156891d131d2SDaniel Vetter 
1569cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1570cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1571776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1572cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1573cfc33bf7SVille Syrjälä 				 port_name(port));
1574cfc33bf7SVille Syrjälä 	}
1575776ad806SJesse Barnes 
1576ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1577ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1578ce99c256SDaniel Vetter 
1579776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1580515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1581776ad806SJesse Barnes 
1582776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1583776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1584776ad806SJesse Barnes 
1585776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1586776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1587776ad806SJesse Barnes 
1588776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1589776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1590776ad806SJesse Barnes 
15919db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
15929db4a9c7SJesse Barnes 		for_each_pipe(pipe)
15939db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
15949db4a9c7SJesse Barnes 					 pipe_name(pipe),
15959db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1596776ad806SJesse Barnes 
1597776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1598776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1599776ad806SJesse Barnes 
1600776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1601776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1602776ad806SJesse Barnes 
1603776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
16048664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
16058664281bSPaulo Zanoni 							  false))
1606fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
16078664281bSPaulo Zanoni 
16088664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
16098664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
16108664281bSPaulo Zanoni 							  false))
1611fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
16128664281bSPaulo Zanoni }
16138664281bSPaulo Zanoni 
16148664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
16158664281bSPaulo Zanoni {
16168664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
16178664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
16185a69b89fSDaniel Vetter 	enum pipe pipe;
16198664281bSPaulo Zanoni 
1620de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1621de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1622de032bf4SPaulo Zanoni 
16235a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
16245a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
16255a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
16265a69b89fSDaniel Vetter 								  false))
1627fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
16285a69b89fSDaniel Vetter 					  pipe_name(pipe));
16295a69b89fSDaniel Vetter 		}
16308664281bSPaulo Zanoni 
16315a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
16325a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1633277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
16345a69b89fSDaniel Vetter 			else
1635277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
16365a69b89fSDaniel Vetter 		}
16375a69b89fSDaniel Vetter 	}
16388bf1e9f1SShuang He 
16398664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
16408664281bSPaulo Zanoni }
16418664281bSPaulo Zanoni 
16428664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
16438664281bSPaulo Zanoni {
16448664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
16458664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
16468664281bSPaulo Zanoni 
1647de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1648de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1649de032bf4SPaulo Zanoni 
16508664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
16518664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
16528664281bSPaulo Zanoni 							  false))
1653fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
16548664281bSPaulo Zanoni 
16558664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
16568664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
16578664281bSPaulo Zanoni 							  false))
1658fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
16598664281bSPaulo Zanoni 
16608664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
16618664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
16628664281bSPaulo Zanoni 							  false))
1663fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
16648664281bSPaulo Zanoni 
16658664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1666776ad806SJesse Barnes }
1667776ad806SJesse Barnes 
166823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
166923e81d69SAdam Jackson {
167023e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
167123e81d69SAdam Jackson 	int pipe;
1672b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
167323e81d69SAdam Jackson 
167410a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
167591d131d2SDaniel Vetter 
1676cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1677cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
167823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1679cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1680cfc33bf7SVille Syrjälä 				 port_name(port));
1681cfc33bf7SVille Syrjälä 	}
168223e81d69SAdam Jackson 
168323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1684ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
168523e81d69SAdam Jackson 
168623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1687515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
168823e81d69SAdam Jackson 
168923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
169023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
169123e81d69SAdam Jackson 
169223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
169323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
169423e81d69SAdam Jackson 
169523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
169623e81d69SAdam Jackson 		for_each_pipe(pipe)
169723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
169823e81d69SAdam Jackson 					 pipe_name(pipe),
169923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
17008664281bSPaulo Zanoni 
17018664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
17028664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
170323e81d69SAdam Jackson }
170423e81d69SAdam Jackson 
1705c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1706c008bc6eSPaulo Zanoni {
1707c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
170840da17c2SDaniel Vetter 	enum pipe pipe;
1709c008bc6eSPaulo Zanoni 
1710c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1711c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1712c008bc6eSPaulo Zanoni 
1713c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1714c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1715c008bc6eSPaulo Zanoni 
1716c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1717c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1718c008bc6eSPaulo Zanoni 
171940da17c2SDaniel Vetter 	for_each_pipe(pipe) {
172040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
172140da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1722c008bc6eSPaulo Zanoni 
172340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
172440da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1725fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
172640da17c2SDaniel Vetter 					  pipe_name(pipe));
1727c008bc6eSPaulo Zanoni 
172840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
172940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17305b3a856bSDaniel Vetter 
173140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
173240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
173340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
173440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1735c008bc6eSPaulo Zanoni 		}
1736c008bc6eSPaulo Zanoni 	}
1737c008bc6eSPaulo Zanoni 
1738c008bc6eSPaulo Zanoni 	/* check event from PCH */
1739c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1740c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1741c008bc6eSPaulo Zanoni 
1742c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1743c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1744c008bc6eSPaulo Zanoni 		else
1745c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1746c008bc6eSPaulo Zanoni 
1747c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1748c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1749c008bc6eSPaulo Zanoni 	}
1750c008bc6eSPaulo Zanoni 
1751c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1752c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1753c008bc6eSPaulo Zanoni }
1754c008bc6eSPaulo Zanoni 
17559719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
17569719fb98SPaulo Zanoni {
17579719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17583b6c42e8SDaniel Vetter 	enum pipe i;
17599719fb98SPaulo Zanoni 
17609719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
17619719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
17629719fb98SPaulo Zanoni 
17639719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
17649719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
17659719fb98SPaulo Zanoni 
17669719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
17679719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
17689719fb98SPaulo Zanoni 
17693b6c42e8SDaniel Vetter 	for_each_pipe(i) {
177040da17c2SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(i)))
17719719fb98SPaulo Zanoni 			drm_handle_vblank(dev, i);
177240da17c2SDaniel Vetter 
177340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
177440da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(i)) {
17759719fb98SPaulo Zanoni 			intel_prepare_page_flip(dev, i);
17769719fb98SPaulo Zanoni 			intel_finish_page_flip_plane(dev, i);
17779719fb98SPaulo Zanoni 		}
17789719fb98SPaulo Zanoni 	}
17799719fb98SPaulo Zanoni 
17809719fb98SPaulo Zanoni 	/* check event from PCH */
17819719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
17829719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
17839719fb98SPaulo Zanoni 
17849719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
17859719fb98SPaulo Zanoni 
17869719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
17879719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
17889719fb98SPaulo Zanoni 	}
17899719fb98SPaulo Zanoni }
17909719fb98SPaulo Zanoni 
1791f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1792b1f14ad0SJesse Barnes {
1793b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1794b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1795f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
17960e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1797b1f14ad0SJesse Barnes 
17988664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
17998664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1800907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
18018664281bSPaulo Zanoni 
1802b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1803b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1804b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
180523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
18060e43406bSChris Wilson 
180744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
180844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
180944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
181044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
181144498aeaSPaulo Zanoni 	 * due to its back queue). */
1812ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
181344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
181444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
181544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1816ab5c608bSBen Widawsky 	}
181744498aeaSPaulo Zanoni 
18180e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
18190e43406bSChris Wilson 	if (gt_iir) {
1820d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
18210e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1822d8fc8a47SPaulo Zanoni 		else
1823d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
18240e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
18250e43406bSChris Wilson 		ret = IRQ_HANDLED;
18260e43406bSChris Wilson 	}
1827b1f14ad0SJesse Barnes 
1828b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
18290e43406bSChris Wilson 	if (de_iir) {
1830f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
18319719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1832f1af8fc1SPaulo Zanoni 		else
1833f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
18340e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
18350e43406bSChris Wilson 		ret = IRQ_HANDLED;
18360e43406bSChris Wilson 	}
18370e43406bSChris Wilson 
1838f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1839f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
18400e43406bSChris Wilson 		if (pm_iir) {
1841d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1842b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
18430e43406bSChris Wilson 			ret = IRQ_HANDLED;
18440e43406bSChris Wilson 		}
1845f1af8fc1SPaulo Zanoni 	}
1846b1f14ad0SJesse Barnes 
1847b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1848b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1849ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
185044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
185144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1852ab5c608bSBen Widawsky 	}
1853b1f14ad0SJesse Barnes 
1854b1f14ad0SJesse Barnes 	return ret;
1855b1f14ad0SJesse Barnes }
1856b1f14ad0SJesse Barnes 
1857abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
1858abd58f01SBen Widawsky {
1859abd58f01SBen Widawsky 	struct drm_device *dev = arg;
1860abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
1861abd58f01SBen Widawsky 	u32 master_ctl;
1862abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1863abd58f01SBen Widawsky 	uint32_t tmp = 0;
1864c42664ccSDaniel Vetter 	enum pipe pipe;
1865abd58f01SBen Widawsky 
1866abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
1867abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
1868abd58f01SBen Widawsky 	if (!master_ctl)
1869abd58f01SBen Widawsky 		return IRQ_NONE;
1870abd58f01SBen Widawsky 
1871abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
1872abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1873abd58f01SBen Widawsky 
1874abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
1875abd58f01SBen Widawsky 
1876abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
1877abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
1878abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
1879abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
1880abd58f01SBen Widawsky 		else if (tmp)
1881abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
1882abd58f01SBen Widawsky 		else
1883abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
1884abd58f01SBen Widawsky 
1885abd58f01SBen Widawsky 		if (tmp) {
1886abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
1887abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1888abd58f01SBen Widawsky 		}
1889abd58f01SBen Widawsky 	}
1890abd58f01SBen Widawsky 
18916d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
18926d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
18936d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
18946d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
18956d766f02SDaniel Vetter 		else if (tmp)
18966d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
18976d766f02SDaniel Vetter 		else
18986d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
18996d766f02SDaniel Vetter 
19006d766f02SDaniel Vetter 		if (tmp) {
19016d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
19026d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
19036d766f02SDaniel Vetter 		}
19046d766f02SDaniel Vetter 	}
19056d766f02SDaniel Vetter 
1906abd58f01SBen Widawsky 	for_each_pipe(pipe) {
1907abd58f01SBen Widawsky 		uint32_t pipe_iir;
1908abd58f01SBen Widawsky 
1909c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
1910c42664ccSDaniel Vetter 			continue;
1911c42664ccSDaniel Vetter 
1912abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
1913abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
1914abd58f01SBen Widawsky 			drm_handle_vblank(dev, pipe);
1915abd58f01SBen Widawsky 
1916abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_FLIP_DONE) {
1917abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
1918abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
1919abd58f01SBen Widawsky 		}
1920abd58f01SBen Widawsky 
19210fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
19220fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
19230fbe7870SDaniel Vetter 
192438d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
192538d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
192638d83c96SDaniel Vetter 								  false))
1927fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
192838d83c96SDaniel Vetter 					  pipe_name(pipe));
192938d83c96SDaniel Vetter 		}
193038d83c96SDaniel Vetter 
193130100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
193230100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
193330100f2bSDaniel Vetter 				  pipe_name(pipe),
193430100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
193530100f2bSDaniel Vetter 		}
1936abd58f01SBen Widawsky 
1937abd58f01SBen Widawsky 		if (pipe_iir) {
1938abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1939abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
1940c42664ccSDaniel Vetter 		} else
1941abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
1942abd58f01SBen Widawsky 	}
1943abd58f01SBen Widawsky 
194492d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
194592d03a80SDaniel Vetter 		/*
194692d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
194792d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
194892d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
194992d03a80SDaniel Vetter 		 */
195092d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
195192d03a80SDaniel Vetter 
195292d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
195392d03a80SDaniel Vetter 
195492d03a80SDaniel Vetter 		if (pch_iir) {
195592d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
195692d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
195792d03a80SDaniel Vetter 		}
195892d03a80SDaniel Vetter 	}
195992d03a80SDaniel Vetter 
1960abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
1961abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1962abd58f01SBen Widawsky 
1963abd58f01SBen Widawsky 	return ret;
1964abd58f01SBen Widawsky }
1965abd58f01SBen Widawsky 
196617e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
196717e1df07SDaniel Vetter 			       bool reset_completed)
196817e1df07SDaniel Vetter {
196917e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
197017e1df07SDaniel Vetter 	int i;
197117e1df07SDaniel Vetter 
197217e1df07SDaniel Vetter 	/*
197317e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
197417e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
197517e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
197617e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
197717e1df07SDaniel Vetter 	 */
197817e1df07SDaniel Vetter 
197917e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
198017e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
198117e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
198217e1df07SDaniel Vetter 
198317e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
198417e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
198517e1df07SDaniel Vetter 
198617e1df07SDaniel Vetter 	/*
198717e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
198817e1df07SDaniel Vetter 	 * reset state is cleared.
198917e1df07SDaniel Vetter 	 */
199017e1df07SDaniel Vetter 	if (reset_completed)
199117e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
199217e1df07SDaniel Vetter }
199317e1df07SDaniel Vetter 
19948a905236SJesse Barnes /**
19958a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
19968a905236SJesse Barnes  * @work: work struct
19978a905236SJesse Barnes  *
19988a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
19998a905236SJesse Barnes  * was detected.
20008a905236SJesse Barnes  */
20018a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
20028a905236SJesse Barnes {
20031f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
20041f83fee0SDaniel Vetter 						    work);
20051f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
20061f83fee0SDaniel Vetter 						    gpu_error);
20078a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2008cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2009cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2010cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
201117e1df07SDaniel Vetter 	int ret;
20128a905236SJesse Barnes 
20135bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
20148a905236SJesse Barnes 
20157db0ba24SDaniel Vetter 	/*
20167db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
20177db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
20187db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
20197db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
20207db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
20217db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
20227db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
20237db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
20247db0ba24SDaniel Vetter 	 */
20257db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
202644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
20275bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
20287db0ba24SDaniel Vetter 				   reset_event);
20291f83fee0SDaniel Vetter 
203017e1df07SDaniel Vetter 		/*
203117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
203217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
203317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
203417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
203517e1df07SDaniel Vetter 		 */
2036f69061beSDaniel Vetter 		ret = i915_reset(dev);
2037f69061beSDaniel Vetter 
203817e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
203917e1df07SDaniel Vetter 
2040f69061beSDaniel Vetter 		if (ret == 0) {
2041f69061beSDaniel Vetter 			/*
2042f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2043f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2044f69061beSDaniel Vetter 			 * complete.
2045f69061beSDaniel Vetter 			 *
2046f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2047f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2048f69061beSDaniel Vetter 			 * updates before
2049f69061beSDaniel Vetter 			 * the counter increment.
2050f69061beSDaniel Vetter 			 */
2051f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
2052f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2053f69061beSDaniel Vetter 
20545bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2055f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
20561f83fee0SDaniel Vetter 		} else {
20572ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2058f316a42cSBen Gamari 		}
20591f83fee0SDaniel Vetter 
206017e1df07SDaniel Vetter 		/*
206117e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
206217e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
206317e1df07SDaniel Vetter 		 */
206417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2065f316a42cSBen Gamari 	}
20668a905236SJesse Barnes }
20678a905236SJesse Barnes 
206835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2069c0e09200SDave Airlie {
20708a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2071bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
207263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2073050ee91fSBen Widawsky 	int pipe, i;
207463eeaf38SJesse Barnes 
207535aed2e6SChris Wilson 	if (!eir)
207635aed2e6SChris Wilson 		return;
207763eeaf38SJesse Barnes 
2078a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
20798a905236SJesse Barnes 
2080bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2081bd9854f9SBen Widawsky 
20828a905236SJesse Barnes 	if (IS_G4X(dev)) {
20838a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
20848a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
20858a905236SJesse Barnes 
2086a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2087a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2088050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2089050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2090a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2091a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
20928a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
20933143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
20948a905236SJesse Barnes 		}
20958a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
20968a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2097a70491ccSJoe Perches 			pr_err("page table error\n");
2098a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
20998a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
21003143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
21018a905236SJesse Barnes 		}
21028a905236SJesse Barnes 	}
21038a905236SJesse Barnes 
2104a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
210563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
210663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2107a70491ccSJoe Perches 			pr_err("page table error\n");
2108a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
210963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
21103143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
211163eeaf38SJesse Barnes 		}
21128a905236SJesse Barnes 	}
21138a905236SJesse Barnes 
211463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2115a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
21169db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2117a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
21189db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
211963eeaf38SJesse Barnes 		/* pipestat has already been acked */
212063eeaf38SJesse Barnes 	}
212163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2122a70491ccSJoe Perches 		pr_err("instruction error\n");
2123a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2124050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2125050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2126a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
212763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
212863eeaf38SJesse Barnes 
2129a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2130a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2131a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
213263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
21333143a2bfSChris Wilson 			POSTING_READ(IPEIR);
213463eeaf38SJesse Barnes 		} else {
213563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
213663eeaf38SJesse Barnes 
2137a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2138a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2139a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2140a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
214163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
21423143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
214363eeaf38SJesse Barnes 		}
214463eeaf38SJesse Barnes 	}
214563eeaf38SJesse Barnes 
214663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
21473143a2bfSChris Wilson 	POSTING_READ(EIR);
214863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
214963eeaf38SJesse Barnes 	if (eir) {
215063eeaf38SJesse Barnes 		/*
215163eeaf38SJesse Barnes 		 * some errors might have become stuck,
215263eeaf38SJesse Barnes 		 * mask them.
215363eeaf38SJesse Barnes 		 */
215463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
215563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
215663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
215763eeaf38SJesse Barnes 	}
215835aed2e6SChris Wilson }
215935aed2e6SChris Wilson 
216035aed2e6SChris Wilson /**
216135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
216235aed2e6SChris Wilson  * @dev: drm device
216335aed2e6SChris Wilson  *
216435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
216535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
216635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
216735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
216835aed2e6SChris Wilson  * of a ring dump etc.).
216935aed2e6SChris Wilson  */
2170527f9e90SChris Wilson void i915_handle_error(struct drm_device *dev, bool wedged)
217135aed2e6SChris Wilson {
217235aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
217335aed2e6SChris Wilson 
217435aed2e6SChris Wilson 	i915_capture_error_state(dev);
217535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
21768a905236SJesse Barnes 
2177ba1234d1SBen Gamari 	if (wedged) {
2178f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2179f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2180ba1234d1SBen Gamari 
218111ed50ecSBen Gamari 		/*
218217e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
218317e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
218417e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
218517e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
218617e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
218717e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
218817e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
218917e1df07SDaniel Vetter 		 *
219017e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
219117e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
219217e1df07SDaniel Vetter 		 * counter atomic_t.
219311ed50ecSBen Gamari 		 */
219417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
219511ed50ecSBen Gamari 	}
219611ed50ecSBen Gamari 
2197122f46baSDaniel Vetter 	/*
2198122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2199122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2200122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2201122f46baSDaniel Vetter 	 * code will deadlock.
2202122f46baSDaniel Vetter 	 */
2203122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
22048a905236SJesse Barnes }
22058a905236SJesse Barnes 
220621ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
22074e5359cdSSimon Farnsworth {
22084e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
22094e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
22104e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
221105394f39SChris Wilson 	struct drm_i915_gem_object *obj;
22124e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
22134e5359cdSSimon Farnsworth 	unsigned long flags;
22144e5359cdSSimon Farnsworth 	bool stall_detected;
22154e5359cdSSimon Farnsworth 
22164e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
22174e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
22184e5359cdSSimon Farnsworth 		return;
22194e5359cdSSimon Farnsworth 
22204e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
22214e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
22224e5359cdSSimon Farnsworth 
2223e7d841caSChris Wilson 	if (work == NULL ||
2224e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2225e7d841caSChris Wilson 	    !work->enable_stall_check) {
22264e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
22274e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
22284e5359cdSSimon Farnsworth 		return;
22294e5359cdSSimon Farnsworth 	}
22304e5359cdSSimon Farnsworth 
22314e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
223205394f39SChris Wilson 	obj = work->pending_flip_obj;
2233a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
22349db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2235446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2236f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
22374e5359cdSSimon Farnsworth 	} else {
22389db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2239f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
224001f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
22414e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
22424e5359cdSSimon Farnsworth 	}
22434e5359cdSSimon Farnsworth 
22444e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
22454e5359cdSSimon Farnsworth 
22464e5359cdSSimon Farnsworth 	if (stall_detected) {
22474e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
22484e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
22494e5359cdSSimon Farnsworth 	}
22504e5359cdSSimon Farnsworth }
22514e5359cdSSimon Farnsworth 
225242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
225342f52ef8SKeith Packard  * we use as a pipe index
225442f52ef8SKeith Packard  */
2255f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
22560a3e67a4SJesse Barnes {
22570a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2258e9d21d7fSKeith Packard 	unsigned long irqflags;
225971e0ffa5SJesse Barnes 
22605eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
226171e0ffa5SJesse Barnes 		return -EINVAL;
22620a3e67a4SJesse Barnes 
22631ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2264f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
22657c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
22667c463586SKeith Packard 				     PIPE_START_VBLANK_INTERRUPT_ENABLE);
22670a3e67a4SJesse Barnes 	else
22687c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
22697c463586SKeith Packard 				     PIPE_VBLANK_INTERRUPT_ENABLE);
22708692d00eSChris Wilson 
22718692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
22728692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
22736b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
22741ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
22758692d00eSChris Wilson 
22760a3e67a4SJesse Barnes 	return 0;
22770a3e67a4SJesse Barnes }
22780a3e67a4SJesse Barnes 
2279f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2280f796cf8fSJesse Barnes {
2281f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2282f796cf8fSJesse Barnes 	unsigned long irqflags;
2283b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
228440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2285f796cf8fSJesse Barnes 
2286f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2287f796cf8fSJesse Barnes 		return -EINVAL;
2288f796cf8fSJesse Barnes 
2289f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2290b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2291b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2292b1f14ad0SJesse Barnes 
2293b1f14ad0SJesse Barnes 	return 0;
2294b1f14ad0SJesse Barnes }
2295b1f14ad0SJesse Barnes 
22967e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
22977e231dbeSJesse Barnes {
22987e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
22997e231dbeSJesse Barnes 	unsigned long irqflags;
230031acc7f5SJesse Barnes 	u32 imr;
23017e231dbeSJesse Barnes 
23027e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
23037e231dbeSJesse Barnes 		return -EINVAL;
23047e231dbeSJesse Barnes 
23057e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
23067e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
23073b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
23087e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
230931acc7f5SJesse Barnes 	else
23107e231dbeSJesse Barnes 		imr &= ~I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23117e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
231231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
231331acc7f5SJesse Barnes 			     PIPE_START_VBLANK_INTERRUPT_ENABLE);
23147e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23157e231dbeSJesse Barnes 
23167e231dbeSJesse Barnes 	return 0;
23177e231dbeSJesse Barnes }
23187e231dbeSJesse Barnes 
2319abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2320abd58f01SBen Widawsky {
2321abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2322abd58f01SBen Widawsky 	unsigned long irqflags;
2323abd58f01SBen Widawsky 
2324abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2325abd58f01SBen Widawsky 		return -EINVAL;
2326abd58f01SBen Widawsky 
2327abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
23287167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
23297167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2330abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2331abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2332abd58f01SBen Widawsky 	return 0;
2333abd58f01SBen Widawsky }
2334abd58f01SBen Widawsky 
233542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
233642f52ef8SKeith Packard  * we use as a pipe index
233742f52ef8SKeith Packard  */
2338f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
23390a3e67a4SJesse Barnes {
23400a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2341e9d21d7fSKeith Packard 	unsigned long irqflags;
23420a3e67a4SJesse Barnes 
23431ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
23448692d00eSChris Wilson 	if (dev_priv->info->gen == 3)
23456b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
23468692d00eSChris Wilson 
23477c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
23487c463586SKeith Packard 			      PIPE_VBLANK_INTERRUPT_ENABLE |
23497c463586SKeith Packard 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23501ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23510a3e67a4SJesse Barnes }
23520a3e67a4SJesse Barnes 
2353f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2354f796cf8fSJesse Barnes {
2355f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2356f796cf8fSJesse Barnes 	unsigned long irqflags;
2357b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
235840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2359f796cf8fSJesse Barnes 
2360f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2361b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2362b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2363b1f14ad0SJesse Barnes }
2364b1f14ad0SJesse Barnes 
23657e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
23667e231dbeSJesse Barnes {
23677e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
23687e231dbeSJesse Barnes 	unsigned long irqflags;
236931acc7f5SJesse Barnes 	u32 imr;
23707e231dbeSJesse Barnes 
23717e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
237231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
237331acc7f5SJesse Barnes 			      PIPE_START_VBLANK_INTERRUPT_ENABLE);
23747e231dbeSJesse Barnes 	imr = I915_READ(VLV_IMR);
23753b6c42e8SDaniel Vetter 	if (pipe == PIPE_A)
23767e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT;
237731acc7f5SJesse Barnes 	else
23787e231dbeSJesse Barnes 		imr |= I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
23797e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, imr);
23807e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
23817e231dbeSJesse Barnes }
23827e231dbeSJesse Barnes 
2383abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2384abd58f01SBen Widawsky {
2385abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2386abd58f01SBen Widawsky 	unsigned long irqflags;
2387abd58f01SBen Widawsky 
2388abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2389abd58f01SBen Widawsky 		return;
2390abd58f01SBen Widawsky 
2391abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
23927167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
23937167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2394abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2395abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2396abd58f01SBen Widawsky }
2397abd58f01SBen Widawsky 
2398893eead0SChris Wilson static u32
2399893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2400852835f3SZou Nan hai {
2401893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2402893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2403893eead0SChris Wilson }
2404893eead0SChris Wilson 
24059107e9d2SChris Wilson static bool
24069107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2407893eead0SChris Wilson {
24089107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
24099107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2410f65d9421SBen Gamari }
2411f65d9421SBen Gamari 
24126274f212SChris Wilson static struct intel_ring_buffer *
24136274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2414a24a11e6SChris Wilson {
2415a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
24166274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2417a24a11e6SChris Wilson 
2418a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2419a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2420a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
24216274f212SChris Wilson 		return NULL;
2422a24a11e6SChris Wilson 
2423a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2424a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2425a24a11e6SChris Wilson 	 */
24266274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2427a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2428a24a11e6SChris Wilson 	do {
2429a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2430a24a11e6SChris Wilson 		if (cmd == ipehr)
2431a24a11e6SChris Wilson 			break;
2432a24a11e6SChris Wilson 
2433a24a11e6SChris Wilson 		acthd -= 4;
2434a24a11e6SChris Wilson 		if (acthd < acthd_min)
24356274f212SChris Wilson 			return NULL;
2436a24a11e6SChris Wilson 	} while (1);
2437a24a11e6SChris Wilson 
24386274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
24396274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2440a24a11e6SChris Wilson }
2441a24a11e6SChris Wilson 
24426274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
24436274f212SChris Wilson {
24446274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
24456274f212SChris Wilson 	struct intel_ring_buffer *signaller;
24466274f212SChris Wilson 	u32 seqno, ctl;
24476274f212SChris Wilson 
24486274f212SChris Wilson 	ring->hangcheck.deadlock = true;
24496274f212SChris Wilson 
24506274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
24516274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
24526274f212SChris Wilson 		return -1;
24536274f212SChris Wilson 
24546274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
24556274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
24566274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
24576274f212SChris Wilson 		return -1;
24586274f212SChris Wilson 
24596274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
24606274f212SChris Wilson }
24616274f212SChris Wilson 
24626274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
24636274f212SChris Wilson {
24646274f212SChris Wilson 	struct intel_ring_buffer *ring;
24656274f212SChris Wilson 	int i;
24666274f212SChris Wilson 
24676274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
24686274f212SChris Wilson 		ring->hangcheck.deadlock = false;
24696274f212SChris Wilson }
24706274f212SChris Wilson 
2471ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2472ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
24731ec14ad3SChris Wilson {
24741ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
24751ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
24769107e9d2SChris Wilson 	u32 tmp;
24779107e9d2SChris Wilson 
24786274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2479f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
24806274f212SChris Wilson 
24819107e9d2SChris Wilson 	if (IS_GEN2(dev))
2482f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
24839107e9d2SChris Wilson 
24849107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
24859107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
24869107e9d2SChris Wilson 	 * and break the hang. This should work on
24879107e9d2SChris Wilson 	 * all but the second generation chipsets.
24889107e9d2SChris Wilson 	 */
24899107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
24901ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
24911ec14ad3SChris Wilson 		DRM_ERROR("Kicking stuck wait on %s\n",
24921ec14ad3SChris Wilson 			  ring->name);
249309e14bf3SChris Wilson 		i915_handle_error(dev, false);
24941ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2495f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
24961ec14ad3SChris Wilson 	}
2497a24a11e6SChris Wilson 
24986274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
24996274f212SChris Wilson 		switch (semaphore_passed(ring)) {
25006274f212SChris Wilson 		default:
2501f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
25026274f212SChris Wilson 		case 1:
2503a24a11e6SChris Wilson 			DRM_ERROR("Kicking stuck semaphore on %s\n",
2504a24a11e6SChris Wilson 				  ring->name);
250509e14bf3SChris Wilson 			i915_handle_error(dev, false);
2506a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2507f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
25086274f212SChris Wilson 		case 0:
2509f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
25106274f212SChris Wilson 		}
25119107e9d2SChris Wilson 	}
25129107e9d2SChris Wilson 
2513f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2514a24a11e6SChris Wilson }
2515d1e61e7fSChris Wilson 
2516f65d9421SBen Gamari /**
2517f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
251805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
251905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
252005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
252105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
252205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2523f65d9421SBen Gamari  */
2524a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2525f65d9421SBen Gamari {
2526f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2527f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2528b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2529b4519513SChris Wilson 	int i;
253005407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
25319107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
25329107e9d2SChris Wilson #define BUSY 1
25339107e9d2SChris Wilson #define KICK 5
25349107e9d2SChris Wilson #define HUNG 20
2535893eead0SChris Wilson 
2536d330a953SJani Nikula 	if (!i915.enable_hangcheck)
25373e0dc6b0SBen Widawsky 		return;
25383e0dc6b0SBen Widawsky 
2539b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
254005407ff8SMika Kuoppala 		u32 seqno, acthd;
25419107e9d2SChris Wilson 		bool busy = true;
2542b4519513SChris Wilson 
25436274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
25446274f212SChris Wilson 
254505407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
254605407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
254705407ff8SMika Kuoppala 
254805407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
25499107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2550da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2551da661464SMika Kuoppala 
25529107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
25539107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2554094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2555f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
25569107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
25579107e9d2SChris Wilson 								  ring->name);
2558f4adcd24SDaniel Vetter 						else
2559f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2560f4adcd24SDaniel Vetter 								 ring->name);
25619107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2562094f9a54SChris Wilson 					}
2563094f9a54SChris Wilson 					/* Safeguard against driver failure */
2564094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
25659107e9d2SChris Wilson 				} else
25669107e9d2SChris Wilson 					busy = false;
256705407ff8SMika Kuoppala 			} else {
25686274f212SChris Wilson 				/* We always increment the hangcheck score
25696274f212SChris Wilson 				 * if the ring is busy and still processing
25706274f212SChris Wilson 				 * the same request, so that no single request
25716274f212SChris Wilson 				 * can run indefinitely (such as a chain of
25726274f212SChris Wilson 				 * batches). The only time we do not increment
25736274f212SChris Wilson 				 * the hangcheck score on this ring, if this
25746274f212SChris Wilson 				 * ring is in a legitimate wait for another
25756274f212SChris Wilson 				 * ring. In that case the waiting ring is a
25766274f212SChris Wilson 				 * victim and we want to be sure we catch the
25776274f212SChris Wilson 				 * right culprit. Then every time we do kick
25786274f212SChris Wilson 				 * the ring, add a small increment to the
25796274f212SChris Wilson 				 * score so that we can catch a batch that is
25806274f212SChris Wilson 				 * being repeatedly kicked and so responsible
25816274f212SChris Wilson 				 * for stalling the machine.
25829107e9d2SChris Wilson 				 */
2583ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2584ad8beaeaSMika Kuoppala 								    acthd);
2585ad8beaeaSMika Kuoppala 
2586ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2587da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2588f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
25896274f212SChris Wilson 					break;
2590f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2591ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
25926274f212SChris Wilson 					break;
2593f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2594ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
25956274f212SChris Wilson 					break;
2596f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2597ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
25986274f212SChris Wilson 					stuck[i] = true;
25996274f212SChris Wilson 					break;
26006274f212SChris Wilson 				}
260105407ff8SMika Kuoppala 			}
26029107e9d2SChris Wilson 		} else {
2603da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2604da661464SMika Kuoppala 
26059107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
26069107e9d2SChris Wilson 			 * attempts across multiple batches.
26079107e9d2SChris Wilson 			 */
26089107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
26099107e9d2SChris Wilson 				ring->hangcheck.score--;
2610cbb465e7SChris Wilson 		}
2611f65d9421SBen Gamari 
261205407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
261305407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
26149107e9d2SChris Wilson 		busy_count += busy;
261505407ff8SMika Kuoppala 	}
261605407ff8SMika Kuoppala 
261705407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2618*b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2619b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
262005407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2621a43adf07SChris Wilson 				 ring->name);
2622a43adf07SChris Wilson 			rings_hung++;
262305407ff8SMika Kuoppala 		}
262405407ff8SMika Kuoppala 	}
262505407ff8SMika Kuoppala 
262605407ff8SMika Kuoppala 	if (rings_hung)
262705407ff8SMika Kuoppala 		return i915_handle_error(dev, true);
262805407ff8SMika Kuoppala 
262905407ff8SMika Kuoppala 	if (busy_count)
263005407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
263105407ff8SMika Kuoppala 		 * being added */
263210cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
263310cd45b6SMika Kuoppala }
263410cd45b6SMika Kuoppala 
263510cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
263610cd45b6SMika Kuoppala {
263710cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
2638d330a953SJani Nikula 	if (!i915.enable_hangcheck)
263910cd45b6SMika Kuoppala 		return;
264010cd45b6SMika Kuoppala 
264199584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
264210cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2643f65d9421SBen Gamari }
2644f65d9421SBen Gamari 
264591738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
264691738a95SPaulo Zanoni {
264791738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
264891738a95SPaulo Zanoni 
264991738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
265091738a95SPaulo Zanoni 		return;
265191738a95SPaulo Zanoni 
265291738a95SPaulo Zanoni 	/* south display irq */
265391738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
265491738a95SPaulo Zanoni 	/*
265591738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
265691738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
265791738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
265891738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
265991738a95SPaulo Zanoni 	 */
266091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
266191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
266291738a95SPaulo Zanoni }
266391738a95SPaulo Zanoni 
2664d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2665d18ea1b5SDaniel Vetter {
2666d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2667d18ea1b5SDaniel Vetter 
2668d18ea1b5SDaniel Vetter 	/* and GT */
2669d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2670d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2671d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2672d18ea1b5SDaniel Vetter 
2673d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2674d18ea1b5SDaniel Vetter 		/* and PM */
2675d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2676d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2677d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2678d18ea1b5SDaniel Vetter 	}
2679d18ea1b5SDaniel Vetter }
2680d18ea1b5SDaniel Vetter 
2681c0e09200SDave Airlie /* drm_dma.h hooks
2682c0e09200SDave Airlie */
2683f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2684036a4a7dSZhenyu Wang {
2685036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2686036a4a7dSZhenyu Wang 
2687036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2688bdfcdb63SDaniel Vetter 
2689036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2690036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
26913143a2bfSChris Wilson 	POSTING_READ(DEIER);
2692036a4a7dSZhenyu Wang 
2693d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2694c650156aSZhenyu Wang 
269591738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
26967d99163dSBen Widawsky }
26977d99163dSBen Widawsky 
26987e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
26997e231dbeSJesse Barnes {
27007e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
27017e231dbeSJesse Barnes 	int pipe;
27027e231dbeSJesse Barnes 
27037e231dbeSJesse Barnes 	/* VLV magic */
27047e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
27057e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
27067e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
27077e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
27087e231dbeSJesse Barnes 
27097e231dbeSJesse Barnes 	/* and GT */
27107e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
27117e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2712d18ea1b5SDaniel Vetter 
2713d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
27147e231dbeSJesse Barnes 
27157e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
27167e231dbeSJesse Barnes 
27177e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
27187e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
27197e231dbeSJesse Barnes 	for_each_pipe(pipe)
27207e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
27217e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
27227e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
27237e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
27247e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
27257e231dbeSJesse Barnes }
27267e231dbeSJesse Barnes 
2727abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2728abd58f01SBen Widawsky {
2729abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2730abd58f01SBen Widawsky 	int pipe;
2731abd58f01SBen Widawsky 
2732abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2733abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2734abd58f01SBen Widawsky 
2735abd58f01SBen Widawsky 	/* IIR can theoretically queue up two events. Be paranoid */
2736abd58f01SBen Widawsky #define GEN8_IRQ_INIT_NDX(type, which) do { \
2737abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2738abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR(which)); \
2739abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2740abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2741abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR(which)); \
2742abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2743abd58f01SBen Widawsky 	} while (0)
2744abd58f01SBen Widawsky 
2745abd58f01SBen Widawsky #define GEN8_IRQ_INIT(type) do { \
2746abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2747abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR); \
2748abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2749abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2750abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR); \
2751abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2752abd58f01SBen Widawsky 	} while (0)
2753abd58f01SBen Widawsky 
2754abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 0);
2755abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 1);
2756abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 2);
2757abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 3);
2758abd58f01SBen Widawsky 
2759abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2760abd58f01SBen Widawsky 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe);
2761abd58f01SBen Widawsky 	}
2762abd58f01SBen Widawsky 
2763abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_PORT);
2764abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_MISC);
2765abd58f01SBen Widawsky 	GEN8_IRQ_INIT(PCU);
2766abd58f01SBen Widawsky #undef GEN8_IRQ_INIT
2767abd58f01SBen Widawsky #undef GEN8_IRQ_INIT_NDX
2768abd58f01SBen Widawsky 
2769abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
277009f2344dSJesse Barnes 
277109f2344dSJesse Barnes 	ibx_irq_preinstall(dev);
2772abd58f01SBen Widawsky }
2773abd58f01SBen Widawsky 
277482a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
277582a28bcfSDaniel Vetter {
277682a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
277782a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
277882a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2779fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
278082a28bcfSDaniel Vetter 
278182a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2782fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
278382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2784cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2785fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
278682a28bcfSDaniel Vetter 	} else {
2787fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
278882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2789cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2790fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
279182a28bcfSDaniel Vetter 	}
279282a28bcfSDaniel Vetter 
2793fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
279482a28bcfSDaniel Vetter 
27957fe0b973SKeith Packard 	/*
27967fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
27977fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
27987fe0b973SKeith Packard 	 *
27997fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
28007fe0b973SKeith Packard 	 */
28017fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
28027fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
28037fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
28047fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
28057fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
28067fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
28077fe0b973SKeith Packard }
28087fe0b973SKeith Packard 
2809d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2810d46da437SPaulo Zanoni {
2811d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
281282a28bcfSDaniel Vetter 	u32 mask;
2813d46da437SPaulo Zanoni 
2814692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2815692a04cfSDaniel Vetter 		return;
2816692a04cfSDaniel Vetter 
28178664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
28188664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2819de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
28208664281bSPaulo Zanoni 	} else {
28218664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
28228664281bSPaulo Zanoni 
28238664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
28248664281bSPaulo Zanoni 	}
2825ab5c608bSBen Widawsky 
2826d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2827d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2828d46da437SPaulo Zanoni }
2829d46da437SPaulo Zanoni 
28300a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
28310a9a8c91SDaniel Vetter {
28320a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
28330a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
28340a9a8c91SDaniel Vetter 
28350a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
28360a9a8c91SDaniel Vetter 
28370a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2838040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
28390a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
284035a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
284135a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
28420a9a8c91SDaniel Vetter 	}
28430a9a8c91SDaniel Vetter 
28440a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
28450a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
28460a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
28470a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
28480a9a8c91SDaniel Vetter 	} else {
28490a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
28500a9a8c91SDaniel Vetter 	}
28510a9a8c91SDaniel Vetter 
28520a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
28530a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
28540a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
28550a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
28560a9a8c91SDaniel Vetter 
28570a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
28580a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
28590a9a8c91SDaniel Vetter 
28600a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
28610a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
28620a9a8c91SDaniel Vetter 
2863605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
28640a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2865605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
28660a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
28670a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
28680a9a8c91SDaniel Vetter 	}
28690a9a8c91SDaniel Vetter }
28700a9a8c91SDaniel Vetter 
2871f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2872036a4a7dSZhenyu Wang {
28734bc9d430SDaniel Vetter 	unsigned long irqflags;
2874036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28758e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
28768e76f8dcSPaulo Zanoni 
28778e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
28788e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
28798e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
28808e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
28818e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
28828e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
28838e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
28848e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
28858e76f8dcSPaulo Zanoni 
28868e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
28878e76f8dcSPaulo Zanoni 	} else {
28888e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
2889ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
28905b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
28915b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
28925b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
28935b3a856bSDaniel Vetter 				DE_POISON);
28948e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
28958e76f8dcSPaulo Zanoni 	}
2896036a4a7dSZhenyu Wang 
28971ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
2898036a4a7dSZhenyu Wang 
2899036a4a7dSZhenyu Wang 	/* should always can generate irq */
2900036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
29011ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
29028e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
29033143a2bfSChris Wilson 	POSTING_READ(DEIER);
2904036a4a7dSZhenyu Wang 
29050a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
2906036a4a7dSZhenyu Wang 
2907d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
29087fe0b973SKeith Packard 
2909f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
29106005ce42SDaniel Vetter 		/* Enable PCU event interrupts
29116005ce42SDaniel Vetter 		 *
29126005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
29134bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
29144bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
29154bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2916f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
29174bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2918f97108d1SJesse Barnes 	}
2919f97108d1SJesse Barnes 
2920036a4a7dSZhenyu Wang 	return 0;
2921036a4a7dSZhenyu Wang }
2922036a4a7dSZhenyu Wang 
29237e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
29247e231dbeSJesse Barnes {
29257e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29267e231dbeSJesse Barnes 	u32 enable_mask;
2927379ef82dSDaniel Vetter 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_EN_VLV |
2928379ef82dSDaniel Vetter 		PIPE_CRC_DONE_ENABLE;
2929b79480baSDaniel Vetter 	unsigned long irqflags;
29307e231dbeSJesse Barnes 
29317e231dbeSJesse Barnes 	enable_mask = I915_DISPLAY_PORT_INTERRUPT;
293231acc7f5SJesse Barnes 	enable_mask |= I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
293331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
293431acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
29357e231dbeSJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
29367e231dbeSJesse Barnes 
293731acc7f5SJesse Barnes 	/*
293831acc7f5SJesse Barnes 	 *Leave vblank interrupts masked initially.  enable/disable will
293931acc7f5SJesse Barnes 	 * toggle them based on usage.
294031acc7f5SJesse Barnes 	 */
294131acc7f5SJesse Barnes 	dev_priv->irq_mask = (~enable_mask) |
294231acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_A_VBLANK_INTERRUPT |
294331acc7f5SJesse Barnes 		I915_DISPLAY_PIPE_B_VBLANK_INTERRUPT;
29447e231dbeSJesse Barnes 
294520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
294620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
294720afbda2SDaniel Vetter 
29487e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
29497e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, enable_mask);
29507e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29517e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(0), 0xffff);
29527e231dbeSJesse Barnes 	I915_WRITE(PIPESTAT(1), 0xffff);
29537e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
29547e231dbeSJesse Barnes 
2955b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
2956b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
2957b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
29583b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_enable);
29593b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
29603b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_enable);
2961b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
296231acc7f5SJesse Barnes 
29637e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29647e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29657e231dbeSJesse Barnes 
29660a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
29677e231dbeSJesse Barnes 
29687e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
29697e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
29707e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
29717e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
29727e231dbeSJesse Barnes #endif
29737e231dbeSJesse Barnes 
29747e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
297520afbda2SDaniel Vetter 
297620afbda2SDaniel Vetter 	return 0;
297720afbda2SDaniel Vetter }
297820afbda2SDaniel Vetter 
2979abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
2980abd58f01SBen Widawsky {
2981abd58f01SBen Widawsky 	int i;
2982abd58f01SBen Widawsky 
2983abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
2984abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
2985abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
2986abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
2987abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
2988abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
2989abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
2990abd58f01SBen Widawsky 		0,
2991abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
2992abd58f01SBen Widawsky 		};
2993abd58f01SBen Widawsky 
2994abd58f01SBen Widawsky 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++) {
2995abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_GT_IIR(i));
2996abd58f01SBen Widawsky 		if (tmp)
2997abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
2998abd58f01SBen Widawsky 				  i, tmp);
2999abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IMR(i), ~gt_interrupts[i]);
3000abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IER(i), gt_interrupts[i]);
3001abd58f01SBen Widawsky 	}
3002abd58f01SBen Widawsky 	POSTING_READ(GEN8_GT_IER(0));
3003abd58f01SBen Widawsky }
3004abd58f01SBen Widawsky 
3005abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3006abd58f01SBen Widawsky {
3007abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
300813b3a0a7SDaniel Vetter 	uint32_t de_pipe_masked = GEN8_PIPE_FLIP_DONE |
30090fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
301038d83c96SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN |
301130100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
301213b3a0a7SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK;
3013abd58f01SBen Widawsky 	int pipe;
301413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
301513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
301613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3017abd58f01SBen Widawsky 
3018abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3019abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_DE_PIPE_IIR(pipe));
3020abd58f01SBen Widawsky 		if (tmp)
3021abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3022abd58f01SBen Widawsky 				  pipe, tmp);
3023abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
3024abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IER(pipe), de_pipe_enables);
3025abd58f01SBen Widawsky 	}
3026abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_ISR(0));
3027abd58f01SBen Widawsky 
30286d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IMR, ~GEN8_AUX_CHANNEL_A);
30296d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IER, GEN8_AUX_CHANNEL_A);
3030abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PORT_IER);
3031abd58f01SBen Widawsky }
3032abd58f01SBen Widawsky 
3033abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3034abd58f01SBen Widawsky {
3035abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3036abd58f01SBen Widawsky 
3037abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3038abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3039abd58f01SBen Widawsky 
3040abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3041abd58f01SBen Widawsky 
3042abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3043abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3044abd58f01SBen Widawsky 
3045abd58f01SBen Widawsky 	return 0;
3046abd58f01SBen Widawsky }
3047abd58f01SBen Widawsky 
3048abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3049abd58f01SBen Widawsky {
3050abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3051abd58f01SBen Widawsky 	int pipe;
3052abd58f01SBen Widawsky 
3053abd58f01SBen Widawsky 	if (!dev_priv)
3054abd58f01SBen Widawsky 		return;
3055abd58f01SBen Widawsky 
3056abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3057abd58f01SBen Widawsky 
3058abd58f01SBen Widawsky #define GEN8_IRQ_FINI_NDX(type, which) do { \
3059abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
3060abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
3061abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3062abd58f01SBen Widawsky 	} while (0)
3063abd58f01SBen Widawsky 
3064abd58f01SBen Widawsky #define GEN8_IRQ_FINI(type) do { \
3065abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
3066abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
3067abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3068abd58f01SBen Widawsky 	} while (0)
3069abd58f01SBen Widawsky 
3070abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 0);
3071abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 1);
3072abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 2);
3073abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 3);
3074abd58f01SBen Widawsky 
3075abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3076abd58f01SBen Widawsky 		GEN8_IRQ_FINI_NDX(DE_PIPE, pipe);
3077abd58f01SBen Widawsky 	}
3078abd58f01SBen Widawsky 
3079abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_PORT);
3080abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_MISC);
3081abd58f01SBen Widawsky 	GEN8_IRQ_FINI(PCU);
3082abd58f01SBen Widawsky #undef GEN8_IRQ_FINI
3083abd58f01SBen Widawsky #undef GEN8_IRQ_FINI_NDX
3084abd58f01SBen Widawsky 
3085abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
3086abd58f01SBen Widawsky }
3087abd58f01SBen Widawsky 
30887e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
30897e231dbeSJesse Barnes {
30907e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30917e231dbeSJesse Barnes 	int pipe;
30927e231dbeSJesse Barnes 
30937e231dbeSJesse Barnes 	if (!dev_priv)
30947e231dbeSJesse Barnes 		return;
30957e231dbeSJesse Barnes 
30963ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3097ac4c16c5SEgbert Eich 
30987e231dbeSJesse Barnes 	for_each_pipe(pipe)
30997e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
31007e231dbeSJesse Barnes 
31017e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
31027e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
31037e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
31047e231dbeSJesse Barnes 	for_each_pipe(pipe)
31057e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
31067e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31077e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
31087e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
31097e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31107e231dbeSJesse Barnes }
31117e231dbeSJesse Barnes 
3112f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3113036a4a7dSZhenyu Wang {
3114036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
31154697995bSJesse Barnes 
31164697995bSJesse Barnes 	if (!dev_priv)
31174697995bSJesse Barnes 		return;
31184697995bSJesse Barnes 
31193ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3120ac4c16c5SEgbert Eich 
3121036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
3122036a4a7dSZhenyu Wang 
3123036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
3124036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
3125036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
31268664281bSPaulo Zanoni 	if (IS_GEN7(dev))
31278664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
3128036a4a7dSZhenyu Wang 
3129036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
3130036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
3131036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3132192aac1fSKeith Packard 
3133ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
3134ab5c608bSBen Widawsky 		return;
3135ab5c608bSBen Widawsky 
3136192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
3137192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
3138192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
31398664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
31408664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
3141036a4a7dSZhenyu Wang }
3142036a4a7dSZhenyu Wang 
3143c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3144c2798b19SChris Wilson {
3145c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3146c2798b19SChris Wilson 	int pipe;
3147c2798b19SChris Wilson 
3148c2798b19SChris Wilson 	for_each_pipe(pipe)
3149c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3150c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3151c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3152c2798b19SChris Wilson 	POSTING_READ16(IER);
3153c2798b19SChris Wilson }
3154c2798b19SChris Wilson 
3155c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3156c2798b19SChris Wilson {
3157c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3158379ef82dSDaniel Vetter 	unsigned long irqflags;
3159c2798b19SChris Wilson 
3160c2798b19SChris Wilson 	I915_WRITE16(EMR,
3161c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3162c2798b19SChris Wilson 
3163c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3164c2798b19SChris Wilson 	dev_priv->irq_mask =
3165c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3166c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3167c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3168c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3169c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3170c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3171c2798b19SChris Wilson 
3172c2798b19SChris Wilson 	I915_WRITE16(IER,
3173c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3174c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3175c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3176c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3177c2798b19SChris Wilson 	POSTING_READ16(IER);
3178c2798b19SChris Wilson 
3179379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3180379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3181379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
31823b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
31833b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3184379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3185379ef82dSDaniel Vetter 
3186c2798b19SChris Wilson 	return 0;
3187c2798b19SChris Wilson }
3188c2798b19SChris Wilson 
318990a72f87SVille Syrjälä /*
319090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
319190a72f87SVille Syrjälä  */
319290a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
31931f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
319490a72f87SVille Syrjälä {
319590a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
31961f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
319790a72f87SVille Syrjälä 
319890a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
319990a72f87SVille Syrjälä 		return false;
320090a72f87SVille Syrjälä 
320190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
320290a72f87SVille Syrjälä 		return false;
320390a72f87SVille Syrjälä 
32041f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
320590a72f87SVille Syrjälä 
320690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
320790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
320890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
320990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
321090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
321190a72f87SVille Syrjälä 	 */
321290a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
321390a72f87SVille Syrjälä 		return false;
321490a72f87SVille Syrjälä 
321590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
321690a72f87SVille Syrjälä 
321790a72f87SVille Syrjälä 	return true;
321890a72f87SVille Syrjälä }
321990a72f87SVille Syrjälä 
3220ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3221c2798b19SChris Wilson {
3222c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3223c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3224c2798b19SChris Wilson 	u16 iir, new_iir;
3225c2798b19SChris Wilson 	u32 pipe_stats[2];
3226c2798b19SChris Wilson 	unsigned long irqflags;
3227c2798b19SChris Wilson 	int pipe;
3228c2798b19SChris Wilson 	u16 flip_mask =
3229c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3230c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3231c2798b19SChris Wilson 
3232c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3233c2798b19SChris Wilson 	if (iir == 0)
3234c2798b19SChris Wilson 		return IRQ_NONE;
3235c2798b19SChris Wilson 
3236c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3237c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3238c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3239c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3240c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3241c2798b19SChris Wilson 		 */
3242c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3243c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3244c2798b19SChris Wilson 			i915_handle_error(dev, false);
3245c2798b19SChris Wilson 
3246c2798b19SChris Wilson 		for_each_pipe(pipe) {
3247c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3248c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3249c2798b19SChris Wilson 
3250c2798b19SChris Wilson 			/*
3251c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3252c2798b19SChris Wilson 			 */
32532d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3254c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3255c2798b19SChris Wilson 		}
3256c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3257c2798b19SChris Wilson 
3258c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3259c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3260c2798b19SChris Wilson 
3261d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3262c2798b19SChris Wilson 
3263c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3264c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3265c2798b19SChris Wilson 
32664356d586SDaniel Vetter 		for_each_pipe(pipe) {
32671f1c2e24SVille Syrjälä 			int plane = pipe;
32683a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
32691f1c2e24SVille Syrjälä 				plane = !plane;
32701f1c2e24SVille Syrjälä 
32714356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
32721f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
32731f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3274c2798b19SChris Wilson 
32754356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3276277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
32772d9d2b0bSVille Syrjälä 
32782d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
32792d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3280fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
32814356d586SDaniel Vetter 		}
3282c2798b19SChris Wilson 
3283c2798b19SChris Wilson 		iir = new_iir;
3284c2798b19SChris Wilson 	}
3285c2798b19SChris Wilson 
3286c2798b19SChris Wilson 	return IRQ_HANDLED;
3287c2798b19SChris Wilson }
3288c2798b19SChris Wilson 
3289c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3290c2798b19SChris Wilson {
3291c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3292c2798b19SChris Wilson 	int pipe;
3293c2798b19SChris Wilson 
3294c2798b19SChris Wilson 	for_each_pipe(pipe) {
3295c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3296c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3297c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3298c2798b19SChris Wilson 	}
3299c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3300c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3301c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3302c2798b19SChris Wilson }
3303c2798b19SChris Wilson 
3304a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3305a266c7d5SChris Wilson {
3306a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3307a266c7d5SChris Wilson 	int pipe;
3308a266c7d5SChris Wilson 
3309a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3310a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3311a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3312a266c7d5SChris Wilson 	}
3313a266c7d5SChris Wilson 
331400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3315a266c7d5SChris Wilson 	for_each_pipe(pipe)
3316a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3317a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3318a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3319a266c7d5SChris Wilson 	POSTING_READ(IER);
3320a266c7d5SChris Wilson }
3321a266c7d5SChris Wilson 
3322a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3323a266c7d5SChris Wilson {
3324a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
332538bde180SChris Wilson 	u32 enable_mask;
3326379ef82dSDaniel Vetter 	unsigned long irqflags;
3327a266c7d5SChris Wilson 
332838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
332938bde180SChris Wilson 
333038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
333138bde180SChris Wilson 	dev_priv->irq_mask =
333238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
333338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
333438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
333538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
333638bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
333738bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
333838bde180SChris Wilson 
333938bde180SChris Wilson 	enable_mask =
334038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
334138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
334238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
334338bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
334438bde180SChris Wilson 		I915_USER_INTERRUPT;
334538bde180SChris Wilson 
3346a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
334720afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
334820afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
334920afbda2SDaniel Vetter 
3350a266c7d5SChris Wilson 		/* Enable in IER... */
3351a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3352a266c7d5SChris Wilson 		/* and unmask in IMR */
3353a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3354a266c7d5SChris Wilson 	}
3355a266c7d5SChris Wilson 
3356a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3357a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3358a266c7d5SChris Wilson 	POSTING_READ(IER);
3359a266c7d5SChris Wilson 
3360f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
336120afbda2SDaniel Vetter 
3362379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3363379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3364379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
33653b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
33663b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3367379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3368379ef82dSDaniel Vetter 
336920afbda2SDaniel Vetter 	return 0;
337020afbda2SDaniel Vetter }
337120afbda2SDaniel Vetter 
337290a72f87SVille Syrjälä /*
337390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
337490a72f87SVille Syrjälä  */
337590a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
337690a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
337790a72f87SVille Syrjälä {
337890a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
337990a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
338090a72f87SVille Syrjälä 
338190a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
338290a72f87SVille Syrjälä 		return false;
338390a72f87SVille Syrjälä 
338490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
338590a72f87SVille Syrjälä 		return false;
338690a72f87SVille Syrjälä 
338790a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
338890a72f87SVille Syrjälä 
338990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
339090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
339190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
339290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
339390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
339490a72f87SVille Syrjälä 	 */
339590a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
339690a72f87SVille Syrjälä 		return false;
339790a72f87SVille Syrjälä 
339890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
339990a72f87SVille Syrjälä 
340090a72f87SVille Syrjälä 	return true;
340190a72f87SVille Syrjälä }
340290a72f87SVille Syrjälä 
3403ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3404a266c7d5SChris Wilson {
3405a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3406a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
34078291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3408a266c7d5SChris Wilson 	unsigned long irqflags;
340938bde180SChris Wilson 	u32 flip_mask =
341038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
341138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
341238bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3413a266c7d5SChris Wilson 
3414a266c7d5SChris Wilson 	iir = I915_READ(IIR);
341538bde180SChris Wilson 	do {
341638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
34178291ee90SChris Wilson 		bool blc_event = false;
3418a266c7d5SChris Wilson 
3419a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3420a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3421a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3422a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3423a266c7d5SChris Wilson 		 */
3424a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3425a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3426a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3427a266c7d5SChris Wilson 
3428a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3429a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3430a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3431a266c7d5SChris Wilson 
343238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3433a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3434a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
343538bde180SChris Wilson 				irq_received = true;
3436a266c7d5SChris Wilson 			}
3437a266c7d5SChris Wilson 		}
3438a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3439a266c7d5SChris Wilson 
3440a266c7d5SChris Wilson 		if (!irq_received)
3441a266c7d5SChris Wilson 			break;
3442a266c7d5SChris Wilson 
3443a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3444a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3445a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3446a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3447b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3448a266c7d5SChris Wilson 
344910a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
345091d131d2SDaniel Vetter 
3451a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
345238bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3453a266c7d5SChris Wilson 		}
3454a266c7d5SChris Wilson 
345538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3456a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3457a266c7d5SChris Wilson 
3458a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3459a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3460a266c7d5SChris Wilson 
3461a266c7d5SChris Wilson 		for_each_pipe(pipe) {
346238bde180SChris Wilson 			int plane = pipe;
34633a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
346438bde180SChris Wilson 				plane = !plane;
34655e2032d4SVille Syrjälä 
346690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
346790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
346890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3469a266c7d5SChris Wilson 
3470a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3471a266c7d5SChris Wilson 				blc_event = true;
34724356d586SDaniel Vetter 
34734356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3474277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
34752d9d2b0bSVille Syrjälä 
34762d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
34772d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3478fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
3479a266c7d5SChris Wilson 		}
3480a266c7d5SChris Wilson 
3481a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3482a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3483a266c7d5SChris Wilson 
3484a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3485a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3486a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3487a266c7d5SChris Wilson 		 * we would never get another interrupt.
3488a266c7d5SChris Wilson 		 *
3489a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3490a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3491a266c7d5SChris Wilson 		 * another one.
3492a266c7d5SChris Wilson 		 *
3493a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3494a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3495a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3496a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3497a266c7d5SChris Wilson 		 * stray interrupts.
3498a266c7d5SChris Wilson 		 */
349938bde180SChris Wilson 		ret = IRQ_HANDLED;
3500a266c7d5SChris Wilson 		iir = new_iir;
350138bde180SChris Wilson 	} while (iir & ~flip_mask);
3502a266c7d5SChris Wilson 
3503d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
35048291ee90SChris Wilson 
3505a266c7d5SChris Wilson 	return ret;
3506a266c7d5SChris Wilson }
3507a266c7d5SChris Wilson 
3508a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3509a266c7d5SChris Wilson {
3510a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3511a266c7d5SChris Wilson 	int pipe;
3512a266c7d5SChris Wilson 
35133ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3514ac4c16c5SEgbert Eich 
3515a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3516a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3517a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3518a266c7d5SChris Wilson 	}
3519a266c7d5SChris Wilson 
352000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
352155b39755SChris Wilson 	for_each_pipe(pipe) {
352255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3523a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
352455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
352555b39755SChris Wilson 	}
3526a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3527a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3528a266c7d5SChris Wilson 
3529a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3530a266c7d5SChris Wilson }
3531a266c7d5SChris Wilson 
3532a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3533a266c7d5SChris Wilson {
3534a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3535a266c7d5SChris Wilson 	int pipe;
3536a266c7d5SChris Wilson 
3537a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3538a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3539a266c7d5SChris Wilson 
3540a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3541a266c7d5SChris Wilson 	for_each_pipe(pipe)
3542a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3543a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3544a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3545a266c7d5SChris Wilson 	POSTING_READ(IER);
3546a266c7d5SChris Wilson }
3547a266c7d5SChris Wilson 
3548a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3549a266c7d5SChris Wilson {
3550a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3551bbba0a97SChris Wilson 	u32 enable_mask;
3552a266c7d5SChris Wilson 	u32 error_mask;
3553b79480baSDaniel Vetter 	unsigned long irqflags;
3554a266c7d5SChris Wilson 
3555a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3556bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3557adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3558bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3559bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3560bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3561bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3562bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3563bbba0a97SChris Wilson 
3564bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
356521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
356621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3567bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3568bbba0a97SChris Wilson 
3569bbba0a97SChris Wilson 	if (IS_G4X(dev))
3570bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3571a266c7d5SChris Wilson 
3572b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3573b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3574b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
35753b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_EVENT_ENABLE);
35763b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_ENABLE);
35773b6c42e8SDaniel Vetter 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_ENABLE);
3578b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3579a266c7d5SChris Wilson 
3580a266c7d5SChris Wilson 	/*
3581a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3582a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3583a266c7d5SChris Wilson 	 */
3584a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3585a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3586a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3587a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3588a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3589a266c7d5SChris Wilson 	} else {
3590a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3591a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3592a266c7d5SChris Wilson 	}
3593a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3594a266c7d5SChris Wilson 
3595a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3596a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3597a266c7d5SChris Wilson 	POSTING_READ(IER);
3598a266c7d5SChris Wilson 
359920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
360020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
360120afbda2SDaniel Vetter 
3602f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
360320afbda2SDaniel Vetter 
360420afbda2SDaniel Vetter 	return 0;
360520afbda2SDaniel Vetter }
360620afbda2SDaniel Vetter 
3607bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
360820afbda2SDaniel Vetter {
360920afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3610e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3611cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
361220afbda2SDaniel Vetter 	u32 hotplug_en;
361320afbda2SDaniel Vetter 
3614b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3615b5ea2d56SDaniel Vetter 
3616bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3617bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3618bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3619adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3620e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3621cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3622cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3623cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3624a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3625a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3626a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3627a266c7d5SChris Wilson 		*/
3628a266c7d5SChris Wilson 		if (IS_G4X(dev))
3629a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
363085fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3631a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3632a266c7d5SChris Wilson 
3633a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3634a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3635a266c7d5SChris Wilson 	}
3636bac56d5bSEgbert Eich }
3637a266c7d5SChris Wilson 
3638ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3639a266c7d5SChris Wilson {
3640a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3641a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3642a266c7d5SChris Wilson 	u32 iir, new_iir;
3643a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3644a266c7d5SChris Wilson 	unsigned long irqflags;
3645a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
364621ad8330SVille Syrjälä 	u32 flip_mask =
364721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
364821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3649a266c7d5SChris Wilson 
3650a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3651a266c7d5SChris Wilson 
3652a266c7d5SChris Wilson 	for (;;) {
3653501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
36542c8ba29fSChris Wilson 		bool blc_event = false;
36552c8ba29fSChris Wilson 
3656a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3657a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3658a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3659a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3660a266c7d5SChris Wilson 		 */
3661a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3662a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3663a266c7d5SChris Wilson 			i915_handle_error(dev, false);
3664a266c7d5SChris Wilson 
3665a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3666a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3667a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3668a266c7d5SChris Wilson 
3669a266c7d5SChris Wilson 			/*
3670a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3671a266c7d5SChris Wilson 			 */
3672a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3673a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3674501e01d7SVille Syrjälä 				irq_received = true;
3675a266c7d5SChris Wilson 			}
3676a266c7d5SChris Wilson 		}
3677a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3678a266c7d5SChris Wilson 
3679a266c7d5SChris Wilson 		if (!irq_received)
3680a266c7d5SChris Wilson 			break;
3681a266c7d5SChris Wilson 
3682a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3683a266c7d5SChris Wilson 
3684a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3685adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3686a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3687b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3688b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
36894f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3690a266c7d5SChris Wilson 
369110a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
3692704cfb87SDaniel Vetter 					      IS_G4X(dev) ? hpd_status_g4x : hpd_status_i915);
369391d131d2SDaniel Vetter 
36944aeebd74SDaniel Vetter 			if (IS_G4X(dev) &&
36954aeebd74SDaniel Vetter 			    (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X))
36964aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
36974aeebd74SDaniel Vetter 
3698a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3699a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3700a266c7d5SChris Wilson 		}
3701a266c7d5SChris Wilson 
370221ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3703a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3704a266c7d5SChris Wilson 
3705a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3706a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3707a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3708a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3709a266c7d5SChris Wilson 
3710a266c7d5SChris Wilson 		for_each_pipe(pipe) {
37112c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
371290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
371390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3714a266c7d5SChris Wilson 
3715a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3716a266c7d5SChris Wilson 				blc_event = true;
37174356d586SDaniel Vetter 
37184356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3719277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3720a266c7d5SChris Wilson 
37212d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
37222d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3723fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
37242d9d2b0bSVille Syrjälä 		}
3725a266c7d5SChris Wilson 
3726a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3727a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3728a266c7d5SChris Wilson 
3729515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3730515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3731515ac2bbSDaniel Vetter 
3732a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3733a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3734a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3735a266c7d5SChris Wilson 		 * we would never get another interrupt.
3736a266c7d5SChris Wilson 		 *
3737a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3738a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3739a266c7d5SChris Wilson 		 * another one.
3740a266c7d5SChris Wilson 		 *
3741a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3742a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3743a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3744a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3745a266c7d5SChris Wilson 		 * stray interrupts.
3746a266c7d5SChris Wilson 		 */
3747a266c7d5SChris Wilson 		iir = new_iir;
3748a266c7d5SChris Wilson 	}
3749a266c7d5SChris Wilson 
3750d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
37512c8ba29fSChris Wilson 
3752a266c7d5SChris Wilson 	return ret;
3753a266c7d5SChris Wilson }
3754a266c7d5SChris Wilson 
3755a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3756a266c7d5SChris Wilson {
3757a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3758a266c7d5SChris Wilson 	int pipe;
3759a266c7d5SChris Wilson 
3760a266c7d5SChris Wilson 	if (!dev_priv)
3761a266c7d5SChris Wilson 		return;
3762a266c7d5SChris Wilson 
37633ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3764ac4c16c5SEgbert Eich 
3765a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3766a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3767a266c7d5SChris Wilson 
3768a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3769a266c7d5SChris Wilson 	for_each_pipe(pipe)
3770a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3771a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3772a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3773a266c7d5SChris Wilson 
3774a266c7d5SChris Wilson 	for_each_pipe(pipe)
3775a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3776a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3777a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3778a266c7d5SChris Wilson }
3779a266c7d5SChris Wilson 
37803ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
3781ac4c16c5SEgbert Eich {
3782ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3783ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3784ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3785ac4c16c5SEgbert Eich 	unsigned long irqflags;
3786ac4c16c5SEgbert Eich 	int i;
3787ac4c16c5SEgbert Eich 
3788ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3789ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3790ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3791ac4c16c5SEgbert Eich 
3792ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3793ac4c16c5SEgbert Eich 			continue;
3794ac4c16c5SEgbert Eich 
3795ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3796ac4c16c5SEgbert Eich 
3797ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3798ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3799ac4c16c5SEgbert Eich 
3800ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
3801ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
3802ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
3803ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
3804ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
3805ac4c16c5SEgbert Eich 				if (!connector->polled)
3806ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
3807ac4c16c5SEgbert Eich 			}
3808ac4c16c5SEgbert Eich 		}
3809ac4c16c5SEgbert Eich 	}
3810ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
3811ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
3812ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3813ac4c16c5SEgbert Eich }
3814ac4c16c5SEgbert Eich 
3815f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
3816f71d4af4SJesse Barnes {
38178b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
38188b2e326dSChris Wilson 
38198b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
382099584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
3821c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
3822a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
38238b2e326dSChris Wilson 
382499584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
382599584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
382661bac78eSDaniel Vetter 		    (unsigned long) dev);
38273ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
3828ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
382961bac78eSDaniel Vetter 
383097a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
38319ee32feaSDaniel Vetter 
38324cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
38334cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
38344cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
38354cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
3836f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
3837f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
3838391f75e2SVille Syrjälä 	} else {
3839391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
3840391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
3841f71d4af4SJesse Barnes 	}
3842f71d4af4SJesse Barnes 
3843c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
3844f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
3845f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
3846c2baf4b7SVille Syrjälä 	}
3847f71d4af4SJesse Barnes 
38487e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
38497e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
38507e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
38517e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
38527e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
38537e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
38547e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
3855fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3856abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
3857abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
3858abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
3859abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
3860abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
3861abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
3862abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
3863abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3864f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
3865f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
3866f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
3867f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
3868f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
3869f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
3870f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
387182a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
3872f71d4af4SJesse Barnes 	} else {
3873c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
3874c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
3875c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
3876c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
3877c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
3878a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
3879a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
3880a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
3881a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
3882a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
388320afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3884c2798b19SChris Wilson 		} else {
3885a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
3886a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
3887a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
3888a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
3889bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
3890c2798b19SChris Wilson 		}
3891f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
3892f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
3893f71d4af4SJesse Barnes 	}
3894f71d4af4SJesse Barnes }
389520afbda2SDaniel Vetter 
389620afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
389720afbda2SDaniel Vetter {
389820afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3899821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3900821450c6SEgbert Eich 	struct drm_connector *connector;
3901b5ea2d56SDaniel Vetter 	unsigned long irqflags;
3902821450c6SEgbert Eich 	int i;
390320afbda2SDaniel Vetter 
3904821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
3905821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
3906821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3907821450c6SEgbert Eich 	}
3908821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
3909821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
3910821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
3911821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
3912821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
3913821450c6SEgbert Eich 	}
3914b5ea2d56SDaniel Vetter 
3915b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3916b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
3917b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
391820afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
391920afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
3920b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
392120afbda2SDaniel Vetter }
3922c67a470bSPaulo Zanoni 
3923c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
3924c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
3925c67a470bSPaulo Zanoni {
3926c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3927c67a470bSPaulo Zanoni 	unsigned long irqflags;
3928c67a470bSPaulo Zanoni 
3929c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3930c67a470bSPaulo Zanoni 
3931c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
3932c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
3933c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
3934c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
3935c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
3936c67a470bSPaulo Zanoni 
39371f2d4531SPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, 0xffffffff);
39381f2d4531SPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, 0xffffffff);
3939c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
3940c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
3941c67a470bSPaulo Zanoni 
3942c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
3943c67a470bSPaulo Zanoni 
3944c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3945c67a470bSPaulo Zanoni }
3946c67a470bSPaulo Zanoni 
3947c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
3948c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
3949c67a470bSPaulo Zanoni {
3950c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3951c67a470bSPaulo Zanoni 	unsigned long irqflags;
39521f2d4531SPaulo Zanoni 	uint32_t val;
3953c67a470bSPaulo Zanoni 
3954c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3955c67a470bSPaulo Zanoni 
3956c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
39571f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "DEIMR is 0x%08x\n", val);
3958c67a470bSPaulo Zanoni 
39591f2d4531SPaulo Zanoni 	val = I915_READ(SDEIMR);
39601f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "SDEIMR is 0x%08x\n", val);
3961c67a470bSPaulo Zanoni 
3962c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
39631f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GTIMR is 0x%08x\n", val);
3964c67a470bSPaulo Zanoni 
3965c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
39661f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GEN6_PMIMR is 0x%08x\n", val);
3967c67a470bSPaulo Zanoni 
3968c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
3969c67a470bSPaulo Zanoni 
3970c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
39711f2d4531SPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv, ~dev_priv->pc8.regsave.sdeimr);
3972c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
3973c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
3974c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
3975c67a470bSPaulo Zanoni 
3976c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3977c67a470bSPaulo Zanoni }
3978