xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision d4eb6b10fcff869969f5c254f6dee6864647accf)
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 
835c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
84f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
855c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
865c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
875c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
885c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
895c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
905c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
915c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
925c502442SPaulo Zanoni } while (0)
935c502442SPaulo Zanoni 
94f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
95a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
965c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
97a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
985c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
995c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1005c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1015c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
102a9d356a6SPaulo Zanoni } while (0)
103a9d356a6SPaulo Zanoni 
104337ba017SPaulo Zanoni /*
105337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
106337ba017SPaulo Zanoni  */
107337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
108337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
109337ba017SPaulo Zanoni 	if (val) { \
110337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
111337ba017SPaulo Zanoni 		     (reg), val); \
112337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
113337ba017SPaulo Zanoni 		POSTING_READ(reg); \
114337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
115337ba017SPaulo Zanoni 		POSTING_READ(reg); \
116337ba017SPaulo Zanoni 	} \
117337ba017SPaulo Zanoni } while (0)
118337ba017SPaulo Zanoni 
11935079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
120337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
12135079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
12235079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
12335079899SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IER(which)); \
12435079899SPaulo Zanoni } while (0)
12535079899SPaulo Zanoni 
12635079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
127337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
12835079899SPaulo Zanoni 	I915_WRITE(type##IMR, (imr_val)); \
12935079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
13035079899SPaulo Zanoni 	POSTING_READ(type##IER); \
13135079899SPaulo Zanoni } while (0)
13235079899SPaulo Zanoni 
133036a4a7dSZhenyu Wang /* For display hotplug interrupt */
134995b6762SChris Wilson static void
1352d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
136036a4a7dSZhenyu Wang {
1374bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1384bc9d430SDaniel Vetter 
1395d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
140c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
1415d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.deimr &= ~mask;
142c67a470bSPaulo Zanoni 		return;
143c67a470bSPaulo Zanoni 	}
144c67a470bSPaulo Zanoni 
1451ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1461ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1471ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1483143a2bfSChris Wilson 		POSTING_READ(DEIMR);
149036a4a7dSZhenyu Wang 	}
150036a4a7dSZhenyu Wang }
151036a4a7dSZhenyu Wang 
1520ff9800aSPaulo Zanoni static void
1532d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
154036a4a7dSZhenyu Wang {
1554bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1564bc9d430SDaniel Vetter 
1575d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
158c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
1595d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.deimr |= mask;
160c67a470bSPaulo Zanoni 		return;
161c67a470bSPaulo Zanoni 	}
162c67a470bSPaulo Zanoni 
1631ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1641ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1651ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1663143a2bfSChris Wilson 		POSTING_READ(DEIMR);
167036a4a7dSZhenyu Wang 	}
168036a4a7dSZhenyu Wang }
169036a4a7dSZhenyu Wang 
17043eaea13SPaulo Zanoni /**
17143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
17243eaea13SPaulo Zanoni  * @dev_priv: driver private
17343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
17443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
17543eaea13SPaulo Zanoni  */
17643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
17743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
17843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
17943eaea13SPaulo Zanoni {
18043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
18143eaea13SPaulo Zanoni 
1825d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
183c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
1845d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.gtimr &= ~interrupt_mask;
1855d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.gtimr |= (~enabled_irq_mask &
186c67a470bSPaulo Zanoni 						interrupt_mask);
187c67a470bSPaulo Zanoni 		return;
188c67a470bSPaulo Zanoni 	}
189c67a470bSPaulo Zanoni 
19043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
19143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
19243eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
19343eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
19443eaea13SPaulo Zanoni }
19543eaea13SPaulo Zanoni 
19643eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19743eaea13SPaulo Zanoni {
19843eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
19943eaea13SPaulo Zanoni }
20043eaea13SPaulo Zanoni 
20143eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
20243eaea13SPaulo Zanoni {
20343eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
20443eaea13SPaulo Zanoni }
20543eaea13SPaulo Zanoni 
206edbfdb45SPaulo Zanoni /**
207edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
208edbfdb45SPaulo Zanoni   * @dev_priv: driver private
209edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
210edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
211edbfdb45SPaulo Zanoni   */
212edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
213edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
214edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
215edbfdb45SPaulo Zanoni {
216605cd25bSPaulo Zanoni 	uint32_t new_val;
217edbfdb45SPaulo Zanoni 
218edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
219edbfdb45SPaulo Zanoni 
2205d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled) {
221c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
2225d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.gen6_pmimr &= ~interrupt_mask;
2235d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.gen6_pmimr |= (~enabled_irq_mask &
224c67a470bSPaulo Zanoni 						     interrupt_mask);
225c67a470bSPaulo Zanoni 		return;
226c67a470bSPaulo Zanoni 	}
227c67a470bSPaulo Zanoni 
228605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
229f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
230f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
231f52ecbcfSPaulo Zanoni 
232605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
233605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
234605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
235edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
236edbfdb45SPaulo Zanoni 	}
237f52ecbcfSPaulo Zanoni }
238edbfdb45SPaulo Zanoni 
239edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
240edbfdb45SPaulo Zanoni {
241edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
242edbfdb45SPaulo Zanoni }
243edbfdb45SPaulo Zanoni 
244edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
245edbfdb45SPaulo Zanoni {
246edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
247edbfdb45SPaulo Zanoni }
248edbfdb45SPaulo Zanoni 
2498664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2508664281bSPaulo Zanoni {
2518664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2528664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2538664281bSPaulo Zanoni 	enum pipe pipe;
2548664281bSPaulo Zanoni 
2554bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2564bc9d430SDaniel Vetter 
2578664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2588664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2598664281bSPaulo Zanoni 
2608664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2618664281bSPaulo Zanoni 			return false;
2628664281bSPaulo Zanoni 	}
2638664281bSPaulo Zanoni 
2648664281bSPaulo Zanoni 	return true;
2658664281bSPaulo Zanoni }
2668664281bSPaulo Zanoni 
2678664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2688664281bSPaulo Zanoni {
2698664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2708664281bSPaulo Zanoni 	enum pipe pipe;
2718664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2728664281bSPaulo Zanoni 
273fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
274fee884edSDaniel Vetter 
2758664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2768664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2778664281bSPaulo Zanoni 
2788664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
2798664281bSPaulo Zanoni 			return false;
2808664281bSPaulo Zanoni 	}
2818664281bSPaulo Zanoni 
2828664281bSPaulo Zanoni 	return true;
2838664281bSPaulo Zanoni }
2848664281bSPaulo Zanoni 
2852d9d2b0bSVille Syrjälä static void i9xx_clear_fifo_underrun(struct drm_device *dev, enum pipe pipe)
2862d9d2b0bSVille Syrjälä {
2872d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
2882d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
2892d9d2b0bSVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0x7fff0000;
2902d9d2b0bSVille Syrjälä 
2912d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
2922d9d2b0bSVille Syrjälä 
2932d9d2b0bSVille Syrjälä 	I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
2942d9d2b0bSVille Syrjälä 	POSTING_READ(reg);
2952d9d2b0bSVille Syrjälä }
2962d9d2b0bSVille Syrjälä 
2978664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
2988664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
2998664281bSPaulo Zanoni {
3008664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3018664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
3028664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
3038664281bSPaulo Zanoni 
3048664281bSPaulo Zanoni 	if (enable)
3058664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
3068664281bSPaulo Zanoni 	else
3078664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
3088664281bSPaulo Zanoni }
3098664281bSPaulo Zanoni 
3108664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
3117336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
3128664281bSPaulo Zanoni {
3138664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3148664281bSPaulo Zanoni 	if (enable) {
3157336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
3167336df65SDaniel Vetter 
3178664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
3188664281bSPaulo Zanoni 			return;
3198664281bSPaulo Zanoni 
3208664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
3218664281bSPaulo Zanoni 	} else {
3227336df65SDaniel Vetter 		bool was_enabled = !(I915_READ(DEIMR) & DE_ERR_INT_IVB);
3237336df65SDaniel Vetter 
3247336df65SDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
3258664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
3267336df65SDaniel Vetter 
3277336df65SDaniel Vetter 		if (!was_enabled &&
3287336df65SDaniel Vetter 		    (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe))) {
3297336df65SDaniel Vetter 			DRM_DEBUG_KMS("uncleared fifo underrun on pipe %c\n",
3307336df65SDaniel Vetter 				      pipe_name(pipe));
3317336df65SDaniel Vetter 		}
3328664281bSPaulo Zanoni 	}
3338664281bSPaulo Zanoni }
3348664281bSPaulo Zanoni 
33538d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
33638d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
33738d83c96SDaniel Vetter {
33838d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
33938d83c96SDaniel Vetter 
34038d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
34138d83c96SDaniel Vetter 
34238d83c96SDaniel Vetter 	if (enable)
34338d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
34438d83c96SDaniel Vetter 	else
34538d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
34638d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
34738d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
34838d83c96SDaniel Vetter }
34938d83c96SDaniel Vetter 
350fee884edSDaniel Vetter /**
351fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
352fee884edSDaniel Vetter  * @dev_priv: driver private
353fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
354fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
355fee884edSDaniel Vetter  */
356fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
357fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
358fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
359fee884edSDaniel Vetter {
360fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
361fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
362fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
363fee884edSDaniel Vetter 
364fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
365fee884edSDaniel Vetter 
3665d584b2eSPaulo Zanoni 	if (dev_priv->pm.irqs_disabled &&
367c67a470bSPaulo Zanoni 	    (interrupt_mask & SDE_HOTPLUG_MASK_CPT)) {
368c67a470bSPaulo Zanoni 		WARN(1, "IRQs disabled\n");
3695d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.sdeimr &= ~interrupt_mask;
3705d584b2eSPaulo Zanoni 		dev_priv->pm.regsave.sdeimr |= (~enabled_irq_mask &
371c67a470bSPaulo Zanoni 						 interrupt_mask);
372c67a470bSPaulo Zanoni 		return;
373c67a470bSPaulo Zanoni 	}
374c67a470bSPaulo Zanoni 
375fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
376fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
377fee884edSDaniel Vetter }
378fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
379fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
380fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
381fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
382fee884edSDaniel Vetter 
383de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
384de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
3858664281bSPaulo Zanoni 					    bool enable)
3868664281bSPaulo Zanoni {
3878664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
388de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
389de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
3908664281bSPaulo Zanoni 
3918664281bSPaulo Zanoni 	if (enable)
392fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
3938664281bSPaulo Zanoni 	else
394fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
3958664281bSPaulo Zanoni }
3968664281bSPaulo Zanoni 
3978664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
3988664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
3998664281bSPaulo Zanoni 					    bool enable)
4008664281bSPaulo Zanoni {
4018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4028664281bSPaulo Zanoni 
4038664281bSPaulo Zanoni 	if (enable) {
4041dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
4051dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
4061dd246fbSDaniel Vetter 
4078664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
4088664281bSPaulo Zanoni 			return;
4098664281bSPaulo Zanoni 
410fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4118664281bSPaulo Zanoni 	} else {
4121dd246fbSDaniel Vetter 		uint32_t tmp = I915_READ(SERR_INT);
4131dd246fbSDaniel Vetter 		bool was_enabled = !(I915_READ(SDEIMR) & SDE_ERROR_CPT);
4141dd246fbSDaniel Vetter 
4151dd246fbSDaniel Vetter 		/* Change the state _after_ we've read out the current one. */
416fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4171dd246fbSDaniel Vetter 
4181dd246fbSDaniel Vetter 		if (!was_enabled &&
4191dd246fbSDaniel Vetter 		    (tmp & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder))) {
4201dd246fbSDaniel Vetter 			DRM_DEBUG_KMS("uncleared pch fifo underrun on pch transcoder %c\n",
4211dd246fbSDaniel Vetter 				      transcoder_name(pch_transcoder));
4221dd246fbSDaniel Vetter 		}
4238664281bSPaulo Zanoni 	}
4248664281bSPaulo Zanoni }
4258664281bSPaulo Zanoni 
4268664281bSPaulo Zanoni /**
4278664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
4288664281bSPaulo Zanoni  * @dev: drm device
4298664281bSPaulo Zanoni  * @pipe: pipe
4308664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4318664281bSPaulo Zanoni  *
4328664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
4338664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
4348664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
4358664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
4368664281bSPaulo Zanoni  * bit for all the pipes.
4378664281bSPaulo Zanoni  *
4388664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4398664281bSPaulo Zanoni  */
440f88d42f1SImre Deak bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
4418664281bSPaulo Zanoni 					     enum pipe pipe, bool enable)
4428664281bSPaulo Zanoni {
4438664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4448664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
4458664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4468664281bSPaulo Zanoni 	bool ret;
4478664281bSPaulo Zanoni 
44877961eb9SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
44977961eb9SImre Deak 
4508664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
4518664281bSPaulo Zanoni 
4528664281bSPaulo Zanoni 	if (enable == ret)
4538664281bSPaulo Zanoni 		goto done;
4548664281bSPaulo Zanoni 
4558664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4568664281bSPaulo Zanoni 
4572d9d2b0bSVille Syrjälä 	if (enable && (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev)))
4582d9d2b0bSVille Syrjälä 		i9xx_clear_fifo_underrun(dev, pipe);
4592d9d2b0bSVille Syrjälä 	else if (IS_GEN5(dev) || IS_GEN6(dev))
4608664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
4618664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
4627336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
46338d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
46438d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
4658664281bSPaulo Zanoni 
4668664281bSPaulo Zanoni done:
467f88d42f1SImre Deak 	return ret;
468f88d42f1SImre Deak }
469f88d42f1SImre Deak 
470f88d42f1SImre Deak bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
471f88d42f1SImre Deak 					   enum pipe pipe, bool enable)
472f88d42f1SImre Deak {
473f88d42f1SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
474f88d42f1SImre Deak 	unsigned long flags;
475f88d42f1SImre Deak 	bool ret;
476f88d42f1SImre Deak 
477f88d42f1SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
478f88d42f1SImre Deak 	ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
4798664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
480f88d42f1SImre Deak 
4818664281bSPaulo Zanoni 	return ret;
4828664281bSPaulo Zanoni }
4838664281bSPaulo Zanoni 
48491d181ddSImre Deak static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
48591d181ddSImre Deak 						  enum pipe pipe)
48691d181ddSImre Deak {
48791d181ddSImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
48891d181ddSImre Deak 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
48991d181ddSImre Deak 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
49091d181ddSImre Deak 
49191d181ddSImre Deak 	return !intel_crtc->cpu_fifo_underrun_disabled;
49291d181ddSImre Deak }
49391d181ddSImre Deak 
4948664281bSPaulo Zanoni /**
4958664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
4968664281bSPaulo Zanoni  * @dev: drm device
4978664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
4988664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4998664281bSPaulo Zanoni  *
5008664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
5018664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
5028664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
5038664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
5048664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
5058664281bSPaulo Zanoni  *
5068664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
5078664281bSPaulo Zanoni  */
5088664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
5098664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
5108664281bSPaulo Zanoni 					   bool enable)
5118664281bSPaulo Zanoni {
5128664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
513de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
514de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5158664281bSPaulo Zanoni 	unsigned long flags;
5168664281bSPaulo Zanoni 	bool ret;
5178664281bSPaulo Zanoni 
518de28075dSDaniel Vetter 	/*
519de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
520de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
521de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
522de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
523de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
524de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
525de28075dSDaniel Vetter 	 */
5268664281bSPaulo Zanoni 
5278664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
5288664281bSPaulo Zanoni 
5298664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
5308664281bSPaulo Zanoni 
5318664281bSPaulo Zanoni 	if (enable == ret)
5328664281bSPaulo Zanoni 		goto done;
5338664281bSPaulo Zanoni 
5348664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
5358664281bSPaulo Zanoni 
5368664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
537de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5388664281bSPaulo Zanoni 	else
5398664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5408664281bSPaulo Zanoni 
5418664281bSPaulo Zanoni done:
5428664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
5438664281bSPaulo Zanoni 	return ret;
5448664281bSPaulo Zanoni }
5458664281bSPaulo Zanoni 
5468664281bSPaulo Zanoni 
547b5ea642aSDaniel Vetter static void
548755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
549755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5507c463586SKeith Packard {
5519db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
552755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5537c463586SKeith Packard 
554b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
555b79480baSDaniel Vetter 
556755e9019SImre Deak 	if (WARN_ON_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
557755e9019SImre Deak 	                 status_mask & ~PIPESTAT_INT_STATUS_MASK))
558755e9019SImre Deak 		return;
559755e9019SImre Deak 
560755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
56146c06a30SVille Syrjälä 		return;
56246c06a30SVille Syrjälä 
56391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
56491d181ddSImre Deak 
5657c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
566755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
56746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5683143a2bfSChris Wilson 	POSTING_READ(reg);
5697c463586SKeith Packard }
5707c463586SKeith Packard 
571b5ea642aSDaniel Vetter static void
572755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
573755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5747c463586SKeith Packard {
5759db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
576755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5777c463586SKeith Packard 
578b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
579b79480baSDaniel Vetter 
580755e9019SImre Deak 	if (WARN_ON_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
581755e9019SImre Deak 	                 status_mask & ~PIPESTAT_INT_STATUS_MASK))
58246c06a30SVille Syrjälä 		return;
58346c06a30SVille Syrjälä 
584755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
585755e9019SImre Deak 		return;
586755e9019SImre Deak 
58791d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
58891d181ddSImre Deak 
589755e9019SImre Deak 	pipestat &= ~enable_mask;
59046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5913143a2bfSChris Wilson 	POSTING_READ(reg);
5927c463586SKeith Packard }
5937c463586SKeith Packard 
59410c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
59510c59c51SImre Deak {
59610c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
59710c59c51SImre Deak 
59810c59c51SImre Deak 	/*
59910c59c51SImre Deak 	 * On pipe A we don't support the PSR interrupt yet, on pipe B the
60010c59c51SImre Deak 	 * same bit MBZ.
60110c59c51SImre Deak 	 */
60210c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
60310c59c51SImre Deak 		return 0;
60410c59c51SImre Deak 
60510c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
60610c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
60710c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
60810c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
60910c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
61010c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
61110c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
61210c59c51SImre Deak 
61310c59c51SImre Deak 	return enable_mask;
61410c59c51SImre Deak }
61510c59c51SImre Deak 
616755e9019SImre Deak void
617755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
618755e9019SImre Deak 		     u32 status_mask)
619755e9019SImre Deak {
620755e9019SImre Deak 	u32 enable_mask;
621755e9019SImre Deak 
62210c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
62310c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
62410c59c51SImre Deak 							   status_mask);
62510c59c51SImre Deak 	else
626755e9019SImre Deak 		enable_mask = status_mask << 16;
627755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
628755e9019SImre Deak }
629755e9019SImre Deak 
630755e9019SImre Deak void
631755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
632755e9019SImre Deak 		      u32 status_mask)
633755e9019SImre Deak {
634755e9019SImre Deak 	u32 enable_mask;
635755e9019SImre Deak 
63610c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
63710c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
63810c59c51SImre Deak 							   status_mask);
63910c59c51SImre Deak 	else
640755e9019SImre Deak 		enable_mask = status_mask << 16;
641755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
642755e9019SImre Deak }
643755e9019SImre Deak 
644c0e09200SDave Airlie /**
645f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
64601c66889SZhao Yakui  */
647f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
64801c66889SZhao Yakui {
6492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6501ec14ad3SChris Wilson 	unsigned long irqflags;
6511ec14ad3SChris Wilson 
652f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
653f49e38ddSJani Nikula 		return;
654f49e38ddSJani Nikula 
6551ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
65601c66889SZhao Yakui 
657755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
658a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6593b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
660755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6611ec14ad3SChris Wilson 
6621ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
66301c66889SZhao Yakui }
66401c66889SZhao Yakui 
66501c66889SZhao Yakui /**
6660a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
6670a3e67a4SJesse Barnes  * @dev: DRM device
6680a3e67a4SJesse Barnes  * @pipe: pipe to check
6690a3e67a4SJesse Barnes  *
6700a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
6710a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
6720a3e67a4SJesse Barnes  * before reading such registers if unsure.
6730a3e67a4SJesse Barnes  */
6740a3e67a4SJesse Barnes static int
6750a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
6760a3e67a4SJesse Barnes {
6772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
678702e7a56SPaulo Zanoni 
679a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
680a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
681a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
682a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
68371f8ba6bSPaulo Zanoni 
684a01025afSDaniel Vetter 		return intel_crtc->active;
685a01025afSDaniel Vetter 	} else {
686a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
687a01025afSDaniel Vetter 	}
6880a3e67a4SJesse Barnes }
6890a3e67a4SJesse Barnes 
6904cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6914cdb83ecSVille Syrjälä {
6924cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6934cdb83ecSVille Syrjälä 	return 0;
6944cdb83ecSVille Syrjälä }
6954cdb83ecSVille Syrjälä 
69642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
69742f52ef8SKeith Packard  * we use as a pipe index
69842f52ef8SKeith Packard  */
699f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
7000a3e67a4SJesse Barnes {
7012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7020a3e67a4SJesse Barnes 	unsigned long high_frame;
7030a3e67a4SJesse Barnes 	unsigned long low_frame;
704391f75e2SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start;
7050a3e67a4SJesse Barnes 
7060a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
70744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7089db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
7090a3e67a4SJesse Barnes 		return 0;
7100a3e67a4SJesse Barnes 	}
7110a3e67a4SJesse Barnes 
712391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
713391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
714391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
715391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
716391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
717391f75e2SVille Syrjälä 
718391f75e2SVille Syrjälä 		vbl_start = mode->crtc_vblank_start * mode->crtc_htotal;
719391f75e2SVille Syrjälä 	} else {
720a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
721391f75e2SVille Syrjälä 		u32 htotal;
722391f75e2SVille Syrjälä 
723391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
724391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
725391f75e2SVille Syrjälä 
726391f75e2SVille Syrjälä 		vbl_start *= htotal;
727391f75e2SVille Syrjälä 	}
728391f75e2SVille Syrjälä 
7299db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7309db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7315eddb70bSChris Wilson 
7320a3e67a4SJesse Barnes 	/*
7330a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7340a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7350a3e67a4SJesse Barnes 	 * register.
7360a3e67a4SJesse Barnes 	 */
7370a3e67a4SJesse Barnes 	do {
7385eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
739391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7405eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7410a3e67a4SJesse Barnes 	} while (high1 != high2);
7420a3e67a4SJesse Barnes 
7435eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
744391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7455eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
746391f75e2SVille Syrjälä 
747391f75e2SVille Syrjälä 	/*
748391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
749391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
750391f75e2SVille Syrjälä 	 * counter against vblank start.
751391f75e2SVille Syrjälä 	 */
752edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7530a3e67a4SJesse Barnes }
7540a3e67a4SJesse Barnes 
755f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
7569880b7a5SJesse Barnes {
7572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7589db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
7599880b7a5SJesse Barnes 
7609880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
76144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
7629db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7639880b7a5SJesse Barnes 		return 0;
7649880b7a5SJesse Barnes 	}
7659880b7a5SJesse Barnes 
7669880b7a5SJesse Barnes 	return I915_READ(reg);
7679880b7a5SJesse Barnes }
7689880b7a5SJesse Barnes 
769ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
770ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
771ad3543edSMario Kleiner 
772095163baSVille Syrjälä static bool ilk_pipe_in_vblank_locked(struct drm_device *dev, enum pipe pipe)
77354ddcbd2SVille Syrjälä {
77454ddcbd2SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
77554ddcbd2SVille Syrjälä 	uint32_t status;
77624302624SVille Syrjälä 	int reg;
77754ddcbd2SVille Syrjälä 
77824302624SVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
77924302624SVille Syrjälä 		status = GEN8_PIPE_VBLANK;
78024302624SVille Syrjälä 		reg = GEN8_DE_PIPE_ISR(pipe);
78124302624SVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
78224302624SVille Syrjälä 		status = DE_PIPE_VBLANK_IVB(pipe);
78324302624SVille Syrjälä 		reg = DEISR;
78454ddcbd2SVille Syrjälä 	} else {
78524302624SVille Syrjälä 		status = DE_PIPE_VBLANK(pipe);
78624302624SVille Syrjälä 		reg = DEISR;
78754ddcbd2SVille Syrjälä 	}
788ad3543edSMario Kleiner 
78924302624SVille Syrjälä 	return __raw_i915_read32(dev_priv, reg) & status;
79054ddcbd2SVille Syrjälä }
79154ddcbd2SVille Syrjälä 
792f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
793abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
794abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
7950af7e4dfSMario Kleiner {
796c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
797c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
798c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
799c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
8003aa18df8SVille Syrjälä 	int position;
8010af7e4dfSMario Kleiner 	int vbl_start, vbl_end, htotal, vtotal;
8020af7e4dfSMario Kleiner 	bool in_vbl = true;
8030af7e4dfSMario Kleiner 	int ret = 0;
804ad3543edSMario Kleiner 	unsigned long irqflags;
8050af7e4dfSMario Kleiner 
806c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
8070af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8089db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8090af7e4dfSMario Kleiner 		return 0;
8100af7e4dfSMario Kleiner 	}
8110af7e4dfSMario Kleiner 
812c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
813c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
814c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
815c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8160af7e4dfSMario Kleiner 
817d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
818d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
819d31faf65SVille Syrjälä 		vbl_end /= 2;
820d31faf65SVille Syrjälä 		vtotal /= 2;
821d31faf65SVille Syrjälä 	}
822d31faf65SVille Syrjälä 
823c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
824c2baf4b7SVille Syrjälä 
825ad3543edSMario Kleiner 	/*
826ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
827ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
828ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
829ad3543edSMario Kleiner 	 */
830ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
831ad3543edSMario Kleiner 
832ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
833ad3543edSMario Kleiner 
834ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
835ad3543edSMario Kleiner 	if (stime)
836ad3543edSMario Kleiner 		*stime = ktime_get();
837ad3543edSMario Kleiner 
8387c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8390af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8400af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8410af7e4dfSMario Kleiner 		 */
8427c06b08aSVille Syrjälä 		if (IS_GEN2(dev))
843ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
8447c06b08aSVille Syrjälä 		else
845ad3543edSMario Kleiner 			position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
84654ddcbd2SVille Syrjälä 
847fcb81823SVille Syrjälä 		if (HAS_DDI(dev)) {
848fcb81823SVille Syrjälä 			/*
849fcb81823SVille Syrjälä 			 * On HSW HDMI outputs there seems to be a 2 line
850fcb81823SVille Syrjälä 			 * difference, whereas eDP has the normal 1 line
851fcb81823SVille Syrjälä 			 * difference that earlier platforms have. External
852fcb81823SVille Syrjälä 			 * DP is unknown. For now just check for the 2 line
853fcb81823SVille Syrjälä 			 * difference case on all output types on HSW+.
854fcb81823SVille Syrjälä 			 *
855fcb81823SVille Syrjälä 			 * This might misinterpret the scanline counter being
856fcb81823SVille Syrjälä 			 * one line too far along on eDP, but that's less
857fcb81823SVille Syrjälä 			 * dangerous than the alternative since that would lead
858fcb81823SVille Syrjälä 			 * the vblank timestamp code astray when it sees a
859fcb81823SVille Syrjälä 			 * scanline count before vblank_start during a vblank
860fcb81823SVille Syrjälä 			 * interrupt.
861fcb81823SVille Syrjälä 			 */
862fcb81823SVille Syrjälä 			in_vbl = ilk_pipe_in_vblank_locked(dev, pipe);
863fcb81823SVille Syrjälä 			if ((in_vbl && (position == vbl_start - 2 ||
864fcb81823SVille Syrjälä 					position == vbl_start - 1)) ||
865fcb81823SVille Syrjälä 			    (!in_vbl && (position == vbl_end - 2 ||
866fcb81823SVille Syrjälä 					 position == vbl_end - 1)))
867fcb81823SVille Syrjälä 				position = (position + 2) % vtotal;
868fcb81823SVille Syrjälä 		} else if (HAS_PCH_SPLIT(dev)) {
86954ddcbd2SVille Syrjälä 			/*
87054ddcbd2SVille Syrjälä 			 * The scanline counter increments at the leading edge
87154ddcbd2SVille Syrjälä 			 * of hsync, ie. it completely misses the active portion
87254ddcbd2SVille Syrjälä 			 * of the line. Fix up the counter at both edges of vblank
87354ddcbd2SVille Syrjälä 			 * to get a more accurate picture whether we're in vblank
87454ddcbd2SVille Syrjälä 			 * or not.
87554ddcbd2SVille Syrjälä 			 */
876095163baSVille Syrjälä 			in_vbl = ilk_pipe_in_vblank_locked(dev, pipe);
87754ddcbd2SVille Syrjälä 			if ((in_vbl && position == vbl_start - 1) ||
87854ddcbd2SVille Syrjälä 			    (!in_vbl && position == vbl_end - 1))
87954ddcbd2SVille Syrjälä 				position = (position + 1) % vtotal;
8800af7e4dfSMario Kleiner 		} else {
881095163baSVille Syrjälä 			/*
882095163baSVille Syrjälä 			 * ISR vblank status bits don't work the way we'd want
883095163baSVille Syrjälä 			 * them to work on non-PCH platforms (for
884095163baSVille Syrjälä 			 * ilk_pipe_in_vblank_locked()), and there doesn't
885095163baSVille Syrjälä 			 * appear any other way to determine if we're currently
886095163baSVille Syrjälä 			 * in vblank.
887095163baSVille Syrjälä 			 *
888095163baSVille Syrjälä 			 * Instead let's assume that we're already in vblank if
889095163baSVille Syrjälä 			 * we got called from the vblank interrupt and the
890095163baSVille Syrjälä 			 * scanline counter value indicates that we're on the
891095163baSVille Syrjälä 			 * line just prior to vblank start. This should result
892095163baSVille Syrjälä 			 * in the correct answer, unless the vblank interrupt
893095163baSVille Syrjälä 			 * delivery really got delayed for almost exactly one
894095163baSVille Syrjälä 			 * full frame/field.
895095163baSVille Syrjälä 			 */
896095163baSVille Syrjälä 			if (flags & DRM_CALLED_FROM_VBLIRQ &&
897095163baSVille Syrjälä 			    position == vbl_start - 1) {
898095163baSVille Syrjälä 				position = (position + 1) % vtotal;
899095163baSVille Syrjälä 
900095163baSVille Syrjälä 				/* Signal this correction as "applied". */
901095163baSVille Syrjälä 				ret |= 0x8;
902095163baSVille Syrjälä 			}
903095163baSVille Syrjälä 		}
904095163baSVille Syrjälä 	} else {
9050af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9060af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9070af7e4dfSMario Kleiner 		 * scanout position.
9080af7e4dfSMario Kleiner 		 */
909ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9100af7e4dfSMario Kleiner 
9113aa18df8SVille Syrjälä 		/* convert to pixel counts */
9123aa18df8SVille Syrjälä 		vbl_start *= htotal;
9133aa18df8SVille Syrjälä 		vbl_end *= htotal;
9143aa18df8SVille Syrjälä 		vtotal *= htotal;
9153aa18df8SVille Syrjälä 	}
9163aa18df8SVille Syrjälä 
917ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
918ad3543edSMario Kleiner 	if (etime)
919ad3543edSMario Kleiner 		*etime = ktime_get();
920ad3543edSMario Kleiner 
921ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
922ad3543edSMario Kleiner 
923ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
924ad3543edSMario Kleiner 
9253aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
9263aa18df8SVille Syrjälä 
9273aa18df8SVille Syrjälä 	/*
9283aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
9293aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
9303aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
9313aa18df8SVille Syrjälä 	 * up since vbl_end.
9323aa18df8SVille Syrjälä 	 */
9333aa18df8SVille Syrjälä 	if (position >= vbl_start)
9343aa18df8SVille Syrjälä 		position -= vbl_end;
9353aa18df8SVille Syrjälä 	else
9363aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
9373aa18df8SVille Syrjälä 
9387c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
9393aa18df8SVille Syrjälä 		*vpos = position;
9403aa18df8SVille Syrjälä 		*hpos = 0;
9413aa18df8SVille Syrjälä 	} else {
9420af7e4dfSMario Kleiner 		*vpos = position / htotal;
9430af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9440af7e4dfSMario Kleiner 	}
9450af7e4dfSMario Kleiner 
9460af7e4dfSMario Kleiner 	/* In vblank? */
9470af7e4dfSMario Kleiner 	if (in_vbl)
9480af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
9490af7e4dfSMario Kleiner 
9500af7e4dfSMario Kleiner 	return ret;
9510af7e4dfSMario Kleiner }
9520af7e4dfSMario Kleiner 
953f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
9540af7e4dfSMario Kleiner 			      int *max_error,
9550af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9560af7e4dfSMario Kleiner 			      unsigned flags)
9570af7e4dfSMario Kleiner {
9584041b853SChris Wilson 	struct drm_crtc *crtc;
9590af7e4dfSMario Kleiner 
9607eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
9614041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9620af7e4dfSMario Kleiner 		return -EINVAL;
9630af7e4dfSMario Kleiner 	}
9640af7e4dfSMario Kleiner 
9650af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9664041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9674041b853SChris Wilson 	if (crtc == NULL) {
9684041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
9694041b853SChris Wilson 		return -EINVAL;
9704041b853SChris Wilson 	}
9714041b853SChris Wilson 
9724041b853SChris Wilson 	if (!crtc->enabled) {
9734041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
9744041b853SChris Wilson 		return -EBUSY;
9754041b853SChris Wilson 	}
9760af7e4dfSMario Kleiner 
9770af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9784041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9794041b853SChris Wilson 						     vblank_time, flags,
9807da903efSVille Syrjälä 						     crtc,
9817da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
9820af7e4dfSMario Kleiner }
9830af7e4dfSMario Kleiner 
98467c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
98567c347ffSJani Nikula 				struct drm_connector *connector)
986321a1b30SEgbert Eich {
987321a1b30SEgbert Eich 	enum drm_connector_status old_status;
988321a1b30SEgbert Eich 
989321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
990321a1b30SEgbert Eich 	old_status = connector->status;
991321a1b30SEgbert Eich 
992321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
99367c347ffSJani Nikula 	if (old_status == connector->status)
99467c347ffSJani Nikula 		return false;
99567c347ffSJani Nikula 
99667c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
997321a1b30SEgbert Eich 		      connector->base.id,
998321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
99967c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
100067c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
100167c347ffSJani Nikula 
100267c347ffSJani Nikula 	return true;
1003321a1b30SEgbert Eich }
1004321a1b30SEgbert Eich 
10055ca58282SJesse Barnes /*
10065ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
10075ca58282SJesse Barnes  */
1008ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
1009ac4c16c5SEgbert Eich 
10105ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
10115ca58282SJesse Barnes {
10122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10132d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
10145ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1015c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
1016cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
1017cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
1018cd569aedSEgbert Eich 	struct drm_connector *connector;
1019cd569aedSEgbert Eich 	unsigned long irqflags;
1020cd569aedSEgbert Eich 	bool hpd_disabled = false;
1021321a1b30SEgbert Eich 	bool changed = false;
1022142e2398SEgbert Eich 	u32 hpd_event_bits;
10235ca58282SJesse Barnes 
102452d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
102552d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
102652d7ecedSDaniel Vetter 		return;
102752d7ecedSDaniel Vetter 
1028a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
1029e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
1030e67189abSJesse Barnes 
1031cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1032142e2398SEgbert Eich 
1033142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
1034142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
1035cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1036cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
1037cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
1038cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
1039cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
1040cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
1041cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
1042cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
1043cd569aedSEgbert Eich 				drm_get_connector_name(connector));
1044cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
1045cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
1046cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
1047cd569aedSEgbert Eich 			hpd_disabled = true;
1048cd569aedSEgbert Eich 		}
1049142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1050142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
1051142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
1052142e2398SEgbert Eich 		}
1053cd569aedSEgbert Eich 	}
1054cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
1055cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
1056cd569aedSEgbert Eich 	  * some connectors */
1057ac4c16c5SEgbert Eich 	if (hpd_disabled) {
1058cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
1059ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
1060ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
1061ac4c16c5SEgbert Eich 	}
1062cd569aedSEgbert Eich 
1063cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1064cd569aedSEgbert Eich 
1065321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1066321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
1067321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1068321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1069cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1070cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1071321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1072321a1b30SEgbert Eich 				changed = true;
1073321a1b30SEgbert Eich 		}
1074321a1b30SEgbert Eich 	}
107540ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
107640ee3381SKeith Packard 
1077321a1b30SEgbert Eich 	if (changed)
1078321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
10795ca58282SJesse Barnes }
10805ca58282SJesse Barnes 
10813ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
10823ca1ccedSVille Syrjälä {
10833ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
10843ca1ccedSVille Syrjälä }
10853ca1ccedSVille Syrjälä 
1086d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1087f97108d1SJesse Barnes {
10882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1089b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
10909270388eSDaniel Vetter 	u8 new_delay;
10919270388eSDaniel Vetter 
1092d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1093f97108d1SJesse Barnes 
109473edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
109573edd18fSDaniel Vetter 
109620e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
10979270388eSDaniel Vetter 
10987648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1099b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1100b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1101f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1102f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1103f97108d1SJesse Barnes 
1104f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1105b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
110620e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
110720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
110820e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
110920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1110b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
111120e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
111220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
111320e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
111420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1115f97108d1SJesse Barnes 	}
1116f97108d1SJesse Barnes 
11177648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
111820e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1119f97108d1SJesse Barnes 
1120d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
11219270388eSDaniel Vetter 
1122f97108d1SJesse Barnes 	return;
1123f97108d1SJesse Barnes }
1124f97108d1SJesse Barnes 
1125549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1126549f7365SChris Wilson 			struct intel_ring_buffer *ring)
1127549f7365SChris Wilson {
1128475553deSChris Wilson 	if (ring->obj == NULL)
1129475553deSChris Wilson 		return;
1130475553deSChris Wilson 
1131814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
11329862e600SChris Wilson 
1133549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
113410cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1135549f7365SChris Wilson }
1136549f7365SChris Wilson 
11374912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11383b8d8d91SJesse Barnes {
11392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11402d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1141edbfdb45SPaulo Zanoni 	u32 pm_iir;
1142dd75fdc8SChris Wilson 	int new_delay, adj;
11433b8d8d91SJesse Barnes 
114459cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1145c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1146c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
11474848405cSBen Widawsky 	/* Make sure not to corrupt PMIMR state used by ringbuffer code */
1148a6706b45SDeepak S 	snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
114959cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11504912d041SBen Widawsky 
115160611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1152a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
115360611c13SPaulo Zanoni 
1154a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
11553b8d8d91SJesse Barnes 		return;
11563b8d8d91SJesse Barnes 
11574fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11587b9e0ae6SChris Wilson 
1159dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11607425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1161dd75fdc8SChris Wilson 		if (adj > 0)
1162dd75fdc8SChris Wilson 			adj *= 2;
1163dd75fdc8SChris Wilson 		else
1164dd75fdc8SChris Wilson 			adj = 1;
1165b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11667425034aSVille Syrjälä 
11677425034aSVille Syrjälä 		/*
11687425034aSVille Syrjälä 		 * For better performance, jump directly
11697425034aSVille Syrjälä 		 * to RPe if we're below it.
11707425034aSVille Syrjälä 		 */
1171b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1172b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1173dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1174b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1175b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1176dd75fdc8SChris Wilson 		else
1177b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1178dd75fdc8SChris Wilson 		adj = 0;
1179dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1180dd75fdc8SChris Wilson 		if (adj < 0)
1181dd75fdc8SChris Wilson 			adj *= 2;
1182dd75fdc8SChris Wilson 		else
1183dd75fdc8SChris Wilson 			adj = -1;
1184b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1185dd75fdc8SChris Wilson 	} else { /* unknown event */
1186b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1187dd75fdc8SChris Wilson 	}
11883b8d8d91SJesse Barnes 
118979249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
119079249636SBen Widawsky 	 * interrupt
119179249636SBen Widawsky 	 */
11921272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1193b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1194b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
119527544369SDeepak S 
1196b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1197dd75fdc8SChris Wilson 
11980a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
11990a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
12000a073b84SJesse Barnes 	else
12014912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
12023b8d8d91SJesse Barnes 
12034fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
12043b8d8d91SJesse Barnes }
12053b8d8d91SJesse Barnes 
1206e3689190SBen Widawsky 
1207e3689190SBen Widawsky /**
1208e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1209e3689190SBen Widawsky  * occurred.
1210e3689190SBen Widawsky  * @work: workqueue struct
1211e3689190SBen Widawsky  *
1212e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1213e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1214e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1215e3689190SBen Widawsky  */
1216e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1217e3689190SBen Widawsky {
12182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12192d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1220e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
122135a85ac6SBen Widawsky 	char *parity_event[6];
1222e3689190SBen Widawsky 	uint32_t misccpctl;
1223e3689190SBen Widawsky 	unsigned long flags;
122435a85ac6SBen Widawsky 	uint8_t slice = 0;
1225e3689190SBen Widawsky 
1226e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1227e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1228e3689190SBen Widawsky 	 * any time we access those registers.
1229e3689190SBen Widawsky 	 */
1230e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1231e3689190SBen Widawsky 
123235a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
123335a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
123435a85ac6SBen Widawsky 		goto out;
123535a85ac6SBen Widawsky 
1236e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1237e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1238e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1239e3689190SBen Widawsky 
124035a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
124135a85ac6SBen Widawsky 		u32 reg;
124235a85ac6SBen Widawsky 
124335a85ac6SBen Widawsky 		slice--;
124435a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
124535a85ac6SBen Widawsky 			break;
124635a85ac6SBen Widawsky 
124735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
124835a85ac6SBen Widawsky 
124935a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
125035a85ac6SBen Widawsky 
125135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1252e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1253e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1254e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1255e3689190SBen Widawsky 
125635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
125735a85ac6SBen Widawsky 		POSTING_READ(reg);
1258e3689190SBen Widawsky 
1259cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1260e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1261e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1262e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
126335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
126435a85ac6SBen Widawsky 		parity_event[5] = NULL;
1265e3689190SBen Widawsky 
12665bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1267e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1268e3689190SBen Widawsky 
126935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
127035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1271e3689190SBen Widawsky 
127235a85ac6SBen Widawsky 		kfree(parity_event[4]);
1273e3689190SBen Widawsky 		kfree(parity_event[3]);
1274e3689190SBen Widawsky 		kfree(parity_event[2]);
1275e3689190SBen Widawsky 		kfree(parity_event[1]);
1276e3689190SBen Widawsky 	}
1277e3689190SBen Widawsky 
127835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
127935a85ac6SBen Widawsky 
128035a85ac6SBen Widawsky out:
128135a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
128235a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
128335a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
128435a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
128535a85ac6SBen Widawsky 
128635a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
128735a85ac6SBen Widawsky }
128835a85ac6SBen Widawsky 
128935a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1290e3689190SBen Widawsky {
12912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1292e3689190SBen Widawsky 
1293040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1294e3689190SBen Widawsky 		return;
1295e3689190SBen Widawsky 
1296d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
129735a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1298d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1299e3689190SBen Widawsky 
130035a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
130135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
130235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
130335a85ac6SBen Widawsky 
130435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
130535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
130635a85ac6SBen Widawsky 
1307a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1308e3689190SBen Widawsky }
1309e3689190SBen Widawsky 
1310f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1311f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1312f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1313f1af8fc1SPaulo Zanoni {
1314f1af8fc1SPaulo Zanoni 	if (gt_iir &
1315f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1316f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1317f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1318f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1319f1af8fc1SPaulo Zanoni }
1320f1af8fc1SPaulo Zanoni 
1321e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1322e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1323e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1324e7b4c6b1SDaniel Vetter {
1325e7b4c6b1SDaniel Vetter 
1326cc609d5dSBen Widawsky 	if (gt_iir &
1327cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1328e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1329cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1330e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1331cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1332e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1333e7b4c6b1SDaniel Vetter 
1334cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1335cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1336cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
133758174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
133858174462SMika Kuoppala 				  gt_iir);
1339e7b4c6b1SDaniel Vetter 	}
1340e3689190SBen Widawsky 
134135a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
134235a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1343e7b4c6b1SDaniel Vetter }
1344e7b4c6b1SDaniel Vetter 
1345abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1346abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1347abd58f01SBen Widawsky 				       u32 master_ctl)
1348abd58f01SBen Widawsky {
1349abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1350abd58f01SBen Widawsky 	uint32_t tmp = 0;
1351abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1352abd58f01SBen Widawsky 
1353abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1354abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1355abd58f01SBen Widawsky 		if (tmp) {
1356abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1357abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1358abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1359abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1360abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1361abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1362abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1363abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1364abd58f01SBen Widawsky 		} else
1365abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1366abd58f01SBen Widawsky 	}
1367abd58f01SBen Widawsky 
1368abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VCS1_IRQ) {
1369abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1370abd58f01SBen Widawsky 		if (tmp) {
1371abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1372abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1373abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1374abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
1375abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1376abd58f01SBen Widawsky 		} else
1377abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1378abd58f01SBen Widawsky 	}
1379abd58f01SBen Widawsky 
1380abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1381abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1382abd58f01SBen Widawsky 		if (tmp) {
1383abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1384abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1385abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1386abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1387abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1388abd58f01SBen Widawsky 		} else
1389abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1390abd58f01SBen Widawsky 	}
1391abd58f01SBen Widawsky 
1392abd58f01SBen Widawsky 	return ret;
1393abd58f01SBen Widawsky }
1394abd58f01SBen Widawsky 
1395b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1396b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1397b543fb04SEgbert Eich 
139810a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1399b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1400b543fb04SEgbert Eich 					 const u32 *hpd)
1401b543fb04SEgbert Eich {
14022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1403b543fb04SEgbert Eich 	int i;
140410a504deSDaniel Vetter 	bool storm_detected = false;
1405b543fb04SEgbert Eich 
140691d131d2SDaniel Vetter 	if (!hotplug_trigger)
140791d131d2SDaniel Vetter 		return;
140891d131d2SDaniel Vetter 
1409cc9bd499SImre Deak 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1410cc9bd499SImre Deak 			  hotplug_trigger);
1411cc9bd499SImre Deak 
1412b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1413b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1414821450c6SEgbert Eich 
14153432087eSChris Wilson 		WARN_ONCE(hpd[i] & hotplug_trigger &&
14168b5565b8SChris Wilson 			  dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED,
1417cba1c073SChris Wilson 			  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1418cba1c073SChris Wilson 			  hotplug_trigger, i, hpd[i]);
1419b8f102e8SEgbert Eich 
1420b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1421b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1422b543fb04SEgbert Eich 			continue;
1423b543fb04SEgbert Eich 
1424bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1425b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1426b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1427b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1428b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1429b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1430b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1431b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1432b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1433142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1434b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
143510a504deSDaniel Vetter 			storm_detected = true;
1436b543fb04SEgbert Eich 		} else {
1437b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1438b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1439b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1440b543fb04SEgbert Eich 		}
1441b543fb04SEgbert Eich 	}
1442b543fb04SEgbert Eich 
144310a504deSDaniel Vetter 	if (storm_detected)
144410a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1445b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
14465876fa0dSDaniel Vetter 
1447645416f5SDaniel Vetter 	/*
1448645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1449645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1450645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1451645416f5SDaniel Vetter 	 * deadlock.
1452645416f5SDaniel Vetter 	 */
1453645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1454b543fb04SEgbert Eich }
1455b543fb04SEgbert Eich 
1456515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1457515ac2bbSDaniel Vetter {
14582d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
145928c70f16SDaniel Vetter 
146028c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1461515ac2bbSDaniel Vetter }
1462515ac2bbSDaniel Vetter 
1463ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1464ce99c256SDaniel Vetter {
14652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
14669ee32feaSDaniel Vetter 
14679ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1468ce99c256SDaniel Vetter }
1469ce99c256SDaniel Vetter 
14708bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1471277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1472eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1473eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14748bc5e955SDaniel Vetter 					 uint32_t crc4)
14758bf1e9f1SShuang He {
14768bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14778bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14788bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1479ac2300d4SDamien Lespiau 	int head, tail;
1480b2c88f5bSDamien Lespiau 
1481d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1482d538bbdfSDamien Lespiau 
14830c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1484d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
14850c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
14860c912c79SDamien Lespiau 		return;
14870c912c79SDamien Lespiau 	}
14880c912c79SDamien Lespiau 
1489d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1490d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1491b2c88f5bSDamien Lespiau 
1492b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1493d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1494b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1495b2c88f5bSDamien Lespiau 		return;
1496b2c88f5bSDamien Lespiau 	}
1497b2c88f5bSDamien Lespiau 
1498b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
14998bf1e9f1SShuang He 
15008bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1501eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1502eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1503eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1504eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1505eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1506b2c88f5bSDamien Lespiau 
1507b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1508d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1509d538bbdfSDamien Lespiau 
1510d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
151107144428SDamien Lespiau 
151207144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15138bf1e9f1SShuang He }
1514277de95eSDaniel Vetter #else
1515277de95eSDaniel Vetter static inline void
1516277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1517277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1518277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1519277de95eSDaniel Vetter 			     uint32_t crc4) {}
1520277de95eSDaniel Vetter #endif
1521eba94eb9SDaniel Vetter 
1522277de95eSDaniel Vetter 
1523277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15245a69b89fSDaniel Vetter {
15255a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15265a69b89fSDaniel Vetter 
1527277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15285a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15295a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15305a69b89fSDaniel Vetter }
15315a69b89fSDaniel Vetter 
1532277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1533eba94eb9SDaniel Vetter {
1534eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1535eba94eb9SDaniel Vetter 
1536277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1537eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1538eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1539eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1540eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15418bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1542eba94eb9SDaniel Vetter }
15435b3a856bSDaniel Vetter 
1544277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15455b3a856bSDaniel Vetter {
15465b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15470b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15480b5c5ed0SDaniel Vetter 
15490b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15500b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15510b5c5ed0SDaniel Vetter 	else
15520b5c5ed0SDaniel Vetter 		res1 = 0;
15530b5c5ed0SDaniel Vetter 
15540b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15550b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15560b5c5ed0SDaniel Vetter 	else
15570b5c5ed0SDaniel Vetter 		res2 = 0;
15585b3a856bSDaniel Vetter 
1559277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15600b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15610b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15620b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15630b5c5ed0SDaniel Vetter 				     res1, res2);
15645b3a856bSDaniel Vetter }
15658bf1e9f1SShuang He 
15661403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15671403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15681403c0d4SPaulo Zanoni  * the work queue. */
15691403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1570baf02a1fSBen Widawsky {
1571a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
157259cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1573a6706b45SDeepak S 		dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1574a6706b45SDeepak S 		snb_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
157559cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
15762adbee62SDaniel Vetter 
15772adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
157841a05a3aSDaniel Vetter 	}
1579baf02a1fSBen Widawsky 
15801403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
158112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
158212638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
158312638c57SBen Widawsky 
158412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
158558174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
158658174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
158758174462SMika Kuoppala 					  pm_iir);
158812638c57SBen Widawsky 		}
158912638c57SBen Widawsky 	}
15901403c0d4SPaulo Zanoni }
1591baf02a1fSBen Widawsky 
1592c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
15937e231dbeSJesse Barnes {
1594c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
159591d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
15967e231dbeSJesse Barnes 	int pipe;
15977e231dbeSJesse Barnes 
159858ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
15997e231dbeSJesse Barnes 	for_each_pipe(pipe) {
160091d181ddSImre Deak 		int reg;
1601bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
160291d181ddSImre Deak 
1603bbb5eebfSDaniel Vetter 		/*
1604bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1605bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1606bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1607bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1608bbb5eebfSDaniel Vetter 		 * handle.
1609bbb5eebfSDaniel Vetter 		 */
1610bbb5eebfSDaniel Vetter 		mask = 0;
1611bbb5eebfSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
1612bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
1613bbb5eebfSDaniel Vetter 
1614bbb5eebfSDaniel Vetter 		switch (pipe) {
1615bbb5eebfSDaniel Vetter 		case PIPE_A:
1616bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1617bbb5eebfSDaniel Vetter 			break;
1618bbb5eebfSDaniel Vetter 		case PIPE_B:
1619bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1620bbb5eebfSDaniel Vetter 			break;
1621bbb5eebfSDaniel Vetter 		}
1622bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1623bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1624bbb5eebfSDaniel Vetter 
1625bbb5eebfSDaniel Vetter 		if (!mask)
162691d181ddSImre Deak 			continue;
162791d181ddSImre Deak 
162891d181ddSImre Deak 		reg = PIPESTAT(pipe);
1629bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1630bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16317e231dbeSJesse Barnes 
16327e231dbeSJesse Barnes 		/*
16337e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16347e231dbeSJesse Barnes 		 */
163591d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
163691d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16377e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16387e231dbeSJesse Barnes 	}
163958ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16407e231dbeSJesse Barnes 
164131acc7f5SJesse Barnes 	for_each_pipe(pipe) {
16427b5562d4SJesse Barnes 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
164331acc7f5SJesse Barnes 			drm_handle_vblank(dev, pipe);
164431acc7f5SJesse Barnes 
1645579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
164631acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
164731acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
164831acc7f5SJesse Barnes 		}
16494356d586SDaniel Vetter 
16504356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1651277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16522d9d2b0bSVille Syrjälä 
16532d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
16542d9d2b0bSVille Syrjälä 		    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1655fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
165631acc7f5SJesse Barnes 	}
165731acc7f5SJesse Barnes 
1658c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1659c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1660c1874ed7SImre Deak }
1661c1874ed7SImre Deak 
166216c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
166316c6c56bSVille Syrjälä {
166416c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
166516c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
166616c6c56bSVille Syrjälä 
166716c6c56bSVille Syrjälä 	if (IS_G4X(dev)) {
166816c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
166916c6c56bSVille Syrjälä 
167016c6c56bSVille Syrjälä 		intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_g4x);
167116c6c56bSVille Syrjälä 	} else {
167216c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
167316c6c56bSVille Syrjälä 
167416c6c56bSVille Syrjälä 		intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
167516c6c56bSVille Syrjälä 	}
167616c6c56bSVille Syrjälä 
167716c6c56bSVille Syrjälä 	if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
167816c6c56bSVille Syrjälä 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
167916c6c56bSVille Syrjälä 		dp_aux_irq_handler(dev);
168016c6c56bSVille Syrjälä 
168116c6c56bSVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
168216c6c56bSVille Syrjälä 	/*
168316c6c56bSVille Syrjälä 	 * Make sure hotplug status is cleared before we clear IIR, or else we
168416c6c56bSVille Syrjälä 	 * may miss hotplug events.
168516c6c56bSVille Syrjälä 	 */
168616c6c56bSVille Syrjälä 	POSTING_READ(PORT_HOTPLUG_STAT);
168716c6c56bSVille Syrjälä }
168816c6c56bSVille Syrjälä 
1689c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1690c1874ed7SImre Deak {
1691c1874ed7SImre Deak 	struct drm_device *dev = (struct drm_device *) arg;
16922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1693c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1694c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1695c1874ed7SImre Deak 
1696c1874ed7SImre Deak 	while (true) {
1697c1874ed7SImre Deak 		iir = I915_READ(VLV_IIR);
1698c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1699c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
1700c1874ed7SImre Deak 
1701c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1702c1874ed7SImre Deak 			goto out;
1703c1874ed7SImre Deak 
1704c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1705c1874ed7SImre Deak 
1706c1874ed7SImre Deak 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1707c1874ed7SImre Deak 
1708c1874ed7SImre Deak 		valleyview_pipestat_irq_handler(dev, iir);
1709c1874ed7SImre Deak 
17107e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
171116c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
171216c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
17137e231dbeSJesse Barnes 
171460611c13SPaulo Zanoni 		if (pm_iir)
1715d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17167e231dbeSJesse Barnes 
17177e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
17187e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
17197e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
17207e231dbeSJesse Barnes 	}
17217e231dbeSJesse Barnes 
17227e231dbeSJesse Barnes out:
17237e231dbeSJesse Barnes 	return ret;
17247e231dbeSJesse Barnes }
17257e231dbeSJesse Barnes 
172623e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1727776ad806SJesse Barnes {
17282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
17299db4a9c7SJesse Barnes 	int pipe;
1730b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1731776ad806SJesse Barnes 
173210a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
173391d131d2SDaniel Vetter 
1734cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1735cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1736776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1737cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1738cfc33bf7SVille Syrjälä 				 port_name(port));
1739cfc33bf7SVille Syrjälä 	}
1740776ad806SJesse Barnes 
1741ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1742ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1743ce99c256SDaniel Vetter 
1744776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1745515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1746776ad806SJesse Barnes 
1747776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1748776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1749776ad806SJesse Barnes 
1750776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1751776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1752776ad806SJesse Barnes 
1753776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1754776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1755776ad806SJesse Barnes 
17569db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
17579db4a9c7SJesse Barnes 		for_each_pipe(pipe)
17589db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17599db4a9c7SJesse Barnes 					 pipe_name(pipe),
17609db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1761776ad806SJesse Barnes 
1762776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1763776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1764776ad806SJesse Barnes 
1765776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1766776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1767776ad806SJesse Barnes 
1768776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
17698664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
17708664281bSPaulo Zanoni 							  false))
1771fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
17728664281bSPaulo Zanoni 
17738664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
17748664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
17758664281bSPaulo Zanoni 							  false))
1776fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
17778664281bSPaulo Zanoni }
17788664281bSPaulo Zanoni 
17798664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
17808664281bSPaulo Zanoni {
17818664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17828664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17835a69b89fSDaniel Vetter 	enum pipe pipe;
17848664281bSPaulo Zanoni 
1785de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1786de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1787de032bf4SPaulo Zanoni 
17885a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
17895a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
17905a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
17915a69b89fSDaniel Vetter 								  false))
1792fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
17935a69b89fSDaniel Vetter 					  pipe_name(pipe));
17945a69b89fSDaniel Vetter 		}
17958664281bSPaulo Zanoni 
17965a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
17975a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1798277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
17995a69b89fSDaniel Vetter 			else
1800277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
18015a69b89fSDaniel Vetter 		}
18025a69b89fSDaniel Vetter 	}
18038bf1e9f1SShuang He 
18048664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
18058664281bSPaulo Zanoni }
18068664281bSPaulo Zanoni 
18078664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
18088664281bSPaulo Zanoni {
18098664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18108664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
18118664281bSPaulo Zanoni 
1812de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1813de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1814de032bf4SPaulo Zanoni 
18158664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
18168664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
18178664281bSPaulo Zanoni 							  false))
1818fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
18198664281bSPaulo Zanoni 
18208664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
18218664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
18228664281bSPaulo Zanoni 							  false))
1823fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
18248664281bSPaulo Zanoni 
18258664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
18268664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
18278664281bSPaulo Zanoni 							  false))
1828fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
18298664281bSPaulo Zanoni 
18308664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1831776ad806SJesse Barnes }
1832776ad806SJesse Barnes 
183323e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
183423e81d69SAdam Jackson {
18352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
183623e81d69SAdam Jackson 	int pipe;
1837b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
183823e81d69SAdam Jackson 
183910a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
184091d131d2SDaniel Vetter 
1841cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1842cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
184323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1844cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1845cfc33bf7SVille Syrjälä 				 port_name(port));
1846cfc33bf7SVille Syrjälä 	}
184723e81d69SAdam Jackson 
184823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1849ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
185023e81d69SAdam Jackson 
185123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1852515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
185323e81d69SAdam Jackson 
185423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
185523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
185623e81d69SAdam Jackson 
185723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
185823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
185923e81d69SAdam Jackson 
186023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
186123e81d69SAdam Jackson 		for_each_pipe(pipe)
186223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
186323e81d69SAdam Jackson 					 pipe_name(pipe),
186423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18658664281bSPaulo Zanoni 
18668664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
18678664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
186823e81d69SAdam Jackson }
186923e81d69SAdam Jackson 
1870c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1871c008bc6eSPaulo Zanoni {
1872c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
187340da17c2SDaniel Vetter 	enum pipe pipe;
1874c008bc6eSPaulo Zanoni 
1875c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1876c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1877c008bc6eSPaulo Zanoni 
1878c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1879c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1880c008bc6eSPaulo Zanoni 
1881c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1882c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1883c008bc6eSPaulo Zanoni 
188440da17c2SDaniel Vetter 	for_each_pipe(pipe) {
188540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
188640da17c2SDaniel Vetter 			drm_handle_vblank(dev, pipe);
1887c008bc6eSPaulo Zanoni 
188840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
188940da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1890fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
189140da17c2SDaniel Vetter 					  pipe_name(pipe));
1892c008bc6eSPaulo Zanoni 
189340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
189440da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
18955b3a856bSDaniel Vetter 
189640da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
189740da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
189840da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
189940da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1900c008bc6eSPaulo Zanoni 		}
1901c008bc6eSPaulo Zanoni 	}
1902c008bc6eSPaulo Zanoni 
1903c008bc6eSPaulo Zanoni 	/* check event from PCH */
1904c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1905c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1906c008bc6eSPaulo Zanoni 
1907c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1908c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1909c008bc6eSPaulo Zanoni 		else
1910c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1911c008bc6eSPaulo Zanoni 
1912c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1913c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1914c008bc6eSPaulo Zanoni 	}
1915c008bc6eSPaulo Zanoni 
1916c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1917c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1918c008bc6eSPaulo Zanoni }
1919c008bc6eSPaulo Zanoni 
19209719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
19219719fb98SPaulo Zanoni {
19229719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
192307d27e20SDamien Lespiau 	enum pipe pipe;
19249719fb98SPaulo Zanoni 
19259719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
19269719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
19279719fb98SPaulo Zanoni 
19289719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
19299719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
19309719fb98SPaulo Zanoni 
19319719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
19329719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
19339719fb98SPaulo Zanoni 
193407d27e20SDamien Lespiau 	for_each_pipe(pipe) {
193507d27e20SDamien Lespiau 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
193607d27e20SDamien Lespiau 			drm_handle_vblank(dev, pipe);
193740da17c2SDaniel Vetter 
193840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
193907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
194007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
194107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
19429719fb98SPaulo Zanoni 		}
19439719fb98SPaulo Zanoni 	}
19449719fb98SPaulo Zanoni 
19459719fb98SPaulo Zanoni 	/* check event from PCH */
19469719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
19479719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
19489719fb98SPaulo Zanoni 
19499719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
19509719fb98SPaulo Zanoni 
19519719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
19529719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
19539719fb98SPaulo Zanoni 	}
19549719fb98SPaulo Zanoni }
19559719fb98SPaulo Zanoni 
1956f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
1957b1f14ad0SJesse Barnes {
1958b1f14ad0SJesse Barnes 	struct drm_device *dev = (struct drm_device *) arg;
19592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1960f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
19610e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
1962b1f14ad0SJesse Barnes 
19638664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
19648664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
1965907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
19668664281bSPaulo Zanoni 
1967b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
1968b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
1969b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
197023a78516SPaulo Zanoni 	POSTING_READ(DEIER);
19710e43406bSChris Wilson 
197244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
197344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
197444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
197544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
197644498aeaSPaulo Zanoni 	 * due to its back queue). */
1977ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
197844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
197944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
198044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
1981ab5c608bSBen Widawsky 	}
198244498aeaSPaulo Zanoni 
19830e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
19840e43406bSChris Wilson 	if (gt_iir) {
1985d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
19860e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
1987d8fc8a47SPaulo Zanoni 		else
1988d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
19890e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
19900e43406bSChris Wilson 		ret = IRQ_HANDLED;
19910e43406bSChris Wilson 	}
1992b1f14ad0SJesse Barnes 
1993b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
19940e43406bSChris Wilson 	if (de_iir) {
1995f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
19969719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
1997f1af8fc1SPaulo Zanoni 		else
1998f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
19990e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
20000e43406bSChris Wilson 		ret = IRQ_HANDLED;
20010e43406bSChris Wilson 	}
20020e43406bSChris Wilson 
2003f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2004f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
20050e43406bSChris Wilson 		if (pm_iir) {
2006d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
2007b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
20080e43406bSChris Wilson 			ret = IRQ_HANDLED;
20090e43406bSChris Wilson 		}
2010f1af8fc1SPaulo Zanoni 	}
2011b1f14ad0SJesse Barnes 
2012b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2013b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2014ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
201544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
201644498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2017ab5c608bSBen Widawsky 	}
2018b1f14ad0SJesse Barnes 
2019b1f14ad0SJesse Barnes 	return ret;
2020b1f14ad0SJesse Barnes }
2021b1f14ad0SJesse Barnes 
2022abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2023abd58f01SBen Widawsky {
2024abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2025abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2026abd58f01SBen Widawsky 	u32 master_ctl;
2027abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2028abd58f01SBen Widawsky 	uint32_t tmp = 0;
2029c42664ccSDaniel Vetter 	enum pipe pipe;
2030abd58f01SBen Widawsky 
2031abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2032abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2033abd58f01SBen Widawsky 	if (!master_ctl)
2034abd58f01SBen Widawsky 		return IRQ_NONE;
2035abd58f01SBen Widawsky 
2036abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2037abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2038abd58f01SBen Widawsky 
2039abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2040abd58f01SBen Widawsky 
2041abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2042abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2043abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
2044abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
2045abd58f01SBen Widawsky 		else if (tmp)
2046abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
2047abd58f01SBen Widawsky 		else
2048abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2049abd58f01SBen Widawsky 
2050abd58f01SBen Widawsky 		if (tmp) {
2051abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2052abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2053abd58f01SBen Widawsky 		}
2054abd58f01SBen Widawsky 	}
2055abd58f01SBen Widawsky 
20566d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
20576d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
20586d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
20596d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
20606d766f02SDaniel Vetter 		else if (tmp)
20616d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
20626d766f02SDaniel Vetter 		else
20636d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
20646d766f02SDaniel Vetter 
20656d766f02SDaniel Vetter 		if (tmp) {
20666d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
20676d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
20686d766f02SDaniel Vetter 		}
20696d766f02SDaniel Vetter 	}
20706d766f02SDaniel Vetter 
2071abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2072abd58f01SBen Widawsky 		uint32_t pipe_iir;
2073abd58f01SBen Widawsky 
2074c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2075c42664ccSDaniel Vetter 			continue;
2076c42664ccSDaniel Vetter 
2077abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2078abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
2079abd58f01SBen Widawsky 			drm_handle_vblank(dev, pipe);
2080abd58f01SBen Widawsky 
2081abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_FLIP_DONE) {
2082abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2083abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2084abd58f01SBen Widawsky 		}
2085abd58f01SBen Widawsky 
20860fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
20870fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
20880fbe7870SDaniel Vetter 
208938d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
209038d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
209138d83c96SDaniel Vetter 								  false))
2092fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
209338d83c96SDaniel Vetter 					  pipe_name(pipe));
209438d83c96SDaniel Vetter 		}
209538d83c96SDaniel Vetter 
209630100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
209730100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
209830100f2bSDaniel Vetter 				  pipe_name(pipe),
209930100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
210030100f2bSDaniel Vetter 		}
2101abd58f01SBen Widawsky 
2102abd58f01SBen Widawsky 		if (pipe_iir) {
2103abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2104abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2105c42664ccSDaniel Vetter 		} else
2106abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2107abd58f01SBen Widawsky 	}
2108abd58f01SBen Widawsky 
210992d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
211092d03a80SDaniel Vetter 		/*
211192d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
211292d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
211392d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
211492d03a80SDaniel Vetter 		 */
211592d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
211692d03a80SDaniel Vetter 
211792d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
211892d03a80SDaniel Vetter 
211992d03a80SDaniel Vetter 		if (pch_iir) {
212092d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
212192d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
212292d03a80SDaniel Vetter 		}
212392d03a80SDaniel Vetter 	}
212492d03a80SDaniel Vetter 
2125abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2126abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2127abd58f01SBen Widawsky 
2128abd58f01SBen Widawsky 	return ret;
2129abd58f01SBen Widawsky }
2130abd58f01SBen Widawsky 
213117e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
213217e1df07SDaniel Vetter 			       bool reset_completed)
213317e1df07SDaniel Vetter {
213417e1df07SDaniel Vetter 	struct intel_ring_buffer *ring;
213517e1df07SDaniel Vetter 	int i;
213617e1df07SDaniel Vetter 
213717e1df07SDaniel Vetter 	/*
213817e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
213917e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
214017e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
214117e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
214217e1df07SDaniel Vetter 	 */
214317e1df07SDaniel Vetter 
214417e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
214517e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
214617e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
214717e1df07SDaniel Vetter 
214817e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
214917e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
215017e1df07SDaniel Vetter 
215117e1df07SDaniel Vetter 	/*
215217e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
215317e1df07SDaniel Vetter 	 * reset state is cleared.
215417e1df07SDaniel Vetter 	 */
215517e1df07SDaniel Vetter 	if (reset_completed)
215617e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
215717e1df07SDaniel Vetter }
215817e1df07SDaniel Vetter 
21598a905236SJesse Barnes /**
21608a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
21618a905236SJesse Barnes  * @work: work struct
21628a905236SJesse Barnes  *
21638a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
21648a905236SJesse Barnes  * was detected.
21658a905236SJesse Barnes  */
21668a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
21678a905236SJesse Barnes {
21681f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
21691f83fee0SDaniel Vetter 						    work);
21702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
21712d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
21728a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2173cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2174cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2175cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
217617e1df07SDaniel Vetter 	int ret;
21778a905236SJesse Barnes 
21785bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
21798a905236SJesse Barnes 
21807db0ba24SDaniel Vetter 	/*
21817db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
21827db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
21837db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
21847db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
21857db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
21867db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
21877db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
21887db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
21897db0ba24SDaniel Vetter 	 */
21907db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
219144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
21925bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
21937db0ba24SDaniel Vetter 				   reset_event);
21941f83fee0SDaniel Vetter 
219517e1df07SDaniel Vetter 		/*
219617e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
219717e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
219817e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
219917e1df07SDaniel Vetter 		 * deadlocks with the reset work.
220017e1df07SDaniel Vetter 		 */
2201f69061beSDaniel Vetter 		ret = i915_reset(dev);
2202f69061beSDaniel Vetter 
220317e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
220417e1df07SDaniel Vetter 
2205f69061beSDaniel Vetter 		if (ret == 0) {
2206f69061beSDaniel Vetter 			/*
2207f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2208f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2209f69061beSDaniel Vetter 			 * complete.
2210f69061beSDaniel Vetter 			 *
2211f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2212f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2213f69061beSDaniel Vetter 			 * updates before
2214f69061beSDaniel Vetter 			 * the counter increment.
2215f69061beSDaniel Vetter 			 */
2216f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
2217f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2218f69061beSDaniel Vetter 
22195bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2220f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
22211f83fee0SDaniel Vetter 		} else {
22222ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2223f316a42cSBen Gamari 		}
22241f83fee0SDaniel Vetter 
222517e1df07SDaniel Vetter 		/*
222617e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
222717e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
222817e1df07SDaniel Vetter 		 */
222917e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2230f316a42cSBen Gamari 	}
22318a905236SJesse Barnes }
22328a905236SJesse Barnes 
223335aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2234c0e09200SDave Airlie {
22358a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2236bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
223763eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2238050ee91fSBen Widawsky 	int pipe, i;
223963eeaf38SJesse Barnes 
224035aed2e6SChris Wilson 	if (!eir)
224135aed2e6SChris Wilson 		return;
224263eeaf38SJesse Barnes 
2243a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
22448a905236SJesse Barnes 
2245bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2246bd9854f9SBen Widawsky 
22478a905236SJesse Barnes 	if (IS_G4X(dev)) {
22488a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
22498a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
22508a905236SJesse Barnes 
2251a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2252a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2253050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2254050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2255a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2256a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
22578a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
22583143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
22598a905236SJesse Barnes 		}
22608a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
22618a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2262a70491ccSJoe Perches 			pr_err("page table error\n");
2263a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
22648a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
22653143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
22668a905236SJesse Barnes 		}
22678a905236SJesse Barnes 	}
22688a905236SJesse Barnes 
2269a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
227063eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
227163eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2272a70491ccSJoe Perches 			pr_err("page table error\n");
2273a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
227463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
22753143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
227663eeaf38SJesse Barnes 		}
22778a905236SJesse Barnes 	}
22788a905236SJesse Barnes 
227963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2280a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
22819db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2282a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
22839db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
228463eeaf38SJesse Barnes 		/* pipestat has already been acked */
228563eeaf38SJesse Barnes 	}
228663eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2287a70491ccSJoe Perches 		pr_err("instruction error\n");
2288a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2289050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2290050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2291a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
229263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
229363eeaf38SJesse Barnes 
2294a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2295a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2296a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
229763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
22983143a2bfSChris Wilson 			POSTING_READ(IPEIR);
229963eeaf38SJesse Barnes 		} else {
230063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
230163eeaf38SJesse Barnes 
2302a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2303a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2304a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2305a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
230663eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
23073143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
230863eeaf38SJesse Barnes 		}
230963eeaf38SJesse Barnes 	}
231063eeaf38SJesse Barnes 
231163eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
23123143a2bfSChris Wilson 	POSTING_READ(EIR);
231363eeaf38SJesse Barnes 	eir = I915_READ(EIR);
231463eeaf38SJesse Barnes 	if (eir) {
231563eeaf38SJesse Barnes 		/*
231663eeaf38SJesse Barnes 		 * some errors might have become stuck,
231763eeaf38SJesse Barnes 		 * mask them.
231863eeaf38SJesse Barnes 		 */
231963eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
232063eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
232163eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
232263eeaf38SJesse Barnes 	}
232335aed2e6SChris Wilson }
232435aed2e6SChris Wilson 
232535aed2e6SChris Wilson /**
232635aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
232735aed2e6SChris Wilson  * @dev: drm device
232835aed2e6SChris Wilson  *
232935aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
233035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
233135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
233235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
233335aed2e6SChris Wilson  * of a ring dump etc.).
233435aed2e6SChris Wilson  */
233558174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
233658174462SMika Kuoppala 		       const char *fmt, ...)
233735aed2e6SChris Wilson {
233835aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
233958174462SMika Kuoppala 	va_list args;
234058174462SMika Kuoppala 	char error_msg[80];
234135aed2e6SChris Wilson 
234258174462SMika Kuoppala 	va_start(args, fmt);
234358174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
234458174462SMika Kuoppala 	va_end(args);
234558174462SMika Kuoppala 
234658174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
234735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
23488a905236SJesse Barnes 
2349ba1234d1SBen Gamari 	if (wedged) {
2350f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2351f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2352ba1234d1SBen Gamari 
235311ed50ecSBen Gamari 		/*
235417e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
235517e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
235617e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
235717e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
235817e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
235917e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
236017e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
236117e1df07SDaniel Vetter 		 *
236217e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
236317e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
236417e1df07SDaniel Vetter 		 * counter atomic_t.
236511ed50ecSBen Gamari 		 */
236617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
236711ed50ecSBen Gamari 	}
236811ed50ecSBen Gamari 
2369122f46baSDaniel Vetter 	/*
2370122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2371122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2372122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2373122f46baSDaniel Vetter 	 * code will deadlock.
2374122f46baSDaniel Vetter 	 */
2375122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
23768a905236SJesse Barnes }
23778a905236SJesse Barnes 
237821ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
23794e5359cdSSimon Farnsworth {
23802d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
23814e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
23824e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
238305394f39SChris Wilson 	struct drm_i915_gem_object *obj;
23844e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
23854e5359cdSSimon Farnsworth 	unsigned long flags;
23864e5359cdSSimon Farnsworth 	bool stall_detected;
23874e5359cdSSimon Farnsworth 
23884e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
23894e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
23904e5359cdSSimon Farnsworth 		return;
23914e5359cdSSimon Farnsworth 
23924e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
23934e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
23944e5359cdSSimon Farnsworth 
2395e7d841caSChris Wilson 	if (work == NULL ||
2396e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2397e7d841caSChris Wilson 	    !work->enable_stall_check) {
23984e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
23994e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
24004e5359cdSSimon Farnsworth 		return;
24014e5359cdSSimon Farnsworth 	}
24024e5359cdSSimon Farnsworth 
24034e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
240405394f39SChris Wilson 	obj = work->pending_flip_obj;
2405a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
24069db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2407446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2408f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
24094e5359cdSSimon Farnsworth 	} else {
24109db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2411f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
241201f2c773SVille Syrjälä 							crtc->y * crtc->fb->pitches[0] +
24134e5359cdSSimon Farnsworth 							crtc->x * crtc->fb->bits_per_pixel/8);
24144e5359cdSSimon Farnsworth 	}
24154e5359cdSSimon Farnsworth 
24164e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
24174e5359cdSSimon Farnsworth 
24184e5359cdSSimon Farnsworth 	if (stall_detected) {
24194e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
24204e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
24214e5359cdSSimon Farnsworth 	}
24224e5359cdSSimon Farnsworth }
24234e5359cdSSimon Farnsworth 
242442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
242542f52ef8SKeith Packard  * we use as a pipe index
242642f52ef8SKeith Packard  */
2427f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
24280a3e67a4SJesse Barnes {
24292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2430e9d21d7fSKeith Packard 	unsigned long irqflags;
243171e0ffa5SJesse Barnes 
24325eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
243371e0ffa5SJesse Barnes 		return -EINVAL;
24340a3e67a4SJesse Barnes 
24351ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2436f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
24377c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2438755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
24390a3e67a4SJesse Barnes 	else
24407c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2441755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
24428692d00eSChris Wilson 
24438692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
24443d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
24456b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
24461ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24478692d00eSChris Wilson 
24480a3e67a4SJesse Barnes 	return 0;
24490a3e67a4SJesse Barnes }
24500a3e67a4SJesse Barnes 
2451f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2452f796cf8fSJesse Barnes {
24532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2454f796cf8fSJesse Barnes 	unsigned long irqflags;
2455b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
245640da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2457f796cf8fSJesse Barnes 
2458f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2459f796cf8fSJesse Barnes 		return -EINVAL;
2460f796cf8fSJesse Barnes 
2461f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2462b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2463b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2464b1f14ad0SJesse Barnes 
2465b1f14ad0SJesse Barnes 	return 0;
2466b1f14ad0SJesse Barnes }
2467b1f14ad0SJesse Barnes 
24687e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
24697e231dbeSJesse Barnes {
24702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
24717e231dbeSJesse Barnes 	unsigned long irqflags;
24727e231dbeSJesse Barnes 
24737e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
24747e231dbeSJesse Barnes 		return -EINVAL;
24757e231dbeSJesse Barnes 
24767e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
247731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2478755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
24797e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
24807e231dbeSJesse Barnes 
24817e231dbeSJesse Barnes 	return 0;
24827e231dbeSJesse Barnes }
24837e231dbeSJesse Barnes 
2484abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2485abd58f01SBen Widawsky {
2486abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2487abd58f01SBen Widawsky 	unsigned long irqflags;
2488abd58f01SBen Widawsky 
2489abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2490abd58f01SBen Widawsky 		return -EINVAL;
2491abd58f01SBen Widawsky 
2492abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
24937167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
24947167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2495abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2496abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2497abd58f01SBen Widawsky 	return 0;
2498abd58f01SBen Widawsky }
2499abd58f01SBen Widawsky 
250042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
250142f52ef8SKeith Packard  * we use as a pipe index
250242f52ef8SKeith Packard  */
2503f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
25040a3e67a4SJesse Barnes {
25052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2506e9d21d7fSKeith Packard 	unsigned long irqflags;
25070a3e67a4SJesse Barnes 
25081ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25093d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
25106b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
25118692d00eSChris Wilson 
25127c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2513755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2514755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
25151ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25160a3e67a4SJesse Barnes }
25170a3e67a4SJesse Barnes 
2518f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2519f796cf8fSJesse Barnes {
25202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2521f796cf8fSJesse Barnes 	unsigned long irqflags;
2522b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
252340da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2524f796cf8fSJesse Barnes 
2525f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2526b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2527b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2528b1f14ad0SJesse Barnes }
2529b1f14ad0SJesse Barnes 
25307e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
25317e231dbeSJesse Barnes {
25322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25337e231dbeSJesse Barnes 	unsigned long irqflags;
25347e231dbeSJesse Barnes 
25357e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
253631acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2537755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
25387e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25397e231dbeSJesse Barnes }
25407e231dbeSJesse Barnes 
2541abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2542abd58f01SBen Widawsky {
2543abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2544abd58f01SBen Widawsky 	unsigned long irqflags;
2545abd58f01SBen Widawsky 
2546abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2547abd58f01SBen Widawsky 		return;
2548abd58f01SBen Widawsky 
2549abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25507167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
25517167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2552abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2553abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2554abd58f01SBen Widawsky }
2555abd58f01SBen Widawsky 
2556893eead0SChris Wilson static u32
2557893eead0SChris Wilson ring_last_seqno(struct intel_ring_buffer *ring)
2558852835f3SZou Nan hai {
2559893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2560893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2561893eead0SChris Wilson }
2562893eead0SChris Wilson 
25639107e9d2SChris Wilson static bool
25649107e9d2SChris Wilson ring_idle(struct intel_ring_buffer *ring, u32 seqno)
2565893eead0SChris Wilson {
25669107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
25679107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2568f65d9421SBen Gamari }
2569f65d9421SBen Gamari 
2570a028c4b0SDaniel Vetter static bool
2571a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2572a028c4b0SDaniel Vetter {
2573a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2574a028c4b0SDaniel Vetter 		/*
2575a028c4b0SDaniel Vetter 		 * FIXME: gen8 semaphore support - currently we don't emit
2576a028c4b0SDaniel Vetter 		 * semaphores on bdw anyway, but this needs to be addressed when
2577a028c4b0SDaniel Vetter 		 * we merge that code.
2578a028c4b0SDaniel Vetter 		 */
2579a028c4b0SDaniel Vetter 		return false;
2580a028c4b0SDaniel Vetter 	} else {
2581a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2582a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2583a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2584a028c4b0SDaniel Vetter 	}
2585a028c4b0SDaniel Vetter }
2586a028c4b0SDaniel Vetter 
25876274f212SChris Wilson static struct intel_ring_buffer *
2588921d42eaSDaniel Vetter semaphore_wait_to_signaller_ring(struct intel_ring_buffer *ring, u32 ipehr)
2589921d42eaSDaniel Vetter {
2590921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2591921d42eaSDaniel Vetter 	struct intel_ring_buffer *signaller;
2592921d42eaSDaniel Vetter 	int i;
2593921d42eaSDaniel Vetter 
2594921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2595921d42eaSDaniel Vetter 		/*
2596921d42eaSDaniel Vetter 		 * FIXME: gen8 semaphore support - currently we don't emit
2597921d42eaSDaniel Vetter 		 * semaphores on bdw anyway, but this needs to be addressed when
2598921d42eaSDaniel Vetter 		 * we merge that code.
2599921d42eaSDaniel Vetter 		 */
2600921d42eaSDaniel Vetter 		return NULL;
2601921d42eaSDaniel Vetter 	} else {
2602921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2603921d42eaSDaniel Vetter 
2604921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2605921d42eaSDaniel Vetter 			if(ring == signaller)
2606921d42eaSDaniel Vetter 				continue;
2607921d42eaSDaniel Vetter 
2608921d42eaSDaniel Vetter 			if (sync_bits ==
2609921d42eaSDaniel Vetter 			    signaller->semaphore_register[ring->id])
2610921d42eaSDaniel Vetter 				return signaller;
2611921d42eaSDaniel Vetter 		}
2612921d42eaSDaniel Vetter 	}
2613921d42eaSDaniel Vetter 
2614921d42eaSDaniel Vetter 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x\n",
2615921d42eaSDaniel Vetter 		  ring->id, ipehr);
2616921d42eaSDaniel Vetter 
2617921d42eaSDaniel Vetter 	return NULL;
2618921d42eaSDaniel Vetter }
2619921d42eaSDaniel Vetter 
2620921d42eaSDaniel Vetter static struct intel_ring_buffer *
26216274f212SChris Wilson semaphore_waits_for(struct intel_ring_buffer *ring, u32 *seqno)
2622a24a11e6SChris Wilson {
2623a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
262488fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
262588fe429dSDaniel Vetter 	int i;
2626a24a11e6SChris Wilson 
2627a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2628a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
26296274f212SChris Wilson 		return NULL;
2630a24a11e6SChris Wilson 
263188fe429dSDaniel Vetter 	/*
263288fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
263388fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
263488fe429dSDaniel Vetter 	 * dwords. Note that we don't care about ACTHD here since that might
263588fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
263688fe429dSDaniel Vetter 	 * ringbuffer itself.
2637a24a11e6SChris Wilson 	 */
263888fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
263988fe429dSDaniel Vetter 
264088fe429dSDaniel Vetter 	for (i = 4; i; --i) {
264188fe429dSDaniel Vetter 		/*
264288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
264388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
264488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
264588fe429dSDaniel Vetter 		 */
264688fe429dSDaniel Vetter 		head &= ring->size - 1;
264788fe429dSDaniel Vetter 
264888fe429dSDaniel Vetter 		/* This here seems to blow up */
264988fe429dSDaniel Vetter 		cmd = ioread32(ring->virtual_start + head);
2650a24a11e6SChris Wilson 		if (cmd == ipehr)
2651a24a11e6SChris Wilson 			break;
2652a24a11e6SChris Wilson 
265388fe429dSDaniel Vetter 		head -= 4;
265488fe429dSDaniel Vetter 	}
2655a24a11e6SChris Wilson 
265688fe429dSDaniel Vetter 	if (!i)
265788fe429dSDaniel Vetter 		return NULL;
265888fe429dSDaniel Vetter 
265988fe429dSDaniel Vetter 	*seqno = ioread32(ring->virtual_start + head + 4) + 1;
2660921d42eaSDaniel Vetter 	return semaphore_wait_to_signaller_ring(ring, ipehr);
2661a24a11e6SChris Wilson }
2662a24a11e6SChris Wilson 
26636274f212SChris Wilson static int semaphore_passed(struct intel_ring_buffer *ring)
26646274f212SChris Wilson {
26656274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
26666274f212SChris Wilson 	struct intel_ring_buffer *signaller;
26676274f212SChris Wilson 	u32 seqno, ctl;
26686274f212SChris Wilson 
26696274f212SChris Wilson 	ring->hangcheck.deadlock = true;
26706274f212SChris Wilson 
26716274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
26726274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
26736274f212SChris Wilson 		return -1;
26746274f212SChris Wilson 
26756274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
26766274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
26776274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
26786274f212SChris Wilson 		return -1;
26796274f212SChris Wilson 
26806274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
26816274f212SChris Wilson }
26826274f212SChris Wilson 
26836274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
26846274f212SChris Wilson {
26856274f212SChris Wilson 	struct intel_ring_buffer *ring;
26866274f212SChris Wilson 	int i;
26876274f212SChris Wilson 
26886274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
26896274f212SChris Wilson 		ring->hangcheck.deadlock = false;
26906274f212SChris Wilson }
26916274f212SChris Wilson 
2692ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
269350877445SChris Wilson ring_stuck(struct intel_ring_buffer *ring, u64 acthd)
26941ec14ad3SChris Wilson {
26951ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
26961ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
26979107e9d2SChris Wilson 	u32 tmp;
26989107e9d2SChris Wilson 
26996274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2700f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
27016274f212SChris Wilson 
27029107e9d2SChris Wilson 	if (IS_GEN2(dev))
2703f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
27049107e9d2SChris Wilson 
27059107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
27069107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
27079107e9d2SChris Wilson 	 * and break the hang. This should work on
27089107e9d2SChris Wilson 	 * all but the second generation chipsets.
27099107e9d2SChris Wilson 	 */
27109107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
27111ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
271258174462SMika Kuoppala 		i915_handle_error(dev, false,
271358174462SMika Kuoppala 				  "Kicking stuck wait on %s",
27141ec14ad3SChris Wilson 				  ring->name);
27151ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2716f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
27171ec14ad3SChris Wilson 	}
2718a24a11e6SChris Wilson 
27196274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
27206274f212SChris Wilson 		switch (semaphore_passed(ring)) {
27216274f212SChris Wilson 		default:
2722f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
27236274f212SChris Wilson 		case 1:
272458174462SMika Kuoppala 			i915_handle_error(dev, false,
272558174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2726a24a11e6SChris Wilson 					  ring->name);
2727a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2728f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
27296274f212SChris Wilson 		case 0:
2730f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
27316274f212SChris Wilson 		}
27329107e9d2SChris Wilson 	}
27339107e9d2SChris Wilson 
2734f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2735a24a11e6SChris Wilson }
2736d1e61e7fSChris Wilson 
2737f65d9421SBen Gamari /**
2738f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
273905407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
274005407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
274105407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
274205407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
274305407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2744f65d9421SBen Gamari  */
2745a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2746f65d9421SBen Gamari {
2747f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
27482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2749b4519513SChris Wilson 	struct intel_ring_buffer *ring;
2750b4519513SChris Wilson 	int i;
275105407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
27529107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
27539107e9d2SChris Wilson #define BUSY 1
27549107e9d2SChris Wilson #define KICK 5
27559107e9d2SChris Wilson #define HUNG 20
2756893eead0SChris Wilson 
2757d330a953SJani Nikula 	if (!i915.enable_hangcheck)
27583e0dc6b0SBen Widawsky 		return;
27593e0dc6b0SBen Widawsky 
2760b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
276150877445SChris Wilson 		u64 acthd;
276250877445SChris Wilson 		u32 seqno;
27639107e9d2SChris Wilson 		bool busy = true;
2764b4519513SChris Wilson 
27656274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
27666274f212SChris Wilson 
276705407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
276805407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
276905407ff8SMika Kuoppala 
277005407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
27719107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2772da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2773da661464SMika Kuoppala 
27749107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
27759107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2776094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2777f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
27789107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
27799107e9d2SChris Wilson 								  ring->name);
2780f4adcd24SDaniel Vetter 						else
2781f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2782f4adcd24SDaniel Vetter 								 ring->name);
27839107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2784094f9a54SChris Wilson 					}
2785094f9a54SChris Wilson 					/* Safeguard against driver failure */
2786094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
27879107e9d2SChris Wilson 				} else
27889107e9d2SChris Wilson 					busy = false;
278905407ff8SMika Kuoppala 			} else {
27906274f212SChris Wilson 				/* We always increment the hangcheck score
27916274f212SChris Wilson 				 * if the ring is busy and still processing
27926274f212SChris Wilson 				 * the same request, so that no single request
27936274f212SChris Wilson 				 * can run indefinitely (such as a chain of
27946274f212SChris Wilson 				 * batches). The only time we do not increment
27956274f212SChris Wilson 				 * the hangcheck score on this ring, if this
27966274f212SChris Wilson 				 * ring is in a legitimate wait for another
27976274f212SChris Wilson 				 * ring. In that case the waiting ring is a
27986274f212SChris Wilson 				 * victim and we want to be sure we catch the
27996274f212SChris Wilson 				 * right culprit. Then every time we do kick
28006274f212SChris Wilson 				 * the ring, add a small increment to the
28016274f212SChris Wilson 				 * score so that we can catch a batch that is
28026274f212SChris Wilson 				 * being repeatedly kicked and so responsible
28036274f212SChris Wilson 				 * for stalling the machine.
28049107e9d2SChris Wilson 				 */
2805ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2806ad8beaeaSMika Kuoppala 								    acthd);
2807ad8beaeaSMika Kuoppala 
2808ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2809da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2810f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
28116274f212SChris Wilson 					break;
2812f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2813ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
28146274f212SChris Wilson 					break;
2815f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2816ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
28176274f212SChris Wilson 					break;
2818f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2819ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
28206274f212SChris Wilson 					stuck[i] = true;
28216274f212SChris Wilson 					break;
28226274f212SChris Wilson 				}
282305407ff8SMika Kuoppala 			}
28249107e9d2SChris Wilson 		} else {
2825da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2826da661464SMika Kuoppala 
28279107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
28289107e9d2SChris Wilson 			 * attempts across multiple batches.
28299107e9d2SChris Wilson 			 */
28309107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
28319107e9d2SChris Wilson 				ring->hangcheck.score--;
2832cbb465e7SChris Wilson 		}
2833f65d9421SBen Gamari 
283405407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
283505407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
28369107e9d2SChris Wilson 		busy_count += busy;
283705407ff8SMika Kuoppala 	}
283805407ff8SMika Kuoppala 
283905407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2840b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2841b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
284205407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2843a43adf07SChris Wilson 				 ring->name);
2844a43adf07SChris Wilson 			rings_hung++;
284505407ff8SMika Kuoppala 		}
284605407ff8SMika Kuoppala 	}
284705407ff8SMika Kuoppala 
284805407ff8SMika Kuoppala 	if (rings_hung)
284958174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
285005407ff8SMika Kuoppala 
285105407ff8SMika Kuoppala 	if (busy_count)
285205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
285305407ff8SMika Kuoppala 		 * being added */
285410cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
285510cd45b6SMika Kuoppala }
285610cd45b6SMika Kuoppala 
285710cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
285810cd45b6SMika Kuoppala {
285910cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
2860d330a953SJani Nikula 	if (!i915.enable_hangcheck)
286110cd45b6SMika Kuoppala 		return;
286210cd45b6SMika Kuoppala 
286399584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
286410cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
2865f65d9421SBen Gamari }
2866f65d9421SBen Gamari 
28671c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
286891738a95SPaulo Zanoni {
286991738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
287091738a95SPaulo Zanoni 
287191738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
287291738a95SPaulo Zanoni 		return;
287391738a95SPaulo Zanoni 
2874f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2875105b122eSPaulo Zanoni 
2876105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2877105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2878622364b6SPaulo Zanoni }
2879105b122eSPaulo Zanoni 
288091738a95SPaulo Zanoni /*
2881622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2882622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2883622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2884622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2885622364b6SPaulo Zanoni  *
2886622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
288791738a95SPaulo Zanoni  */
2888622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2889622364b6SPaulo Zanoni {
2890622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2891622364b6SPaulo Zanoni 
2892622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
2893622364b6SPaulo Zanoni 		return;
2894622364b6SPaulo Zanoni 
2895622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
289691738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
289791738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
289891738a95SPaulo Zanoni }
289991738a95SPaulo Zanoni 
29007c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
2901d18ea1b5SDaniel Vetter {
2902d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
2903d18ea1b5SDaniel Vetter 
2904f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
2905a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
2906f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
2907d18ea1b5SDaniel Vetter }
2908d18ea1b5SDaniel Vetter 
2909c0e09200SDave Airlie /* drm_dma.h hooks
2910c0e09200SDave Airlie */
2911f71d4af4SJesse Barnes static void ironlake_irq_preinstall(struct drm_device *dev)
2912036a4a7dSZhenyu Wang {
29132d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2914036a4a7dSZhenyu Wang 
2915036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xeffe);
2916bdfcdb63SDaniel Vetter 
2917f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
2918c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
2919c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
2920c6d954c1SPaulo Zanoni 
29217c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
2922c650156aSZhenyu Wang 
29231c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
29247d99163dSBen Widawsky }
29257d99163dSBen Widawsky 
29267e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
29277e231dbeSJesse Barnes {
29282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
29297e231dbeSJesse Barnes 	int pipe;
29307e231dbeSJesse Barnes 
29317e231dbeSJesse Barnes 	/* VLV magic */
29327e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
29337e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
29347e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
29357e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
29367e231dbeSJesse Barnes 
29377e231dbeSJesse Barnes 	/* and GT */
29387e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
29397e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
2940d18ea1b5SDaniel Vetter 
29417c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
29427e231dbeSJesse Barnes 
29437e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
29447e231dbeSJesse Barnes 
29457e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
29467e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
29477e231dbeSJesse Barnes 	for_each_pipe(pipe)
29487e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
29497e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
29507e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
29517e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
29527e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
29537e231dbeSJesse Barnes }
29547e231dbeSJesse Barnes 
2955abd58f01SBen Widawsky static void gen8_irq_preinstall(struct drm_device *dev)
2956abd58f01SBen Widawsky {
2957abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2958abd58f01SBen Widawsky 	int pipe;
2959abd58f01SBen Widawsky 
2960abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2961abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2962abd58f01SBen Widawsky 
2963f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 0);
2964f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 1);
2965f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 2);
2966f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 3);
2967abd58f01SBen Widawsky 
2968abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2969f86f3fb0SPaulo Zanoni 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
2970abd58f01SBen Widawsky 	}
2971abd58f01SBen Widawsky 
2972f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
2973f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
2974f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
297509f2344dSJesse Barnes 
29761c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
2977abd58f01SBen Widawsky }
2978abd58f01SBen Widawsky 
297982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
298082a28bcfSDaniel Vetter {
29812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
298282a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
298382a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
2984fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
298582a28bcfSDaniel Vetter 
298682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
2987fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
298882a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2989cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2990fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
299182a28bcfSDaniel Vetter 	} else {
2992fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
299382a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
2994cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
2995fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
299682a28bcfSDaniel Vetter 	}
299782a28bcfSDaniel Vetter 
2998fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
299982a28bcfSDaniel Vetter 
30007fe0b973SKeith Packard 	/*
30017fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
30027fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
30037fe0b973SKeith Packard 	 *
30047fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
30057fe0b973SKeith Packard 	 */
30067fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
30077fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
30087fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
30097fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
30107fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
30117fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
30127fe0b973SKeith Packard }
30137fe0b973SKeith Packard 
3014d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3015d46da437SPaulo Zanoni {
30162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
301782a28bcfSDaniel Vetter 	u32 mask;
3018d46da437SPaulo Zanoni 
3019692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3020692a04cfSDaniel Vetter 		return;
3021692a04cfSDaniel Vetter 
3022105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
30235c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3024105b122eSPaulo Zanoni 	else
30255c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
30268664281bSPaulo Zanoni 
3027337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3028d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3029d46da437SPaulo Zanoni }
3030d46da437SPaulo Zanoni 
30310a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
30320a9a8c91SDaniel Vetter {
30330a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
30340a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
30350a9a8c91SDaniel Vetter 
30360a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
30370a9a8c91SDaniel Vetter 
30380a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3039040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
30400a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
304135a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
304235a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
30430a9a8c91SDaniel Vetter 	}
30440a9a8c91SDaniel Vetter 
30450a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
30460a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
30470a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
30480a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
30490a9a8c91SDaniel Vetter 	} else {
30500a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
30510a9a8c91SDaniel Vetter 	}
30520a9a8c91SDaniel Vetter 
305335079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
30540a9a8c91SDaniel Vetter 
30550a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3056a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
30570a9a8c91SDaniel Vetter 
30580a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
30590a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
30600a9a8c91SDaniel Vetter 
3061605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
306235079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
30630a9a8c91SDaniel Vetter 	}
30640a9a8c91SDaniel Vetter }
30650a9a8c91SDaniel Vetter 
3066f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3067036a4a7dSZhenyu Wang {
30684bc9d430SDaniel Vetter 	unsigned long irqflags;
30692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30708e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
30718e76f8dcSPaulo Zanoni 
30728e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
30738e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
30748e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
30758e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
30765c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
30778e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
30785c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
30798e76f8dcSPaulo Zanoni 	} else {
30808e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3081ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
30825b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
30835b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
30845b3a856bSDaniel Vetter 				DE_POISON);
30855c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
30865c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
30878e76f8dcSPaulo Zanoni 	}
3088036a4a7dSZhenyu Wang 
30891ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3090036a4a7dSZhenyu Wang 
3091622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3092622364b6SPaulo Zanoni 
309335079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3094036a4a7dSZhenyu Wang 
30950a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3096036a4a7dSZhenyu Wang 
3097d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
30987fe0b973SKeith Packard 
3099f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
31006005ce42SDaniel Vetter 		/* Enable PCU event interrupts
31016005ce42SDaniel Vetter 		 *
31026005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
31034bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
31044bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
31054bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3106f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
31074bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3108f97108d1SJesse Barnes 	}
3109f97108d1SJesse Barnes 
3110036a4a7dSZhenyu Wang 	return 0;
3111036a4a7dSZhenyu Wang }
3112036a4a7dSZhenyu Wang 
3113f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3114f8b79e58SImre Deak {
3115f8b79e58SImre Deak 	u32 pipestat_mask;
3116f8b79e58SImre Deak 	u32 iir_mask;
3117f8b79e58SImre Deak 
3118f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3119f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3120f8b79e58SImre Deak 
3121f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3122f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3123f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3124f8b79e58SImre Deak 
3125f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3126f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3127f8b79e58SImre Deak 
3128f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3129f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3130f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3131f8b79e58SImre Deak 
3132f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3133f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3134f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3135f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3136f8b79e58SImre Deak 
3137f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3138f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3139f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3140f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3141f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3142f8b79e58SImre Deak }
3143f8b79e58SImre Deak 
3144f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3145f8b79e58SImre Deak {
3146f8b79e58SImre Deak 	u32 pipestat_mask;
3147f8b79e58SImre Deak 	u32 iir_mask;
3148f8b79e58SImre Deak 
3149f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3150f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
31516c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3152f8b79e58SImre Deak 
3153f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3154f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3155f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3156f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3157f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3158f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3159f8b79e58SImre Deak 
3160f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3161f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3162f8b79e58SImre Deak 
3163f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3164f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3165f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3166f8b79e58SImre Deak 
3167f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3168f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3169f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3170f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3171f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3172f8b79e58SImre Deak }
3173f8b79e58SImre Deak 
3174f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3175f8b79e58SImre Deak {
3176f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3177f8b79e58SImre Deak 
3178f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3179f8b79e58SImre Deak 		return;
3180f8b79e58SImre Deak 
3181f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3182f8b79e58SImre Deak 
3183f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3184f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3185f8b79e58SImre Deak }
3186f8b79e58SImre Deak 
3187f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3188f8b79e58SImre Deak {
3189f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3190f8b79e58SImre Deak 
3191f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3192f8b79e58SImre Deak 		return;
3193f8b79e58SImre Deak 
3194f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3195f8b79e58SImre Deak 
3196f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3197f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3198f8b79e58SImre Deak }
3199f8b79e58SImre Deak 
32007e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
32017e231dbeSJesse Barnes {
32022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3203b79480baSDaniel Vetter 	unsigned long irqflags;
32047e231dbeSJesse Barnes 
3205f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
32067e231dbeSJesse Barnes 
320720afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
320820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
320920afbda2SDaniel Vetter 
32107e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3211f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
32127e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
32137e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
32147e231dbeSJesse Barnes 
3215b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3216b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3217b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3218f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3219f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3220b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
322131acc7f5SJesse Barnes 
32227e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
32237e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
32247e231dbeSJesse Barnes 
32250a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
32267e231dbeSJesse Barnes 
32277e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
32287e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
32297e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
32307e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
32317e231dbeSJesse Barnes #endif
32327e231dbeSJesse Barnes 
32337e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
323420afbda2SDaniel Vetter 
323520afbda2SDaniel Vetter 	return 0;
323620afbda2SDaniel Vetter }
323720afbda2SDaniel Vetter 
3238abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3239abd58f01SBen Widawsky {
3240abd58f01SBen Widawsky 	int i;
3241abd58f01SBen Widawsky 
3242abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3243abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3244abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3245abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3246abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3247abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3248abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3249abd58f01SBen Widawsky 		0,
3250abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3251abd58f01SBen Widawsky 		};
3252abd58f01SBen Widawsky 
3253337ba017SPaulo Zanoni 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++)
325435079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(GT, i, ~gt_interrupts[i], gt_interrupts[i]);
3255abd58f01SBen Widawsky }
3256abd58f01SBen Widawsky 
3257abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3258abd58f01SBen Widawsky {
3259abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
326013b3a0a7SDaniel Vetter 	uint32_t de_pipe_masked = GEN8_PIPE_FLIP_DONE |
32610fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
326230100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
32635c673b60SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
32645c673b60SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN;
3265abd58f01SBen Widawsky 	int pipe;
326613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
326713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
326813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3269abd58f01SBen Widawsky 
3270337ba017SPaulo Zanoni 	for_each_pipe(pipe)
327135079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe, dev_priv->de_irq_mask[pipe],
327235079899SPaulo Zanoni 				  de_pipe_enables);
3273abd58f01SBen Widawsky 
327435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
3275abd58f01SBen Widawsky }
3276abd58f01SBen Widawsky 
3277abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3278abd58f01SBen Widawsky {
3279abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3280abd58f01SBen Widawsky 
3281622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3282622364b6SPaulo Zanoni 
3283abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3284abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3285abd58f01SBen Widawsky 
3286abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3287abd58f01SBen Widawsky 
3288abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3289abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3290abd58f01SBen Widawsky 
3291abd58f01SBen Widawsky 	return 0;
3292abd58f01SBen Widawsky }
3293abd58f01SBen Widawsky 
3294abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3295abd58f01SBen Widawsky {
3296abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3297abd58f01SBen Widawsky 	int pipe;
3298abd58f01SBen Widawsky 
3299abd58f01SBen Widawsky 	if (!dev_priv)
3300abd58f01SBen Widawsky 		return;
3301abd58f01SBen Widawsky 
3302*d4eb6b10SPaulo Zanoni 	intel_hpd_irq_uninstall(dev_priv);
3303*d4eb6b10SPaulo Zanoni 
3304abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3305abd58f01SBen Widawsky 
3306f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 0);
3307f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 1);
3308f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 2);
3309f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 3);
3310abd58f01SBen Widawsky 
3311f86f3fb0SPaulo Zanoni 	for_each_pipe(pipe)
3312f86f3fb0SPaulo Zanoni 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3313abd58f01SBen Widawsky 
3314f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3315f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3316f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
33178f6ff03dSPaulo Zanoni 
33181c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3319abd58f01SBen Widawsky }
3320abd58f01SBen Widawsky 
33217e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
33227e231dbeSJesse Barnes {
33232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3324f8b79e58SImre Deak 	unsigned long irqflags;
33257e231dbeSJesse Barnes 	int pipe;
33267e231dbeSJesse Barnes 
33277e231dbeSJesse Barnes 	if (!dev_priv)
33287e231dbeSJesse Barnes 		return;
33297e231dbeSJesse Barnes 
33303ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3331ac4c16c5SEgbert Eich 
33327e231dbeSJesse Barnes 	for_each_pipe(pipe)
33337e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
33347e231dbeSJesse Barnes 
33357e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
33367e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
33377e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3338f8b79e58SImre Deak 
3339f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3340f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3341f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3342f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3343f8b79e58SImre Deak 
3344f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3345f8b79e58SImre Deak 
33467e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
33477e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
33487e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
33497e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
33507e231dbeSJesse Barnes }
33517e231dbeSJesse Barnes 
3352f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3353036a4a7dSZhenyu Wang {
33542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33554697995bSJesse Barnes 
33564697995bSJesse Barnes 	if (!dev_priv)
33574697995bSJesse Barnes 		return;
33584697995bSJesse Barnes 
33593ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3360ac4c16c5SEgbert Eich 
3361036a4a7dSZhenyu Wang 	I915_WRITE(HWSTAM, 0xffffffff);
3362036a4a7dSZhenyu Wang 
3363f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
33648664281bSPaulo Zanoni 	if (IS_GEN7(dev))
3365c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3366036a4a7dSZhenyu Wang 
33677c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3368192aac1fSKeith Packard 
33691c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3370036a4a7dSZhenyu Wang }
3371036a4a7dSZhenyu Wang 
3372c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3373c2798b19SChris Wilson {
33742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3375c2798b19SChris Wilson 	int pipe;
3376c2798b19SChris Wilson 
3377c2798b19SChris Wilson 	for_each_pipe(pipe)
3378c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3379c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3380c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3381c2798b19SChris Wilson 	POSTING_READ16(IER);
3382c2798b19SChris Wilson }
3383c2798b19SChris Wilson 
3384c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3385c2798b19SChris Wilson {
33862d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3387379ef82dSDaniel Vetter 	unsigned long irqflags;
3388c2798b19SChris Wilson 
3389c2798b19SChris Wilson 	I915_WRITE16(EMR,
3390c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3391c2798b19SChris Wilson 
3392c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3393c2798b19SChris Wilson 	dev_priv->irq_mask =
3394c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3395c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3396c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3397c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3398c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3399c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3400c2798b19SChris Wilson 
3401c2798b19SChris Wilson 	I915_WRITE16(IER,
3402c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3403c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3404c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3405c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3406c2798b19SChris Wilson 	POSTING_READ16(IER);
3407c2798b19SChris Wilson 
3408379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3409379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3410379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3411755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3412755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3413379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3414379ef82dSDaniel Vetter 
3415c2798b19SChris Wilson 	return 0;
3416c2798b19SChris Wilson }
3417c2798b19SChris Wilson 
341890a72f87SVille Syrjälä /*
341990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
342090a72f87SVille Syrjälä  */
342190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
34221f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
342390a72f87SVille Syrjälä {
34242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34251f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
342690a72f87SVille Syrjälä 
342790a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
342890a72f87SVille Syrjälä 		return false;
342990a72f87SVille Syrjälä 
343090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
343190a72f87SVille Syrjälä 		return false;
343290a72f87SVille Syrjälä 
34331f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
343490a72f87SVille Syrjälä 
343590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
343690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
343790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
343890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
343990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
344090a72f87SVille Syrjälä 	 */
344190a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
344290a72f87SVille Syrjälä 		return false;
344390a72f87SVille Syrjälä 
344490a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
344590a72f87SVille Syrjälä 
344690a72f87SVille Syrjälä 	return true;
344790a72f87SVille Syrjälä }
344890a72f87SVille Syrjälä 
3449ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3450c2798b19SChris Wilson {
3451c2798b19SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
34522d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3453c2798b19SChris Wilson 	u16 iir, new_iir;
3454c2798b19SChris Wilson 	u32 pipe_stats[2];
3455c2798b19SChris Wilson 	unsigned long irqflags;
3456c2798b19SChris Wilson 	int pipe;
3457c2798b19SChris Wilson 	u16 flip_mask =
3458c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3459c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3460c2798b19SChris Wilson 
3461c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3462c2798b19SChris Wilson 	if (iir == 0)
3463c2798b19SChris Wilson 		return IRQ_NONE;
3464c2798b19SChris Wilson 
3465c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3466c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3467c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3468c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3469c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3470c2798b19SChris Wilson 		 */
3471c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3472c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
347358174462SMika Kuoppala 			i915_handle_error(dev, false,
347458174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
347558174462SMika Kuoppala 					  iir);
3476c2798b19SChris Wilson 
3477c2798b19SChris Wilson 		for_each_pipe(pipe) {
3478c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3479c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3480c2798b19SChris Wilson 
3481c2798b19SChris Wilson 			/*
3482c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3483c2798b19SChris Wilson 			 */
34842d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3485c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3486c2798b19SChris Wilson 		}
3487c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3488c2798b19SChris Wilson 
3489c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3490c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3491c2798b19SChris Wilson 
3492d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3493c2798b19SChris Wilson 
3494c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3495c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3496c2798b19SChris Wilson 
34974356d586SDaniel Vetter 		for_each_pipe(pipe) {
34981f1c2e24SVille Syrjälä 			int plane = pipe;
34993a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
35001f1c2e24SVille Syrjälä 				plane = !plane;
35011f1c2e24SVille Syrjälä 
35024356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
35031f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
35041f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3505c2798b19SChris Wilson 
35064356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3507277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
35082d9d2b0bSVille Syrjälä 
35092d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
35102d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3511fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
35124356d586SDaniel Vetter 		}
3513c2798b19SChris Wilson 
3514c2798b19SChris Wilson 		iir = new_iir;
3515c2798b19SChris Wilson 	}
3516c2798b19SChris Wilson 
3517c2798b19SChris Wilson 	return IRQ_HANDLED;
3518c2798b19SChris Wilson }
3519c2798b19SChris Wilson 
3520c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3521c2798b19SChris Wilson {
35222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3523c2798b19SChris Wilson 	int pipe;
3524c2798b19SChris Wilson 
3525c2798b19SChris Wilson 	for_each_pipe(pipe) {
3526c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3527c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3528c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3529c2798b19SChris Wilson 	}
3530c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3531c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3532c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3533c2798b19SChris Wilson }
3534c2798b19SChris Wilson 
3535a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3536a266c7d5SChris Wilson {
35372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3538a266c7d5SChris Wilson 	int pipe;
3539a266c7d5SChris Wilson 
3540a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3541a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3542a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3543a266c7d5SChris Wilson 	}
3544a266c7d5SChris Wilson 
354500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3546a266c7d5SChris Wilson 	for_each_pipe(pipe)
3547a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3548a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3549a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3550a266c7d5SChris Wilson 	POSTING_READ(IER);
3551a266c7d5SChris Wilson }
3552a266c7d5SChris Wilson 
3553a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3554a266c7d5SChris Wilson {
35552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
355638bde180SChris Wilson 	u32 enable_mask;
3557379ef82dSDaniel Vetter 	unsigned long irqflags;
3558a266c7d5SChris Wilson 
355938bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
356038bde180SChris Wilson 
356138bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
356238bde180SChris Wilson 	dev_priv->irq_mask =
356338bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
356438bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
356538bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
356638bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
356738bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
356838bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
356938bde180SChris Wilson 
357038bde180SChris Wilson 	enable_mask =
357138bde180SChris Wilson 		I915_ASLE_INTERRUPT |
357238bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
357338bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
357438bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
357538bde180SChris Wilson 		I915_USER_INTERRUPT;
357638bde180SChris Wilson 
3577a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
357820afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
357920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
358020afbda2SDaniel Vetter 
3581a266c7d5SChris Wilson 		/* Enable in IER... */
3582a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3583a266c7d5SChris Wilson 		/* and unmask in IMR */
3584a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3585a266c7d5SChris Wilson 	}
3586a266c7d5SChris Wilson 
3587a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3588a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3589a266c7d5SChris Wilson 	POSTING_READ(IER);
3590a266c7d5SChris Wilson 
3591f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
359220afbda2SDaniel Vetter 
3593379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3594379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3595379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3596755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3597755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3598379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3599379ef82dSDaniel Vetter 
360020afbda2SDaniel Vetter 	return 0;
360120afbda2SDaniel Vetter }
360220afbda2SDaniel Vetter 
360390a72f87SVille Syrjälä /*
360490a72f87SVille Syrjälä  * Returns true when a page flip has completed.
360590a72f87SVille Syrjälä  */
360690a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
360790a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
360890a72f87SVille Syrjälä {
36092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
361090a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
361190a72f87SVille Syrjälä 
361290a72f87SVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
361390a72f87SVille Syrjälä 		return false;
361490a72f87SVille Syrjälä 
361590a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
361690a72f87SVille Syrjälä 		return false;
361790a72f87SVille Syrjälä 
361890a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
361990a72f87SVille Syrjälä 
362090a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
362190a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
362290a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
362390a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
362490a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
362590a72f87SVille Syrjälä 	 */
362690a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
362790a72f87SVille Syrjälä 		return false;
362890a72f87SVille Syrjälä 
362990a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
363090a72f87SVille Syrjälä 
363190a72f87SVille Syrjälä 	return true;
363290a72f87SVille Syrjälä }
363390a72f87SVille Syrjälä 
3634ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3635a266c7d5SChris Wilson {
3636a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
36372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36388291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3639a266c7d5SChris Wilson 	unsigned long irqflags;
364038bde180SChris Wilson 	u32 flip_mask =
364138bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
364238bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
364338bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3644a266c7d5SChris Wilson 
3645a266c7d5SChris Wilson 	iir = I915_READ(IIR);
364638bde180SChris Wilson 	do {
364738bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
36488291ee90SChris Wilson 		bool blc_event = false;
3649a266c7d5SChris Wilson 
3650a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3651a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3652a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3653a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3654a266c7d5SChris Wilson 		 */
3655a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3656a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
365758174462SMika Kuoppala 			i915_handle_error(dev, false,
365858174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
365958174462SMika Kuoppala 					  iir);
3660a266c7d5SChris Wilson 
3661a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3662a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3663a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3664a266c7d5SChris Wilson 
366538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3666a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3667a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
366838bde180SChris Wilson 				irq_received = true;
3669a266c7d5SChris Wilson 			}
3670a266c7d5SChris Wilson 		}
3671a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3672a266c7d5SChris Wilson 
3673a266c7d5SChris Wilson 		if (!irq_received)
3674a266c7d5SChris Wilson 			break;
3675a266c7d5SChris Wilson 
3676a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
367716c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
367816c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
367916c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3680a266c7d5SChris Wilson 
368138bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3682a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3683a266c7d5SChris Wilson 
3684a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3685a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3686a266c7d5SChris Wilson 
3687a266c7d5SChris Wilson 		for_each_pipe(pipe) {
368838bde180SChris Wilson 			int plane = pipe;
36893a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
369038bde180SChris Wilson 				plane = !plane;
36915e2032d4SVille Syrjälä 
369290a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
369390a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
369490a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3695a266c7d5SChris Wilson 
3696a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3697a266c7d5SChris Wilson 				blc_event = true;
36984356d586SDaniel Vetter 
36994356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3700277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37012d9d2b0bSVille Syrjälä 
37022d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
37032d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3704fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
3705a266c7d5SChris Wilson 		}
3706a266c7d5SChris Wilson 
3707a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3708a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3709a266c7d5SChris Wilson 
3710a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3711a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3712a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3713a266c7d5SChris Wilson 		 * we would never get another interrupt.
3714a266c7d5SChris Wilson 		 *
3715a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3716a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3717a266c7d5SChris Wilson 		 * another one.
3718a266c7d5SChris Wilson 		 *
3719a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3720a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3721a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3722a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3723a266c7d5SChris Wilson 		 * stray interrupts.
3724a266c7d5SChris Wilson 		 */
372538bde180SChris Wilson 		ret = IRQ_HANDLED;
3726a266c7d5SChris Wilson 		iir = new_iir;
372738bde180SChris Wilson 	} while (iir & ~flip_mask);
3728a266c7d5SChris Wilson 
3729d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
37308291ee90SChris Wilson 
3731a266c7d5SChris Wilson 	return ret;
3732a266c7d5SChris Wilson }
3733a266c7d5SChris Wilson 
3734a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3735a266c7d5SChris Wilson {
37362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3737a266c7d5SChris Wilson 	int pipe;
3738a266c7d5SChris Wilson 
37393ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3740ac4c16c5SEgbert Eich 
3741a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3742a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3743a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3744a266c7d5SChris Wilson 	}
3745a266c7d5SChris Wilson 
374600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
374755b39755SChris Wilson 	for_each_pipe(pipe) {
374855b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3749a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
375055b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
375155b39755SChris Wilson 	}
3752a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3753a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3754a266c7d5SChris Wilson 
3755a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3756a266c7d5SChris Wilson }
3757a266c7d5SChris Wilson 
3758a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3759a266c7d5SChris Wilson {
37602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3761a266c7d5SChris Wilson 	int pipe;
3762a266c7d5SChris Wilson 
3763a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3764a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3765a266c7d5SChris Wilson 
3766a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3767a266c7d5SChris Wilson 	for_each_pipe(pipe)
3768a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3769a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3770a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3771a266c7d5SChris Wilson 	POSTING_READ(IER);
3772a266c7d5SChris Wilson }
3773a266c7d5SChris Wilson 
3774a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3775a266c7d5SChris Wilson {
37762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3777bbba0a97SChris Wilson 	u32 enable_mask;
3778a266c7d5SChris Wilson 	u32 error_mask;
3779b79480baSDaniel Vetter 	unsigned long irqflags;
3780a266c7d5SChris Wilson 
3781a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3782bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3783adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3784bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3785bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3786bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3787bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3788bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3789bbba0a97SChris Wilson 
3790bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
379121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
379221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3793bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3794bbba0a97SChris Wilson 
3795bbba0a97SChris Wilson 	if (IS_G4X(dev))
3796bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3797a266c7d5SChris Wilson 
3798b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3799b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3800b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3801755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3802755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3803755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3804b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3805a266c7d5SChris Wilson 
3806a266c7d5SChris Wilson 	/*
3807a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3808a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3809a266c7d5SChris Wilson 	 */
3810a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3811a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3812a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3813a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3814a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3815a266c7d5SChris Wilson 	} else {
3816a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3817a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3818a266c7d5SChris Wilson 	}
3819a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
3820a266c7d5SChris Wilson 
3821a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3822a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3823a266c7d5SChris Wilson 	POSTING_READ(IER);
3824a266c7d5SChris Wilson 
382520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
382620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
382720afbda2SDaniel Vetter 
3828f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
382920afbda2SDaniel Vetter 
383020afbda2SDaniel Vetter 	return 0;
383120afbda2SDaniel Vetter }
383220afbda2SDaniel Vetter 
3833bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
383420afbda2SDaniel Vetter {
38352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3836e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3837cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
383820afbda2SDaniel Vetter 	u32 hotplug_en;
383920afbda2SDaniel Vetter 
3840b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
3841b5ea2d56SDaniel Vetter 
3842bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
3843bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
3844bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
3845adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
3846e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
3847cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3848cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3849cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
3850a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
3851a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
3852a266c7d5SChris Wilson 		   seconds later.  So just do it once.
3853a266c7d5SChris Wilson 		*/
3854a266c7d5SChris Wilson 		if (IS_G4X(dev))
3855a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
385685fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
3857a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
3858a266c7d5SChris Wilson 
3859a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
3860a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
3861a266c7d5SChris Wilson 	}
3862bac56d5bSEgbert Eich }
3863a266c7d5SChris Wilson 
3864ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
3865a266c7d5SChris Wilson {
3866a266c7d5SChris Wilson 	struct drm_device *dev = (struct drm_device *) arg;
38672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3868a266c7d5SChris Wilson 	u32 iir, new_iir;
3869a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
3870a266c7d5SChris Wilson 	unsigned long irqflags;
3871a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
387221ad8330SVille Syrjälä 	u32 flip_mask =
387321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
387421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3875a266c7d5SChris Wilson 
3876a266c7d5SChris Wilson 	iir = I915_READ(IIR);
3877a266c7d5SChris Wilson 
3878a266c7d5SChris Wilson 	for (;;) {
3879501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
38802c8ba29fSChris Wilson 		bool blc_event = false;
38812c8ba29fSChris Wilson 
3882a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3883a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3884a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3885a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3886a266c7d5SChris Wilson 		 */
3887a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3888a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
388958174462SMika Kuoppala 			i915_handle_error(dev, false,
389058174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
389158174462SMika Kuoppala 					  iir);
3892a266c7d5SChris Wilson 
3893a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3894a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3895a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3896a266c7d5SChris Wilson 
3897a266c7d5SChris Wilson 			/*
3898a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3899a266c7d5SChris Wilson 			 */
3900a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3901a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3902501e01d7SVille Syrjälä 				irq_received = true;
3903a266c7d5SChris Wilson 			}
3904a266c7d5SChris Wilson 		}
3905a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3906a266c7d5SChris Wilson 
3907a266c7d5SChris Wilson 		if (!irq_received)
3908a266c7d5SChris Wilson 			break;
3909a266c7d5SChris Wilson 
3910a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
3911a266c7d5SChris Wilson 
3912a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
391316c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
391416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3915a266c7d5SChris Wilson 
391621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
3917a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3918a266c7d5SChris Wilson 
3919a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3920a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3921a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
3922a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
3923a266c7d5SChris Wilson 
3924a266c7d5SChris Wilson 		for_each_pipe(pipe) {
39252c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
392690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
392790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
3928a266c7d5SChris Wilson 
3929a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3930a266c7d5SChris Wilson 				blc_event = true;
39314356d586SDaniel Vetter 
39324356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3933277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
3934a266c7d5SChris Wilson 
39352d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
39362d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3937fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
39382d9d2b0bSVille Syrjälä 		}
3939a266c7d5SChris Wilson 
3940a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3941a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3942a266c7d5SChris Wilson 
3943515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
3944515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
3945515ac2bbSDaniel Vetter 
3946a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3947a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3948a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3949a266c7d5SChris Wilson 		 * we would never get another interrupt.
3950a266c7d5SChris Wilson 		 *
3951a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3952a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3953a266c7d5SChris Wilson 		 * another one.
3954a266c7d5SChris Wilson 		 *
3955a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3956a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3957a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3958a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3959a266c7d5SChris Wilson 		 * stray interrupts.
3960a266c7d5SChris Wilson 		 */
3961a266c7d5SChris Wilson 		iir = new_iir;
3962a266c7d5SChris Wilson 	}
3963a266c7d5SChris Wilson 
3964d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
39652c8ba29fSChris Wilson 
3966a266c7d5SChris Wilson 	return ret;
3967a266c7d5SChris Wilson }
3968a266c7d5SChris Wilson 
3969a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
3970a266c7d5SChris Wilson {
39712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3972a266c7d5SChris Wilson 	int pipe;
3973a266c7d5SChris Wilson 
3974a266c7d5SChris Wilson 	if (!dev_priv)
3975a266c7d5SChris Wilson 		return;
3976a266c7d5SChris Wilson 
39773ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3978ac4c16c5SEgbert Eich 
3979a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3980a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3981a266c7d5SChris Wilson 
3982a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
3983a266c7d5SChris Wilson 	for_each_pipe(pipe)
3984a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3985a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3986a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3987a266c7d5SChris Wilson 
3988a266c7d5SChris Wilson 	for_each_pipe(pipe)
3989a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
3990a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
3991a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3992a266c7d5SChris Wilson }
3993a266c7d5SChris Wilson 
39943ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
3995ac4c16c5SEgbert Eich {
39962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = (struct drm_i915_private *)data;
3997ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
3998ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
3999ac4c16c5SEgbert Eich 	unsigned long irqflags;
4000ac4c16c5SEgbert Eich 	int i;
4001ac4c16c5SEgbert Eich 
4002ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4003ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4004ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4005ac4c16c5SEgbert Eich 
4006ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4007ac4c16c5SEgbert Eich 			continue;
4008ac4c16c5SEgbert Eich 
4009ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4010ac4c16c5SEgbert Eich 
4011ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4012ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4013ac4c16c5SEgbert Eich 
4014ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4015ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4016ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4017ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
4018ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4019ac4c16c5SEgbert Eich 				if (!connector->polled)
4020ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4021ac4c16c5SEgbert Eich 			}
4022ac4c16c5SEgbert Eich 		}
4023ac4c16c5SEgbert Eich 	}
4024ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4025ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4026ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4027ac4c16c5SEgbert Eich }
4028ac4c16c5SEgbert Eich 
4029f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4030f71d4af4SJesse Barnes {
40318b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
40328b2e326dSChris Wilson 
40338b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
403499584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4035c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4036a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
40378b2e326dSChris Wilson 
4038a6706b45SDeepak S 	/* Let's track the enabled rps events */
4039a6706b45SDeepak S 	dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4040a6706b45SDeepak S 
404199584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
404299584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
404361bac78eSDaniel Vetter 		    (unsigned long) dev);
40443ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4045ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
404661bac78eSDaniel Vetter 
404797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
40489ee32feaSDaniel Vetter 
40494cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
40504cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
40514cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
40524cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4053f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4054f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4055391f75e2SVille Syrjälä 	} else {
4056391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4057391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4058f71d4af4SJesse Barnes 	}
4059f71d4af4SJesse Barnes 
4060c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4061f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4062f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4063c2baf4b7SVille Syrjälä 	}
4064f71d4af4SJesse Barnes 
40657e231dbeSJesse Barnes 	if (IS_VALLEYVIEW(dev)) {
40667e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
40677e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
40687e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
40697e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
40707e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
40717e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4072fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4073abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4074abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4075abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
4076abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4077abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4078abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4079abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4080abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4081f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4082f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4083f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
4084f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4085f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4086f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4087f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
408882a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4089f71d4af4SJesse Barnes 	} else {
4090c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4091c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4092c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4093c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4094c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4095a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4096a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4097a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4098a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4099a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
410020afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4101c2798b19SChris Wilson 		} else {
4102a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4103a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4104a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4105a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4106bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4107c2798b19SChris Wilson 		}
4108f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4109f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4110f71d4af4SJesse Barnes 	}
4111f71d4af4SJesse Barnes }
411220afbda2SDaniel Vetter 
411320afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
411420afbda2SDaniel Vetter {
411520afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4116821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4117821450c6SEgbert Eich 	struct drm_connector *connector;
4118b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4119821450c6SEgbert Eich 	int i;
412020afbda2SDaniel Vetter 
4121821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4122821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4123821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4124821450c6SEgbert Eich 	}
4125821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4126821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4127821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
4128821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4129821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4130821450c6SEgbert Eich 	}
4131b5ea2d56SDaniel Vetter 
4132b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4133b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4134b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
413520afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
413620afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4137b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
413820afbda2SDaniel Vetter }
4139c67a470bSPaulo Zanoni 
41405d584b2eSPaulo Zanoni /* Disable interrupts so we can allow runtime PM. */
41415d584b2eSPaulo Zanoni void hsw_runtime_pm_disable_interrupts(struct drm_device *dev)
4142c67a470bSPaulo Zanoni {
4143c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4144c67a470bSPaulo Zanoni 	unsigned long irqflags;
4145c67a470bSPaulo Zanoni 
4146c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4147c67a470bSPaulo Zanoni 
41485d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.deimr = I915_READ(DEIMR);
41495d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.sdeimr = I915_READ(SDEIMR);
41505d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.gtimr = I915_READ(GTIMR);
41515d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.gtier = I915_READ(GTIER);
41525d584b2eSPaulo Zanoni 	dev_priv->pm.regsave.gen6_pmimr = I915_READ(GEN6_PMIMR);
4153c67a470bSPaulo Zanoni 
41541f2d4531SPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, 0xffffffff);
41551f2d4531SPaulo Zanoni 	ibx_disable_display_interrupt(dev_priv, 0xffffffff);
4156c67a470bSPaulo Zanoni 	ilk_disable_gt_irq(dev_priv, 0xffffffff);
4157c67a470bSPaulo Zanoni 	snb_disable_pm_irq(dev_priv, 0xffffffff);
4158c67a470bSPaulo Zanoni 
41595d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = true;
4160c67a470bSPaulo Zanoni 
4161c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4162c67a470bSPaulo Zanoni }
4163c67a470bSPaulo Zanoni 
41645d584b2eSPaulo Zanoni /* Restore interrupts so we can recover from runtime PM. */
41655d584b2eSPaulo Zanoni void hsw_runtime_pm_restore_interrupts(struct drm_device *dev)
4166c67a470bSPaulo Zanoni {
4167c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4168c67a470bSPaulo Zanoni 	unsigned long irqflags;
41691f2d4531SPaulo Zanoni 	uint32_t val;
4170c67a470bSPaulo Zanoni 
4171c67a470bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4172c67a470bSPaulo Zanoni 
4173c67a470bSPaulo Zanoni 	val = I915_READ(DEIMR);
41741f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "DEIMR is 0x%08x\n", val);
4175c67a470bSPaulo Zanoni 
41761f2d4531SPaulo Zanoni 	val = I915_READ(SDEIMR);
41771f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "SDEIMR is 0x%08x\n", val);
4178c67a470bSPaulo Zanoni 
4179c67a470bSPaulo Zanoni 	val = I915_READ(GTIMR);
41801f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GTIMR is 0x%08x\n", val);
4181c67a470bSPaulo Zanoni 
4182c67a470bSPaulo Zanoni 	val = I915_READ(GEN6_PMIMR);
41831f2d4531SPaulo Zanoni 	WARN(val != 0xffffffff, "GEN6_PMIMR is 0x%08x\n", val);
4184c67a470bSPaulo Zanoni 
41855d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = false;
4186c67a470bSPaulo Zanoni 
41875d584b2eSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, ~dev_priv->pm.regsave.deimr);
41885d584b2eSPaulo Zanoni 	ibx_enable_display_interrupt(dev_priv, ~dev_priv->pm.regsave.sdeimr);
41895d584b2eSPaulo Zanoni 	ilk_enable_gt_irq(dev_priv, ~dev_priv->pm.regsave.gtimr);
41905d584b2eSPaulo Zanoni 	snb_enable_pm_irq(dev_priv, ~dev_priv->pm.regsave.gen6_pmimr);
41915d584b2eSPaulo Zanoni 	I915_WRITE(GTIER, dev_priv->pm.regsave.gtier);
4192c67a470bSPaulo Zanoni 
4193c67a470bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4194c67a470bSPaulo Zanoni }
4195