xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 508774452d657e8d307e1c52682ffcdf743e992d)
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 
895d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
90c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
915d584b2eSPaulo Zanoni 		dev_priv->pm.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 
1075d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
108c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
1095d584b2eSPaulo Zanoni 		dev_priv->pm.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 
1325d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
133c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
1345d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.gtimr &= ~interrupt_mask;
1355d584b2eSPaulo Zanoni 		dev_priv->pm.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 
1705d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
171c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
1725d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.gen6_pmimr &= ~interrupt_mask;
1735d584b2eSPaulo Zanoni 		dev_priv->pm.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 
3165d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled &&
317c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
318c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
3195d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.sdeimr &= ~interrupt_mask;
3205d584b2eSPaulo Zanoni 		dev_priv->pm.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  */
390f88d42f1SImre Deak 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 	bool ret;
3978664281bSPaulo Zanoni 
39877961eb9SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
39977961eb9SImre Deak 
4008664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
4018664281bSPaulo Zanoni 
4028664281bSPaulo Zanoni 	if (enable == ret)
4038664281bSPaulo Zanoni 		goto done;
4048664281bSPaulo Zanoni 
4058664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4068664281bSPaulo Zanoni 
4072d9d2b0bSVille Syrjälä 	if (enable && (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev)))
4082d9d2b0bSVille Syrjälä 		i9xx_clear_fifo_underrun(dev, pipe);
4092d9d2b0bSVille Syrjälä 	else if (IS_GEN5(dev) || IS_GEN6(dev))
4108664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
4118664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
4127336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
41338d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
41438d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
4158664281bSPaulo Zanoni 
4168664281bSPaulo Zanoni done:
417f88d42f1SImre Deak 	return ret;
418f88d42f1SImre Deak }
419f88d42f1SImre Deak 
420f88d42f1SImre Deak bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
421f88d42f1SImre Deak 					   enum pipe pipe, bool enable)
422f88d42f1SImre Deak {
423f88d42f1SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
424f88d42f1SImre Deak 	unsigned long flags;
425f88d42f1SImre Deak 	bool ret;
426f88d42f1SImre Deak 
427f88d42f1SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
428f88d42f1SImre Deak 	ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
4298664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
430f88d42f1SImre Deak 
4318664281bSPaulo Zanoni 	return ret;
4328664281bSPaulo Zanoni }
4338664281bSPaulo Zanoni 
43491d181ddSImre Deak static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
43591d181ddSImre Deak 						  enum pipe pipe)
43691d181ddSImre Deak {
43791d181ddSImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
43891d181ddSImre Deak 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
43991d181ddSImre Deak 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
44091d181ddSImre Deak 
44191d181ddSImre Deak 	return !intel_crtc->cpu_fifo_underrun_disabled;
44291d181ddSImre Deak }
44391d181ddSImre Deak 
4448664281bSPaulo Zanoni /**
4458664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
4468664281bSPaulo Zanoni  * @dev: drm device
4478664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
4488664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4498664281bSPaulo Zanoni  *
4508664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
4518664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
4528664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
4538664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
4548664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
4558664281bSPaulo Zanoni  *
4568664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4578664281bSPaulo Zanoni  */
4588664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
4598664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
4608664281bSPaulo Zanoni 					   bool enable)
4618664281bSPaulo Zanoni {
4628664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
463de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
464de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4658664281bSPaulo Zanoni 	unsigned long flags;
4668664281bSPaulo Zanoni 	bool ret;
4678664281bSPaulo Zanoni 
468de28075dSDaniel Vetter 	/*
469de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
470de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
471de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
472de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
473de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
474de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
475de28075dSDaniel Vetter 	 */
4768664281bSPaulo Zanoni 
4778664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
4788664281bSPaulo Zanoni 
4798664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
4808664281bSPaulo Zanoni 
4818664281bSPaulo Zanoni 	if (enable == ret)
4828664281bSPaulo Zanoni 		goto done;
4838664281bSPaulo Zanoni 
4848664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
4858664281bSPaulo Zanoni 
4868664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
487de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4888664281bSPaulo Zanoni 	else
4898664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
4908664281bSPaulo Zanoni 
4918664281bSPaulo Zanoni done:
4928664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
4938664281bSPaulo Zanoni 	return ret;
4948664281bSPaulo Zanoni }
4958664281bSPaulo Zanoni 
4968664281bSPaulo Zanoni 
497b5ea642aSDaniel Vetter static void
498755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
499755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5007c463586SKeith Packard {
5019db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
502755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5037c463586SKeith Packard 
504b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
505b79480baSDaniel Vetter 
506755e9019SImre Deak 	if (WARN_ON_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
507755e9019SImre Deak 	                 status_mask & ~PIPESTAT_INT_STATUS_MASK))
508755e9019SImre Deak 		return;
509755e9019SImre Deak 
510755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
51146c06a30SVille Syrjälä 		return;
51246c06a30SVille Syrjälä 
51391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
51491d181ddSImre Deak 
5157c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
516755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
51746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5183143a2bfSChris Wilson 	POSTING_READ(reg);
5197c463586SKeith Packard }
5207c463586SKeith Packard 
521b5ea642aSDaniel Vetter static void
522755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
523755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5247c463586SKeith Packard {
5259db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
526755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5277c463586SKeith Packard 
528b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
529b79480baSDaniel Vetter 
530755e9019SImre Deak 	if (WARN_ON_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
531755e9019SImre Deak 	                 status_mask & ~PIPESTAT_INT_STATUS_MASK))
53246c06a30SVille Syrjälä 		return;
53346c06a30SVille Syrjälä 
534755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
535755e9019SImre Deak 		return;
536755e9019SImre Deak 
53791d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
53891d181ddSImre Deak 
539755e9019SImre Deak 	pipestat &= ~enable_mask;
54046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5413143a2bfSChris Wilson 	POSTING_READ(reg);
5427c463586SKeith Packard }
5437c463586SKeith Packard 
54410c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
54510c59c51SImre Deak {
54610c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
54710c59c51SImre Deak 
54810c59c51SImre Deak 	/*
54910c59c51SImre Deak 	 * On pipe A we don't support the PSR interrupt yet, on pipe B the
55010c59c51SImre Deak 	 * same bit MBZ.
55110c59c51SImre Deak 	 */
55210c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
55310c59c51SImre Deak 		return 0;
55410c59c51SImre Deak 
55510c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
55610c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
55710c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
55810c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
55910c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
56010c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
56110c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
56210c59c51SImre Deak 
56310c59c51SImre Deak 	return enable_mask;
56410c59c51SImre Deak }
56510c59c51SImre Deak 
566755e9019SImre Deak void
567755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
568755e9019SImre Deak 		     u32 status_mask)
569755e9019SImre Deak {
570755e9019SImre Deak 	u32 enable_mask;
571755e9019SImre Deak 
57210c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
57310c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
57410c59c51SImre Deak 							   status_mask);
57510c59c51SImre Deak 	else
576755e9019SImre Deak 		enable_mask = status_mask << 16;
577755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
578755e9019SImre Deak }
579755e9019SImre Deak 
580755e9019SImre Deak void
581755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
582755e9019SImre Deak 		      u32 status_mask)
583755e9019SImre Deak {
584755e9019SImre Deak 	u32 enable_mask;
585755e9019SImre Deak 
58610c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
58710c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
58810c59c51SImre Deak 							   status_mask);
58910c59c51SImre Deak 	else
590755e9019SImre Deak 		enable_mask = status_mask << 16;
591755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
592755e9019SImre Deak }
593755e9019SImre Deak 
594c0e09200SDave Airlie /**
595f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
59601c66889SZhao Yakui  */
597f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
59801c66889SZhao Yakui {
5991ec14ad3SChris Wilson 	drm_i915_private_t *dev_priv = dev->dev_private;
6001ec14ad3SChris Wilson 	unsigned long irqflags;
6011ec14ad3SChris Wilson 
602f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
603f49e38ddSJani Nikula 		return;
604f49e38ddSJani Nikula 
6051ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
60601c66889SZhao Yakui 
607755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
608a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6093b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
610755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6111ec14ad3SChris Wilson 
6121ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
61301c66889SZhao Yakui }
61401c66889SZhao Yakui 
61501c66889SZhao Yakui /**
6160a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
6170a3e67a4SJesse Barnes  * @dev: DRM device
6180a3e67a4SJesse Barnes  * @pipe: pipe to check
6190a3e67a4SJesse Barnes  *
6200a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
6210a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
6220a3e67a4SJesse Barnes  * before reading such registers if unsure.
6230a3e67a4SJesse Barnes  */
6240a3e67a4SJesse Barnes static int
6250a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
6260a3e67a4SJesse Barnes {
6270a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
628702e7a56SPaulo Zanoni 
629a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
630a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
631a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
632a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
63371f8ba6bSPaulo Zanoni 
634a01025afSDaniel Vetter 		return intel_crtc->active;
635a01025afSDaniel Vetter 	} else {
636a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
637a01025afSDaniel Vetter 	}
6380a3e67a4SJesse Barnes }
6390a3e67a4SJesse Barnes 
6404cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6414cdb83ecSVille Syrjälä {
6424cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6434cdb83ecSVille Syrjälä 	return 0;
6444cdb83ecSVille Syrjälä }
6454cdb83ecSVille Syrjälä 
64642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
64742f52ef8SKeith Packard  * we use as a pipe index
64842f52ef8SKeith Packard  */
649f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6500a3e67a4SJesse Barnes {
6510a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
6520a3e67a4SJesse Barnes 	unsigned long high_frame;
6530a3e67a4SJesse Barnes 	unsigned long low_frame;
654391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
6550a3e67a4SJesse Barnes 
6560a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
65744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
6589db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
6590a3e67a4SJesse Barnes 		return 0;
6600a3e67a4SJesse Barnes 	}
6610a3e67a4SJesse Barnes 
662391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
663391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
664391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
665391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
666391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
667391f75e2SVille Syrjälä 
668391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
669391f75e2SVille Syrjälä 	} else {
670a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
671391f75e2SVille Syrjälä 		u32 htotal;
672391f75e2SVille Syrjälä 
673391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
674391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
675391f75e2SVille Syrjälä 
676391f75e2SVille Syrjälä 		vbl_start *= htotal;
677391f75e2SVille Syrjälä 	}
678391f75e2SVille Syrjälä 
6799db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6809db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6815eddb70bSChris Wilson 
6820a3e67a4SJesse Barnes 	/*
6830a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6840a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6850a3e67a4SJesse Barnes 	 * register.
6860a3e67a4SJesse Barnes 	 */
6870a3e67a4SJesse Barnes 	do {
6885eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
689391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6905eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6910a3e67a4SJesse Barnes 	} while (high1 != high2);
6920a3e67a4SJesse Barnes 
6935eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
694391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6955eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
696391f75e2SVille Syrjälä 
697391f75e2SVille Syrjälä 	/*
698391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
699391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
700391f75e2SVille Syrjälä 	 * counter against vblank start.
701391f75e2SVille Syrjälä 	 */
702edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7030a3e67a4SJesse Barnes }
7040a3e67a4SJesse Barnes 
705f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7069880b7a5SJesse Barnes {
7079880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7089db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7099880b7a5SJesse Barnes 
7109880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
71144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7129db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7139880b7a5SJesse Barnes 		return 0;
7149880b7a5SJesse Barnes 	}
7159880b7a5SJesse Barnes 
7169880b7a5SJesse Barnes 	return I915_READ(reg);
7179880b7a5SJesse Barnes }
7189880b7a5SJesse Barnes 
719ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
720ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
721ad3543edSMario Kleiner #define __raw_i915_read16(dev_priv__, reg__) readw((dev_priv__)->regs + (reg__))
722ad3543edSMario Kleiner 
723095163baSVille Syrjälä static bool ilk_pipe_in_vblank_locked(struct drm_device *dev, enum pipe pipe)
72454ddcbd2SVille Syrjälä {
72554ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
72654ddcbd2SVille Syrjälä 	uint32_t status;
72754ddcbd2SVille Syrjälä 
728095163baSVille Syrjälä 	if (INTEL_INFO(dev)->gen < 7) {
72954ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
73054ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
73154ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
73254ddcbd2SVille Syrjälä 	} else {
73354ddcbd2SVille Syrjälä 		switch (pipe) {
73454ddcbd2SVille Syrjälä 		default:
73554ddcbd2SVille Syrjälä 		case PIPE_A:
73654ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
73754ddcbd2SVille Syrjälä 			break;
73854ddcbd2SVille Syrjälä 		case PIPE_B:
73954ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
74054ddcbd2SVille Syrjälä 			break;
74154ddcbd2SVille Syrjälä 		case PIPE_C:
74254ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
74354ddcbd2SVille Syrjälä 			break;
74454ddcbd2SVille Syrjälä 		}
74554ddcbd2SVille Syrjälä 	}
746ad3543edSMario Kleiner 
747095163baSVille Syrjälä 	return __raw_i915_read32(dev_priv, DEISR) & status;
74854ddcbd2SVille Syrjälä }
74954ddcbd2SVille Syrjälä 
750f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
751abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
752abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
7530af7e4dfSMario Kleiner {
754c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
755c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
756c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
757c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
7583aa18df8SVille Syrjälä 	int position;
7590af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
7600af7e4dfSMario Kleiner 	bool in_vbl = true;
7610af7e4dfSMario Kleiner 	int ret = 0;
762ad3543edSMario Kleiner 	unsigned long irqflags;
7630af7e4dfSMario Kleiner 
764c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
7650af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7669db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7670af7e4dfSMario Kleiner 		return 0;
7680af7e4dfSMario Kleiner 	}
7690af7e4dfSMario Kleiner 
770c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
771c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
772c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
773c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7740af7e4dfSMario Kleiner 
775d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
776d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
777d31faf65SVille Syrjälä 		vbl_end /= 2;
778d31faf65SVille Syrjälä 		vtotal /= 2;
779d31faf65SVille Syrjälä 	}
780d31faf65SVille Syrjälä 
781c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
782c2baf4b7SVille Syrjälä 
783ad3543edSMario Kleiner 	/*
784ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
785ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
786ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
787ad3543edSMario Kleiner 	 */
788ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
789ad3543edSMario Kleiner 
790ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
791ad3543edSMario Kleiner 
792ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
793ad3543edSMario Kleiner 	if (stime)
794ad3543edSMario Kleiner 		*stime = ktime_get();
795ad3543edSMario Kleiner 
7967c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7970af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7980af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7990af7e4dfSMario Kleiner 		 */
8007c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
801ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
8027c06b08aSVille Syrjälä 		else
803ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
80454ddcbd2SVille Syrjälä 
805095163baSVille Syrjälä 		if (HAS_PCH_SPLIT(dev)) {
80654ddcbd2SVille Syrjälä 			/*
80754ddcbd2SVille Syrjälä 			 * The scanline counter increments at the leading edge
80854ddcbd2SVille Syrjälä 			 * of hsync, ie. it completely misses the active portion
80954ddcbd2SVille Syrjälä 			 * of the line. Fix up the counter at both edges of vblank
81054ddcbd2SVille Syrjälä 			 * to get a more accurate picture whether we're in vblank
81154ddcbd2SVille Syrjälä 			 * or not.
81254ddcbd2SVille Syrjälä 			 */
813095163baSVille Syrjälä 			in_vbl = ilk_pipe_in_vblank_locked(dev, pipe);
81454ddcbd2SVille Syrjälä 			if ((in_vbl && position == vbl_start - 1) ||
81554ddcbd2SVille Syrjälä 			    (!in_vbl && position == vbl_end - 1))
81654ddcbd2SVille Syrjälä 				position = (position + 1) % vtotal;
8170af7e4dfSMario Kleiner 		} else {
818095163baSVille Syrjälä 			/*
819095163baSVille Syrjälä 			 * ISR vblank status bits don't work the way we'd want
820095163baSVille Syrjälä 			 * them to work on non-PCH platforms (for
821095163baSVille Syrjälä 			 * ilk_pipe_in_vblank_locked()), and there doesn't
822095163baSVille Syrjälä 			 * appear any other way to determine if we're currently
823095163baSVille Syrjälä 			 * in vblank.
824095163baSVille Syrjälä 			 *
825095163baSVille Syrjälä 			 * Instead let's assume that we're already in vblank if
826095163baSVille Syrjälä 			 * we got called from the vblank interrupt and the
827095163baSVille Syrjälä 			 * scanline counter value indicates that we're on the
828095163baSVille Syrjälä 			 * line just prior to vblank start. This should result
829095163baSVille Syrjälä 			 * in the correct answer, unless the vblank interrupt
830095163baSVille Syrjälä 			 * delivery really got delayed for almost exactly one
831095163baSVille Syrjälä 			 * full frame/field.
832095163baSVille Syrjälä 			 */
833095163baSVille Syrjälä 			if (flags & DRM_CALLED_FROM_VBLIRQ &&
834095163baSVille Syrjälä 			    position == vbl_start - 1) {
835095163baSVille Syrjälä 				position = (position + 1) % vtotal;
836095163baSVille Syrjälä 
837095163baSVille Syrjälä 				/* Signal this correction as "applied". */
838095163baSVille Syrjälä 				ret |= 0x8;
839095163baSVille Syrjälä 			}
840095163baSVille Syrjälä 		}
841095163baSVille Syrjälä 	} else {
8420af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8430af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8440af7e4dfSMario Kleiner 		 * scanout position.
8450af7e4dfSMario Kleiner 		 */
846ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8470af7e4dfSMario Kleiner 
8483aa18df8SVille Syrjälä 		/* convert to pixel counts */
8493aa18df8SVille Syrjälä 		vbl_start *= htotal;
8503aa18df8SVille Syrjälä 		vbl_end *= htotal;
8513aa18df8SVille Syrjälä 		vtotal *= htotal;
8523aa18df8SVille Syrjälä 	}
8533aa18df8SVille Syrjälä 
854ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
855ad3543edSMario Kleiner 	if (etime)
856ad3543edSMario Kleiner 		*etime = ktime_get();
857ad3543edSMario Kleiner 
858ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
859ad3543edSMario Kleiner 
860ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
861ad3543edSMario Kleiner 
8623aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8633aa18df8SVille Syrjälä 
8643aa18df8SVille Syrjälä 	/*
8653aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8663aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8673aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8683aa18df8SVille Syrjälä 	 * up since vbl_end.
8693aa18df8SVille Syrjälä 	 */
8703aa18df8SVille Syrjälä 	if (position >= vbl_start)
8713aa18df8SVille Syrjälä 		position -= vbl_end;
8723aa18df8SVille Syrjälä 	else
8733aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8743aa18df8SVille Syrjälä 
8757c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8763aa18df8SVille Syrjälä 		*vpos = position;
8773aa18df8SVille Syrjälä 		*hpos = 0;
8783aa18df8SVille Syrjälä 	} else {
8790af7e4dfSMario Kleiner 		*vpos = position / htotal;
8800af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8810af7e4dfSMario Kleiner 	}
8820af7e4dfSMario Kleiner 
8830af7e4dfSMario Kleiner 	/* In vblank? */
8840af7e4dfSMario Kleiner 	if (in_vbl)
8850af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
8860af7e4dfSMario Kleiner 
8870af7e4dfSMario Kleiner 	return ret;
8880af7e4dfSMario Kleiner }
8890af7e4dfSMario Kleiner 
890f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8910af7e4dfSMario Kleiner 			      int *max_error,
8920af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8930af7e4dfSMario Kleiner 			      unsigned flags)
8940af7e4dfSMario Kleiner {
8954041b853SChris Wilson 	struct drm_crtc *crtc;
8960af7e4dfSMario Kleiner 
8977eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8984041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8990af7e4dfSMario Kleiner 		return -EINVAL;
9000af7e4dfSMario Kleiner 	}
9010af7e4dfSMario Kleiner 
9020af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9034041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9044041b853SChris Wilson 	if (crtc == NULL) {
9054041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9064041b853SChris Wilson 		return -EINVAL;
9074041b853SChris Wilson 	}
9084041b853SChris Wilson 
9094041b853SChris Wilson 	if (!crtc->enabled) {
9104041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9114041b853SChris Wilson 		return -EBUSY;
9124041b853SChris Wilson 	}
9130af7e4dfSMario Kleiner 
9140af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9154041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9164041b853SChris Wilson 						     vblank_time, flags,
9177da903efSVille Syrjälä 						     crtc,
9187da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
9190af7e4dfSMario Kleiner }
9200af7e4dfSMario Kleiner 
92167c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
92267c347ffSJani Nikula 				struct drm_connector *connector)
923321a1b30SEgbert Eich {
924321a1b30SEgbert Eich 	enum drm_connector_status old_status;
925321a1b30SEgbert Eich 
926321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
927321a1b30SEgbert Eich 	old_status = connector->status;
928321a1b30SEgbert Eich 
929321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
93067c347ffSJani Nikula 	if (old_status == connector->status)
93167c347ffSJani Nikula 		return false;
93267c347ffSJani Nikula 
93367c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
934321a1b30SEgbert Eich 		      connector->base.id,
935321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
93667c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
93767c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
93867c347ffSJani Nikula 
93967c347ffSJani Nikula 	return true;
940321a1b30SEgbert Eich }
941321a1b30SEgbert Eich 
9425ca58282SJesse Barnes /*
9435ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
9445ca58282SJesse Barnes  */
945ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
946ac4c16c5SEgbert Eich 
9475ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
9485ca58282SJesse Barnes {
9495ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
9505ca58282SJesse Barnes 						    hotplug_work);
9515ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
952c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
953cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
954cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
955cd569aedSEgbert Eich 	struct drm_connector *connector;
956cd569aedSEgbert Eich 	unsigned long irqflags;
957cd569aedSEgbert Eich 	bool hpd_disabled = false;
958321a1b30SEgbert Eich 	bool changed = false;
959142e2398SEgbert Eich 	u32 hpd_event_bits;
9605ca58282SJesse Barnes 
96152d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
96252d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
96352d7ecedSDaniel Vetter 		return;
96452d7ecedSDaniel Vetter 
965a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
966e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
967e67189abSJesse Barnes 
968cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
969142e2398SEgbert Eich 
970142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
971142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
972cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
973cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
974cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
975cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
976cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
977cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
978cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
979cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
980cd569aedSEgbert Eich 				drm_get_connector_name(connector));
981cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
982cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
983cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
984cd569aedSEgbert Eich 			hpd_disabled = true;
985cd569aedSEgbert Eich 		}
986142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
987142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
988142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
989142e2398SEgbert Eich 		}
990cd569aedSEgbert Eich 	}
991cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
992cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
993cd569aedSEgbert Eich 	  * some connectors */
994ac4c16c5SEgbert Eich 	if (hpd_disabled) {
995cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
996ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
997ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
998ac4c16c5SEgbert Eich 	}
999cd569aedSEgbert Eich 
1000cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1001cd569aedSEgbert Eich 
1002321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1003321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
1004321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1005321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1006cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1007cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1008321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1009321a1b30SEgbert Eich 				changed = true;
1010321a1b30SEgbert Eich 		}
1011321a1b30SEgbert Eich 	}
101240ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
101340ee3381SKeith Packard 
1014321a1b30SEgbert Eich 	if (changed)
1015321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
10165ca58282SJesse Barnes }
10175ca58282SJesse Barnes 
10183ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
10193ca1ccedSVille Syrjälä {
10203ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
10213ca1ccedSVille Syrjälä }
10223ca1ccedSVille Syrjälä 
1023d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1024f97108d1SJesse Barnes {
1025f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
1026b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10279270388eSDaniel Vetter 	u8 new_delay;
10289270388eSDaniel Vetter 
1029d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1030f97108d1SJesse Barnes 
103173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
103273edd18fSDaniel Vetter 
103320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10349270388eSDaniel Vetter 
10357648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1036b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1037b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1038f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1039f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1040f97108d1SJesse Barnes 
1041f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1042b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
104320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
104420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
104520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
104620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1047b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
104820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
104920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
105020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
105120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1052f97108d1SJesse Barnes 	}
1053f97108d1SJesse Barnes 
10547648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
105520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1056f97108d1SJesse Barnes 
1057d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10589270388eSDaniel Vetter 
1059f97108d1SJesse Barnes 	return;
1060f97108d1SJesse Barnes }
1061f97108d1SJesse Barnes 
1062549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1063549f7365SChris Wilson 			struct intel_ring_buffer *ring)
1064549f7365SChris Wilson {
1065475553deSChris Wilson 	if (ring->obj == NULL)
1066475553deSChris Wilson 		return;
1067475553deSChris Wilson 
1068814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
10699862e600SChris Wilson 
1070549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
107110cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1072549f7365SChris Wilson }
1073549f7365SChris Wilson 
107476c3552fSDeepak S void gen6_set_pm_mask(struct drm_i915_private *dev_priv,
107527544369SDeepak S 			     u32 pm_iir, int new_delay)
107627544369SDeepak S {
107727544369SDeepak S 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1078b39fb297SBen Widawsky 		if (new_delay >= dev_priv->rps.max_freq_softlimit) {
107927544369SDeepak S 			/* Mask UP THRESHOLD Interrupts */
108027544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
108127544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
108227544369SDeepak S 				   GEN6_PM_RP_UP_THRESHOLD);
108327544369SDeepak S 			dev_priv->rps.rp_up_masked = true;
108427544369SDeepak S 		}
108527544369SDeepak S 		if (dev_priv->rps.rp_down_masked) {
108627544369SDeepak S 			/* UnMask DOWN THRESHOLD Interrupts */
108727544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
108827544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
108927544369SDeepak S 				   ~GEN6_PM_RP_DOWN_THRESHOLD);
109027544369SDeepak S 			dev_priv->rps.rp_down_masked = false;
109127544369SDeepak S 		}
109227544369SDeepak S 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1093b39fb297SBen Widawsky 		if (new_delay <= dev_priv->rps.min_freq_softlimit) {
109427544369SDeepak S 			/* Mask DOWN THRESHOLD Interrupts */
109527544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
109627544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
109727544369SDeepak S 				   GEN6_PM_RP_DOWN_THRESHOLD);
109827544369SDeepak S 			dev_priv->rps.rp_down_masked = true;
109927544369SDeepak S 		}
110027544369SDeepak S 
110127544369SDeepak S 		if (dev_priv->rps.rp_up_masked) {
110227544369SDeepak S 			/* UnMask UP THRESHOLD Interrupts */
110327544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
110427544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
110527544369SDeepak S 				   ~GEN6_PM_RP_UP_THRESHOLD);
110627544369SDeepak S 			dev_priv->rps.rp_up_masked = false;
110727544369SDeepak S 		}
110827544369SDeepak S 	}
110927544369SDeepak S }
111027544369SDeepak S 
11114912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11123b8d8d91SJesse Barnes {
11134912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1114c6a828d3SDaniel Vetter 						    rps.work);
1115edbfdb45SPaulo Zanoni 	u32 pm_iir;
1116dd75fdc8SChris Wilson 	int new_delay, adj;
11173b8d8d91SJesse Barnes 
111859cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1119c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1120c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
11214848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
1122a6706b45SDeepak S 	snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
112359cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11244912d041SBen Widawsky 
112560611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1126a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
112760611c13SPaulo Zanoni 
1128a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
11293b8d8d91SJesse Barnes 		return;
11303b8d8d91SJesse Barnes 
11314fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11327b9e0ae6SChris Wilson 
1133dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11347425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1135dd75fdc8SChris Wilson 		if (adj > 0)
1136dd75fdc8SChris Wilson 			adj *= 2;
1137dd75fdc8SChris Wilson 		else
1138dd75fdc8SChris Wilson 			adj = 1;
1139b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11407425034aSVille Syrjälä 
11417425034aSVille Syrjälä 		/*
11427425034aSVille Syrjälä 		 * For better performance, jump directly
11437425034aSVille Syrjälä 		 * to RPe if we're below it.
11447425034aSVille Syrjälä 		 */
1145b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1146b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1147dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1148b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1149b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1150dd75fdc8SChris Wilson 		else
1151b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1152dd75fdc8SChris Wilson 		adj = 0;
1153dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1154dd75fdc8SChris Wilson 		if (adj < 0)
1155dd75fdc8SChris Wilson 			adj *= 2;
1156dd75fdc8SChris Wilson 		else
1157dd75fdc8SChris Wilson 			adj = -1;
1158b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1159dd75fdc8SChris Wilson 	} else { /* unknown event */
1160b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1161dd75fdc8SChris Wilson 	}
11623b8d8d91SJesse Barnes 
116379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
116479249636SBen Widawsky 	 * interrupt
116579249636SBen Widawsky 	 */
11661272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1167b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1168b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
116927544369SDeepak S 
117027544369SDeepak S 	gen6_set_pm_mask(dev_priv, pm_iir, new_delay);
1171b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1172dd75fdc8SChris Wilson 
11730a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
11740a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
11750a073b84SJesse Barnes 	else
11764912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
11773b8d8d91SJesse Barnes 
11784fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11793b8d8d91SJesse Barnes }
11803b8d8d91SJesse Barnes 
1181e3689190SBen Widawsky 
1182e3689190SBen Widawsky /**
1183e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1184e3689190SBen Widawsky  * occurred.
1185e3689190SBen Widawsky  * @work: workqueue struct
1186e3689190SBen Widawsky  *
1187e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1188e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1189e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1190e3689190SBen Widawsky  */
1191e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1192e3689190SBen Widawsky {
1193e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1194a4da4fa4SDaniel Vetter 						    l3_parity.error_work);
1195e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
119635a85ac6SBen Widawsky 	char *parity_event[6];
1197e3689190SBen Widawsky 	uint32_t misccpctl;
1198e3689190SBen Widawsky 	unsigned long flags;
119935a85ac6SBen Widawsky 	uint8_t slice = 0;
1200e3689190SBen Widawsky 
1201e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1202e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1203e3689190SBen Widawsky 	 * any time we access those registers.
1204e3689190SBen Widawsky 	 */
1205e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1206e3689190SBen Widawsky 
120735a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
120835a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
120935a85ac6SBen Widawsky 		goto out;
121035a85ac6SBen Widawsky 
1211e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1212e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1213e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1214e3689190SBen Widawsky 
121535a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
121635a85ac6SBen Widawsky 		u32 reg;
121735a85ac6SBen Widawsky 
121835a85ac6SBen Widawsky 		slice--;
121935a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
122035a85ac6SBen Widawsky 			break;
122135a85ac6SBen Widawsky 
122235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
122335a85ac6SBen Widawsky 
122435a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
122535a85ac6SBen Widawsky 
122635a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1227e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1228e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1229e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1230e3689190SBen Widawsky 
123135a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
123235a85ac6SBen Widawsky 		POSTING_READ(reg);
1233e3689190SBen Widawsky 
1234cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1235e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1236e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1237e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
123835a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
123935a85ac6SBen Widawsky 		parity_event[5] = NULL;
1240e3689190SBen Widawsky 
12415bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1242e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1243e3689190SBen Widawsky 
124435a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
124535a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1246e3689190SBen Widawsky 
124735a85ac6SBen Widawsky 		kfree(parity_event[4]);
1248e3689190SBen Widawsky 		kfree(parity_event[3]);
1249e3689190SBen Widawsky 		kfree(parity_event[2]);
1250e3689190SBen Widawsky 		kfree(parity_event[1]);
1251e3689190SBen Widawsky 	}
1252e3689190SBen Widawsky 
125335a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
125435a85ac6SBen Widawsky 
125535a85ac6SBen Widawsky out:
125635a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
125735a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
125835a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
125935a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
126035a85ac6SBen Widawsky 
126135a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
126235a85ac6SBen Widawsky }
126335a85ac6SBen Widawsky 
126435a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1265e3689190SBen Widawsky {
1266e3689190SBen Widawsky 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1267e3689190SBen Widawsky 
1268040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1269e3689190SBen Widawsky 		return;
1270e3689190SBen Widawsky 
1271d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
127235a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1273d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1274e3689190SBen Widawsky 
127535a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
127635a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
127735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
127835a85ac6SBen Widawsky 
127935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
128035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
128135a85ac6SBen Widawsky 
1282a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1283e3689190SBen Widawsky }
1284e3689190SBen Widawsky 
1285f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1286f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1287f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1288f1af8fc1SPaulo Zanoni {
1289f1af8fc1SPaulo Zanoni 	if (gt_iir &
1290f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1291f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1292f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1293f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1294f1af8fc1SPaulo Zanoni }
1295f1af8fc1SPaulo Zanoni 
1296e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1297e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1298e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1299e7b4c6b1SDaniel Vetter {
1300e7b4c6b1SDaniel Vetter 
1301cc609d5dSBen Widawsky 	if (gt_iir &
1302cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1303e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1304cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1305e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1306cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1307e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1308e7b4c6b1SDaniel Vetter 
1309cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1310cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1311cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
131258174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
131358174462SMika Kuoppala 				  gt_iir);
1314e7b4c6b1SDaniel Vetter 	}
1315e3689190SBen Widawsky 
131635a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
131735a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1318e7b4c6b1SDaniel Vetter }
1319e7b4c6b1SDaniel Vetter 
1320abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1321abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1322abd58f01SBen Widawsky 				       u32 master_ctl)
1323abd58f01SBen Widawsky {
1324abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1325abd58f01SBen Widawsky 	uint32_t tmp = 0;
1326abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1327abd58f01SBen Widawsky 
1328abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1329abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1330abd58f01SBen Widawsky 		if (tmp) {
1331abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1332abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1333abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1334abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1335abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1336abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1337abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1338abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1339abd58f01SBen Widawsky 		} else
1340abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1341abd58f01SBen Widawsky 	}
1342abd58f01SBen Widawsky 
1343abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VCS1_IRQ) {
1344abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1345abd58f01SBen Widawsky 		if (tmp) {
1346abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1347abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1348abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1349abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
1350abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1351abd58f01SBen Widawsky 		} else
1352abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1353abd58f01SBen Widawsky 	}
1354abd58f01SBen Widawsky 
1355abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1356abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1357abd58f01SBen Widawsky 		if (tmp) {
1358abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1359abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1360abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1361abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1362abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1363abd58f01SBen Widawsky 		} else
1364abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1365abd58f01SBen Widawsky 	}
1366abd58f01SBen Widawsky 
1367abd58f01SBen Widawsky 	return ret;
1368abd58f01SBen Widawsky }
1369abd58f01SBen Widawsky 
1370b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1371b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1372b543fb04SEgbert Eich 
137310a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1374b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1375b543fb04SEgbert Eich 					 const u32 *hpd)
1376b543fb04SEgbert Eich {
1377b543fb04SEgbert Eich 	drm_i915_private_t *dev_priv = dev->dev_private;
1378b543fb04SEgbert Eich 	int i;
137910a504deSDaniel Vetter 	bool storm_detected = false;
1380b543fb04SEgbert Eich 
138191d131d2SDaniel Vetter 	if (!hotplug_trigger)
138291d131d2SDaniel Vetter 		return;
138391d131d2SDaniel Vetter 
1384cc9bd499SImre Deak 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1385cc9bd499SImre Deak 			  hotplug_trigger);
1386cc9bd499SImre Deak 
1387b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1388b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1389821450c6SEgbert Eich 
13903432087eSChris Wilson 		WARN_ONCE(hpd[i] & hotplug_trigger &&
13918b5565b8SChris Wilson 			  dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED,
1392cba1c073SChris Wilson 			  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1393cba1c073SChris Wilson 			  hotplug_trigger, i, hpd[i]);
1394b8f102e8SEgbert Eich 
1395b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1396b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1397b543fb04SEgbert Eich 			continue;
1398b543fb04SEgbert Eich 
1399bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1400b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1401b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1402b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1403b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1404b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1405b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1406b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1407b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1408142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1409b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
141010a504deSDaniel Vetter 			storm_detected = true;
1411b543fb04SEgbert Eich 		} else {
1412b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1413b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1414b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1415b543fb04SEgbert Eich 		}
1416b543fb04SEgbert Eich 	}
1417b543fb04SEgbert Eich 
141810a504deSDaniel Vetter 	if (storm_detected)
141910a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1420b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
14215876fa0dSDaniel Vetter 
1422645416f5SDaniel Vetter 	/*
1423645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1424645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1425645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1426645416f5SDaniel Vetter 	 * deadlock.
1427645416f5SDaniel Vetter 	 */
1428645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1429b543fb04SEgbert Eich }
1430b543fb04SEgbert Eich 
1431515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1432515ac2bbSDaniel Vetter {
143328c70f16SDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
143428c70f16SDaniel Vetter 
143528c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1436515ac2bbSDaniel Vetter }
1437515ac2bbSDaniel Vetter 
1438ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1439ce99c256SDaniel Vetter {
14409ee32feaSDaniel Vetter 	struct drm_i915_private *dev_priv = (drm_i915_private_t *) dev->dev_private;
14419ee32feaSDaniel Vetter 
14429ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1443ce99c256SDaniel Vetter }
1444ce99c256SDaniel Vetter 
14458bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1446277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1447eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1448eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14498bc5e955SDaniel Vetter 					 uint32_t crc4)
14508bf1e9f1SShuang He {
14518bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14528bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14538bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1454ac2300d4SDamien Lespiau 	int head, tail;
1455b2c88f5bSDamien Lespiau 
1456d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1457d538bbdfSDamien Lespiau 
14580c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1459d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
14600c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
14610c912c79SDamien Lespiau 		return;
14620c912c79SDamien Lespiau 	}
14630c912c79SDamien Lespiau 
1464d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1465d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1466b2c88f5bSDamien Lespiau 
1467b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1468d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1469b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1470b2c88f5bSDamien Lespiau 		return;
1471b2c88f5bSDamien Lespiau 	}
1472b2c88f5bSDamien Lespiau 
1473b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
14748bf1e9f1SShuang He 
14758bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1476eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1477eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1478eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1479eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1480eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1481b2c88f5bSDamien Lespiau 
1482b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1483d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1484d538bbdfSDamien Lespiau 
1485d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
148607144428SDamien Lespiau 
148707144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
14888bf1e9f1SShuang He }
1489277de95eSDaniel Vetter #else
1490277de95eSDaniel Vetter static inline void
1491277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1492277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1493277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1494277de95eSDaniel Vetter 			     uint32_t crc4) {}
1495277de95eSDaniel Vetter #endif
1496eba94eb9SDaniel Vetter 
1497277de95eSDaniel Vetter 
1498277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14995a69b89fSDaniel Vetter {
15005a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15015a69b89fSDaniel Vetter 
1502277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15035a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15045a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15055a69b89fSDaniel Vetter }
15065a69b89fSDaniel Vetter 
1507277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1508eba94eb9SDaniel Vetter {
1509eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1510eba94eb9SDaniel Vetter 
1511277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1512eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1513eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1514eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1515eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15168bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1517eba94eb9SDaniel Vetter }
15185b3a856bSDaniel Vetter 
1519277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15205b3a856bSDaniel Vetter {
15215b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15220b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15230b5c5ed0SDaniel Vetter 
15240b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15250b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15260b5c5ed0SDaniel Vetter 	else
15270b5c5ed0SDaniel Vetter 		res1 = 0;
15280b5c5ed0SDaniel Vetter 
15290b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15300b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15310b5c5ed0SDaniel Vetter 	else
15320b5c5ed0SDaniel Vetter 		res2 = 0;
15335b3a856bSDaniel Vetter 
1534277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15350b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15360b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15370b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15380b5c5ed0SDaniel Vetter 				     res1, res2);
15395b3a856bSDaniel Vetter }
15408bf1e9f1SShuang He 
15411403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15421403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15431403c0d4SPaulo Zanoni  * the work queue. */
15441403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1545baf02a1fSBen Widawsky {
1546a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
154759cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1548a6706b45SDeepak S 		dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1549a6706b45SDeepak S 		snb_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
155059cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
15512adbee62SDaniel Vetter 
15522adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
155341a05a3aSDaniel Vetter 	}
1554baf02a1fSBen Widawsky 
15551403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
155612638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
155712638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
155812638c57SBen Widawsky 
155912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
156058174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
156158174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
156258174462SMika Kuoppala 					  pm_iir);
156312638c57SBen Widawsky 		}
156412638c57SBen Widawsky 	}
15651403c0d4SPaulo Zanoni }
1566baf02a1fSBen Widawsky 
1567c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
15687e231dbeSJesse Barnes {
1569c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
157091d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
15717e231dbeSJesse Barnes 	int pipe;
15727e231dbeSJesse Barnes 
157358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
15747e231dbeSJesse Barnes 	for_each_pipe(pipe) {
157591d181ddSImre Deak 		int reg;
1576bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
157791d181ddSImre Deak 
1578bbb5eebfSDaniel Vetter 		/*
1579bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1580bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1581bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1582bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1583bbb5eebfSDaniel Vetter 		 * handle.
1584bbb5eebfSDaniel Vetter 		 */
1585bbb5eebfSDaniel Vetter 		mask = 0;
1586bbb5eebfSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
1587bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
1588bbb5eebfSDaniel Vetter 
1589bbb5eebfSDaniel Vetter 		switch (pipe) {
1590bbb5eebfSDaniel Vetter 		case PIPE_A:
1591bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1592bbb5eebfSDaniel Vetter 			break;
1593bbb5eebfSDaniel Vetter 		case PIPE_B:
1594bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1595bbb5eebfSDaniel Vetter 			break;
1596bbb5eebfSDaniel Vetter 		}
1597bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1598bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1599bbb5eebfSDaniel Vetter 
1600bbb5eebfSDaniel Vetter 		if (!mask)
160191d181ddSImre Deak 			continue;
160291d181ddSImre Deak 
160391d181ddSImre Deak 		reg = PIPESTAT(pipe);
1604bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1605bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16067e231dbeSJesse Barnes 
16077e231dbeSJesse Barnes 		/*
16087e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16097e231dbeSJesse Barnes 		 */
161091d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
161191d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16127e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16137e231dbeSJesse Barnes 	}
161458ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16157e231dbeSJesse Barnes 
161631acc7f5SJesse Barnes 	for_each_pipe(pipe) {
16177b5562d4SJesse Barnes 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
161831acc7f5SJesse Barnes 			drm_handle_vblank(dev, pipe);
161931acc7f5SJesse Barnes 
1620579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
162131acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
162231acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
162331acc7f5SJesse Barnes 		}
16244356d586SDaniel Vetter 
16254356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1626277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16272d9d2b0bSVille Syrjälä 
16282d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
16292d9d2b0bSVille Syrjälä 		    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1630fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
163131acc7f5SJesse Barnes 	}
163231acc7f5SJesse Barnes 
1633c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1634c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1635c1874ed7SImre Deak }
1636c1874ed7SImre Deak 
1637c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1638c1874ed7SImre Deak {
1639c1874ed7SImre Deak 	struct drm_device *dev = (struct drm_device *) arg;
1640c1874ed7SImre Deak 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1641c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1642c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1643c1874ed7SImre Deak 
1644c1874ed7SImre Deak 	while (true) {
1645c1874ed7SImre Deak 		iir = I915_READ(VLV_IIR);
1646c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1647c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
1648c1874ed7SImre Deak 
1649c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1650c1874ed7SImre Deak 			goto out;
1651c1874ed7SImre Deak 
1652c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1653c1874ed7SImre Deak 
1654c1874ed7SImre Deak 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1655c1874ed7SImre Deak 
1656c1874ed7SImre Deak 		valleyview_pipestat_irq_handler(dev, iir);
1657c1874ed7SImre Deak 
16587e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
16597e231dbeSJesse Barnes 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
16607e231dbeSJesse Barnes 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
1661b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
16627e231dbeSJesse Barnes 
166310a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
166491d131d2SDaniel Vetter 
16654aeebd74SDaniel Vetter 			if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
16664aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
16674aeebd74SDaniel Vetter 
16687e231dbeSJesse Barnes 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
16697e231dbeSJesse Barnes 			I915_READ(PORT_HOTPLUG_STAT);
16707e231dbeSJesse Barnes 		}
16717e231dbeSJesse Barnes 
16727e231dbeSJesse Barnes 
167360611c13SPaulo Zanoni 		if (pm_iir)
1674d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
16757e231dbeSJesse Barnes 
16767e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
16777e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
16787e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
16797e231dbeSJesse Barnes 	}
16807e231dbeSJesse Barnes 
16817e231dbeSJesse Barnes out:
16827e231dbeSJesse Barnes 	return ret;
16837e231dbeSJesse Barnes }
16847e231dbeSJesse Barnes 
168523e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1686776ad806SJesse Barnes {
1687776ad806SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
16889db4a9c7SJesse Barnes 	int pipe;
1689b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1690776ad806SJesse Barnes 
169110a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
169291d131d2SDaniel Vetter 
1693cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1694cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1695776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1696cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1697cfc33bf7SVille Syrjälä 				 port_name(port));
1698cfc33bf7SVille Syrjälä 	}
1699776ad806SJesse Barnes 
1700ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1701ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1702ce99c256SDaniel Vetter 
1703776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1704515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1705776ad806SJesse Barnes 
1706776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1707776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1708776ad806SJesse Barnes 
1709776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1710776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1711776ad806SJesse Barnes 
1712776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1713776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1714776ad806SJesse Barnes 
17159db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
17169db4a9c7SJesse Barnes 		for_each_pipe(pipe)
17179db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17189db4a9c7SJesse Barnes 					 pipe_name(pipe),
17199db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1720776ad806SJesse Barnes 
1721776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1722776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1723776ad806SJesse Barnes 
1724776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1725776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1726776ad806SJesse Barnes 
1727776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
17288664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
17298664281bSPaulo Zanoni 							  false))
1730fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
17318664281bSPaulo Zanoni 
17328664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
17338664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
17348664281bSPaulo Zanoni 							  false))
1735fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
17368664281bSPaulo Zanoni }
17378664281bSPaulo Zanoni 
17388664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
17398664281bSPaulo Zanoni {
17408664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17418664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17425a69b89fSDaniel Vetter 	enum pipe pipe;
17438664281bSPaulo Zanoni 
1744de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1745de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1746de032bf4SPaulo Zanoni 
17475a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
17485a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
17495a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
17505a69b89fSDaniel Vetter 								  false))
1751fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
17525a69b89fSDaniel Vetter 					  pipe_name(pipe));
17535a69b89fSDaniel Vetter 		}
17548664281bSPaulo Zanoni 
17555a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
17565a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1757277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
17585a69b89fSDaniel Vetter 			else
1759277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
17605a69b89fSDaniel Vetter 		}
17615a69b89fSDaniel Vetter 	}
17628bf1e9f1SShuang He 
17638664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
17648664281bSPaulo Zanoni }
17658664281bSPaulo Zanoni 
17668664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
17678664281bSPaulo Zanoni {
17688664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17698664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
17708664281bSPaulo Zanoni 
1771de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1772de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1773de032bf4SPaulo Zanoni 
17748664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
17758664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
17768664281bSPaulo Zanoni 							  false))
1777fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
17788664281bSPaulo Zanoni 
17798664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
17808664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
17818664281bSPaulo Zanoni 							  false))
1782fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
17838664281bSPaulo Zanoni 
17848664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
17858664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
17868664281bSPaulo Zanoni 							  false))
1787fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
17888664281bSPaulo Zanoni 
17898664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1790776ad806SJesse Barnes }
1791776ad806SJesse Barnes 
179223e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
179323e81d69SAdam Jackson {
179423e81d69SAdam Jackson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
179523e81d69SAdam Jackson 	int pipe;
1796b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
179723e81d69SAdam Jackson 
179810a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
179991d131d2SDaniel Vetter 
1800cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1801cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
180223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1803cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1804cfc33bf7SVille Syrjälä 				 port_name(port));
1805cfc33bf7SVille Syrjälä 	}
180623e81d69SAdam Jackson 
180723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1808ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
180923e81d69SAdam Jackson 
181023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1811515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
181223e81d69SAdam Jackson 
181323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
181423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
181523e81d69SAdam Jackson 
181623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
181723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
181823e81d69SAdam Jackson 
181923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
182023e81d69SAdam Jackson 		for_each_pipe(pipe)
182123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
182223e81d69SAdam Jackson 					 pipe_name(pipe),
182323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18248664281bSPaulo Zanoni 
18258664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
18268664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
182723e81d69SAdam Jackson }
182823e81d69SAdam Jackson 
1829c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1830c008bc6eSPaulo Zanoni {
1831c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
183240da17c2SDaniel Vetter 	enum pipe pipe;
1833c008bc6eSPaulo Zanoni 
1834c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1835c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1836c008bc6eSPaulo Zanoni 
1837c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1838c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1839c008bc6eSPaulo Zanoni 
1840c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1841c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1842c008bc6eSPaulo Zanoni 
184340da17c2SDaniel Vetter 	for_each_pipe(pipe) {
184440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
184540da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1846c008bc6eSPaulo Zanoni 
184740da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
184840da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1849fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
185040da17c2SDaniel Vetter 					  pipe_name(pipe));
1851c008bc6eSPaulo Zanoni 
185240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
185340da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
18545b3a856bSDaniel Vetter 
185540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
185640da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
185740da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
185840da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1859c008bc6eSPaulo Zanoni 		}
1860c008bc6eSPaulo Zanoni 	}
1861c008bc6eSPaulo Zanoni 
1862c008bc6eSPaulo Zanoni 	/* check event from PCH */
1863c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1864c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1865c008bc6eSPaulo Zanoni 
1866c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1867c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1868c008bc6eSPaulo Zanoni 		else
1869c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1870c008bc6eSPaulo Zanoni 
1871c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1872c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1873c008bc6eSPaulo Zanoni 	}
1874c008bc6eSPaulo Zanoni 
1875c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1876c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1877c008bc6eSPaulo Zanoni }
1878c008bc6eSPaulo Zanoni 
18799719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
18809719fb98SPaulo Zanoni {
18819719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
188207d27e20SDamien Lespiau 	enum pipe pipe;
18839719fb98SPaulo Zanoni 
18849719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
18859719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
18869719fb98SPaulo Zanoni 
18879719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
18889719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
18899719fb98SPaulo Zanoni 
18909719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
18919719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
18929719fb98SPaulo Zanoni 
189307d27e20SDamien Lespiau 	for_each_pipe(pipe) {
189407d27e20SDamien Lespiau 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
189507d27e20SDamien Lespiau 			drm_handle_vblank(dev, pipe);
189640da17c2SDaniel Vetter 
189740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
189807d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
189907d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
190007d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
19019719fb98SPaulo Zanoni 		}
19029719fb98SPaulo Zanoni 	}
19039719fb98SPaulo Zanoni 
19049719fb98SPaulo Zanoni 	/* check event from PCH */
19059719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
19069719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
19079719fb98SPaulo Zanoni 
19089719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
19099719fb98SPaulo Zanoni 
19109719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
19119719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
19129719fb98SPaulo Zanoni 	}
19139719fb98SPaulo Zanoni }
19149719fb98SPaulo Zanoni 
1915f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1916b1f14ad0SJesse Barnes {
1917b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
1918b1f14ad0SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
1919f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
19200e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1921b1f14ad0SJesse Barnes 
19228664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
19238664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1924907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
19258664281bSPaulo Zanoni 
1926b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1927b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1928b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
192923a78516SPaulo Zanoni 	POSTING_READ(DEIER);
19300e43406bSChris Wilson 
193144498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
193244498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
193344498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
193444498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
193544498aeaSPaulo Zanoni 	 * due to its back queue). */
1936ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
193744498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
193844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
193944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1940ab5c608bSBen Widawsky 	}
194144498aeaSPaulo Zanoni 
19420e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
19430e43406bSChris Wilson 	if (gt_iir) {
1944d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
19450e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1946d8fc8a47SPaulo Zanoni 		else
1947d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
19480e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
19490e43406bSChris Wilson 		ret = IRQ_HANDLED;
19500e43406bSChris Wilson 	}
1951b1f14ad0SJesse Barnes 
1952b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
19530e43406bSChris Wilson 	if (de_iir) {
1954f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
19559719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1956f1af8fc1SPaulo Zanoni 		else
1957f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
19580e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
19590e43406bSChris Wilson 		ret = IRQ_HANDLED;
19600e43406bSChris Wilson 	}
19610e43406bSChris Wilson 
1962f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
1963f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
19640e43406bSChris Wilson 		if (pm_iir) {
1965d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
1966b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
19670e43406bSChris Wilson 			ret = IRQ_HANDLED;
19680e43406bSChris Wilson 		}
1969f1af8fc1SPaulo Zanoni 	}
1970b1f14ad0SJesse Barnes 
1971b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
1972b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
1973ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
197444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
197544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1976ab5c608bSBen Widawsky 	}
1977b1f14ad0SJesse Barnes 
1978b1f14ad0SJesse Barnes 	return ret;
1979b1f14ad0SJesse Barnes }
1980b1f14ad0SJesse Barnes 
1981abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
1982abd58f01SBen Widawsky {
1983abd58f01SBen Widawsky 	struct drm_device *dev = arg;
1984abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
1985abd58f01SBen Widawsky 	u32 master_ctl;
1986abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1987abd58f01SBen Widawsky 	uint32_t tmp = 0;
1988c42664ccSDaniel Vetter 	enum pipe pipe;
1989abd58f01SBen Widawsky 
1990abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
1991abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
1992abd58f01SBen Widawsky 	if (!master_ctl)
1993abd58f01SBen Widawsky 		return IRQ_NONE;
1994abd58f01SBen Widawsky 
1995abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
1996abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
1997abd58f01SBen Widawsky 
1998abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
1999abd58f01SBen Widawsky 
2000abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2001abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2002abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
2003abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
2004abd58f01SBen Widawsky 		else if (tmp)
2005abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
2006abd58f01SBen Widawsky 		else
2007abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2008abd58f01SBen Widawsky 
2009abd58f01SBen Widawsky 		if (tmp) {
2010abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2011abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2012abd58f01SBen Widawsky 		}
2013abd58f01SBen Widawsky 	}
2014abd58f01SBen Widawsky 
20156d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
20166d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
20176d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
20186d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
20196d766f02SDaniel Vetter 		else if (tmp)
20206d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
20216d766f02SDaniel Vetter 		else
20226d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
20236d766f02SDaniel Vetter 
20246d766f02SDaniel Vetter 		if (tmp) {
20256d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
20266d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
20276d766f02SDaniel Vetter 		}
20286d766f02SDaniel Vetter 	}
20296d766f02SDaniel Vetter 
2030abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2031abd58f01SBen Widawsky 		uint32_t pipe_iir;
2032abd58f01SBen Widawsky 
2033c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2034c42664ccSDaniel Vetter 			continue;
2035c42664ccSDaniel Vetter 
2036abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2037abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
2038abd58f01SBen Widawsky 			drm_handle_vblank(dev, pipe);
2039abd58f01SBen Widawsky 
2040abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_FLIP_DONE) {
2041abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2042abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2043abd58f01SBen Widawsky 		}
2044abd58f01SBen Widawsky 
20450fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
20460fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
20470fbe7870SDaniel Vetter 
204838d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
204938d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
205038d83c96SDaniel Vetter 								  false))
2051fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
205238d83c96SDaniel Vetter 					  pipe_name(pipe));
205338d83c96SDaniel Vetter 		}
205438d83c96SDaniel Vetter 
205530100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
205630100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
205730100f2bSDaniel Vetter 				  pipe_name(pipe),
205830100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
205930100f2bSDaniel Vetter 		}
2060abd58f01SBen Widawsky 
2061abd58f01SBen Widawsky 		if (pipe_iir) {
2062abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2063abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2064c42664ccSDaniel Vetter 		} else
2065abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2066abd58f01SBen Widawsky 	}
2067abd58f01SBen Widawsky 
206892d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
206992d03a80SDaniel Vetter 		/*
207092d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
207192d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
207292d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
207392d03a80SDaniel Vetter 		 */
207492d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
207592d03a80SDaniel Vetter 
207692d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
207792d03a80SDaniel Vetter 
207892d03a80SDaniel Vetter 		if (pch_iir) {
207992d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
208092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
208192d03a80SDaniel Vetter 		}
208292d03a80SDaniel Vetter 	}
208392d03a80SDaniel Vetter 
2084abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2085abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2086abd58f01SBen Widawsky 
2087abd58f01SBen Widawsky 	return ret;
2088abd58f01SBen Widawsky }
2089abd58f01SBen Widawsky 
209017e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
209117e1df07SDaniel Vetter 			       bool reset_completed)
209217e1df07SDaniel Vetter {
209317e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
209417e1df07SDaniel Vetter 	int i;
209517e1df07SDaniel Vetter 
209617e1df07SDaniel Vetter 	/*
209717e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
209817e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
209917e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
210017e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
210117e1df07SDaniel Vetter 	 */
210217e1df07SDaniel Vetter 
210317e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
210417e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
210517e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
210617e1df07SDaniel Vetter 
210717e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
210817e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
210917e1df07SDaniel Vetter 
211017e1df07SDaniel Vetter 	/*
211117e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
211217e1df07SDaniel Vetter 	 * reset state is cleared.
211317e1df07SDaniel Vetter 	 */
211417e1df07SDaniel Vetter 	if (reset_completed)
211517e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
211617e1df07SDaniel Vetter }
211717e1df07SDaniel Vetter 
21188a905236SJesse Barnes /**
21198a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
21208a905236SJesse Barnes  * @work: work struct
21218a905236SJesse Barnes  *
21228a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
21238a905236SJesse Barnes  * was detected.
21248a905236SJesse Barnes  */
21258a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
21268a905236SJesse Barnes {
21271f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
21281f83fee0SDaniel Vetter 						    work);
21291f83fee0SDaniel Vetter 	drm_i915_private_t *dev_priv = container_of(error, drm_i915_private_t,
21301f83fee0SDaniel Vetter 						    gpu_error);
21318a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2132cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2133cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2134cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
213517e1df07SDaniel Vetter 	int ret;
21368a905236SJesse Barnes 
21375bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
21388a905236SJesse Barnes 
21397db0ba24SDaniel Vetter 	/*
21407db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
21417db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
21427db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
21437db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
21447db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
21457db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
21467db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
21477db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
21487db0ba24SDaniel Vetter 	 */
21497db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
215044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
21515bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
21527db0ba24SDaniel Vetter 				   reset_event);
21531f83fee0SDaniel Vetter 
215417e1df07SDaniel Vetter 		/*
215517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
215617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
215717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
215817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
215917e1df07SDaniel Vetter 		 */
2160f69061beSDaniel Vetter 		ret = i915_reset(dev);
2161f69061beSDaniel Vetter 
216217e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
216317e1df07SDaniel Vetter 
2164f69061beSDaniel Vetter 		if (ret == 0) {
2165f69061beSDaniel Vetter 			/*
2166f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2167f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2168f69061beSDaniel Vetter 			 * complete.
2169f69061beSDaniel Vetter 			 *
2170f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2171f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2172f69061beSDaniel Vetter 			 * updates before
2173f69061beSDaniel Vetter 			 * the counter increment.
2174f69061beSDaniel Vetter 			 */
2175f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
2176f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2177f69061beSDaniel Vetter 
21785bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2179f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
21801f83fee0SDaniel Vetter 		} else {
21812ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2182f316a42cSBen Gamari 		}
21831f83fee0SDaniel Vetter 
218417e1df07SDaniel Vetter 		/*
218517e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
218617e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
218717e1df07SDaniel Vetter 		 */
218817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2189f316a42cSBen Gamari 	}
21908a905236SJesse Barnes }
21918a905236SJesse Barnes 
219235aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2193c0e09200SDave Airlie {
21948a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2195bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
219663eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2197050ee91fSBen Widawsky 	int pipe, i;
219863eeaf38SJesse Barnes 
219935aed2e6SChris Wilson 	if (!eir)
220035aed2e6SChris Wilson 		return;
220163eeaf38SJesse Barnes 
2202a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
22038a905236SJesse Barnes 
2204bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2205bd9854f9SBen Widawsky 
22068a905236SJesse Barnes 	if (IS_G4X(dev)) {
22078a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
22088a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
22098a905236SJesse Barnes 
2210a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2211a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2212050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2213050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2214a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2215a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
22168a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
22173143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
22188a905236SJesse Barnes 		}
22198a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
22208a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2221a70491ccSJoe Perches 			pr_err("page table error\n");
2222a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
22238a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
22243143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
22258a905236SJesse Barnes 		}
22268a905236SJesse Barnes 	}
22278a905236SJesse Barnes 
2228a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
222963eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
223063eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2231a70491ccSJoe Perches 			pr_err("page table error\n");
2232a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
223363eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
22343143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
223563eeaf38SJesse Barnes 		}
22368a905236SJesse Barnes 	}
22378a905236SJesse Barnes 
223863eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2239a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
22409db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2241a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
22429db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
224363eeaf38SJesse Barnes 		/* pipestat has already been acked */
224463eeaf38SJesse Barnes 	}
224563eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2246a70491ccSJoe Perches 		pr_err("instruction error\n");
2247a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2248050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2249050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2250a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
225163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
225263eeaf38SJesse Barnes 
2253a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2254a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2255a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
225663eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
22573143a2bfSChris Wilson 			POSTING_READ(IPEIR);
225863eeaf38SJesse Barnes 		} else {
225963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
226063eeaf38SJesse Barnes 
2261a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2262a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2263a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2264a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
226563eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
22663143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
226763eeaf38SJesse Barnes 		}
226863eeaf38SJesse Barnes 	}
226963eeaf38SJesse Barnes 
227063eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
22713143a2bfSChris Wilson 	POSTING_READ(EIR);
227263eeaf38SJesse Barnes 	eir = I915_READ(EIR);
227363eeaf38SJesse Barnes 	if (eir) {
227463eeaf38SJesse Barnes 		/*
227563eeaf38SJesse Barnes 		 * some errors might have become stuck,
227663eeaf38SJesse Barnes 		 * mask them.
227763eeaf38SJesse Barnes 		 */
227863eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
227963eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
228063eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
228163eeaf38SJesse Barnes 	}
228235aed2e6SChris Wilson }
228335aed2e6SChris Wilson 
228435aed2e6SChris Wilson /**
228535aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
228635aed2e6SChris Wilson  * @dev: drm device
228735aed2e6SChris Wilson  *
228835aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
228935aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
229035aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
229135aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
229235aed2e6SChris Wilson  * of a ring dump etc.).
229335aed2e6SChris Wilson  */
229458174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
229558174462SMika Kuoppala 		       const char *fmt, ...)
229635aed2e6SChris Wilson {
229735aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
229858174462SMika Kuoppala 	va_list args;
229958174462SMika Kuoppala 	char error_msg[80];
230035aed2e6SChris Wilson 
230158174462SMika Kuoppala 	va_start(args, fmt);
230258174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
230358174462SMika Kuoppala 	va_end(args);
230458174462SMika Kuoppala 
230558174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
230635aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
23078a905236SJesse Barnes 
2308ba1234d1SBen Gamari 	if (wedged) {
2309f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2310f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2311ba1234d1SBen Gamari 
231211ed50ecSBen Gamari 		/*
231317e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
231417e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
231517e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
231617e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
231717e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
231817e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
231917e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
232017e1df07SDaniel Vetter 		 *
232117e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
232217e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
232317e1df07SDaniel Vetter 		 * counter atomic_t.
232411ed50ecSBen Gamari 		 */
232517e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
232611ed50ecSBen Gamari 	}
232711ed50ecSBen Gamari 
2328122f46baSDaniel Vetter 	/*
2329122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2330122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2331122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2332122f46baSDaniel Vetter 	 * code will deadlock.
2333122f46baSDaniel Vetter 	 */
2334122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
23358a905236SJesse Barnes }
23368a905236SJesse Barnes 
233721ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
23384e5359cdSSimon Farnsworth {
23394e5359cdSSimon Farnsworth 	drm_i915_private_t *dev_priv = dev->dev_private;
23404e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
23414e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
234205394f39SChris Wilson 	struct drm_i915_gem_object *obj;
23434e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
23444e5359cdSSimon Farnsworth 	unsigned long flags;
23454e5359cdSSimon Farnsworth 	bool stall_detected;
23464e5359cdSSimon Farnsworth 
23474e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
23484e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
23494e5359cdSSimon Farnsworth 		return;
23504e5359cdSSimon Farnsworth 
23514e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
23524e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
23534e5359cdSSimon Farnsworth 
2354e7d841caSChris Wilson 	if (work == NULL ||
2355e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2356e7d841caSChris Wilson 	    !work->enable_stall_check) {
23574e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
23584e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
23594e5359cdSSimon Farnsworth 		return;
23604e5359cdSSimon Farnsworth 	}
23614e5359cdSSimon Farnsworth 
23624e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
236305394f39SChris Wilson 	obj = work->pending_flip_obj;
2364a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
23659db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2366446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2367f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
23684e5359cdSSimon Farnsworth 	} else {
23699db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2370f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
237101f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
23724e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
23734e5359cdSSimon Farnsworth 	}
23744e5359cdSSimon Farnsworth 
23754e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
23764e5359cdSSimon Farnsworth 
23774e5359cdSSimon Farnsworth 	if (stall_detected) {
23784e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
23794e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
23804e5359cdSSimon Farnsworth 	}
23814e5359cdSSimon Farnsworth }
23824e5359cdSSimon Farnsworth 
238342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
238442f52ef8SKeith Packard  * we use as a pipe index
238542f52ef8SKeith Packard  */
2386f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
23870a3e67a4SJesse Barnes {
23880a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2389e9d21d7fSKeith Packard 	unsigned long irqflags;
239071e0ffa5SJesse Barnes 
23915eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
239271e0ffa5SJesse Barnes 		return -EINVAL;
23930a3e67a4SJesse Barnes 
23941ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2395f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
23967c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2397755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
23980a3e67a4SJesse Barnes 	else
23997c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2400755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
24018692d00eSChris Wilson 
24028692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
24033d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
24046b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
24051ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24068692d00eSChris Wilson 
24070a3e67a4SJesse Barnes 	return 0;
24080a3e67a4SJesse Barnes }
24090a3e67a4SJesse Barnes 
2410f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2411f796cf8fSJesse Barnes {
2412f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2413f796cf8fSJesse Barnes 	unsigned long irqflags;
2414b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
241540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2416f796cf8fSJesse Barnes 
2417f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2418f796cf8fSJesse Barnes 		return -EINVAL;
2419f796cf8fSJesse Barnes 
2420f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2421b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2422b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2423b1f14ad0SJesse Barnes 
2424b1f14ad0SJesse Barnes 	return 0;
2425b1f14ad0SJesse Barnes }
2426b1f14ad0SJesse Barnes 
24277e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
24287e231dbeSJesse Barnes {
24297e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24307e231dbeSJesse Barnes 	unsigned long irqflags;
24317e231dbeSJesse Barnes 
24327e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
24337e231dbeSJesse Barnes 		return -EINVAL;
24347e231dbeSJesse Barnes 
24357e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
243631acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2437755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
24387e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24397e231dbeSJesse Barnes 
24407e231dbeSJesse Barnes 	return 0;
24417e231dbeSJesse Barnes }
24427e231dbeSJesse Barnes 
2443abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2444abd58f01SBen Widawsky {
2445abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2446abd58f01SBen Widawsky 	unsigned long irqflags;
2447abd58f01SBen Widawsky 
2448abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2449abd58f01SBen Widawsky 		return -EINVAL;
2450abd58f01SBen Widawsky 
2451abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
24527167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
24537167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2454abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2455abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2456abd58f01SBen Widawsky 	return 0;
2457abd58f01SBen Widawsky }
2458abd58f01SBen Widawsky 
245942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
246042f52ef8SKeith Packard  * we use as a pipe index
246142f52ef8SKeith Packard  */
2462f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
24630a3e67a4SJesse Barnes {
24640a3e67a4SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2465e9d21d7fSKeith Packard 	unsigned long irqflags;
24660a3e67a4SJesse Barnes 
24671ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
24683d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
24696b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
24708692d00eSChris Wilson 
24717c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2472755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2473755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
24741ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24750a3e67a4SJesse Barnes }
24760a3e67a4SJesse Barnes 
2477f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2478f796cf8fSJesse Barnes {
2479f796cf8fSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2480f796cf8fSJesse Barnes 	unsigned long irqflags;
2481b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
248240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2483f796cf8fSJesse Barnes 
2484f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2485b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2486b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2487b1f14ad0SJesse Barnes }
2488b1f14ad0SJesse Barnes 
24897e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
24907e231dbeSJesse Barnes {
24917e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
24927e231dbeSJesse Barnes 	unsigned long irqflags;
24937e231dbeSJesse Barnes 
24947e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
249531acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2496755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
24977e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24987e231dbeSJesse Barnes }
24997e231dbeSJesse Barnes 
2500abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2501abd58f01SBen Widawsky {
2502abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2503abd58f01SBen Widawsky 	unsigned long irqflags;
2504abd58f01SBen Widawsky 
2505abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2506abd58f01SBen Widawsky 		return;
2507abd58f01SBen Widawsky 
2508abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25097167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
25107167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2511abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2512abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2513abd58f01SBen Widawsky }
2514abd58f01SBen Widawsky 
2515893eead0SChris Wilson static u32
2516893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2517852835f3SZou Nan hai {
2518893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2519893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2520893eead0SChris Wilson }
2521893eead0SChris Wilson 
25229107e9d2SChris Wilson static bool
25239107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2524893eead0SChris Wilson {
25259107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
25269107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2527f65d9421SBen Gamari }
2528f65d9421SBen Gamari 
25296274f212SChris Wilson static struct intel_ring_buffer *
25306274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2531a24a11e6SChris Wilson {
2532a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
253388fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
253488fe429dSDaniel Vetter 	int i;
2535a24a11e6SChris Wilson 
2536a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2537a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2538a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
25396274f212SChris Wilson 		return NULL;
2540a24a11e6SChris Wilson 
254188fe429dSDaniel Vetter 	/*
254288fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
254388fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
254488fe429dSDaniel Vetter 	 * dwords. Note that we don't care about ACTHD here since that might
254588fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
254688fe429dSDaniel Vetter 	 * ringbuffer itself.
2547a24a11e6SChris Wilson 	 */
254888fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
254988fe429dSDaniel Vetter 
255088fe429dSDaniel Vetter 	for (i = 4; i; --i) {
255188fe429dSDaniel Vetter 		/*
255288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
255388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
255488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
255588fe429dSDaniel Vetter 		 */
255688fe429dSDaniel Vetter 		head &= ring->size - 1;
255788fe429dSDaniel Vetter 
255888fe429dSDaniel Vetter 		/* This here seems to blow up */
255988fe429dSDaniel Vetter 		cmd = ioread32(ring->virtual_start + head);
2560a24a11e6SChris Wilson 		if (cmd == ipehr)
2561a24a11e6SChris Wilson 			break;
2562a24a11e6SChris Wilson 
256388fe429dSDaniel Vetter 		head -= 4;
256488fe429dSDaniel Vetter 	}
2565a24a11e6SChris Wilson 
256688fe429dSDaniel Vetter 	if (!i)
256788fe429dSDaniel Vetter 		return NULL;
256888fe429dSDaniel Vetter 
256988fe429dSDaniel Vetter 	*seqno = ioread32(ring->virtual_start + head + 4) + 1;
25706274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2571a24a11e6SChris Wilson }
2572a24a11e6SChris Wilson 
25736274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
25746274f212SChris Wilson {
25756274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
25766274f212SChris Wilson 	struct intel_ring_buffer *signaller;
25776274f212SChris Wilson 	u32 seqno, ctl;
25786274f212SChris Wilson 
25796274f212SChris Wilson 	ring->hangcheck.deadlock = true;
25806274f212SChris Wilson 
25816274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
25826274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
25836274f212SChris Wilson 		return -1;
25846274f212SChris Wilson 
25856274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
25866274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
25876274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
25886274f212SChris Wilson 		return -1;
25896274f212SChris Wilson 
25906274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
25916274f212SChris Wilson }
25926274f212SChris Wilson 
25936274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
25946274f212SChris Wilson {
25956274f212SChris Wilson 	struct intel_ring_buffer *ring;
25966274f212SChris Wilson 	int i;
25976274f212SChris Wilson 
25986274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
25996274f212SChris Wilson 		ring->hangcheck.deadlock = false;
26006274f212SChris Wilson }
26016274f212SChris Wilson 
2602ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2603*50877445SChris Wilson ring_stuck(struct intel_ring_buffer *ring, u64 acthd)
26041ec14ad3SChris Wilson {
26051ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
26061ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26079107e9d2SChris Wilson 	u32 tmp;
26089107e9d2SChris Wilson 
26096274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2610f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
26116274f212SChris Wilson 
26129107e9d2SChris Wilson 	if (IS_GEN2(dev))
2613f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
26149107e9d2SChris Wilson 
26159107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
26169107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
26179107e9d2SChris Wilson 	 * and break the hang. This should work on
26189107e9d2SChris Wilson 	 * all but the second generation chipsets.
26199107e9d2SChris Wilson 	 */
26209107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
26211ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
262258174462SMika Kuoppala 		i915_handle_error(dev, false,
262358174462SMika Kuoppala 				  "Kicking stuck wait on %s",
26241ec14ad3SChris Wilson 				  ring->name);
26251ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2626f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
26271ec14ad3SChris Wilson 	}
2628a24a11e6SChris Wilson 
26296274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
26306274f212SChris Wilson 		switch (semaphore_passed(ring)) {
26316274f212SChris Wilson 		default:
2632f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
26336274f212SChris Wilson 		case 1:
263458174462SMika Kuoppala 			i915_handle_error(dev, false,
263558174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2636a24a11e6SChris Wilson 					  ring->name);
2637a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2638f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
26396274f212SChris Wilson 		case 0:
2640f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
26416274f212SChris Wilson 		}
26429107e9d2SChris Wilson 	}
26439107e9d2SChris Wilson 
2644f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2645a24a11e6SChris Wilson }
2646d1e61e7fSChris Wilson 
2647f65d9421SBen Gamari /**
2648f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
264905407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
265005407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
265105407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
265205407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
265305407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2654f65d9421SBen Gamari  */
2655a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2656f65d9421SBen Gamari {
2657f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2658f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2659b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2660b4519513SChris Wilson 	int i;
266105407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
26629107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
26639107e9d2SChris Wilson #define BUSY 1
26649107e9d2SChris Wilson #define KICK 5
26659107e9d2SChris Wilson #define HUNG 20
2666893eead0SChris Wilson 
2667d330a953SJani Nikula 	if (!i915.enable_hangcheck)
26683e0dc6b0SBen Widawsky 		return;
26693e0dc6b0SBen Widawsky 
2670b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
2671*50877445SChris Wilson 		u64 acthd;
2672*50877445SChris Wilson 		u32 seqno;
26739107e9d2SChris Wilson 		bool busy = true;
2674b4519513SChris Wilson 
26756274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
26766274f212SChris Wilson 
267705407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
267805407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
267905407ff8SMika Kuoppala 
268005407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
26819107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2682da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2683da661464SMika Kuoppala 
26849107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
26859107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2686094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2687f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
26889107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
26899107e9d2SChris Wilson 								  ring->name);
2690f4adcd24SDaniel Vetter 						else
2691f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2692f4adcd24SDaniel Vetter 								 ring->name);
26939107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2694094f9a54SChris Wilson 					}
2695094f9a54SChris Wilson 					/* Safeguard against driver failure */
2696094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
26979107e9d2SChris Wilson 				} else
26989107e9d2SChris Wilson 					busy = false;
269905407ff8SMika Kuoppala 			} else {
27006274f212SChris Wilson 				/* We always increment the hangcheck score
27016274f212SChris Wilson 				 * if the ring is busy and still processing
27026274f212SChris Wilson 				 * the same request, so that no single request
27036274f212SChris Wilson 				 * can run indefinitely (such as a chain of
27046274f212SChris Wilson 				 * batches). The only time we do not increment
27056274f212SChris Wilson 				 * the hangcheck score on this ring, if this
27066274f212SChris Wilson 				 * ring is in a legitimate wait for another
27076274f212SChris Wilson 				 * ring. In that case the waiting ring is a
27086274f212SChris Wilson 				 * victim and we want to be sure we catch the
27096274f212SChris Wilson 				 * right culprit. Then every time we do kick
27106274f212SChris Wilson 				 * the ring, add a small increment to the
27116274f212SChris Wilson 				 * score so that we can catch a batch that is
27126274f212SChris Wilson 				 * being repeatedly kicked and so responsible
27136274f212SChris Wilson 				 * for stalling the machine.
27149107e9d2SChris Wilson 				 */
2715ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2716ad8beaeaSMika Kuoppala 								    acthd);
2717ad8beaeaSMika Kuoppala 
2718ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2719da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2720f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
27216274f212SChris Wilson 					break;
2722f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2723ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
27246274f212SChris Wilson 					break;
2725f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2726ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
27276274f212SChris Wilson 					break;
2728f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2729ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
27306274f212SChris Wilson 					stuck[i] = true;
27316274f212SChris Wilson 					break;
27326274f212SChris Wilson 				}
273305407ff8SMika Kuoppala 			}
27349107e9d2SChris Wilson 		} else {
2735da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2736da661464SMika Kuoppala 
27379107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
27389107e9d2SChris Wilson 			 * attempts across multiple batches.
27399107e9d2SChris Wilson 			 */
27409107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
27419107e9d2SChris Wilson 				ring->hangcheck.score--;
2742cbb465e7SChris Wilson 		}
2743f65d9421SBen Gamari 
274405407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
274505407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
27469107e9d2SChris Wilson 		busy_count += busy;
274705407ff8SMika Kuoppala 	}
274805407ff8SMika Kuoppala 
274905407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2750b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2751b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
275205407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2753a43adf07SChris Wilson 				 ring->name);
2754a43adf07SChris Wilson 			rings_hung++;
275505407ff8SMika Kuoppala 		}
275605407ff8SMika Kuoppala 	}
275705407ff8SMika Kuoppala 
275805407ff8SMika Kuoppala 	if (rings_hung)
275958174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
276005407ff8SMika Kuoppala 
276105407ff8SMika Kuoppala 	if (busy_count)
276205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
276305407ff8SMika Kuoppala 		 * being added */
276410cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
276510cd45b6SMika Kuoppala }
276610cd45b6SMika Kuoppala 
276710cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
276810cd45b6SMika Kuoppala {
276910cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
2770d330a953SJani Nikula 	if (!i915.enable_hangcheck)
277110cd45b6SMika Kuoppala 		return;
277210cd45b6SMika Kuoppala 
277399584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
277410cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2775f65d9421SBen Gamari }
2776f65d9421SBen Gamari 
277791738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
277891738a95SPaulo Zanoni {
277991738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
278091738a95SPaulo Zanoni 
278191738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
278291738a95SPaulo Zanoni 		return;
278391738a95SPaulo Zanoni 
278491738a95SPaulo Zanoni 	/* south display irq */
278591738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
278691738a95SPaulo Zanoni 	/*
278791738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
278891738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
278991738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
279091738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
279191738a95SPaulo Zanoni 	 */
279291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
279391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
279491738a95SPaulo Zanoni }
279591738a95SPaulo Zanoni 
2796d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2797d18ea1b5SDaniel Vetter {
2798d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2799d18ea1b5SDaniel Vetter 
2800d18ea1b5SDaniel Vetter 	/* and GT */
2801d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2802d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2803d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2804d18ea1b5SDaniel Vetter 
2805d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2806d18ea1b5SDaniel Vetter 		/* and PM */
2807d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2808d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2809d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2810d18ea1b5SDaniel Vetter 	}
2811d18ea1b5SDaniel Vetter }
2812d18ea1b5SDaniel Vetter 
2813c0e09200SDave Airlie /* drm_dma.h hooks
2814c0e09200SDave Airlie */
2815f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2816036a4a7dSZhenyu Wang {
2817036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2818036a4a7dSZhenyu Wang 
2819036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2820bdfcdb63SDaniel Vetter 
2821036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2822036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
28233143a2bfSChris Wilson 	POSTING_READ(DEIER);
2824036a4a7dSZhenyu Wang 
2825d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2826c650156aSZhenyu Wang 
282791738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
28287d99163dSBen Widawsky }
28297d99163dSBen Widawsky 
28307e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
28317e231dbeSJesse Barnes {
28327e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28337e231dbeSJesse Barnes 	int pipe;
28347e231dbeSJesse Barnes 
28357e231dbeSJesse Barnes 	/* VLV magic */
28367e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
28377e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
28387e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
28397e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
28407e231dbeSJesse Barnes 
28417e231dbeSJesse Barnes 	/* and GT */
28427e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
28437e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2844d18ea1b5SDaniel Vetter 
2845d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
28467e231dbeSJesse Barnes 
28477e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
28487e231dbeSJesse Barnes 
28497e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
28507e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
28517e231dbeSJesse Barnes 	for_each_pipe(pipe)
28527e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28537e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28547e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
28557e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
28567e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28577e231dbeSJesse Barnes }
28587e231dbeSJesse Barnes 
2859abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2860abd58f01SBen Widawsky {
2861abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2862abd58f01SBen Widawsky 	int pipe;
2863abd58f01SBen Widawsky 
2864abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2865abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2866abd58f01SBen Widawsky 
2867abd58f01SBen Widawsky 	/* IIR can theoretically queue up two events. Be paranoid */
2868abd58f01SBen Widawsky #define GEN8_IRQ_INIT_NDX(type, which) do { \
2869abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2870abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR(which)); \
2871abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2872abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2873abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR(which)); \
2874abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2875abd58f01SBen Widawsky 	} while (0)
2876abd58f01SBen Widawsky 
2877abd58f01SBen Widawsky #define GEN8_IRQ_INIT(type) do { \
2878abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2879abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR); \
2880abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2881abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2882abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR); \
2883abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2884abd58f01SBen Widawsky 	} while (0)
2885abd58f01SBen Widawsky 
2886abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 0);
2887abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 1);
2888abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 2);
2889abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 3);
2890abd58f01SBen Widawsky 
2891abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2892abd58f01SBen Widawsky 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe);
2893abd58f01SBen Widawsky 	}
2894abd58f01SBen Widawsky 
2895abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_PORT);
2896abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_MISC);
2897abd58f01SBen Widawsky 	GEN8_IRQ_INIT(PCU);
2898abd58f01SBen Widawsky #undef GEN8_IRQ_INIT
2899abd58f01SBen Widawsky #undef GEN8_IRQ_INIT_NDX
2900abd58f01SBen Widawsky 
2901abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
290209f2344dSJesse Barnes 
290309f2344dSJesse Barnes 	ibx_irq_preinstall(dev);
2904abd58f01SBen Widawsky }
2905abd58f01SBen Widawsky 
290682a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
290782a28bcfSDaniel Vetter {
290882a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
290982a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
291082a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2911fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
291282a28bcfSDaniel Vetter 
291382a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2914fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
291582a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2916cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2917fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
291882a28bcfSDaniel Vetter 	} else {
2919fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
292082a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2921cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2922fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
292382a28bcfSDaniel Vetter 	}
292482a28bcfSDaniel Vetter 
2925fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
292682a28bcfSDaniel Vetter 
29277fe0b973SKeith Packard 	/*
29287fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
29297fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
29307fe0b973SKeith Packard 	 *
29317fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
29327fe0b973SKeith Packard 	 */
29337fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
29347fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
29357fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
29367fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
29377fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
29387fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
29397fe0b973SKeith Packard }
29407fe0b973SKeith Packard 
2941d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2942d46da437SPaulo Zanoni {
2943d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
294482a28bcfSDaniel Vetter 	u32 mask;
2945d46da437SPaulo Zanoni 
2946692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2947692a04cfSDaniel Vetter 		return;
2948692a04cfSDaniel Vetter 
29498664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
29508664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2951de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
29528664281bSPaulo Zanoni 	} else {
29538664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
29548664281bSPaulo Zanoni 
29558664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
29568664281bSPaulo Zanoni 	}
2957ab5c608bSBen Widawsky 
2958d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2959d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2960d46da437SPaulo Zanoni }
2961d46da437SPaulo Zanoni 
29620a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
29630a9a8c91SDaniel Vetter {
29640a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
29650a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
29660a9a8c91SDaniel Vetter 
29670a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
29680a9a8c91SDaniel Vetter 
29690a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2970040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
29710a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
297235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
297335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
29740a9a8c91SDaniel Vetter 	}
29750a9a8c91SDaniel Vetter 
29760a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
29770a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
29780a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
29790a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
29800a9a8c91SDaniel Vetter 	} else {
29810a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
29820a9a8c91SDaniel Vetter 	}
29830a9a8c91SDaniel Vetter 
29840a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
29850a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
29860a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
29870a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
29880a9a8c91SDaniel Vetter 
29890a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2990a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
29910a9a8c91SDaniel Vetter 
29920a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
29930a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
29940a9a8c91SDaniel Vetter 
2995605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
29960a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2997605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
29980a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
29990a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
30000a9a8c91SDaniel Vetter 	}
30010a9a8c91SDaniel Vetter }
30020a9a8c91SDaniel Vetter 
3003f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3004036a4a7dSZhenyu Wang {
30054bc9d430SDaniel Vetter 	unsigned long irqflags;
3006036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
30078e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
30088e76f8dcSPaulo Zanoni 
30098e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
30108e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
30118e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
30128e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
30138e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
30148e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
30158e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
30168e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
30178e76f8dcSPaulo Zanoni 
30188e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
30198e76f8dcSPaulo Zanoni 	} else {
30208e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3021ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
30225b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
30235b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
30245b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
30255b3a856bSDaniel Vetter 				DE_POISON);
30268e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
30278e76f8dcSPaulo Zanoni 	}
3028036a4a7dSZhenyu Wang 
30291ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3030036a4a7dSZhenyu Wang 
3031036a4a7dSZhenyu Wang 	/* should always can generate irq */
3032036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
30331ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
30348e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
30353143a2bfSChris Wilson 	POSTING_READ(DEIER);
3036036a4a7dSZhenyu Wang 
30370a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3038036a4a7dSZhenyu Wang 
3039d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
30407fe0b973SKeith Packard 
3041f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
30426005ce42SDaniel Vetter 		/* Enable PCU event interrupts
30436005ce42SDaniel Vetter 		 *
30446005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
30454bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
30464bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
30474bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3048f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
30494bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3050f97108d1SJesse Barnes 	}
3051f97108d1SJesse Barnes 
3052036a4a7dSZhenyu Wang 	return 0;
3053036a4a7dSZhenyu Wang }
3054036a4a7dSZhenyu Wang 
3055f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3056f8b79e58SImre Deak {
3057f8b79e58SImre Deak 	u32 pipestat_mask;
3058f8b79e58SImre Deak 	u32 iir_mask;
3059f8b79e58SImre Deak 
3060f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3061f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3062f8b79e58SImre Deak 
3063f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3064f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3065f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3066f8b79e58SImre Deak 
3067f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3068f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3069f8b79e58SImre Deak 
3070f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3071f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3072f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3073f8b79e58SImre Deak 
3074f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3075f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3076f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3077f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3078f8b79e58SImre Deak 
3079f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3080f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3081f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3082f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3083f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3084f8b79e58SImre Deak }
3085f8b79e58SImre Deak 
3086f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3087f8b79e58SImre Deak {
3088f8b79e58SImre Deak 	u32 pipestat_mask;
3089f8b79e58SImre Deak 	u32 iir_mask;
3090f8b79e58SImre Deak 
3091f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3092f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
30936c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3094f8b79e58SImre Deak 
3095f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3096f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3097f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3098f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3099f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3100f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3101f8b79e58SImre Deak 
3102f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3103f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3104f8b79e58SImre Deak 
3105f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3106f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3107f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3108f8b79e58SImre Deak 
3109f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3110f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3111f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3112f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3113f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3114f8b79e58SImre Deak }
3115f8b79e58SImre Deak 
3116f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3117f8b79e58SImre Deak {
3118f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3119f8b79e58SImre Deak 
3120f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3121f8b79e58SImre Deak 		return;
3122f8b79e58SImre Deak 
3123f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3124f8b79e58SImre Deak 
3125f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3126f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3127f8b79e58SImre Deak }
3128f8b79e58SImre Deak 
3129f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3130f8b79e58SImre Deak {
3131f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3132f8b79e58SImre Deak 
3133f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3134f8b79e58SImre Deak 		return;
3135f8b79e58SImre Deak 
3136f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3137f8b79e58SImre Deak 
3138f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3139f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3140f8b79e58SImre Deak }
3141f8b79e58SImre Deak 
31427e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
31437e231dbeSJesse Barnes {
31447e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3145b79480baSDaniel Vetter 	unsigned long irqflags;
31467e231dbeSJesse Barnes 
3147f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
31487e231dbeSJesse Barnes 
314920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
315020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
315120afbda2SDaniel Vetter 
31527e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3153f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
31547e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31557e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31567e231dbeSJesse Barnes 
3157b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3158b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3159b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3160f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3161f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3162b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
316331acc7f5SJesse Barnes 
31647e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31657e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31667e231dbeSJesse Barnes 
31670a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
31687e231dbeSJesse Barnes 
31697e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
31707e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
31717e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31727e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
31737e231dbeSJesse Barnes #endif
31747e231dbeSJesse Barnes 
31757e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
317620afbda2SDaniel Vetter 
317720afbda2SDaniel Vetter 	return 0;
317820afbda2SDaniel Vetter }
317920afbda2SDaniel Vetter 
3180abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3181abd58f01SBen Widawsky {
3182abd58f01SBen Widawsky 	int i;
3183abd58f01SBen Widawsky 
3184abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3185abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3186abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3187abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3188abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3189abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3190abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3191abd58f01SBen Widawsky 		0,
3192abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3193abd58f01SBen Widawsky 		};
3194abd58f01SBen Widawsky 
3195abd58f01SBen Widawsky 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++) {
3196abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_GT_IIR(i));
3197abd58f01SBen Widawsky 		if (tmp)
3198abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3199abd58f01SBen Widawsky 				  i, tmp);
3200abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IMR(i), ~gt_interrupts[i]);
3201abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IER(i), gt_interrupts[i]);
3202abd58f01SBen Widawsky 	}
3203abd58f01SBen Widawsky 	POSTING_READ(GEN8_GT_IER(0));
3204abd58f01SBen Widawsky }
3205abd58f01SBen Widawsky 
3206abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3207abd58f01SBen Widawsky {
3208abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
320913b3a0a7SDaniel Vetter 	uint32_t de_pipe_masked = GEN8_PIPE_FLIP_DONE |
32100fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
321138d83c96SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN |
321230100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
321313b3a0a7SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK;
3214abd58f01SBen Widawsky 	int pipe;
321513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
321613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
321713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3218abd58f01SBen Widawsky 
3219abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3220abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_DE_PIPE_IIR(pipe));
3221abd58f01SBen Widawsky 		if (tmp)
3222abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3223abd58f01SBen Widawsky 				  pipe, tmp);
3224abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
3225abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IER(pipe), de_pipe_enables);
3226abd58f01SBen Widawsky 	}
3227abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_ISR(0));
3228abd58f01SBen Widawsky 
32296d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IMR, ~GEN8_AUX_CHANNEL_A);
32306d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IER, GEN8_AUX_CHANNEL_A);
3231abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PORT_IER);
3232abd58f01SBen Widawsky }
3233abd58f01SBen Widawsky 
3234abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3235abd58f01SBen Widawsky {
3236abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3237abd58f01SBen Widawsky 
3238abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3239abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3240abd58f01SBen Widawsky 
3241abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3242abd58f01SBen Widawsky 
3243abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3244abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3245abd58f01SBen Widawsky 
3246abd58f01SBen Widawsky 	return 0;
3247abd58f01SBen Widawsky }
3248abd58f01SBen Widawsky 
3249abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3250abd58f01SBen Widawsky {
3251abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3252abd58f01SBen Widawsky 	int pipe;
3253abd58f01SBen Widawsky 
3254abd58f01SBen Widawsky 	if (!dev_priv)
3255abd58f01SBen Widawsky 		return;
3256abd58f01SBen Widawsky 
3257abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3258abd58f01SBen Widawsky 
3259abd58f01SBen Widawsky #define GEN8_IRQ_FINI_NDX(type, which) do { \
3260abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
3261abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
3262abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3263abd58f01SBen Widawsky 	} while (0)
3264abd58f01SBen Widawsky 
3265abd58f01SBen Widawsky #define GEN8_IRQ_FINI(type) do { \
3266abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
3267abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
3268abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3269abd58f01SBen Widawsky 	} while (0)
3270abd58f01SBen Widawsky 
3271abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 0);
3272abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 1);
3273abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 2);
3274abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 3);
3275abd58f01SBen Widawsky 
3276abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3277abd58f01SBen Widawsky 		GEN8_IRQ_FINI_NDX(DE_PIPE, pipe);
3278abd58f01SBen Widawsky 	}
3279abd58f01SBen Widawsky 
3280abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_PORT);
3281abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_MISC);
3282abd58f01SBen Widawsky 	GEN8_IRQ_FINI(PCU);
3283abd58f01SBen Widawsky #undef GEN8_IRQ_FINI
3284abd58f01SBen Widawsky #undef GEN8_IRQ_FINI_NDX
3285abd58f01SBen Widawsky 
3286abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
3287abd58f01SBen Widawsky }
3288abd58f01SBen Widawsky 
32897e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
32907e231dbeSJesse Barnes {
32917e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3292f8b79e58SImre Deak 	unsigned long irqflags;
32937e231dbeSJesse Barnes 	int pipe;
32947e231dbeSJesse Barnes 
32957e231dbeSJesse Barnes 	if (!dev_priv)
32967e231dbeSJesse Barnes 		return;
32977e231dbeSJesse Barnes 
32983ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3299ac4c16c5SEgbert Eich 
33007e231dbeSJesse Barnes 	for_each_pipe(pipe)
33017e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
33027e231dbeSJesse Barnes 
33037e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
33047e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
33057e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3306f8b79e58SImre Deak 
3307f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3308f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3309f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3310f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3311f8b79e58SImre Deak 
3312f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3313f8b79e58SImre Deak 
33147e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
33157e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
33167e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
33177e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
33187e231dbeSJesse Barnes }
33197e231dbeSJesse Barnes 
3320f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3321036a4a7dSZhenyu Wang {
3322036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
33234697995bSJesse Barnes 
33244697995bSJesse Barnes 	if (!dev_priv)
33254697995bSJesse Barnes 		return;
33264697995bSJesse Barnes 
33273ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3328ac4c16c5SEgbert Eich 
3329036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
3330036a4a7dSZhenyu Wang 
3331036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
3332036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
3333036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
33348664281bSPaulo Zanoni 	if (IS_GEN7(dev))
33358664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
3336036a4a7dSZhenyu Wang 
3337036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
3338036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
3339036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3340192aac1fSKeith Packard 
3341ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
3342ab5c608bSBen Widawsky 		return;
3343ab5c608bSBen Widawsky 
3344192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
3345192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
3346192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
33478664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
33488664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
3349036a4a7dSZhenyu Wang }
3350036a4a7dSZhenyu Wang 
3351c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3352c2798b19SChris Wilson {
3353c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3354c2798b19SChris Wilson 	int pipe;
3355c2798b19SChris Wilson 
3356c2798b19SChris Wilson 	for_each_pipe(pipe)
3357c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3358c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3359c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3360c2798b19SChris Wilson 	POSTING_READ16(IER);
3361c2798b19SChris Wilson }
3362c2798b19SChris Wilson 
3363c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3364c2798b19SChris Wilson {
3365c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3366379ef82dSDaniel Vetter 	unsigned long irqflags;
3367c2798b19SChris Wilson 
3368c2798b19SChris Wilson 	I915_WRITE16(EMR,
3369c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3370c2798b19SChris Wilson 
3371c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3372c2798b19SChris Wilson 	dev_priv->irq_mask =
3373c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3374c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3375c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3376c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3377c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3378c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3379c2798b19SChris Wilson 
3380c2798b19SChris Wilson 	I915_WRITE16(IER,
3381c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3382c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3383c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3384c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3385c2798b19SChris Wilson 	POSTING_READ16(IER);
3386c2798b19SChris Wilson 
3387379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3388379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3389379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3390755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3391755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3392379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3393379ef82dSDaniel Vetter 
3394c2798b19SChris Wilson 	return 0;
3395c2798b19SChris Wilson }
3396c2798b19SChris Wilson 
339790a72f87SVille Syrjälä /*
339890a72f87SVille Syrjälä  * Returns true when a page flip has completed.
339990a72f87SVille Syrjälä  */
340090a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
34011f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
340290a72f87SVille Syrjälä {
340390a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
34041f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
340590a72f87SVille Syrjälä 
340690a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
340790a72f87SVille Syrjälä 		return false;
340890a72f87SVille Syrjälä 
340990a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
341090a72f87SVille Syrjälä 		return false;
341190a72f87SVille Syrjälä 
34121f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
341390a72f87SVille Syrjälä 
341490a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
341590a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
341690a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
341790a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
341890a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
341990a72f87SVille Syrjälä 	 */
342090a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
342190a72f87SVille Syrjälä 		return false;
342290a72f87SVille Syrjälä 
342390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
342490a72f87SVille Syrjälä 
342590a72f87SVille Syrjälä 	return true;
342690a72f87SVille Syrjälä }
342790a72f87SVille Syrjälä 
3428ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3429c2798b19SChris Wilson {
3430c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3431c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3432c2798b19SChris Wilson 	u16 iir, new_iir;
3433c2798b19SChris Wilson 	u32 pipe_stats[2];
3434c2798b19SChris Wilson 	unsigned long irqflags;
3435c2798b19SChris Wilson 	int pipe;
3436c2798b19SChris Wilson 	u16 flip_mask =
3437c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3438c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3439c2798b19SChris Wilson 
3440c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3441c2798b19SChris Wilson 	if (iir == 0)
3442c2798b19SChris Wilson 		return IRQ_NONE;
3443c2798b19SChris Wilson 
3444c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3445c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3446c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3447c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3448c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3449c2798b19SChris Wilson 		 */
3450c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3451c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
345258174462SMika Kuoppala 			i915_handle_error(dev, false,
345358174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
345458174462SMika Kuoppala 					  iir);
3455c2798b19SChris Wilson 
3456c2798b19SChris Wilson 		for_each_pipe(pipe) {
3457c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3458c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3459c2798b19SChris Wilson 
3460c2798b19SChris Wilson 			/*
3461c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3462c2798b19SChris Wilson 			 */
34632d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3464c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3465c2798b19SChris Wilson 		}
3466c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3467c2798b19SChris Wilson 
3468c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3469c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3470c2798b19SChris Wilson 
3471d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3472c2798b19SChris Wilson 
3473c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3474c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3475c2798b19SChris Wilson 
34764356d586SDaniel Vetter 		for_each_pipe(pipe) {
34771f1c2e24SVille Syrjälä 			int plane = pipe;
34783a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
34791f1c2e24SVille Syrjälä 				plane = !plane;
34801f1c2e24SVille Syrjälä 
34814356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
34821f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
34831f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3484c2798b19SChris Wilson 
34854356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3486277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
34872d9d2b0bSVille Syrjälä 
34882d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
34892d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3490fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
34914356d586SDaniel Vetter 		}
3492c2798b19SChris Wilson 
3493c2798b19SChris Wilson 		iir = new_iir;
3494c2798b19SChris Wilson 	}
3495c2798b19SChris Wilson 
3496c2798b19SChris Wilson 	return IRQ_HANDLED;
3497c2798b19SChris Wilson }
3498c2798b19SChris Wilson 
3499c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3500c2798b19SChris Wilson {
3501c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3502c2798b19SChris Wilson 	int pipe;
3503c2798b19SChris Wilson 
3504c2798b19SChris Wilson 	for_each_pipe(pipe) {
3505c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3506c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3507c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3508c2798b19SChris Wilson 	}
3509c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3510c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3511c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3512c2798b19SChris Wilson }
3513c2798b19SChris Wilson 
3514a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3515a266c7d5SChris Wilson {
3516a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3517a266c7d5SChris Wilson 	int pipe;
3518a266c7d5SChris Wilson 
3519a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3520a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3521a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3522a266c7d5SChris Wilson 	}
3523a266c7d5SChris Wilson 
352400d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3525a266c7d5SChris Wilson 	for_each_pipe(pipe)
3526a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3527a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3528a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3529a266c7d5SChris Wilson 	POSTING_READ(IER);
3530a266c7d5SChris Wilson }
3531a266c7d5SChris Wilson 
3532a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3533a266c7d5SChris Wilson {
3534a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
353538bde180SChris Wilson 	u32 enable_mask;
3536379ef82dSDaniel Vetter 	unsigned long irqflags;
3537a266c7d5SChris Wilson 
353838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
353938bde180SChris Wilson 
354038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
354138bde180SChris Wilson 	dev_priv->irq_mask =
354238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
354338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
354438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
354538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
354638bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
354738bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
354838bde180SChris Wilson 
354938bde180SChris Wilson 	enable_mask =
355038bde180SChris Wilson 		I915_ASLE_INTERRUPT |
355138bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
355238bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
355338bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
355438bde180SChris Wilson 		I915_USER_INTERRUPT;
355538bde180SChris Wilson 
3556a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
355720afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
355820afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
355920afbda2SDaniel Vetter 
3560a266c7d5SChris Wilson 		/* Enable in IER... */
3561a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3562a266c7d5SChris Wilson 		/* and unmask in IMR */
3563a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3564a266c7d5SChris Wilson 	}
3565a266c7d5SChris Wilson 
3566a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3567a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3568a266c7d5SChris Wilson 	POSTING_READ(IER);
3569a266c7d5SChris Wilson 
3570f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
357120afbda2SDaniel Vetter 
3572379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3573379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3574379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3575755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3576755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3577379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3578379ef82dSDaniel Vetter 
357920afbda2SDaniel Vetter 	return 0;
358020afbda2SDaniel Vetter }
358120afbda2SDaniel Vetter 
358290a72f87SVille Syrjälä /*
358390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
358490a72f87SVille Syrjälä  */
358590a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
358690a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
358790a72f87SVille Syrjälä {
358890a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
358990a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
359090a72f87SVille Syrjälä 
359190a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
359290a72f87SVille Syrjälä 		return false;
359390a72f87SVille Syrjälä 
359490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
359590a72f87SVille Syrjälä 		return false;
359690a72f87SVille Syrjälä 
359790a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
359890a72f87SVille Syrjälä 
359990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
360090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
360190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
360290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
360390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
360490a72f87SVille Syrjälä 	 */
360590a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
360690a72f87SVille Syrjälä 		return false;
360790a72f87SVille Syrjälä 
360890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
360990a72f87SVille Syrjälä 
361090a72f87SVille Syrjälä 	return true;
361190a72f87SVille Syrjälä }
361290a72f87SVille Syrjälä 
3613ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3614a266c7d5SChris Wilson {
3615a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3616a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
36178291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3618a266c7d5SChris Wilson 	unsigned long irqflags;
361938bde180SChris Wilson 	u32 flip_mask =
362038bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
362138bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
362238bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3623a266c7d5SChris Wilson 
3624a266c7d5SChris Wilson 	iir = I915_READ(IIR);
362538bde180SChris Wilson 	do {
362638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
36278291ee90SChris Wilson 		bool blc_event = false;
3628a266c7d5SChris Wilson 
3629a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3630a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3631a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3632a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3633a266c7d5SChris Wilson 		 */
3634a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3635a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
363658174462SMika Kuoppala 			i915_handle_error(dev, false,
363758174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
363858174462SMika Kuoppala 					  iir);
3639a266c7d5SChris Wilson 
3640a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3641a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3642a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3643a266c7d5SChris Wilson 
364438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3645a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3646a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
364738bde180SChris Wilson 				irq_received = true;
3648a266c7d5SChris Wilson 			}
3649a266c7d5SChris Wilson 		}
3650a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3651a266c7d5SChris Wilson 
3652a266c7d5SChris Wilson 		if (!irq_received)
3653a266c7d5SChris Wilson 			break;
3654a266c7d5SChris Wilson 
3655a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3656a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3657a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3658a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3659b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3660a266c7d5SChris Wilson 
366110a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
366291d131d2SDaniel Vetter 
3663a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
366438bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3665a266c7d5SChris Wilson 		}
3666a266c7d5SChris Wilson 
366738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3668a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3669a266c7d5SChris Wilson 
3670a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3671a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3672a266c7d5SChris Wilson 
3673a266c7d5SChris Wilson 		for_each_pipe(pipe) {
367438bde180SChris Wilson 			int plane = pipe;
36753a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
367638bde180SChris Wilson 				plane = !plane;
36775e2032d4SVille Syrjälä 
367890a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
367990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
368090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3681a266c7d5SChris Wilson 
3682a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3683a266c7d5SChris Wilson 				blc_event = true;
36844356d586SDaniel Vetter 
36854356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3686277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
36872d9d2b0bSVille Syrjälä 
36882d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
36892d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3690fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
3691a266c7d5SChris Wilson 		}
3692a266c7d5SChris Wilson 
3693a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3694a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3695a266c7d5SChris Wilson 
3696a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3697a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3698a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3699a266c7d5SChris Wilson 		 * we would never get another interrupt.
3700a266c7d5SChris Wilson 		 *
3701a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3702a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3703a266c7d5SChris Wilson 		 * another one.
3704a266c7d5SChris Wilson 		 *
3705a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3706a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3707a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3708a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3709a266c7d5SChris Wilson 		 * stray interrupts.
3710a266c7d5SChris Wilson 		 */
371138bde180SChris Wilson 		ret = IRQ_HANDLED;
3712a266c7d5SChris Wilson 		iir = new_iir;
371338bde180SChris Wilson 	} while (iir & ~flip_mask);
3714a266c7d5SChris Wilson 
3715d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
37168291ee90SChris Wilson 
3717a266c7d5SChris Wilson 	return ret;
3718a266c7d5SChris Wilson }
3719a266c7d5SChris Wilson 
3720a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3721a266c7d5SChris Wilson {
3722a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3723a266c7d5SChris Wilson 	int pipe;
3724a266c7d5SChris Wilson 
37253ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3726ac4c16c5SEgbert Eich 
3727a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3728a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3729a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3730a266c7d5SChris Wilson 	}
3731a266c7d5SChris Wilson 
373200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
373355b39755SChris Wilson 	for_each_pipe(pipe) {
373455b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3735a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
373655b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
373755b39755SChris Wilson 	}
3738a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3739a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3740a266c7d5SChris Wilson 
3741a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3742a266c7d5SChris Wilson }
3743a266c7d5SChris Wilson 
3744a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3745a266c7d5SChris Wilson {
3746a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3747a266c7d5SChris Wilson 	int pipe;
3748a266c7d5SChris Wilson 
3749a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3750a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3751a266c7d5SChris Wilson 
3752a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3753a266c7d5SChris Wilson 	for_each_pipe(pipe)
3754a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3755a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3756a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3757a266c7d5SChris Wilson 	POSTING_READ(IER);
3758a266c7d5SChris Wilson }
3759a266c7d5SChris Wilson 
3760a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3761a266c7d5SChris Wilson {
3762a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3763bbba0a97SChris Wilson 	u32 enable_mask;
3764a266c7d5SChris Wilson 	u32 error_mask;
3765b79480baSDaniel Vetter 	unsigned long irqflags;
3766a266c7d5SChris Wilson 
3767a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3768bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3769adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3770bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3771bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3772bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3773bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3774bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3775bbba0a97SChris Wilson 
3776bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
377721ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
377821ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3779bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3780bbba0a97SChris Wilson 
3781bbba0a97SChris Wilson 	if (IS_G4X(dev))
3782bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3783a266c7d5SChris Wilson 
3784b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3785b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3786b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3787755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3788755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3789755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3790b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3791a266c7d5SChris Wilson 
3792a266c7d5SChris Wilson 	/*
3793a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3794a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3795a266c7d5SChris Wilson 	 */
3796a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3797a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3798a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3799a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3800a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3801a266c7d5SChris Wilson 	} else {
3802a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3803a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3804a266c7d5SChris Wilson 	}
3805a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3806a266c7d5SChris Wilson 
3807a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3808a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3809a266c7d5SChris Wilson 	POSTING_READ(IER);
3810a266c7d5SChris Wilson 
381120afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
381220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
381320afbda2SDaniel Vetter 
3814f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
381520afbda2SDaniel Vetter 
381620afbda2SDaniel Vetter 	return 0;
381720afbda2SDaniel Vetter }
381820afbda2SDaniel Vetter 
3819bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
382020afbda2SDaniel Vetter {
382120afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3822e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3823cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
382420afbda2SDaniel Vetter 	u32 hotplug_en;
382520afbda2SDaniel Vetter 
3826b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3827b5ea2d56SDaniel Vetter 
3828bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3829bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3830bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3831adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3832e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3833cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3834cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3835cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3836a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3837a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3838a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3839a266c7d5SChris Wilson 		*/
3840a266c7d5SChris Wilson 		if (IS_G4X(dev))
3841a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
384285fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3843a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3844a266c7d5SChris Wilson 
3845a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3846a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3847a266c7d5SChris Wilson 	}
3848bac56d5bSEgbert Eich }
3849a266c7d5SChris Wilson 
3850ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3851a266c7d5SChris Wilson {
3852a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3853a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3854a266c7d5SChris Wilson 	u32 iir, new_iir;
3855a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3856a266c7d5SChris Wilson 	unsigned long irqflags;
3857a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
385821ad8330SVille Syrjälä 	u32 flip_mask =
385921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
386021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3861a266c7d5SChris Wilson 
3862a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3863a266c7d5SChris Wilson 
3864a266c7d5SChris Wilson 	for (;;) {
3865501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
38662c8ba29fSChris Wilson 		bool blc_event = false;
38672c8ba29fSChris Wilson 
3868a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3869a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3870a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3871a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3872a266c7d5SChris Wilson 		 */
3873a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3874a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
387558174462SMika Kuoppala 			i915_handle_error(dev, false,
387658174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
387758174462SMika Kuoppala 					  iir);
3878a266c7d5SChris Wilson 
3879a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3880a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3881a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3882a266c7d5SChris Wilson 
3883a266c7d5SChris Wilson 			/*
3884a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3885a266c7d5SChris Wilson 			 */
3886a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3887a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3888501e01d7SVille Syrjälä 				irq_received = true;
3889a266c7d5SChris Wilson 			}
3890a266c7d5SChris Wilson 		}
3891a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3892a266c7d5SChris Wilson 
3893a266c7d5SChris Wilson 		if (!irq_received)
3894a266c7d5SChris Wilson 			break;
3895a266c7d5SChris Wilson 
3896a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3897a266c7d5SChris Wilson 
3898a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3899adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3900a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3901b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3902b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
39034f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3904a266c7d5SChris Wilson 
390510a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
3906704cfb87SDaniel Vetter 					      IS_G4X(dev) ? hpd_status_g4x : hpd_status_i915);
390791d131d2SDaniel Vetter 
39084aeebd74SDaniel Vetter 			if (IS_G4X(dev) &&
39094aeebd74SDaniel Vetter 			    (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X))
39104aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
39114aeebd74SDaniel Vetter 
3912a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3913a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3914a266c7d5SChris Wilson 		}
3915a266c7d5SChris Wilson 
391621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3917a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3918a266c7d5SChris Wilson 
3919a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3920a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3921a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3922a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3923a266c7d5SChris Wilson 
3924a266c7d5SChris Wilson 		for_each_pipe(pipe) {
39252c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
392690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
392790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3928a266c7d5SChris Wilson 
3929a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3930a266c7d5SChris Wilson 				blc_event = true;
39314356d586SDaniel Vetter 
39324356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3933277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3934a266c7d5SChris Wilson 
39352d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
39362d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3937fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
39382d9d2b0bSVille Syrjälä 		}
3939a266c7d5SChris Wilson 
3940a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3941a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3942a266c7d5SChris Wilson 
3943515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3944515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3945515ac2bbSDaniel Vetter 
3946a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3947a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3948a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3949a266c7d5SChris Wilson 		 * we would never get another interrupt.
3950a266c7d5SChris Wilson 		 *
3951a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3952a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3953a266c7d5SChris Wilson 		 * another one.
3954a266c7d5SChris Wilson 		 *
3955a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3956a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3957a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3958a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3959a266c7d5SChris Wilson 		 * stray interrupts.
3960a266c7d5SChris Wilson 		 */
3961a266c7d5SChris Wilson 		iir = new_iir;
3962a266c7d5SChris Wilson 	}
3963a266c7d5SChris Wilson 
3964d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
39652c8ba29fSChris Wilson 
3966a266c7d5SChris Wilson 	return ret;
3967a266c7d5SChris Wilson }
3968a266c7d5SChris Wilson 
3969a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3970a266c7d5SChris Wilson {
3971a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3972a266c7d5SChris Wilson 	int pipe;
3973a266c7d5SChris Wilson 
3974a266c7d5SChris Wilson 	if (!dev_priv)
3975a266c7d5SChris Wilson 		return;
3976a266c7d5SChris Wilson 
39773ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3978ac4c16c5SEgbert Eich 
3979a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3980a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3981a266c7d5SChris Wilson 
3982a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3983a266c7d5SChris Wilson 	for_each_pipe(pipe)
3984a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3985a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3986a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3987a266c7d5SChris Wilson 
3988a266c7d5SChris Wilson 	for_each_pipe(pipe)
3989a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3990a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3991a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3992a266c7d5SChris Wilson }
3993a266c7d5SChris Wilson 
39943ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
3995ac4c16c5SEgbert Eich {
3996ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3997ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3998ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3999ac4c16c5SEgbert Eich 	unsigned long irqflags;
4000ac4c16c5SEgbert Eich 	int i;
4001ac4c16c5SEgbert Eich 
4002ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4003ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4004ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4005ac4c16c5SEgbert Eich 
4006ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4007ac4c16c5SEgbert Eich 			continue;
4008ac4c16c5SEgbert Eich 
4009ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4010ac4c16c5SEgbert Eich 
4011ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4012ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4013ac4c16c5SEgbert Eich 
4014ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4015ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4016ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4017ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
4018ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4019ac4c16c5SEgbert Eich 				if (!connector->polled)
4020ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4021ac4c16c5SEgbert Eich 			}
4022ac4c16c5SEgbert Eich 		}
4023ac4c16c5SEgbert Eich 	}
4024ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4025ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4026ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4027ac4c16c5SEgbert Eich }
4028ac4c16c5SEgbert Eich 
4029f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4030f71d4af4SJesse Barnes {
40318b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
40328b2e326dSChris Wilson 
40338b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
403499584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4035c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4036a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
40378b2e326dSChris Wilson 
4038a6706b45SDeepak S 	/* Let's track the enabled rps events */
4039a6706b45SDeepak S 	dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4040a6706b45SDeepak S 
404199584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
404299584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
404361bac78eSDaniel Vetter 		    (unsigned long) dev);
40443ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4045ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
404661bac78eSDaniel Vetter 
404797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
40489ee32feaSDaniel Vetter 
40494cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
40504cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
40514cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
40524cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4053f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4054f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4055391f75e2SVille Syrjälä 	} else {
4056391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4057391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4058f71d4af4SJesse Barnes 	}
4059f71d4af4SJesse Barnes 
4060c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4061f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4062f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4063c2baf4b7SVille Syrjälä 	}
4064f71d4af4SJesse Barnes 
40657e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
40667e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
40677e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
40687e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
40697e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
40707e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
40717e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4072fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4073abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4074abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4075abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
4076abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4077abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4078abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4079abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4080abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4081f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4082f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4083f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
4084f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4085f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4086f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4087f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
408882a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4089f71d4af4SJesse Barnes 	} else {
4090c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4091c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4092c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4093c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4094c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4095a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4096a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4097a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4098a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4099a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
410020afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4101c2798b19SChris Wilson 		} else {
4102a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4103a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4104a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4105a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4106bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4107c2798b19SChris Wilson 		}
4108f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4109f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4110f71d4af4SJesse Barnes 	}
4111f71d4af4SJesse Barnes }
411220afbda2SDaniel Vetter 
411320afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
411420afbda2SDaniel Vetter {
411520afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4116821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4117821450c6SEgbert Eich 	struct drm_connector *connector;
4118b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4119821450c6SEgbert Eich 	int i;
412020afbda2SDaniel Vetter 
4121821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4122821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4123821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4124821450c6SEgbert Eich 	}
4125821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4126821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4127821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
4128821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4129821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4130821450c6SEgbert Eich 	}
4131b5ea2d56SDaniel Vetter 
4132b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4133b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4134b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
413520afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
413620afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4137b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
413820afbda2SDaniel Vetter }
4139c67a470bSPaulo Zanoni 
41405d584b2eSPaulo Zanoni /* Disable interrupts so we can allow runtime PM. */
41415d584b2eSPaulo Zanoni void hsw_runtime_pm_disable_interrupts(struct drm_device *dev)
4142c67a470bSPaulo Zanoni {
4143c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4144c67a470bSPaulo Zanoni 	unsigned long irqflags;
4145c67a470bSPaulo Zanoni 
4146c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4147c67a470bSPaulo Zanoni 
41485d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.deimr = I915_READ(DEIMR);
41495d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.sdeimr = I915_READ(SDEIMR);
41505d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.gtimr = I915_READ(GTIMR);
41515d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.gtier = I915_READ(GTIER);
41525d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
4153c67a470bSPaulo Zanoni 
41541f2d4531SPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, 0xffffffff);
41551f2d4531SPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, 0xffffffff);
4156c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
4157c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
4158c67a470bSPaulo Zanoni 
41595d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = true;
4160c67a470bSPaulo Zanoni 
4161c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4162c67a470bSPaulo Zanoni }
4163c67a470bSPaulo Zanoni 
41645d584b2eSPaulo Zanoni /* Restore interrupts so we can recover from runtime PM. */
41655d584b2eSPaulo Zanoni void hsw_runtime_pm_restore_interrupts(struct drm_device *dev)
4166c67a470bSPaulo Zanoni {
4167c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4168c67a470bSPaulo Zanoni 	unsigned long irqflags;
41691f2d4531SPaulo Zanoni 	uint32_t val;
4170c67a470bSPaulo Zanoni 
4171c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4172c67a470bSPaulo Zanoni 
4173c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
41741f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "DEIMR is 0x%08x\n", val);
4175c67a470bSPaulo Zanoni 
41761f2d4531SPaulo Zanoni 	val = I915_READ(SDEIMR);
41771f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "SDEIMR is 0x%08x\n", val);
4178c67a470bSPaulo Zanoni 
4179c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
41801f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GTIMR is 0x%08x\n", val);
4181c67a470bSPaulo Zanoni 
4182c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
41831f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GEN6_PMIMR is 0x%08x\n", val);
4184c67a470bSPaulo Zanoni 
41855d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = false;
4186c67a470bSPaulo Zanoni 
41875d584b2eSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pm.regsave.deimr);
41885d584b2eSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv, ~dev_priv->pm.regsave.sdeimr);
41895d584b2eSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pm.regsave.gtimr);
41905d584b2eSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pm.regsave.gen6_pmimr);
41915d584b2eSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pm.regsave.gtier);
4192c67a470bSPaulo Zanoni 
4193c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4194c67a470bSPaulo Zanoni }
4195