xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 77961eb984c7e5394bd29cc7be2ab0bf0cc7e7b1)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
41e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
45e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
46e5868a31SEgbert Eich };
47e5868a31SEgbert Eich 
48e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
56e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
63e5868a31SEgbert Eich };
64e5868a31SEgbert Eich 
65704cfb87SDaniel Vetter static const u32 hpd_status_g4x[] = {
66e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
72e5868a31SEgbert Eich };
73e5868a31SEgbert Eich 
74e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
75e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
81e5868a31SEgbert Eich };
82e5868a31SEgbert Eich 
83036a4a7dSZhenyu Wang /* For display hotplug interrupt */
84995b6762SChris Wilson static void
85f2b115e6SAdam Jackson ironlake_enable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
86036a4a7dSZhenyu Wang {
874bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
884bc9d430SDaniel Vetter 
89c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
90c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
91c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr &= ~mask;
92c67a470bSPaulo Zanoni 		return;
93c67a470bSPaulo Zanoni 	}
94c67a470bSPaulo Zanoni 
951ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
961ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
971ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
983143a2bfSChris Wilson 		POSTING_READ(DEIMR);
99036a4a7dSZhenyu Wang 	}
100036a4a7dSZhenyu Wang }
101036a4a7dSZhenyu Wang 
1020ff9800aSPaulo Zanoni static void
103f2b115e6SAdam Jackson ironlake_disable_display_irq(drm_i915_private_t *dev_priv, u32 mask)
104036a4a7dSZhenyu Wang {
1054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1064bc9d430SDaniel Vetter 
107c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
108c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
109c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.deimr |= mask;
110c67a470bSPaulo Zanoni 		return;
111c67a470bSPaulo Zanoni 	}
112c67a470bSPaulo Zanoni 
1131ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1141ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1151ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1163143a2bfSChris Wilson 		POSTING_READ(DEIMR);
117036a4a7dSZhenyu Wang 	}
118036a4a7dSZhenyu Wang }
119036a4a7dSZhenyu Wang 
12043eaea13SPaulo Zanoni /**
12143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
12243eaea13SPaulo Zanoni  * @dev_priv: driver private
12343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
12443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
12543eaea13SPaulo Zanoni  */
12643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
12743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
12843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
12943eaea13SPaulo Zanoni {
13043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
13143eaea13SPaulo Zanoni 
132c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
133c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
134c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr &= ~interrupt_mask;
135c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gtimr |= (~enabled_irq_mask &
136c67a470bSPaulo Zanoni 						interrupt_mask);
137c67a470bSPaulo Zanoni 		return;
138c67a470bSPaulo Zanoni 	}
139c67a470bSPaulo Zanoni 
14043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
14143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
14243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
14343eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
14443eaea13SPaulo Zanoni }
14543eaea13SPaulo Zanoni 
14643eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
14743eaea13SPaulo Zanoni {
14843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
14943eaea13SPaulo Zanoni }
15043eaea13SPaulo Zanoni 
15143eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
15243eaea13SPaulo Zanoni {
15343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
15443eaea13SPaulo Zanoni }
15543eaea13SPaulo Zanoni 
156edbfdb45SPaulo Zanoni /**
157edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
158edbfdb45SPaulo Zanoni   * @dev_priv: driver private
159edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
160edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
161edbfdb45SPaulo Zanoni   */
162edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
163edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
164edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
165edbfdb45SPaulo Zanoni {
166605cd25bSPaulo Zanoni 	uint32_t new_val;
167edbfdb45SPaulo Zanoni 
168edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
169edbfdb45SPaulo Zanoni 
170c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled) {
171c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
172c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr &= ~interrupt_mask;
173c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.gen6_pmimr |= (~enabled_irq_mask &
174c67a470bSPaulo Zanoni 						     interrupt_mask);
175c67a470bSPaulo Zanoni 		return;
176c67a470bSPaulo Zanoni 	}
177c67a470bSPaulo Zanoni 
178605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
179f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
180f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
181f52ecbcfSPaulo Zanoni 
182605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
183605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
184605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
185edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
186edbfdb45SPaulo Zanoni 	}
187f52ecbcfSPaulo Zanoni }
188edbfdb45SPaulo Zanoni 
189edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
190edbfdb45SPaulo Zanoni {
191edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
192edbfdb45SPaulo Zanoni }
193edbfdb45SPaulo Zanoni 
194edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
195edbfdb45SPaulo Zanoni {
196edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
197edbfdb45SPaulo Zanoni }
198edbfdb45SPaulo Zanoni 
1998664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2008664281bSPaulo Zanoni {
2018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2028664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2038664281bSPaulo Zanoni 	enum pipe pipe;
2048664281bSPaulo Zanoni 
2054bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2064bc9d430SDaniel Vetter 
2078664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2088664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2098664281bSPaulo Zanoni 
2108664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2118664281bSPaulo Zanoni 			return false;
2128664281bSPaulo Zanoni 	}
2138664281bSPaulo Zanoni 
2148664281bSPaulo Zanoni 	return true;
2158664281bSPaulo Zanoni }
2168664281bSPaulo Zanoni 
2178664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2188664281bSPaulo Zanoni {
2198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2208664281bSPaulo Zanoni 	enum pipe pipe;
2218664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2228664281bSPaulo Zanoni 
223fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
224fee884edSDaniel Vetter 
2258664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2268664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2278664281bSPaulo Zanoni 
2288664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
2298664281bSPaulo Zanoni 			return false;
2308664281bSPaulo Zanoni 	}
2318664281bSPaulo Zanoni 
2328664281bSPaulo Zanoni 	return true;
2338664281bSPaulo Zanoni }
2348664281bSPaulo Zanoni 
2352d9d2b0bSVille Syrjälä static void i9xx_clear_fifo_underrun(struct drm_device *dev, enum pipe pipe)
2362d9d2b0bSVille Syrjälä {
2372d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
2382d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
2392d9d2b0bSVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
2402d9d2b0bSVille Syrjälä 
2412d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
2422d9d2b0bSVille Syrjälä 
2432d9d2b0bSVille Syrjälä 	I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
2442d9d2b0bSVille Syrjälä 	POSTING_READ(reg);
2452d9d2b0bSVille Syrjälä }
2462d9d2b0bSVille Syrjälä 
2478664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2488664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2498664281bSPaulo Zanoni {
2508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2518664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
2528664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
2538664281bSPaulo Zanoni 
2548664281bSPaulo Zanoni 	if (enable)
2558664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
2568664281bSPaulo Zanoni 	else
2578664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
2588664281bSPaulo Zanoni }
2598664281bSPaulo Zanoni 
2608664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
2617336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
2628664281bSPaulo Zanoni {
2638664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2648664281bSPaulo Zanoni 	if (enable) {
2657336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
2667336df65SDaniel Vetter 
2678664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
2688664281bSPaulo Zanoni 			return;
2698664281bSPaulo Zanoni 
2708664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
2718664281bSPaulo Zanoni 	} else {
2727336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
2737336df65SDaniel Vetter 
2747336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
2758664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
2767336df65SDaniel Vetter 
2777336df65SDaniel Vetter 		if (!was_enabled &&
2787336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
2797336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
2807336df65SDaniel Vetter 				      pipe_name(pipe));
2817336df65SDaniel Vetter 		}
2828664281bSPaulo Zanoni 	}
2838664281bSPaulo Zanoni }
2848664281bSPaulo Zanoni 
28538d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
28638d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
28738d83c96SDaniel Vetter {
28838d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
28938d83c96SDaniel Vetter 
29038d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
29138d83c96SDaniel Vetter 
29238d83c96SDaniel Vetter 	if (enable)
29338d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
29438d83c96SDaniel Vetter 	else
29538d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
29638d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
29738d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
29838d83c96SDaniel Vetter }
29938d83c96SDaniel Vetter 
300fee884edSDaniel Vetter /**
301fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
302fee884edSDaniel Vetter  * @dev_priv: driver private
303fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
304fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
305fee884edSDaniel Vetter  */
306fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
307fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
308fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
309fee884edSDaniel Vetter {
310fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
311fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
312fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
313fee884edSDaniel Vetter 
314fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
315fee884edSDaniel Vetter 
316c67a470bSPaulo Zanoni 	if (dev_priv->pc8.irqs_disabled &&
317c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
318c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
319c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr &= ~interrupt_mask;
320c67a470bSPaulo Zanoni 		dev_priv->pc8.regsave.sdeimr |= (~enabled_irq_mask &
321c67a470bSPaulo Zanoni 						 interrupt_mask);
322c67a470bSPaulo Zanoni 		return;
323c67a470bSPaulo Zanoni 	}
324c67a470bSPaulo Zanoni 
325fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
326fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
327fee884edSDaniel Vetter }
328fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
329fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
330fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
331fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
332fee884edSDaniel Vetter 
333de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
334de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3358664281bSPaulo Zanoni 					    bool enable)
3368664281bSPaulo Zanoni {
3378664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
338de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
339de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3408664281bSPaulo Zanoni 
3418664281bSPaulo Zanoni 	if (enable)
342fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3438664281bSPaulo Zanoni 	else
344fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3458664281bSPaulo Zanoni }
3468664281bSPaulo Zanoni 
3478664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3488664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3498664281bSPaulo Zanoni 					    bool enable)
3508664281bSPaulo Zanoni {
3518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3528664281bSPaulo Zanoni 
3538664281bSPaulo Zanoni 	if (enable) {
3541dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
3551dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
3561dd246fbSDaniel Vetter 
3578664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
3588664281bSPaulo Zanoni 			return;
3598664281bSPaulo Zanoni 
360fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3618664281bSPaulo Zanoni 	} else {
3621dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
3631dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
3641dd246fbSDaniel Vetter 
3651dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
366fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
3671dd246fbSDaniel Vetter 
3681dd246fbSDaniel Vetter 		if (!was_enabled &&
3691dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
3701dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
3711dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
3721dd246fbSDaniel Vetter 		}
3738664281bSPaulo Zanoni 	}
3748664281bSPaulo Zanoni }
3758664281bSPaulo Zanoni 
3768664281bSPaulo Zanoni /**
3778664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
3788664281bSPaulo Zanoni  * @dev: drm device
3798664281bSPaulo Zanoni  * @pipe: pipe
3808664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
3818664281bSPaulo Zanoni  *
3828664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
3838664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
3848664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
3858664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
3868664281bSPaulo Zanoni  * bit for all the pipes.
3878664281bSPaulo Zanoni  *
3888664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
3898664281bSPaulo Zanoni  */
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 
398*77961eb9SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
399*77961eb9SImre 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 {
670391f75e2SVille Syrjälä 		enum transcoder cpu_transcoder =
671391f75e2SVille Syrjälä 			intel_pipe_to_cpu_transcoder(dev_priv, pipe);
672391f75e2SVille Syrjälä 		u32 htotal;
673391f75e2SVille Syrjälä 
674391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
675391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
676391f75e2SVille Syrjälä 
677391f75e2SVille Syrjälä 		vbl_start *= htotal;
678391f75e2SVille Syrjälä 	}
679391f75e2SVille Syrjälä 
6809db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6819db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6825eddb70bSChris Wilson 
6830a3e67a4SJesse Barnes 	/*
6840a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6850a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6860a3e67a4SJesse Barnes 	 * register.
6870a3e67a4SJesse Barnes 	 */
6880a3e67a4SJesse Barnes 	do {
6895eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
690391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6915eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6920a3e67a4SJesse Barnes 	} while (high1 != high2);
6930a3e67a4SJesse Barnes 
6945eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
695391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6965eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
697391f75e2SVille Syrjälä 
698391f75e2SVille Syrjälä 	/*
699391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
700391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
701391f75e2SVille Syrjälä 	 * counter against vblank start.
702391f75e2SVille Syrjälä 	 */
703edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7040a3e67a4SJesse Barnes }
7050a3e67a4SJesse Barnes 
706f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7079880b7a5SJesse Barnes {
7089880b7a5SJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
7099db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7109880b7a5SJesse Barnes 
7119880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
71244d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7139db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7149880b7a5SJesse Barnes 		return 0;
7159880b7a5SJesse Barnes 	}
7169880b7a5SJesse Barnes 
7179880b7a5SJesse Barnes 	return I915_READ(reg);
7189880b7a5SJesse Barnes }
7199880b7a5SJesse Barnes 
720ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
721ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
722ad3543edSMario Kleiner #define __raw_i915_read16(dev_priv__, reg__) readw((dev_priv__)->regs + (reg__))
723ad3543edSMario Kleiner 
724095163baSVille Syrjälä static bool ilk_pipe_in_vblank_locked(struct drm_device *dev, enum pipe pipe)
72554ddcbd2SVille Syrjälä {
72654ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
72754ddcbd2SVille Syrjälä 	uint32_t status;
72854ddcbd2SVille Syrjälä 
729095163baSVille Syrjälä 	if (INTEL_INFO(dev)->gen < 7) {
73054ddcbd2SVille Syrjälä 		status = pipe == PIPE_A ?
73154ddcbd2SVille Syrjälä 			DE_PIPEA_VBLANK :
73254ddcbd2SVille Syrjälä 			DE_PIPEB_VBLANK;
73354ddcbd2SVille Syrjälä 	} else {
73454ddcbd2SVille Syrjälä 		switch (pipe) {
73554ddcbd2SVille Syrjälä 		default:
73654ddcbd2SVille Syrjälä 		case PIPE_A:
73754ddcbd2SVille Syrjälä 			status = DE_PIPEA_VBLANK_IVB;
73854ddcbd2SVille Syrjälä 			break;
73954ddcbd2SVille Syrjälä 		case PIPE_B:
74054ddcbd2SVille Syrjälä 			status = DE_PIPEB_VBLANK_IVB;
74154ddcbd2SVille Syrjälä 			break;
74254ddcbd2SVille Syrjälä 		case PIPE_C:
74354ddcbd2SVille Syrjälä 			status = DE_PIPEC_VBLANK_IVB;
74454ddcbd2SVille Syrjälä 			break;
74554ddcbd2SVille Syrjälä 		}
74654ddcbd2SVille Syrjälä 	}
747ad3543edSMario Kleiner 
748095163baSVille Syrjälä 	return __raw_i915_read32(dev_priv, DEISR) & status;
74954ddcbd2SVille Syrjälä }
75054ddcbd2SVille Syrjälä 
751f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
752abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
753abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
7540af7e4dfSMario Kleiner {
755c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
756c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
757c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
758c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
7593aa18df8SVille Syrjälä 	int position;
7600af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
7610af7e4dfSMario Kleiner 	bool in_vbl = true;
7620af7e4dfSMario Kleiner 	int ret = 0;
763ad3543edSMario Kleiner 	unsigned long irqflags;
7640af7e4dfSMario Kleiner 
765c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
7660af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7679db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7680af7e4dfSMario Kleiner 		return 0;
7690af7e4dfSMario Kleiner 	}
7700af7e4dfSMario Kleiner 
771c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
772c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
773c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
774c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7750af7e4dfSMario Kleiner 
776d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
777d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
778d31faf65SVille Syrjälä 		vbl_end /= 2;
779d31faf65SVille Syrjälä 		vtotal /= 2;
780d31faf65SVille Syrjälä 	}
781d31faf65SVille Syrjälä 
782c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
783c2baf4b7SVille Syrjälä 
784ad3543edSMario Kleiner 	/*
785ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
786ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
787ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
788ad3543edSMario Kleiner 	 */
789ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
790ad3543edSMario Kleiner 
791ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
792ad3543edSMario Kleiner 
793ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
794ad3543edSMario Kleiner 	if (stime)
795ad3543edSMario Kleiner 		*stime = ktime_get();
796ad3543edSMario Kleiner 
7977c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7980af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7990af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8000af7e4dfSMario Kleiner 		 */
8017c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
802ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
8037c06b08aSVille Syrjälä 		else
804ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
80554ddcbd2SVille Syrjälä 
806095163baSVille Syrjälä 		if (HAS_PCH_SPLIT(dev)) {
80754ddcbd2SVille Syrjälä 			/*
80854ddcbd2SVille Syrjälä 			 * The scanline counter increments at the leading edge
80954ddcbd2SVille Syrjälä 			 * of hsync, ie. it completely misses the active portion
81054ddcbd2SVille Syrjälä 			 * of the line. Fix up the counter at both edges of vblank
81154ddcbd2SVille Syrjälä 			 * to get a more accurate picture whether we're in vblank
81254ddcbd2SVille Syrjälä 			 * or not.
81354ddcbd2SVille Syrjälä 			 */
814095163baSVille Syrjälä 			in_vbl = ilk_pipe_in_vblank_locked(dev, pipe);
81554ddcbd2SVille Syrjälä 			if ((in_vbl && position == vbl_start - 1) ||
81654ddcbd2SVille Syrjälä 			    (!in_vbl && position == vbl_end - 1))
81754ddcbd2SVille Syrjälä 				position = (position + 1) % vtotal;
8180af7e4dfSMario Kleiner 		} else {
819095163baSVille Syrjälä 			/*
820095163baSVille Syrjälä 			 * ISR vblank status bits don't work the way we'd want
821095163baSVille Syrjälä 			 * them to work on non-PCH platforms (for
822095163baSVille Syrjälä 			 * ilk_pipe_in_vblank_locked()), and there doesn't
823095163baSVille Syrjälä 			 * appear any other way to determine if we're currently
824095163baSVille Syrjälä 			 * in vblank.
825095163baSVille Syrjälä 			 *
826095163baSVille Syrjälä 			 * Instead let's assume that we're already in vblank if
827095163baSVille Syrjälä 			 * we got called from the vblank interrupt and the
828095163baSVille Syrjälä 			 * scanline counter value indicates that we're on the
829095163baSVille Syrjälä 			 * line just prior to vblank start. This should result
830095163baSVille Syrjälä 			 * in the correct answer, unless the vblank interrupt
831095163baSVille Syrjälä 			 * delivery really got delayed for almost exactly one
832095163baSVille Syrjälä 			 * full frame/field.
833095163baSVille Syrjälä 			 */
834095163baSVille Syrjälä 			if (flags & DRM_CALLED_FROM_VBLIRQ &&
835095163baSVille Syrjälä 			    position == vbl_start - 1) {
836095163baSVille Syrjälä 				position = (position + 1) % vtotal;
837095163baSVille Syrjälä 
838095163baSVille Syrjälä 				/* Signal this correction as "applied". */
839095163baSVille Syrjälä 				ret |= 0x8;
840095163baSVille Syrjälä 			}
841095163baSVille Syrjälä 		}
842095163baSVille Syrjälä 	} else {
8430af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8440af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8450af7e4dfSMario Kleiner 		 * scanout position.
8460af7e4dfSMario Kleiner 		 */
847ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8480af7e4dfSMario Kleiner 
8493aa18df8SVille Syrjälä 		/* convert to pixel counts */
8503aa18df8SVille Syrjälä 		vbl_start *= htotal;
8513aa18df8SVille Syrjälä 		vbl_end *= htotal;
8523aa18df8SVille Syrjälä 		vtotal *= htotal;
8533aa18df8SVille Syrjälä 	}
8543aa18df8SVille Syrjälä 
855ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
856ad3543edSMario Kleiner 	if (etime)
857ad3543edSMario Kleiner 		*etime = ktime_get();
858ad3543edSMario Kleiner 
859ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
860ad3543edSMario Kleiner 
861ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
862ad3543edSMario Kleiner 
8633aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8643aa18df8SVille Syrjälä 
8653aa18df8SVille Syrjälä 	/*
8663aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8673aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8683aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8693aa18df8SVille Syrjälä 	 * up since vbl_end.
8703aa18df8SVille Syrjälä 	 */
8713aa18df8SVille Syrjälä 	if (position >= vbl_start)
8723aa18df8SVille Syrjälä 		position -= vbl_end;
8733aa18df8SVille Syrjälä 	else
8743aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8753aa18df8SVille Syrjälä 
8767c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8773aa18df8SVille Syrjälä 		*vpos = position;
8783aa18df8SVille Syrjälä 		*hpos = 0;
8793aa18df8SVille Syrjälä 	} else {
8800af7e4dfSMario Kleiner 		*vpos = position / htotal;
8810af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8820af7e4dfSMario Kleiner 	}
8830af7e4dfSMario Kleiner 
8840af7e4dfSMario Kleiner 	/* In vblank? */
8850af7e4dfSMario Kleiner 	if (in_vbl)
8860af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
8870af7e4dfSMario Kleiner 
8880af7e4dfSMario Kleiner 	return ret;
8890af7e4dfSMario Kleiner }
8900af7e4dfSMario Kleiner 
891f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8920af7e4dfSMario Kleiner 			      int *max_error,
8930af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8940af7e4dfSMario Kleiner 			      unsigned flags)
8950af7e4dfSMario Kleiner {
8964041b853SChris Wilson 	struct drm_crtc *crtc;
8970af7e4dfSMario Kleiner 
8987eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8994041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9000af7e4dfSMario Kleiner 		return -EINVAL;
9010af7e4dfSMario Kleiner 	}
9020af7e4dfSMario Kleiner 
9030af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9044041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9054041b853SChris Wilson 	if (crtc == NULL) {
9064041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9074041b853SChris Wilson 		return -EINVAL;
9084041b853SChris Wilson 	}
9094041b853SChris Wilson 
9104041b853SChris Wilson 	if (!crtc->enabled) {
9114041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9124041b853SChris Wilson 		return -EBUSY;
9134041b853SChris Wilson 	}
9140af7e4dfSMario Kleiner 
9150af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9164041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9174041b853SChris Wilson 						     vblank_time, flags,
9187da903efSVille Syrjälä 						     crtc,
9197da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
9200af7e4dfSMario Kleiner }
9210af7e4dfSMario Kleiner 
92267c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
92367c347ffSJani Nikula 				struct drm_connector *connector)
924321a1b30SEgbert Eich {
925321a1b30SEgbert Eich 	enum drm_connector_status old_status;
926321a1b30SEgbert Eich 
927321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
928321a1b30SEgbert Eich 	old_status = connector->status;
929321a1b30SEgbert Eich 
930321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
93167c347ffSJani Nikula 	if (old_status == connector->status)
93267c347ffSJani Nikula 		return false;
93367c347ffSJani Nikula 
93467c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
935321a1b30SEgbert Eich 		      connector->base.id,
936321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
93767c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
93867c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
93967c347ffSJani Nikula 
94067c347ffSJani Nikula 	return true;
941321a1b30SEgbert Eich }
942321a1b30SEgbert Eich 
9435ca58282SJesse Barnes /*
9445ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
9455ca58282SJesse Barnes  */
946ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
947ac4c16c5SEgbert Eich 
9485ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
9495ca58282SJesse Barnes {
9505ca58282SJesse Barnes 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
9515ca58282SJesse Barnes 						    hotplug_work);
9525ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
953c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
954cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
955cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
956cd569aedSEgbert Eich 	struct drm_connector *connector;
957cd569aedSEgbert Eich 	unsigned long irqflags;
958cd569aedSEgbert Eich 	bool hpd_disabled = false;
959321a1b30SEgbert Eich 	bool changed = false;
960142e2398SEgbert Eich 	u32 hpd_event_bits;
9615ca58282SJesse Barnes 
96252d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
96352d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
96452d7ecedSDaniel Vetter 		return;
96552d7ecedSDaniel Vetter 
966a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
967e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
968e67189abSJesse Barnes 
969cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
970142e2398SEgbert Eich 
971142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
972142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
973cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
974cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
975cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
976cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
977cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
978cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
979cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
980cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
981cd569aedSEgbert Eich 				drm_get_connector_name(connector));
982cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
983cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
984cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
985cd569aedSEgbert Eich 			hpd_disabled = true;
986cd569aedSEgbert Eich 		}
987142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
988142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
989142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
990142e2398SEgbert Eich 		}
991cd569aedSEgbert Eich 	}
992cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
993cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
994cd569aedSEgbert Eich 	  * some connectors */
995ac4c16c5SEgbert Eich 	if (hpd_disabled) {
996cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
997ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
998ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
999ac4c16c5SEgbert Eich 	}
1000cd569aedSEgbert Eich 
1001cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1002cd569aedSEgbert Eich 
1003321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1004321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
1005321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1006321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1007cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1008cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1009321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1010321a1b30SEgbert Eich 				changed = true;
1011321a1b30SEgbert Eich 		}
1012321a1b30SEgbert Eich 	}
101340ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
101440ee3381SKeith Packard 
1015321a1b30SEgbert Eich 	if (changed)
1016321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
10175ca58282SJesse Barnes }
10185ca58282SJesse Barnes 
10193ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
10203ca1ccedSVille Syrjälä {
10213ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
10223ca1ccedSVille Syrjälä }
10233ca1ccedSVille Syrjälä 
1024d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1025f97108d1SJesse Barnes {
1026f97108d1SJesse Barnes 	drm_i915_private_t *dev_priv = dev->dev_private;
1027b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10289270388eSDaniel Vetter 	u8 new_delay;
10299270388eSDaniel Vetter 
1030d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1031f97108d1SJesse Barnes 
103273edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
103373edd18fSDaniel Vetter 
103420e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10359270388eSDaniel Vetter 
10367648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1037b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1038b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1039f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1040f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1041f97108d1SJesse Barnes 
1042f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1043b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
104420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
104520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
104620e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
104720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1048b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
104920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
105020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
105120e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
105220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1053f97108d1SJesse Barnes 	}
1054f97108d1SJesse Barnes 
10557648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
105620e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1057f97108d1SJesse Barnes 
1058d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
10599270388eSDaniel Vetter 
1060f97108d1SJesse Barnes 	return;
1061f97108d1SJesse Barnes }
1062f97108d1SJesse Barnes 
1063549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1064549f7365SChris Wilson 			struct intel_ring_buffer *ring)
1065549f7365SChris Wilson {
1066475553deSChris Wilson 	if (ring->obj == NULL)
1067475553deSChris Wilson 		return;
1068475553deSChris Wilson 
1069814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
10709862e600SChris Wilson 
1071549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
107210cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1073549f7365SChris Wilson }
1074549f7365SChris Wilson 
107576c3552fSDeepak S void gen6_set_pm_mask(struct drm_i915_private *dev_priv,
107627544369SDeepak S 			     u32 pm_iir, int new_delay)
107727544369SDeepak S {
107827544369SDeepak S 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
107927544369SDeepak S 		if (new_delay >= dev_priv->rps.max_delay) {
108027544369SDeepak S 			/* Mask UP THRESHOLD Interrupts */
108127544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
108227544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
108327544369SDeepak S 				   GEN6_PM_RP_UP_THRESHOLD);
108427544369SDeepak S 			dev_priv->rps.rp_up_masked = true;
108527544369SDeepak S 		}
108627544369SDeepak S 		if (dev_priv->rps.rp_down_masked) {
108727544369SDeepak S 			/* UnMask DOWN THRESHOLD Interrupts */
108827544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
108927544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
109027544369SDeepak S 				   ~GEN6_PM_RP_DOWN_THRESHOLD);
109127544369SDeepak S 			dev_priv->rps.rp_down_masked = false;
109227544369SDeepak S 		}
109327544369SDeepak S 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
109427544369SDeepak S 		if (new_delay <= dev_priv->rps.min_delay) {
109527544369SDeepak S 			/* Mask DOWN THRESHOLD Interrupts */
109627544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
109727544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) |
109827544369SDeepak S 				   GEN6_PM_RP_DOWN_THRESHOLD);
109927544369SDeepak S 			dev_priv->rps.rp_down_masked = true;
110027544369SDeepak S 		}
110127544369SDeepak S 
110227544369SDeepak S 		if (dev_priv->rps.rp_up_masked) {
110327544369SDeepak S 			/* UnMask UP THRESHOLD Interrupts */
110427544369SDeepak S 			I915_WRITE(GEN6_PMINTRMSK,
110527544369SDeepak S 				   I915_READ(GEN6_PMINTRMSK) &
110627544369SDeepak S 				   ~GEN6_PM_RP_UP_THRESHOLD);
110727544369SDeepak S 			dev_priv->rps.rp_up_masked = false;
110827544369SDeepak S 		}
110927544369SDeepak S 	}
111027544369SDeepak S }
111127544369SDeepak S 
11124912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11133b8d8d91SJesse Barnes {
11144912d041SBen Widawsky 	drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
1115c6a828d3SDaniel Vetter 						    rps.work);
1116edbfdb45SPaulo Zanoni 	u32 pm_iir;
1117dd75fdc8SChris Wilson 	int new_delay, adj;
11183b8d8d91SJesse Barnes 
111959cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1120c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1121c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
11224848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
1123edbfdb45SPaulo Zanoni 	snb_enable_pm_irq(dev_priv, GEN6_PM_RPS_EVENTS);
112459cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11254912d041SBen Widawsky 
112660611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
112760611c13SPaulo Zanoni 	WARN_ON(pm_iir & ~GEN6_PM_RPS_EVENTS);
112860611c13SPaulo Zanoni 
11294848405cSBen Widawsky 	if ((pm_iir & GEN6_PM_RPS_EVENTS) == 0)
11303b8d8d91SJesse Barnes 		return;
11313b8d8d91SJesse Barnes 
11324fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11337b9e0ae6SChris Wilson 
1134dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11357425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1136dd75fdc8SChris Wilson 		if (adj > 0)
1137dd75fdc8SChris Wilson 			adj *= 2;
1138dd75fdc8SChris Wilson 		else
1139dd75fdc8SChris Wilson 			adj = 1;
1140dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
11417425034aSVille Syrjälä 
11427425034aSVille Syrjälä 		/*
11437425034aSVille Syrjälä 		 * For better performance, jump directly
11447425034aSVille Syrjälä 		 * to RPe if we're below it.
11457425034aSVille Syrjälä 		 */
1146dd75fdc8SChris Wilson 		if (new_delay < dev_priv->rps.rpe_delay)
11477425034aSVille Syrjälä 			new_delay = dev_priv->rps.rpe_delay;
1148dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1149dd75fdc8SChris Wilson 		if (dev_priv->rps.cur_delay > dev_priv->rps.rpe_delay)
1150dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.rpe_delay;
1151dd75fdc8SChris Wilson 		else
1152dd75fdc8SChris Wilson 			new_delay = dev_priv->rps.min_delay;
1153dd75fdc8SChris Wilson 		adj = 0;
1154dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1155dd75fdc8SChris Wilson 		if (adj < 0)
1156dd75fdc8SChris Wilson 			adj *= 2;
1157dd75fdc8SChris Wilson 		else
1158dd75fdc8SChris Wilson 			adj = -1;
1159dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay + adj;
1160dd75fdc8SChris Wilson 	} else { /* unknown event */
1161dd75fdc8SChris Wilson 		new_delay = dev_priv->rps.cur_delay;
1162dd75fdc8SChris Wilson 	}
11633b8d8d91SJesse Barnes 
116479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
116579249636SBen Widawsky 	 * interrupt
116679249636SBen Widawsky 	 */
11671272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
11681272e7b8SVille Syrjälä 			    dev_priv->rps.min_delay, dev_priv->rps.max_delay);
116927544369SDeepak S 
117027544369SDeepak S 	gen6_set_pm_mask(dev_priv, pm_iir, new_delay);
1171dd75fdc8SChris Wilson 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_delay;
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 {
154641a05a3aSDaniel Vetter 	if (pm_iir & GEN6_PM_RPS_EVENTS) {
154759cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
15484848405cSBen Widawsky 		dev_priv->rps.pm_iir |= pm_iir & GEN6_PM_RPS_EVENTS;
15494d3b3d5fSPaulo Zanoni 		snb_disable_pm_irq(dev_priv, pm_iir & GEN6_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;
25336274f212SChris Wilson 	u32 cmd, ipehr, acthd, acthd_min;
2534a24a11e6SChris Wilson 
2535a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2536a24a11e6SChris Wilson 	if ((ipehr & ~(0x3 << 16)) !=
2537a24a11e6SChris Wilson 	    (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE | MI_SEMAPHORE_REGISTER))
25386274f212SChris Wilson 		return NULL;
2539a24a11e6SChris Wilson 
2540a24a11e6SChris Wilson 	/* ACTHD is likely pointing to the dword after the actual command,
2541a24a11e6SChris Wilson 	 * so scan backwards until we find the MBOX.
2542a24a11e6SChris Wilson 	 */
25436274f212SChris Wilson 	acthd = intel_ring_get_active_head(ring) & HEAD_ADDR;
2544a24a11e6SChris Wilson 	acthd_min = max((int)acthd - 3 * 4, 0);
2545a24a11e6SChris Wilson 	do {
2546a24a11e6SChris Wilson 		cmd = ioread32(ring->virtual_start + acthd);
2547a24a11e6SChris Wilson 		if (cmd == ipehr)
2548a24a11e6SChris Wilson 			break;
2549a24a11e6SChris Wilson 
2550a24a11e6SChris Wilson 		acthd -= 4;
2551a24a11e6SChris Wilson 		if (acthd < acthd_min)
25526274f212SChris Wilson 			return NULL;
2553a24a11e6SChris Wilson 	} while (1);
2554a24a11e6SChris Wilson 
25556274f212SChris Wilson 	*seqno = ioread32(ring->virtual_start+acthd+4)+1;
25566274f212SChris Wilson 	return &dev_priv->ring[(ring->id + (((ipehr >> 17) & 1) + 1)) % 3];
2557a24a11e6SChris Wilson }
2558a24a11e6SChris Wilson 
25596274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
25606274f212SChris Wilson {
25616274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
25626274f212SChris Wilson 	struct intel_ring_buffer *signaller;
25636274f212SChris Wilson 	u32 seqno, ctl;
25646274f212SChris Wilson 
25656274f212SChris Wilson 	ring->hangcheck.deadlock = true;
25666274f212SChris Wilson 
25676274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
25686274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
25696274f212SChris Wilson 		return -1;
25706274f212SChris Wilson 
25716274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
25726274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
25736274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
25746274f212SChris Wilson 		return -1;
25756274f212SChris Wilson 
25766274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
25776274f212SChris Wilson }
25786274f212SChris Wilson 
25796274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
25806274f212SChris Wilson {
25816274f212SChris Wilson 	struct intel_ring_buffer *ring;
25826274f212SChris Wilson 	int i;
25836274f212SChris Wilson 
25846274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
25856274f212SChris Wilson 		ring->hangcheck.deadlock = false;
25866274f212SChris Wilson }
25876274f212SChris Wilson 
2588ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2589ad8beaeaSMika Kuoppala ring_stuck(struct intel_ring_buffer *ring, u32 acthd)
25901ec14ad3SChris Wilson {
25911ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
25921ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
25939107e9d2SChris Wilson 	u32 tmp;
25949107e9d2SChris Wilson 
25956274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2596f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
25976274f212SChris Wilson 
25989107e9d2SChris Wilson 	if (IS_GEN2(dev))
2599f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
26009107e9d2SChris Wilson 
26019107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
26029107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
26039107e9d2SChris Wilson 	 * and break the hang. This should work on
26049107e9d2SChris Wilson 	 * all but the second generation chipsets.
26059107e9d2SChris Wilson 	 */
26069107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
26071ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
260858174462SMika Kuoppala 		i915_handle_error(dev, false,
260958174462SMika Kuoppala 				  "Kicking stuck wait on %s",
26101ec14ad3SChris Wilson 				  ring->name);
26111ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2612f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
26131ec14ad3SChris Wilson 	}
2614a24a11e6SChris Wilson 
26156274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
26166274f212SChris Wilson 		switch (semaphore_passed(ring)) {
26176274f212SChris Wilson 		default:
2618f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
26196274f212SChris Wilson 		case 1:
262058174462SMika Kuoppala 			i915_handle_error(dev, false,
262158174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2622a24a11e6SChris Wilson 					  ring->name);
2623a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2624f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
26256274f212SChris Wilson 		case 0:
2626f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
26276274f212SChris Wilson 		}
26289107e9d2SChris Wilson 	}
26299107e9d2SChris Wilson 
2630f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2631a24a11e6SChris Wilson }
2632d1e61e7fSChris Wilson 
2633f65d9421SBen Gamari /**
2634f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
263505407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
263605407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
263705407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
263805407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
263905407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2640f65d9421SBen Gamari  */
2641a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2642f65d9421SBen Gamari {
2643f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
2644f65d9421SBen Gamari 	drm_i915_private_t *dev_priv = dev->dev_private;
2645b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2646b4519513SChris Wilson 	int i;
264705407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
26489107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
26499107e9d2SChris Wilson #define BUSY 1
26509107e9d2SChris Wilson #define KICK 5
26519107e9d2SChris Wilson #define HUNG 20
2652893eead0SChris Wilson 
2653d330a953SJani Nikula 	if (!i915.enable_hangcheck)
26543e0dc6b0SBen Widawsky 		return;
26553e0dc6b0SBen Widawsky 
2656b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
265705407ff8SMika Kuoppala 		u32 seqno, acthd;
26589107e9d2SChris Wilson 		bool busy = true;
2659b4519513SChris Wilson 
26606274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
26616274f212SChris Wilson 
266205407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
266305407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
266405407ff8SMika Kuoppala 
266505407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
26669107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2667da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2668da661464SMika Kuoppala 
26699107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
26709107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2671094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2672f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
26739107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
26749107e9d2SChris Wilson 								  ring->name);
2675f4adcd24SDaniel Vetter 						else
2676f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2677f4adcd24SDaniel Vetter 								 ring->name);
26789107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2679094f9a54SChris Wilson 					}
2680094f9a54SChris Wilson 					/* Safeguard against driver failure */
2681094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
26829107e9d2SChris Wilson 				} else
26839107e9d2SChris Wilson 					busy = false;
268405407ff8SMika Kuoppala 			} else {
26856274f212SChris Wilson 				/* We always increment the hangcheck score
26866274f212SChris Wilson 				 * if the ring is busy and still processing
26876274f212SChris Wilson 				 * the same request, so that no single request
26886274f212SChris Wilson 				 * can run indefinitely (such as a chain of
26896274f212SChris Wilson 				 * batches). The only time we do not increment
26906274f212SChris Wilson 				 * the hangcheck score on this ring, if this
26916274f212SChris Wilson 				 * ring is in a legitimate wait for another
26926274f212SChris Wilson 				 * ring. In that case the waiting ring is a
26936274f212SChris Wilson 				 * victim and we want to be sure we catch the
26946274f212SChris Wilson 				 * right culprit. Then every time we do kick
26956274f212SChris Wilson 				 * the ring, add a small increment to the
26966274f212SChris Wilson 				 * score so that we can catch a batch that is
26976274f212SChris Wilson 				 * being repeatedly kicked and so responsible
26986274f212SChris Wilson 				 * for stalling the machine.
26999107e9d2SChris Wilson 				 */
2700ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2701ad8beaeaSMika Kuoppala 								    acthd);
2702ad8beaeaSMika Kuoppala 
2703ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2704da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2705f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
27066274f212SChris Wilson 					break;
2707f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2708ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
27096274f212SChris Wilson 					break;
2710f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2711ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
27126274f212SChris Wilson 					break;
2713f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2714ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
27156274f212SChris Wilson 					stuck[i] = true;
27166274f212SChris Wilson 					break;
27176274f212SChris Wilson 				}
271805407ff8SMika Kuoppala 			}
27199107e9d2SChris Wilson 		} else {
2720da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2721da661464SMika Kuoppala 
27229107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
27239107e9d2SChris Wilson 			 * attempts across multiple batches.
27249107e9d2SChris Wilson 			 */
27259107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
27269107e9d2SChris Wilson 				ring->hangcheck.score--;
2727cbb465e7SChris Wilson 		}
2728f65d9421SBen Gamari 
272905407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
273005407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
27319107e9d2SChris Wilson 		busy_count += busy;
273205407ff8SMika Kuoppala 	}
273305407ff8SMika Kuoppala 
273405407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2735b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2736b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
273705407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2738a43adf07SChris Wilson 				 ring->name);
2739a43adf07SChris Wilson 			rings_hung++;
274005407ff8SMika Kuoppala 		}
274105407ff8SMika Kuoppala 	}
274205407ff8SMika Kuoppala 
274305407ff8SMika Kuoppala 	if (rings_hung)
274458174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
274505407ff8SMika Kuoppala 
274605407ff8SMika Kuoppala 	if (busy_count)
274705407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
274805407ff8SMika Kuoppala 		 * being added */
274910cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
275010cd45b6SMika Kuoppala }
275110cd45b6SMika Kuoppala 
275210cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
275310cd45b6SMika Kuoppala {
275410cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
2755d330a953SJani Nikula 	if (!i915.enable_hangcheck)
275610cd45b6SMika Kuoppala 		return;
275710cd45b6SMika Kuoppala 
275899584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
275910cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2760f65d9421SBen Gamari }
2761f65d9421SBen Gamari 
276291738a95SPaulo Zanoni static void ibx_irq_preinstall(struct drm_device *dev)
276391738a95SPaulo Zanoni {
276491738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
276591738a95SPaulo Zanoni 
276691738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
276791738a95SPaulo Zanoni 		return;
276891738a95SPaulo Zanoni 
276991738a95SPaulo Zanoni 	/* south display irq */
277091738a95SPaulo Zanoni 	I915_WRITE(SDEIMR, 0xffffffff);
277191738a95SPaulo Zanoni 	/*
277291738a95SPaulo Zanoni 	 * SDEIER is also touched by the interrupt handler to work around missed
277391738a95SPaulo Zanoni 	 * PCH interrupts. Hence we can't update it after the interrupt handler
277491738a95SPaulo Zanoni 	 * is enabled - instead we unconditionally enable all PCH interrupt
277591738a95SPaulo Zanoni 	 * sources here, but then only unmask them as needed with SDEIMR.
277691738a95SPaulo Zanoni 	 */
277791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
277891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
277991738a95SPaulo Zanoni }
278091738a95SPaulo Zanoni 
2781d18ea1b5SDaniel Vetter static void gen5_gt_irq_preinstall(struct drm_device *dev)
2782d18ea1b5SDaniel Vetter {
2783d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2784d18ea1b5SDaniel Vetter 
2785d18ea1b5SDaniel Vetter 	/* and GT */
2786d18ea1b5SDaniel Vetter 	I915_WRITE(GTIMR, 0xffffffff);
2787d18ea1b5SDaniel Vetter 	I915_WRITE(GTIER, 0x0);
2788d18ea1b5SDaniel Vetter 	POSTING_READ(GTIER);
2789d18ea1b5SDaniel Vetter 
2790d18ea1b5SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
2791d18ea1b5SDaniel Vetter 		/* and PM */
2792d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIMR, 0xffffffff);
2793d18ea1b5SDaniel Vetter 		I915_WRITE(GEN6_PMIER, 0x0);
2794d18ea1b5SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
2795d18ea1b5SDaniel Vetter 	}
2796d18ea1b5SDaniel Vetter }
2797d18ea1b5SDaniel Vetter 
2798c0e09200SDave Airlie /* drm_dma.h hooks
2799c0e09200SDave Airlie */
2800f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2801036a4a7dSZhenyu Wang {
2802036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
2803036a4a7dSZhenyu Wang 
2804036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2805bdfcdb63SDaniel Vetter 
2806036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
2807036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
28083143a2bfSChris Wilson 	POSTING_READ(DEIER);
2809036a4a7dSZhenyu Wang 
2810d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
2811c650156aSZhenyu Wang 
281291738a95SPaulo Zanoni 	ibx_irq_preinstall(dev);
28137d99163dSBen Widawsky }
28147d99163dSBen Widawsky 
28157e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
28167e231dbeSJesse Barnes {
28177e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
28187e231dbeSJesse Barnes 	int pipe;
28197e231dbeSJesse Barnes 
28207e231dbeSJesse Barnes 	/* VLV magic */
28217e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
28227e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
28237e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
28247e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
28257e231dbeSJesse Barnes 
28267e231dbeSJesse Barnes 	/* and GT */
28277e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
28287e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2829d18ea1b5SDaniel Vetter 
2830d18ea1b5SDaniel Vetter 	gen5_gt_irq_preinstall(dev);
28317e231dbeSJesse Barnes 
28327e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
28337e231dbeSJesse Barnes 
28347e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
28357e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
28367e231dbeSJesse Barnes 	for_each_pipe(pipe)
28377e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
28387e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
28397e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
28407e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
28417e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
28427e231dbeSJesse Barnes }
28437e231dbeSJesse Barnes 
2844abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2845abd58f01SBen Widawsky {
2846abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2847abd58f01SBen Widawsky 	int pipe;
2848abd58f01SBen Widawsky 
2849abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2850abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2851abd58f01SBen Widawsky 
2852abd58f01SBen Widawsky 	/* IIR can theoretically queue up two events. Be paranoid */
2853abd58f01SBen Widawsky #define GEN8_IRQ_INIT_NDX(type, which) do { \
2854abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
2855abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR(which)); \
2856abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
2857abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2858abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR(which)); \
2859abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
2860abd58f01SBen Widawsky 	} while (0)
2861abd58f01SBen Widawsky 
2862abd58f01SBen Widawsky #define GEN8_IRQ_INIT(type) do { \
2863abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
2864abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IMR); \
2865abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
2866abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2867abd58f01SBen Widawsky 		POSTING_READ(GEN8_##type##_IIR); \
2868abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
2869abd58f01SBen Widawsky 	} while (0)
2870abd58f01SBen Widawsky 
2871abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 0);
2872abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 1);
2873abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 2);
2874abd58f01SBen Widawsky 	GEN8_IRQ_INIT_NDX(GT, 3);
2875abd58f01SBen Widawsky 
2876abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2877abd58f01SBen Widawsky 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe);
2878abd58f01SBen Widawsky 	}
2879abd58f01SBen Widawsky 
2880abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_PORT);
2881abd58f01SBen Widawsky 	GEN8_IRQ_INIT(DE_MISC);
2882abd58f01SBen Widawsky 	GEN8_IRQ_INIT(PCU);
2883abd58f01SBen Widawsky #undef GEN8_IRQ_INIT
2884abd58f01SBen Widawsky #undef GEN8_IRQ_INIT_NDX
2885abd58f01SBen Widawsky 
2886abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
288709f2344dSJesse Barnes 
288809f2344dSJesse Barnes 	ibx_irq_preinstall(dev);
2889abd58f01SBen Widawsky }
2890abd58f01SBen Widawsky 
289182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
289282a28bcfSDaniel Vetter {
289382a28bcfSDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
289482a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
289582a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2896fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
289782a28bcfSDaniel Vetter 
289882a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2899fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
290082a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2901cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2902fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
290382a28bcfSDaniel Vetter 	} else {
2904fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
290582a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2906cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2907fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
290882a28bcfSDaniel Vetter 	}
290982a28bcfSDaniel Vetter 
2910fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
291182a28bcfSDaniel Vetter 
29127fe0b973SKeith Packard 	/*
29137fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
29147fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
29157fe0b973SKeith Packard 	 *
29167fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
29177fe0b973SKeith Packard 	 */
29187fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
29197fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
29207fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
29217fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
29227fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
29237fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
29247fe0b973SKeith Packard }
29257fe0b973SKeith Packard 
2926d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
2927d46da437SPaulo Zanoni {
2928d46da437SPaulo Zanoni 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
292982a28bcfSDaniel Vetter 	u32 mask;
2930d46da437SPaulo Zanoni 
2931692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
2932692a04cfSDaniel Vetter 		return;
2933692a04cfSDaniel Vetter 
29348664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev)) {
29358664281bSPaulo Zanoni 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_TRANSB_FIFO_UNDER |
2936de032bf4SPaulo Zanoni 		       SDE_TRANSA_FIFO_UNDER | SDE_POISON;
29378664281bSPaulo Zanoni 	} else {
29388664281bSPaulo Zanoni 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT | SDE_ERROR_CPT;
29398664281bSPaulo Zanoni 
29408664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
29418664281bSPaulo Zanoni 	}
2942ab5c608bSBen Widawsky 
2943d46da437SPaulo Zanoni 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
2944d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
2945d46da437SPaulo Zanoni }
2946d46da437SPaulo Zanoni 
29470a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
29480a9a8c91SDaniel Vetter {
29490a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
29500a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
29510a9a8c91SDaniel Vetter 
29520a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
29530a9a8c91SDaniel Vetter 
29540a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
2955040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
29560a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
295735a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
295835a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
29590a9a8c91SDaniel Vetter 	}
29600a9a8c91SDaniel Vetter 
29610a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
29620a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
29630a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
29640a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
29650a9a8c91SDaniel Vetter 	} else {
29660a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
29670a9a8c91SDaniel Vetter 	}
29680a9a8c91SDaniel Vetter 
29690a9a8c91SDaniel Vetter 	I915_WRITE(GTIIR, I915_READ(GTIIR));
29700a9a8c91SDaniel Vetter 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
29710a9a8c91SDaniel Vetter 	I915_WRITE(GTIER, gt_irqs);
29720a9a8c91SDaniel Vetter 	POSTING_READ(GTIER);
29730a9a8c91SDaniel Vetter 
29740a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
29750a9a8c91SDaniel Vetter 		pm_irqs |= GEN6_PM_RPS_EVENTS;
29760a9a8c91SDaniel Vetter 
29770a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
29780a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
29790a9a8c91SDaniel Vetter 
2980605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
29810a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
2982605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
29830a9a8c91SDaniel Vetter 		I915_WRITE(GEN6_PMIER, pm_irqs);
29840a9a8c91SDaniel Vetter 		POSTING_READ(GEN6_PMIER);
29850a9a8c91SDaniel Vetter 	}
29860a9a8c91SDaniel Vetter }
29870a9a8c91SDaniel Vetter 
2988f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
2989036a4a7dSZhenyu Wang {
29904bc9d430SDaniel Vetter 	unsigned long irqflags;
2991036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
29928e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
29938e76f8dcSPaulo Zanoni 
29948e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
29958e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
29968e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
29978e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
29988e76f8dcSPaulo Zanoni 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB |
29998e76f8dcSPaulo Zanoni 				DE_ERR_INT_IVB);
30008e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
30018e76f8dcSPaulo Zanoni 			      DE_PIPEA_VBLANK_IVB);
30028e76f8dcSPaulo Zanoni 
30038e76f8dcSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
30048e76f8dcSPaulo Zanoni 	} else {
30058e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3006ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
30075b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
30085b3a856bSDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
30095b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
30105b3a856bSDaniel Vetter 				DE_POISON);
30118e76f8dcSPaulo Zanoni 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT;
30128e76f8dcSPaulo Zanoni 	}
3013036a4a7dSZhenyu Wang 
30141ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3015036a4a7dSZhenyu Wang 
3016036a4a7dSZhenyu Wang 	/* should always can generate irq */
3017036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
30181ec14ad3SChris Wilson 	I915_WRITE(DEIMR, dev_priv->irq_mask);
30198e76f8dcSPaulo Zanoni 	I915_WRITE(DEIER, display_mask | extra_mask);
30203143a2bfSChris Wilson 	POSTING_READ(DEIER);
3021036a4a7dSZhenyu Wang 
30220a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3023036a4a7dSZhenyu Wang 
3024d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
30257fe0b973SKeith Packard 
3026f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
30276005ce42SDaniel Vetter 		/* Enable PCU event interrupts
30286005ce42SDaniel Vetter 		 *
30296005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
30304bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
30314bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
30324bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3033f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
30344bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3035f97108d1SJesse Barnes 	}
3036f97108d1SJesse Barnes 
3037036a4a7dSZhenyu Wang 	return 0;
3038036a4a7dSZhenyu Wang }
3039036a4a7dSZhenyu Wang 
3040f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3041f8b79e58SImre Deak {
3042f8b79e58SImre Deak 	u32 pipestat_mask;
3043f8b79e58SImre Deak 	u32 iir_mask;
3044f8b79e58SImre Deak 
3045f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3046f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3047f8b79e58SImre Deak 
3048f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3049f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3050f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3051f8b79e58SImre Deak 
3052f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3053f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3054f8b79e58SImre Deak 
3055f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3056f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3057f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3058f8b79e58SImre Deak 
3059f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3060f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3061f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3062f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3063f8b79e58SImre Deak 
3064f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3065f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3066f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3067f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3068f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3069f8b79e58SImre Deak }
3070f8b79e58SImre Deak 
3071f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3072f8b79e58SImre Deak {
3073f8b79e58SImre Deak 	u32 pipestat_mask;
3074f8b79e58SImre Deak 	u32 iir_mask;
3075f8b79e58SImre Deak 
3076f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3077f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3078f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
3079f8b79e58SImre Deak 
3080f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3081f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3082f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3083f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3084f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3085f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3086f8b79e58SImre Deak 
3087f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3088f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3089f8b79e58SImre Deak 
3090f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3091f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3092f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3093f8b79e58SImre Deak 
3094f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3095f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3096f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3097f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3098f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3099f8b79e58SImre Deak }
3100f8b79e58SImre Deak 
3101f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3102f8b79e58SImre Deak {
3103f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3104f8b79e58SImre Deak 
3105f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3106f8b79e58SImre Deak 		return;
3107f8b79e58SImre Deak 
3108f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3109f8b79e58SImre Deak 
3110f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3111f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3112f8b79e58SImre Deak }
3113f8b79e58SImre Deak 
3114f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3115f8b79e58SImre Deak {
3116f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3117f8b79e58SImre Deak 
3118f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3119f8b79e58SImre Deak 		return;
3120f8b79e58SImre Deak 
3121f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3122f8b79e58SImre Deak 
3123f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3124f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3125f8b79e58SImre Deak }
3126f8b79e58SImre Deak 
31277e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
31287e231dbeSJesse Barnes {
31297e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3130b79480baSDaniel Vetter 	unsigned long irqflags;
31317e231dbeSJesse Barnes 
3132f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
31337e231dbeSJesse Barnes 
313420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
313520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
313620afbda2SDaniel Vetter 
31377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3138f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
31397e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31407e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31417e231dbeSJesse Barnes 
3142b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3143b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3144b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3145f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3146f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3147b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
314831acc7f5SJesse Barnes 
31497e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31507e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31517e231dbeSJesse Barnes 
31520a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
31537e231dbeSJesse Barnes 
31547e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
31557e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
31567e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31577e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
31587e231dbeSJesse Barnes #endif
31597e231dbeSJesse Barnes 
31607e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
316120afbda2SDaniel Vetter 
316220afbda2SDaniel Vetter 	return 0;
316320afbda2SDaniel Vetter }
316420afbda2SDaniel Vetter 
3165abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3166abd58f01SBen Widawsky {
3167abd58f01SBen Widawsky 	int i;
3168abd58f01SBen Widawsky 
3169abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3170abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3171abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3172abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3173abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3174abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3175abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3176abd58f01SBen Widawsky 		0,
3177abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3178abd58f01SBen Widawsky 		};
3179abd58f01SBen Widawsky 
3180abd58f01SBen Widawsky 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++) {
3181abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_GT_IIR(i));
3182abd58f01SBen Widawsky 		if (tmp)
3183abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3184abd58f01SBen Widawsky 				  i, tmp);
3185abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IMR(i), ~gt_interrupts[i]);
3186abd58f01SBen Widawsky 		I915_WRITE(GEN8_GT_IER(i), gt_interrupts[i]);
3187abd58f01SBen Widawsky 	}
3188abd58f01SBen Widawsky 	POSTING_READ(GEN8_GT_IER(0));
3189abd58f01SBen Widawsky }
3190abd58f01SBen Widawsky 
3191abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3192abd58f01SBen Widawsky {
3193abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
319413b3a0a7SDaniel Vetter 	uint32_t de_pipe_masked = GEN8_PIPE_FLIP_DONE |
31950fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
319638d83c96SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN |
319730100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
319813b3a0a7SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK;
3199abd58f01SBen Widawsky 	int pipe;
320013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
320113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
320213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3203abd58f01SBen Widawsky 
3204abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3205abd58f01SBen Widawsky 		u32 tmp = I915_READ(GEN8_DE_PIPE_IIR(pipe));
3206abd58f01SBen Widawsky 		if (tmp)
3207abd58f01SBen Widawsky 			DRM_ERROR("Interrupt (%d) should have been masked in pre-install 0x%08x\n",
3208abd58f01SBen Widawsky 				  pipe, tmp);
3209abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
3210abd58f01SBen Widawsky 		I915_WRITE(GEN8_DE_PIPE_IER(pipe), de_pipe_enables);
3211abd58f01SBen Widawsky 	}
3212abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_ISR(0));
3213abd58f01SBen Widawsky 
32146d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IMR, ~GEN8_AUX_CHANNEL_A);
32156d766f02SDaniel Vetter 	I915_WRITE(GEN8_DE_PORT_IER, GEN8_AUX_CHANNEL_A);
3216abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PORT_IER);
3217abd58f01SBen Widawsky }
3218abd58f01SBen Widawsky 
3219abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3220abd58f01SBen Widawsky {
3221abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3222abd58f01SBen Widawsky 
3223abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3224abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3225abd58f01SBen Widawsky 
3226abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3227abd58f01SBen Widawsky 
3228abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3229abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3230abd58f01SBen Widawsky 
3231abd58f01SBen Widawsky 	return 0;
3232abd58f01SBen Widawsky }
3233abd58f01SBen Widawsky 
3234abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3235abd58f01SBen Widawsky {
3236abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3237abd58f01SBen Widawsky 	int pipe;
3238abd58f01SBen Widawsky 
3239abd58f01SBen Widawsky 	if (!dev_priv)
3240abd58f01SBen Widawsky 		return;
3241abd58f01SBen Widawsky 
3242abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3243abd58f01SBen Widawsky 
3244abd58f01SBen Widawsky #define GEN8_IRQ_FINI_NDX(type, which) do { \
3245abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
3246abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER(which), 0); \
3247abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
3248abd58f01SBen Widawsky 	} while (0)
3249abd58f01SBen Widawsky 
3250abd58f01SBen Widawsky #define GEN8_IRQ_FINI(type) do { \
3251abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IMR, 0xffffffff); \
3252abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IER, 0); \
3253abd58f01SBen Widawsky 		I915_WRITE(GEN8_##type##_IIR, 0xffffffff); \
3254abd58f01SBen Widawsky 	} while (0)
3255abd58f01SBen Widawsky 
3256abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 0);
3257abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 1);
3258abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 2);
3259abd58f01SBen Widawsky 	GEN8_IRQ_FINI_NDX(GT, 3);
3260abd58f01SBen Widawsky 
3261abd58f01SBen Widawsky 	for_each_pipe(pipe) {
3262abd58f01SBen Widawsky 		GEN8_IRQ_FINI_NDX(DE_PIPE, pipe);
3263abd58f01SBen Widawsky 	}
3264abd58f01SBen Widawsky 
3265abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_PORT);
3266abd58f01SBen Widawsky 	GEN8_IRQ_FINI(DE_MISC);
3267abd58f01SBen Widawsky 	GEN8_IRQ_FINI(PCU);
3268abd58f01SBen Widawsky #undef GEN8_IRQ_FINI
3269abd58f01SBen Widawsky #undef GEN8_IRQ_FINI_NDX
3270abd58f01SBen Widawsky 
3271abd58f01SBen Widawsky 	POSTING_READ(GEN8_PCU_IIR);
3272abd58f01SBen Widawsky }
3273abd58f01SBen Widawsky 
32747e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
32757e231dbeSJesse Barnes {
32767e231dbeSJesse Barnes 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3277f8b79e58SImre Deak 	unsigned long irqflags;
32787e231dbeSJesse Barnes 	int pipe;
32797e231dbeSJesse Barnes 
32807e231dbeSJesse Barnes 	if (!dev_priv)
32817e231dbeSJesse Barnes 		return;
32827e231dbeSJesse Barnes 
32833ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3284ac4c16c5SEgbert Eich 
32857e231dbeSJesse Barnes 	for_each_pipe(pipe)
32867e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
32877e231dbeSJesse Barnes 
32887e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
32897e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
32907e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3291f8b79e58SImre Deak 
3292f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3293f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3294f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3295f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3296f8b79e58SImre Deak 
3297f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3298f8b79e58SImre Deak 
32997e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
33007e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
33017e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
33027e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
33037e231dbeSJesse Barnes }
33047e231dbeSJesse Barnes 
3305f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3306036a4a7dSZhenyu Wang {
3307036a4a7dSZhenyu Wang 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
33084697995bSJesse Barnes 
33094697995bSJesse Barnes 	if (!dev_priv)
33104697995bSJesse Barnes 		return;
33114697995bSJesse Barnes 
33123ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3313ac4c16c5SEgbert Eich 
3314036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
3315036a4a7dSZhenyu Wang 
3316036a4a7dSZhenyu Wang 	I915_WRITE(DEIMR, 0xffffffff);
3317036a4a7dSZhenyu Wang 	I915_WRITE(DEIER, 0x0);
3318036a4a7dSZhenyu Wang 	I915_WRITE(DEIIR, I915_READ(DEIIR));
33198664281bSPaulo Zanoni 	if (IS_GEN7(dev))
33208664281bSPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, I915_READ(GEN7_ERR_INT));
3321036a4a7dSZhenyu Wang 
3322036a4a7dSZhenyu Wang 	I915_WRITE(GTIMR, 0xffffffff);
3323036a4a7dSZhenyu Wang 	I915_WRITE(GTIER, 0x0);
3324036a4a7dSZhenyu Wang 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3325192aac1fSKeith Packard 
3326ab5c608bSBen Widawsky 	if (HAS_PCH_NOP(dev))
3327ab5c608bSBen Widawsky 		return;
3328ab5c608bSBen Widawsky 
3329192aac1fSKeith Packard 	I915_WRITE(SDEIMR, 0xffffffff);
3330192aac1fSKeith Packard 	I915_WRITE(SDEIER, 0x0);
3331192aac1fSKeith Packard 	I915_WRITE(SDEIIR, I915_READ(SDEIIR));
33328664281bSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
33338664281bSPaulo Zanoni 		I915_WRITE(SERR_INT, I915_READ(SERR_INT));
3334036a4a7dSZhenyu Wang }
3335036a4a7dSZhenyu Wang 
3336c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3337c2798b19SChris Wilson {
3338c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3339c2798b19SChris Wilson 	int pipe;
3340c2798b19SChris Wilson 
3341c2798b19SChris Wilson 	for_each_pipe(pipe)
3342c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3343c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3344c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3345c2798b19SChris Wilson 	POSTING_READ16(IER);
3346c2798b19SChris Wilson }
3347c2798b19SChris Wilson 
3348c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3349c2798b19SChris Wilson {
3350c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3351379ef82dSDaniel Vetter 	unsigned long irqflags;
3352c2798b19SChris Wilson 
3353c2798b19SChris Wilson 	I915_WRITE16(EMR,
3354c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3355c2798b19SChris Wilson 
3356c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3357c2798b19SChris Wilson 	dev_priv->irq_mask =
3358c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3359c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3360c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3361c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3362c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3363c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3364c2798b19SChris Wilson 
3365c2798b19SChris Wilson 	I915_WRITE16(IER,
3366c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3367c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3368c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3369c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3370c2798b19SChris Wilson 	POSTING_READ16(IER);
3371c2798b19SChris Wilson 
3372379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3373379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3374379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3375755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3376755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3377379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3378379ef82dSDaniel Vetter 
3379c2798b19SChris Wilson 	return 0;
3380c2798b19SChris Wilson }
3381c2798b19SChris Wilson 
338290a72f87SVille Syrjälä /*
338390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
338490a72f87SVille Syrjälä  */
338590a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
33861f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
338790a72f87SVille Syrjälä {
338890a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
33891f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
339090a72f87SVille Syrjälä 
339190a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
339290a72f87SVille Syrjälä 		return false;
339390a72f87SVille Syrjälä 
339490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
339590a72f87SVille Syrjälä 		return false;
339690a72f87SVille Syrjälä 
33971f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
339890a72f87SVille Syrjälä 
339990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
340090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
340190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
340290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
340390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
340490a72f87SVille Syrjälä 	 */
340590a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
340690a72f87SVille Syrjälä 		return false;
340790a72f87SVille Syrjälä 
340890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
340990a72f87SVille Syrjälä 
341090a72f87SVille Syrjälä 	return true;
341190a72f87SVille Syrjälä }
341290a72f87SVille Syrjälä 
3413ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3414c2798b19SChris Wilson {
3415c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3416c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3417c2798b19SChris Wilson 	u16 iir, new_iir;
3418c2798b19SChris Wilson 	u32 pipe_stats[2];
3419c2798b19SChris Wilson 	unsigned long irqflags;
3420c2798b19SChris Wilson 	int pipe;
3421c2798b19SChris Wilson 	u16 flip_mask =
3422c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3423c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3424c2798b19SChris Wilson 
3425c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3426c2798b19SChris Wilson 	if (iir == 0)
3427c2798b19SChris Wilson 		return IRQ_NONE;
3428c2798b19SChris Wilson 
3429c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3430c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3431c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3432c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3433c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3434c2798b19SChris Wilson 		 */
3435c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3436c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
343758174462SMika Kuoppala 			i915_handle_error(dev, false,
343858174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
343958174462SMika Kuoppala 					  iir);
3440c2798b19SChris Wilson 
3441c2798b19SChris Wilson 		for_each_pipe(pipe) {
3442c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3443c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3444c2798b19SChris Wilson 
3445c2798b19SChris Wilson 			/*
3446c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3447c2798b19SChris Wilson 			 */
34482d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3449c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3450c2798b19SChris Wilson 		}
3451c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3452c2798b19SChris Wilson 
3453c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3454c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3455c2798b19SChris Wilson 
3456d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3457c2798b19SChris Wilson 
3458c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3459c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3460c2798b19SChris Wilson 
34614356d586SDaniel Vetter 		for_each_pipe(pipe) {
34621f1c2e24SVille Syrjälä 			int plane = pipe;
34633a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
34641f1c2e24SVille Syrjälä 				plane = !plane;
34651f1c2e24SVille Syrjälä 
34664356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
34671f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
34681f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3469c2798b19SChris Wilson 
34704356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3471277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
34722d9d2b0bSVille Syrjälä 
34732d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
34742d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3475fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
34764356d586SDaniel Vetter 		}
3477c2798b19SChris Wilson 
3478c2798b19SChris Wilson 		iir = new_iir;
3479c2798b19SChris Wilson 	}
3480c2798b19SChris Wilson 
3481c2798b19SChris Wilson 	return IRQ_HANDLED;
3482c2798b19SChris Wilson }
3483c2798b19SChris Wilson 
3484c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3485c2798b19SChris Wilson {
3486c2798b19SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3487c2798b19SChris Wilson 	int pipe;
3488c2798b19SChris Wilson 
3489c2798b19SChris Wilson 	for_each_pipe(pipe) {
3490c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3491c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3492c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3493c2798b19SChris Wilson 	}
3494c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3495c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3496c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3497c2798b19SChris Wilson }
3498c2798b19SChris Wilson 
3499a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3500a266c7d5SChris Wilson {
3501a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3502a266c7d5SChris Wilson 	int pipe;
3503a266c7d5SChris Wilson 
3504a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3505a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3506a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3507a266c7d5SChris Wilson 	}
3508a266c7d5SChris Wilson 
350900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3510a266c7d5SChris Wilson 	for_each_pipe(pipe)
3511a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3512a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3513a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3514a266c7d5SChris Wilson 	POSTING_READ(IER);
3515a266c7d5SChris Wilson }
3516a266c7d5SChris Wilson 
3517a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3518a266c7d5SChris Wilson {
3519a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
352038bde180SChris Wilson 	u32 enable_mask;
3521379ef82dSDaniel Vetter 	unsigned long irqflags;
3522a266c7d5SChris Wilson 
352338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
352438bde180SChris Wilson 
352538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
352638bde180SChris Wilson 	dev_priv->irq_mask =
352738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
352838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
352938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
353038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
353138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
353238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
353338bde180SChris Wilson 
353438bde180SChris Wilson 	enable_mask =
353538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
353638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
353738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
353838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
353938bde180SChris Wilson 		I915_USER_INTERRUPT;
354038bde180SChris Wilson 
3541a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
354220afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
354320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
354420afbda2SDaniel Vetter 
3545a266c7d5SChris Wilson 		/* Enable in IER... */
3546a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3547a266c7d5SChris Wilson 		/* and unmask in IMR */
3548a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3549a266c7d5SChris Wilson 	}
3550a266c7d5SChris Wilson 
3551a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3552a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3553a266c7d5SChris Wilson 	POSTING_READ(IER);
3554a266c7d5SChris Wilson 
3555f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
355620afbda2SDaniel Vetter 
3557379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3558379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3559379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3560755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3561755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3562379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3563379ef82dSDaniel Vetter 
356420afbda2SDaniel Vetter 	return 0;
356520afbda2SDaniel Vetter }
356620afbda2SDaniel Vetter 
356790a72f87SVille Syrjälä /*
356890a72f87SVille Syrjälä  * Returns true when a page flip has completed.
356990a72f87SVille Syrjälä  */
357090a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
357190a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
357290a72f87SVille Syrjälä {
357390a72f87SVille Syrjälä 	drm_i915_private_t *dev_priv = dev->dev_private;
357490a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
357590a72f87SVille Syrjälä 
357690a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
357790a72f87SVille Syrjälä 		return false;
357890a72f87SVille Syrjälä 
357990a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
358090a72f87SVille Syrjälä 		return false;
358190a72f87SVille Syrjälä 
358290a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
358390a72f87SVille Syrjälä 
358490a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
358590a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
358690a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
358790a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
358890a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
358990a72f87SVille Syrjälä 	 */
359090a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
359190a72f87SVille Syrjälä 		return false;
359290a72f87SVille Syrjälä 
359390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
359490a72f87SVille Syrjälä 
359590a72f87SVille Syrjälä 	return true;
359690a72f87SVille Syrjälä }
359790a72f87SVille Syrjälä 
3598ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3599a266c7d5SChris Wilson {
3600a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3601a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
36028291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3603a266c7d5SChris Wilson 	unsigned long irqflags;
360438bde180SChris Wilson 	u32 flip_mask =
360538bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
360638bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
360738bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3608a266c7d5SChris Wilson 
3609a266c7d5SChris Wilson 	iir = I915_READ(IIR);
361038bde180SChris Wilson 	do {
361138bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
36128291ee90SChris Wilson 		bool blc_event = false;
3613a266c7d5SChris Wilson 
3614a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3615a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3616a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3617a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3618a266c7d5SChris Wilson 		 */
3619a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3620a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
362158174462SMika Kuoppala 			i915_handle_error(dev, false,
362258174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
362358174462SMika Kuoppala 					  iir);
3624a266c7d5SChris Wilson 
3625a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3626a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3627a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3628a266c7d5SChris Wilson 
362938bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3630a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3631a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
363238bde180SChris Wilson 				irq_received = true;
3633a266c7d5SChris Wilson 			}
3634a266c7d5SChris Wilson 		}
3635a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3636a266c7d5SChris Wilson 
3637a266c7d5SChris Wilson 		if (!irq_received)
3638a266c7d5SChris Wilson 			break;
3639a266c7d5SChris Wilson 
3640a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3641a266c7d5SChris Wilson 		if ((I915_HAS_HOTPLUG(dev)) &&
3642a266c7d5SChris Wilson 		    (iir & I915_DISPLAY_PORT_INTERRUPT)) {
3643a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3644b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
3645a266c7d5SChris Wilson 
364610a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
364791d131d2SDaniel Vetter 
3648a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
364938bde180SChris Wilson 			POSTING_READ(PORT_HOTPLUG_STAT);
3650a266c7d5SChris Wilson 		}
3651a266c7d5SChris Wilson 
365238bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3653a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3654a266c7d5SChris Wilson 
3655a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3656a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3657a266c7d5SChris Wilson 
3658a266c7d5SChris Wilson 		for_each_pipe(pipe) {
365938bde180SChris Wilson 			int plane = pipe;
36603a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
366138bde180SChris Wilson 				plane = !plane;
36625e2032d4SVille Syrjälä 
366390a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
366490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
366590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3666a266c7d5SChris Wilson 
3667a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3668a266c7d5SChris Wilson 				blc_event = true;
36694356d586SDaniel Vetter 
36704356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3671277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
36722d9d2b0bSVille Syrjälä 
36732d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
36742d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3675fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
3676a266c7d5SChris Wilson 		}
3677a266c7d5SChris Wilson 
3678a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3679a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3680a266c7d5SChris Wilson 
3681a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3682a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3683a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3684a266c7d5SChris Wilson 		 * we would never get another interrupt.
3685a266c7d5SChris Wilson 		 *
3686a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3687a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3688a266c7d5SChris Wilson 		 * another one.
3689a266c7d5SChris Wilson 		 *
3690a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3691a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3692a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3693a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3694a266c7d5SChris Wilson 		 * stray interrupts.
3695a266c7d5SChris Wilson 		 */
369638bde180SChris Wilson 		ret = IRQ_HANDLED;
3697a266c7d5SChris Wilson 		iir = new_iir;
369838bde180SChris Wilson 	} while (iir & ~flip_mask);
3699a266c7d5SChris Wilson 
3700d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
37018291ee90SChris Wilson 
3702a266c7d5SChris Wilson 	return ret;
3703a266c7d5SChris Wilson }
3704a266c7d5SChris Wilson 
3705a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3706a266c7d5SChris Wilson {
3707a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3708a266c7d5SChris Wilson 	int pipe;
3709a266c7d5SChris Wilson 
37103ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3711ac4c16c5SEgbert Eich 
3712a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3713a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3714a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3715a266c7d5SChris Wilson 	}
3716a266c7d5SChris Wilson 
371700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
371855b39755SChris Wilson 	for_each_pipe(pipe) {
371955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3720a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
372155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
372255b39755SChris Wilson 	}
3723a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3724a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3725a266c7d5SChris Wilson 
3726a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3727a266c7d5SChris Wilson }
3728a266c7d5SChris Wilson 
3729a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3730a266c7d5SChris Wilson {
3731a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3732a266c7d5SChris Wilson 	int pipe;
3733a266c7d5SChris Wilson 
3734a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3735a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3736a266c7d5SChris Wilson 
3737a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3738a266c7d5SChris Wilson 	for_each_pipe(pipe)
3739a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3740a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3741a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3742a266c7d5SChris Wilson 	POSTING_READ(IER);
3743a266c7d5SChris Wilson }
3744a266c7d5SChris Wilson 
3745a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3746a266c7d5SChris Wilson {
3747a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3748bbba0a97SChris Wilson 	u32 enable_mask;
3749a266c7d5SChris Wilson 	u32 error_mask;
3750b79480baSDaniel Vetter 	unsigned long irqflags;
3751a266c7d5SChris Wilson 
3752a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3753bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3754adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3755bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3756bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3757bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3758bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3759bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3760bbba0a97SChris Wilson 
3761bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
376221ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
376321ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3764bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3765bbba0a97SChris Wilson 
3766bbba0a97SChris Wilson 	if (IS_G4X(dev))
3767bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3768a266c7d5SChris Wilson 
3769b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3770b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3771b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3772755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3773755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3774755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3775b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3776a266c7d5SChris Wilson 
3777a266c7d5SChris Wilson 	/*
3778a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3779a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3780a266c7d5SChris Wilson 	 */
3781a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3782a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3783a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3784a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3785a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3786a266c7d5SChris Wilson 	} else {
3787a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3788a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3789a266c7d5SChris Wilson 	}
3790a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3791a266c7d5SChris Wilson 
3792a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3793a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3794a266c7d5SChris Wilson 	POSTING_READ(IER);
3795a266c7d5SChris Wilson 
379620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
379720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
379820afbda2SDaniel Vetter 
3799f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
380020afbda2SDaniel Vetter 
380120afbda2SDaniel Vetter 	return 0;
380220afbda2SDaniel Vetter }
380320afbda2SDaniel Vetter 
3804bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
380520afbda2SDaniel Vetter {
380620afbda2SDaniel Vetter 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3807e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3808cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
380920afbda2SDaniel Vetter 	u32 hotplug_en;
381020afbda2SDaniel Vetter 
3811b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3812b5ea2d56SDaniel Vetter 
3813bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3814bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3815bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3816adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3817e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3818cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3819cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3820cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3821a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3822a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3823a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3824a266c7d5SChris Wilson 		*/
3825a266c7d5SChris Wilson 		if (IS_G4X(dev))
3826a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
382785fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3828a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3829a266c7d5SChris Wilson 
3830a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3831a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3832a266c7d5SChris Wilson 	}
3833bac56d5bSEgbert Eich }
3834a266c7d5SChris Wilson 
3835ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3836a266c7d5SChris Wilson {
3837a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
3838a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3839a266c7d5SChris Wilson 	u32 iir, new_iir;
3840a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3841a266c7d5SChris Wilson 	unsigned long irqflags;
3842a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
384321ad8330SVille Syrjälä 	u32 flip_mask =
384421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
384521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3846a266c7d5SChris Wilson 
3847a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3848a266c7d5SChris Wilson 
3849a266c7d5SChris Wilson 	for (;;) {
3850501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
38512c8ba29fSChris Wilson 		bool blc_event = false;
38522c8ba29fSChris Wilson 
3853a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3854a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3855a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3856a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3857a266c7d5SChris Wilson 		 */
3858a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3859a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
386058174462SMika Kuoppala 			i915_handle_error(dev, false,
386158174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
386258174462SMika Kuoppala 					  iir);
3863a266c7d5SChris Wilson 
3864a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3865a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3866a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3867a266c7d5SChris Wilson 
3868a266c7d5SChris Wilson 			/*
3869a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3870a266c7d5SChris Wilson 			 */
3871a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3872a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3873501e01d7SVille Syrjälä 				irq_received = true;
3874a266c7d5SChris Wilson 			}
3875a266c7d5SChris Wilson 		}
3876a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3877a266c7d5SChris Wilson 
3878a266c7d5SChris Wilson 		if (!irq_received)
3879a266c7d5SChris Wilson 			break;
3880a266c7d5SChris Wilson 
3881a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3882a266c7d5SChris Wilson 
3883a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
3884adca4730SChris Wilson 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
3885a266c7d5SChris Wilson 			u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
3886b543fb04SEgbert Eich 			u32 hotplug_trigger = hotplug_status & (IS_G4X(dev) ?
3887b543fb04SEgbert Eich 								  HOTPLUG_INT_STATUS_G4X :
38884f7fd709SDaniel Vetter 								  HOTPLUG_INT_STATUS_I915);
3889a266c7d5SChris Wilson 
389010a504deSDaniel Vetter 			intel_hpd_irq_handler(dev, hotplug_trigger,
3891704cfb87SDaniel Vetter 					      IS_G4X(dev) ? hpd_status_g4x : hpd_status_i915);
389291d131d2SDaniel Vetter 
38934aeebd74SDaniel Vetter 			if (IS_G4X(dev) &&
38944aeebd74SDaniel Vetter 			    (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X))
38954aeebd74SDaniel Vetter 				dp_aux_irq_handler(dev);
38964aeebd74SDaniel Vetter 
3897a266c7d5SChris Wilson 			I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
3898a266c7d5SChris Wilson 			I915_READ(PORT_HOTPLUG_STAT);
3899a266c7d5SChris Wilson 		}
3900a266c7d5SChris Wilson 
390121ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3902a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3903a266c7d5SChris Wilson 
3904a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3905a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3906a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3907a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3908a266c7d5SChris Wilson 
3909a266c7d5SChris Wilson 		for_each_pipe(pipe) {
39102c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
391190a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
391290a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3913a266c7d5SChris Wilson 
3914a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3915a266c7d5SChris Wilson 				blc_event = true;
39164356d586SDaniel Vetter 
39174356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3918277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3919a266c7d5SChris Wilson 
39202d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
39212d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3922fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
39232d9d2b0bSVille Syrjälä 		}
3924a266c7d5SChris Wilson 
3925a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3926a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3927a266c7d5SChris Wilson 
3928515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3929515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3930515ac2bbSDaniel Vetter 
3931a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3932a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3933a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3934a266c7d5SChris Wilson 		 * we would never get another interrupt.
3935a266c7d5SChris Wilson 		 *
3936a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3937a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3938a266c7d5SChris Wilson 		 * another one.
3939a266c7d5SChris Wilson 		 *
3940a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3941a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3942a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3943a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3944a266c7d5SChris Wilson 		 * stray interrupts.
3945a266c7d5SChris Wilson 		 */
3946a266c7d5SChris Wilson 		iir = new_iir;
3947a266c7d5SChris Wilson 	}
3948a266c7d5SChris Wilson 
3949d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
39502c8ba29fSChris Wilson 
3951a266c7d5SChris Wilson 	return ret;
3952a266c7d5SChris Wilson }
3953a266c7d5SChris Wilson 
3954a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3955a266c7d5SChris Wilson {
3956a266c7d5SChris Wilson 	drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
3957a266c7d5SChris Wilson 	int pipe;
3958a266c7d5SChris Wilson 
3959a266c7d5SChris Wilson 	if (!dev_priv)
3960a266c7d5SChris Wilson 		return;
3961a266c7d5SChris Wilson 
39623ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3963ac4c16c5SEgbert Eich 
3964a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3965a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3966a266c7d5SChris Wilson 
3967a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3968a266c7d5SChris Wilson 	for_each_pipe(pipe)
3969a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3970a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3971a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3972a266c7d5SChris Wilson 
3973a266c7d5SChris Wilson 	for_each_pipe(pipe)
3974a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3975a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3976a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3977a266c7d5SChris Wilson }
3978a266c7d5SChris Wilson 
39793ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
3980ac4c16c5SEgbert Eich {
3981ac4c16c5SEgbert Eich 	drm_i915_private_t *dev_priv = (drm_i915_private_t *)data;
3982ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3983ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3984ac4c16c5SEgbert Eich 	unsigned long irqflags;
3985ac4c16c5SEgbert Eich 	int i;
3986ac4c16c5SEgbert Eich 
3987ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3988ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
3989ac4c16c5SEgbert Eich 		struct drm_connector *connector;
3990ac4c16c5SEgbert Eich 
3991ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
3992ac4c16c5SEgbert Eich 			continue;
3993ac4c16c5SEgbert Eich 
3994ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
3995ac4c16c5SEgbert Eich 
3996ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
3997ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
3998ac4c16c5SEgbert Eich 
3999ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4000ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4001ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4002ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
4003ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4004ac4c16c5SEgbert Eich 				if (!connector->polled)
4005ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4006ac4c16c5SEgbert Eich 			}
4007ac4c16c5SEgbert Eich 		}
4008ac4c16c5SEgbert Eich 	}
4009ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4010ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4011ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4012ac4c16c5SEgbert Eich }
4013ac4c16c5SEgbert Eich 
4014f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4015f71d4af4SJesse Barnes {
40168b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
40178b2e326dSChris Wilson 
40188b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
401999584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4020c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4021a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
40228b2e326dSChris Wilson 
402399584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
402499584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
402561bac78eSDaniel Vetter 		    (unsigned long) dev);
40263ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4027ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
402861bac78eSDaniel Vetter 
402997a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
40309ee32feaSDaniel Vetter 
40314cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
40324cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
40334cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
40344cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4035f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4036f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4037391f75e2SVille Syrjälä 	} else {
4038391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4039391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4040f71d4af4SJesse Barnes 	}
4041f71d4af4SJesse Barnes 
4042c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4043f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4044f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4045c2baf4b7SVille Syrjälä 	}
4046f71d4af4SJesse Barnes 
40477e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
40487e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
40497e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
40507e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
40517e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
40527e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
40537e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4054fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4055abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4056abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4057abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
4058abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4059abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4060abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4061abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4062abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4063f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4064f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4065f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
4066f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4067f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4068f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4069f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
407082a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4071f71d4af4SJesse Barnes 	} else {
4072c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4073c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4074c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4075c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4076c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4077a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4078a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4079a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4080a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4081a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
408220afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4083c2798b19SChris Wilson 		} else {
4084a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4085a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4086a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4087a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4088bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4089c2798b19SChris Wilson 		}
4090f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4091f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4092f71d4af4SJesse Barnes 	}
4093f71d4af4SJesse Barnes }
409420afbda2SDaniel Vetter 
409520afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
409620afbda2SDaniel Vetter {
409720afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4098821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4099821450c6SEgbert Eich 	struct drm_connector *connector;
4100b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4101821450c6SEgbert Eich 	int i;
410220afbda2SDaniel Vetter 
4103821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4104821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4105821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4106821450c6SEgbert Eich 	}
4107821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4108821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4109821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
4110821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4111821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4112821450c6SEgbert Eich 	}
4113b5ea2d56SDaniel Vetter 
4114b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4115b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4116b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
411720afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
411820afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4119b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
412020afbda2SDaniel Vetter }
4121c67a470bSPaulo Zanoni 
4122c67a470bSPaulo Zanoni /* Disable interrupts so we can allow Package C8+. */
4123c67a470bSPaulo Zanoni void hsw_pc8_disable_interrupts(struct drm_device *dev)
4124c67a470bSPaulo Zanoni {
4125c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4126c67a470bSPaulo Zanoni 	unsigned long irqflags;
4127c67a470bSPaulo Zanoni 
4128c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4129c67a470bSPaulo Zanoni 
4130c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.deimr = I915_READ(DEIMR);
4131c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.sdeimr = I915_READ(SDEIMR);
4132c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtimr = I915_READ(GTIMR);
4133c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gtier = I915_READ(GTIER);
4134c67a470bSPaulo Zanoni 	dev_priv->pc8.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
4135c67a470bSPaulo Zanoni 
41361f2d4531SPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, 0xffffffff);
41371f2d4531SPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, 0xffffffff);
4138c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
4139c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
4140c67a470bSPaulo Zanoni 
4141c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = true;
4142c67a470bSPaulo Zanoni 
4143c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4144c67a470bSPaulo Zanoni }
4145c67a470bSPaulo Zanoni 
4146c67a470bSPaulo Zanoni /* Restore interrupts so we can recover from Package C8+. */
4147c67a470bSPaulo Zanoni void hsw_pc8_restore_interrupts(struct drm_device *dev)
4148c67a470bSPaulo Zanoni {
4149c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4150c67a470bSPaulo Zanoni 	unsigned long irqflags;
41511f2d4531SPaulo Zanoni 	uint32_t val;
4152c67a470bSPaulo Zanoni 
4153c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4154c67a470bSPaulo Zanoni 
4155c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
41561f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "DEIMR is 0x%08x\n", val);
4157c67a470bSPaulo Zanoni 
41581f2d4531SPaulo Zanoni 	val = I915_READ(SDEIMR);
41591f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "SDEIMR is 0x%08x\n", val);
4160c67a470bSPaulo Zanoni 
4161c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
41621f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GTIMR is 0x%08x\n", val);
4163c67a470bSPaulo Zanoni 
4164c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
41651f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GEN6_PMIMR is 0x%08x\n", val);
4166c67a470bSPaulo Zanoni 
4167c67a470bSPaulo Zanoni 	dev_priv->pc8.irqs_disabled = false;
4168c67a470bSPaulo Zanoni 
4169c67a470bSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pc8.regsave.deimr);
41701f2d4531SPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv, ~dev_priv->pc8.regsave.sdeimr);
4171c67a470bSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pc8.regsave.gtimr);
4172c67a470bSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pc8.regsave.gen6_pmimr);
4173c67a470bSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pc8.regsave.gtier);
4174c67a470bSPaulo Zanoni 
4175c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4176c67a470bSPaulo Zanoni }
4177