xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision cebd87a07748b649f22356efd56f478248aec300)
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 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
129f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139337ba017SPaulo Zanoni /*
140337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
141337ba017SPaulo Zanoni  */
142337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
143337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
144337ba017SPaulo Zanoni 	if (val) { \
145337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
146337ba017SPaulo Zanoni 		     (reg), val); \
147337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
148337ba017SPaulo Zanoni 		POSTING_READ(reg); \
149337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
150337ba017SPaulo Zanoni 		POSTING_READ(reg); \
151337ba017SPaulo Zanoni 	} \
152337ba017SPaulo Zanoni } while (0)
153337ba017SPaulo Zanoni 
15435079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
155337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
15635079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1577d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1587d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
15935079899SPaulo Zanoni } while (0)
16035079899SPaulo Zanoni 
16135079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
162337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
16335079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1647d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1657d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
16635079899SPaulo Zanoni } while (0)
16735079899SPaulo Zanoni 
168c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
169c9a9a268SImre Deak 
170d9dc34f1SVille Syrjälä /**
171d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
172d9dc34f1SVille Syrjälä  * @dev_priv: driver private
173d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
174d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
175d9dc34f1SVille Syrjälä  */
176d9dc34f1SVille Syrjälä static void ilk_update_display_irq(struct drm_i915_private *dev_priv,
177d9dc34f1SVille Syrjälä 				   uint32_t interrupt_mask,
178d9dc34f1SVille Syrjälä 				   uint32_t enabled_irq_mask)
179036a4a7dSZhenyu Wang {
180d9dc34f1SVille Syrjälä 	uint32_t new_val;
181d9dc34f1SVille Syrjälä 
1824bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1834bc9d430SDaniel Vetter 
184d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
185d9dc34f1SVille Syrjälä 
1869df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
187c67a470bSPaulo Zanoni 		return;
188c67a470bSPaulo Zanoni 
189d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
190d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
191d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
192d9dc34f1SVille Syrjälä 
193d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
194d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
1951ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1963143a2bfSChris Wilson 		POSTING_READ(DEIMR);
197036a4a7dSZhenyu Wang 	}
198036a4a7dSZhenyu Wang }
199036a4a7dSZhenyu Wang 
20047339cd9SDaniel Vetter void
201d9dc34f1SVille Syrjälä ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
202d9dc34f1SVille Syrjälä {
203d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, mask);
204d9dc34f1SVille Syrjälä }
205d9dc34f1SVille Syrjälä 
206d9dc34f1SVille Syrjälä void
2072d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
208036a4a7dSZhenyu Wang {
209d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, 0);
210036a4a7dSZhenyu Wang }
211036a4a7dSZhenyu Wang 
21243eaea13SPaulo Zanoni /**
21343eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
21443eaea13SPaulo Zanoni  * @dev_priv: driver private
21543eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
21643eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
21743eaea13SPaulo Zanoni  */
21843eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
21943eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
22043eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
22143eaea13SPaulo Zanoni {
22243eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
22343eaea13SPaulo Zanoni 
22415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
22515a17aaeSDaniel Vetter 
2269df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
227c67a470bSPaulo Zanoni 		return;
228c67a470bSPaulo Zanoni 
22943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
23043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
23143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
23243eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
23343eaea13SPaulo Zanoni }
23443eaea13SPaulo Zanoni 
235480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
23643eaea13SPaulo Zanoni {
23743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
23843eaea13SPaulo Zanoni }
23943eaea13SPaulo Zanoni 
240480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
24143eaea13SPaulo Zanoni {
24243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
24343eaea13SPaulo Zanoni }
24443eaea13SPaulo Zanoni 
245b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
246b900b949SImre Deak {
247b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
248b900b949SImre Deak }
249b900b949SImre Deak 
250a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
251a72fbc3aSImre Deak {
252a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
253a72fbc3aSImre Deak }
254a72fbc3aSImre Deak 
255b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
256b900b949SImre Deak {
257b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
258b900b949SImre Deak }
259b900b949SImre Deak 
260edbfdb45SPaulo Zanoni /**
261edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
262edbfdb45SPaulo Zanoni   * @dev_priv: driver private
263edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
264edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
265edbfdb45SPaulo Zanoni   */
266edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
267edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
268edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
269edbfdb45SPaulo Zanoni {
270605cd25bSPaulo Zanoni 	uint32_t new_val;
271edbfdb45SPaulo Zanoni 
27215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
27315a17aaeSDaniel Vetter 
274edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
275edbfdb45SPaulo Zanoni 
276605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
277f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
278f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
279f52ecbcfSPaulo Zanoni 
280605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
281605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
282a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
283a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
284edbfdb45SPaulo Zanoni 	}
285f52ecbcfSPaulo Zanoni }
286edbfdb45SPaulo Zanoni 
287480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
288edbfdb45SPaulo Zanoni {
2899939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2909939fba2SImre Deak 		return;
2919939fba2SImre Deak 
292edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
293edbfdb45SPaulo Zanoni }
294edbfdb45SPaulo Zanoni 
2959939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
2969939fba2SImre Deak 				  uint32_t mask)
2979939fba2SImre Deak {
2989939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
2999939fba2SImre Deak }
3009939fba2SImre Deak 
301480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
302edbfdb45SPaulo Zanoni {
3039939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3049939fba2SImre Deak 		return;
3059939fba2SImre Deak 
3069939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
307edbfdb45SPaulo Zanoni }
308edbfdb45SPaulo Zanoni 
3093cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3103cc134e3SImre Deak {
3113cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
3123cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
3133cc134e3SImre Deak 
3143cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3153cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3163cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3173cc134e3SImre Deak 	POSTING_READ(reg);
318096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3193cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3203cc134e3SImre Deak }
3213cc134e3SImre Deak 
322b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
323b900b949SImre Deak {
324b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
325b900b949SImre Deak 
326b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
32778e68d36SImre Deak 
328b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3293cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
330d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
33178e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
33278e68d36SImre Deak 				dev_priv->pm_rps_events);
333b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
33478e68d36SImre Deak 
335b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
336b900b949SImre Deak }
337b900b949SImre Deak 
33859d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
33959d02a1fSImre Deak {
34059d02a1fSImre Deak 	/*
341f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
34259d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
343f24eeb19SImre Deak 	 *
344f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
34559d02a1fSImre Deak 	 */
34659d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
34759d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
34859d02a1fSImre Deak 
34959d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
35059d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
35159d02a1fSImre Deak 
35259d02a1fSImre Deak 	return mask;
35359d02a1fSImre Deak }
35459d02a1fSImre Deak 
355b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
356b900b949SImre Deak {
357b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
358b900b949SImre Deak 
359d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
360d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
361d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
362d4d70aa5SImre Deak 
363d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
364d4d70aa5SImre Deak 
3659939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3669939fba2SImre Deak 
36759d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3689939fba2SImre Deak 
3699939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
370b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
371b900b949SImre Deak 				~dev_priv->pm_rps_events);
37258072ccbSImre Deak 
37358072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
37458072ccbSImre Deak 
37558072ccbSImre Deak 	synchronize_irq(dev->irq);
376b900b949SImre Deak }
377b900b949SImre Deak 
3780961021aSBen Widawsky /**
3793a3b3c7dSVille Syrjälä   * bdw_update_port_irq - update DE port interrupt
3803a3b3c7dSVille Syrjälä   * @dev_priv: driver private
3813a3b3c7dSVille Syrjälä   * @interrupt_mask: mask of interrupt bits to update
3823a3b3c7dSVille Syrjälä   * @enabled_irq_mask: mask of interrupt bits to enable
3833a3b3c7dSVille Syrjälä   */
3843a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
3853a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
3863a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
3873a3b3c7dSVille Syrjälä {
3883a3b3c7dSVille Syrjälä 	uint32_t new_val;
3893a3b3c7dSVille Syrjälä 	uint32_t old_val;
3903a3b3c7dSVille Syrjälä 
3913a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
3923a3b3c7dSVille Syrjälä 
3933a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
3943a3b3c7dSVille Syrjälä 
3953a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3963a3b3c7dSVille Syrjälä 		return;
3973a3b3c7dSVille Syrjälä 
3983a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
3993a3b3c7dSVille Syrjälä 
4003a3b3c7dSVille Syrjälä 	new_val = old_val;
4013a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4023a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4033a3b3c7dSVille Syrjälä 
4043a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4053a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4063a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4073a3b3c7dSVille Syrjälä 	}
4083a3b3c7dSVille Syrjälä }
4093a3b3c7dSVille Syrjälä 
4103a3b3c7dSVille Syrjälä /**
411fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
412fee884edSDaniel Vetter  * @dev_priv: driver private
413fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
414fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
415fee884edSDaniel Vetter  */
41647339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
417fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
418fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
419fee884edSDaniel Vetter {
420fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
421fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
422fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
423fee884edSDaniel Vetter 
42415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
42515a17aaeSDaniel Vetter 
426fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
427fee884edSDaniel Vetter 
4289df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
429c67a470bSPaulo Zanoni 		return;
430c67a470bSPaulo Zanoni 
431fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
432fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
433fee884edSDaniel Vetter }
4348664281bSPaulo Zanoni 
435b5ea642aSDaniel Vetter static void
436755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
437755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4387c463586SKeith Packard {
4399db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
440755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4417c463586SKeith Packard 
442b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
443d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
444b79480baSDaniel Vetter 
44504feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
44604feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
44704feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
44804feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
449755e9019SImre Deak 		return;
450755e9019SImre Deak 
451755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
45246c06a30SVille Syrjälä 		return;
45346c06a30SVille Syrjälä 
45491d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
45591d181ddSImre Deak 
4567c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
457755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
45846c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4593143a2bfSChris Wilson 	POSTING_READ(reg);
4607c463586SKeith Packard }
4617c463586SKeith Packard 
462b5ea642aSDaniel Vetter static void
463755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
464755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
4657c463586SKeith Packard {
4669db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
467755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4687c463586SKeith Packard 
469b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
470d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
471b79480baSDaniel Vetter 
47204feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
47304feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
47404feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
47504feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
47646c06a30SVille Syrjälä 		return;
47746c06a30SVille Syrjälä 
478755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
479755e9019SImre Deak 		return;
480755e9019SImre Deak 
48191d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
48291d181ddSImre Deak 
483755e9019SImre Deak 	pipestat &= ~enable_mask;
48446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4853143a2bfSChris Wilson 	POSTING_READ(reg);
4867c463586SKeith Packard }
4877c463586SKeith Packard 
48810c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
48910c59c51SImre Deak {
49010c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
49110c59c51SImre Deak 
49210c59c51SImre Deak 	/*
493724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
494724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
49510c59c51SImre Deak 	 */
49610c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
49710c59c51SImre Deak 		return 0;
498724a6905SVille Syrjälä 	/*
499724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
500724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
501724a6905SVille Syrjälä 	 */
502724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
503724a6905SVille Syrjälä 		return 0;
50410c59c51SImre Deak 
50510c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
50610c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
50710c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
50810c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
50910c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
51010c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
51110c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
51210c59c51SImre Deak 
51310c59c51SImre Deak 	return enable_mask;
51410c59c51SImre Deak }
51510c59c51SImre Deak 
516755e9019SImre Deak void
517755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
518755e9019SImre Deak 		     u32 status_mask)
519755e9019SImre Deak {
520755e9019SImre Deak 	u32 enable_mask;
521755e9019SImre Deak 
52210c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
52310c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
52410c59c51SImre Deak 							   status_mask);
52510c59c51SImre Deak 	else
526755e9019SImre Deak 		enable_mask = status_mask << 16;
527755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
528755e9019SImre Deak }
529755e9019SImre Deak 
530755e9019SImre Deak void
531755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
532755e9019SImre Deak 		      u32 status_mask)
533755e9019SImre Deak {
534755e9019SImre Deak 	u32 enable_mask;
535755e9019SImre Deak 
53610c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
53710c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
53810c59c51SImre Deak 							   status_mask);
53910c59c51SImre Deak 	else
540755e9019SImre Deak 		enable_mask = status_mask << 16;
541755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
542755e9019SImre Deak }
543755e9019SImre Deak 
544c0e09200SDave Airlie /**
545f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
54601c66889SZhao Yakui  */
547f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
54801c66889SZhao Yakui {
5492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5501ec14ad3SChris Wilson 
551f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
552f49e38ddSJani Nikula 		return;
553f49e38ddSJani Nikula 
55413321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
55501c66889SZhao Yakui 
556755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
557a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5583b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
559755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5601ec14ad3SChris Wilson 
56113321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
56201c66889SZhao Yakui }
56301c66889SZhao Yakui 
564f75f3746SVille Syrjälä /*
565f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
566f75f3746SVille Syrjälä  * around the vertical blanking period.
567f75f3746SVille Syrjälä  *
568f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
569f75f3746SVille Syrjälä  *  vblank_start >= 3
570f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
571f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
572f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
573f75f3746SVille Syrjälä  *
574f75f3746SVille Syrjälä  *           start of vblank:
575f75f3746SVille Syrjälä  *           latch double buffered registers
576f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
577f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
578f75f3746SVille Syrjälä  *           |
579f75f3746SVille Syrjälä  *           |          frame start:
580f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
581f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
582f75f3746SVille Syrjälä  *           |          |
583f75f3746SVille Syrjälä  *           |          |  start of vsync:
584f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
585f75f3746SVille Syrjälä  *           |          |  |
586f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
587f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
588f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
589f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
590f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
591f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
592f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
593f75f3746SVille Syrjälä  *       |          |                                         |
594f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
595f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
596f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
597f75f3746SVille Syrjälä  *
598f75f3746SVille Syrjälä  * x  = horizontal active
599f75f3746SVille Syrjälä  * _  = horizontal blanking
600f75f3746SVille Syrjälä  * hs = horizontal sync
601f75f3746SVille Syrjälä  * va = vertical active
602f75f3746SVille Syrjälä  * vb = vertical blanking
603f75f3746SVille Syrjälä  * vs = vertical sync
604f75f3746SVille Syrjälä  * vbs = vblank_start (number)
605f75f3746SVille Syrjälä  *
606f75f3746SVille Syrjälä  * Summary:
607f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
608f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
609f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
610f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
611f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
612f75f3746SVille Syrjälä  */
613f75f3746SVille Syrjälä 
6144cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6154cdb83ecSVille Syrjälä {
6164cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6174cdb83ecSVille Syrjälä 	return 0;
6184cdb83ecSVille Syrjälä }
6194cdb83ecSVille Syrjälä 
62042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
62142f52ef8SKeith Packard  * we use as a pipe index
62242f52ef8SKeith Packard  */
623f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6240a3e67a4SJesse Barnes {
6252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6260a3e67a4SJesse Barnes 	unsigned long high_frame;
6270a3e67a4SJesse Barnes 	unsigned long low_frame;
6280b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
629391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
630391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
631fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
632391f75e2SVille Syrjälä 
6330b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6340b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6350b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6360b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6370b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
638391f75e2SVille Syrjälä 
6390b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6400b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6410b2a8e09SVille Syrjälä 
6420b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6430b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6440b2a8e09SVille Syrjälä 
6459db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6469db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6475eddb70bSChris Wilson 
6480a3e67a4SJesse Barnes 	/*
6490a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6500a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6510a3e67a4SJesse Barnes 	 * register.
6520a3e67a4SJesse Barnes 	 */
6530a3e67a4SJesse Barnes 	do {
6545eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
655391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6565eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6570a3e67a4SJesse Barnes 	} while (high1 != high2);
6580a3e67a4SJesse Barnes 
6595eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
660391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6615eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
662391f75e2SVille Syrjälä 
663391f75e2SVille Syrjälä 	/*
664391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
665391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
666391f75e2SVille Syrjälä 	 * counter against vblank start.
667391f75e2SVille Syrjälä 	 */
668edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6690a3e67a4SJesse Barnes }
6700a3e67a4SJesse Barnes 
671f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6729880b7a5SJesse Barnes {
6732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6749db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6759880b7a5SJesse Barnes 
6769880b7a5SJesse Barnes 	return I915_READ(reg);
6779880b7a5SJesse Barnes }
6789880b7a5SJesse Barnes 
679ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
680ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
681ad3543edSMario Kleiner 
682a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
683a225f079SVille Syrjälä {
684a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
685a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
686fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
687a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
68880715b2fSVille Syrjälä 	int position, vtotal;
689a225f079SVille Syrjälä 
69080715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
691a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
692a225f079SVille Syrjälä 		vtotal /= 2;
693a225f079SVille Syrjälä 
694a225f079SVille Syrjälä 	if (IS_GEN2(dev))
695a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
696a225f079SVille Syrjälä 	else
697a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
698a225f079SVille Syrjälä 
699a225f079SVille Syrjälä 	/*
70080715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
70180715b2fSVille Syrjälä 	 * scanline_offset adjustment.
702a225f079SVille Syrjälä 	 */
70380715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
704a225f079SVille Syrjälä }
705a225f079SVille Syrjälä 
706f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
707abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
708abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
7090af7e4dfSMario Kleiner {
710c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
711c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
712c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
713fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
7143aa18df8SVille Syrjälä 	int position;
71578e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7160af7e4dfSMario Kleiner 	bool in_vbl = true;
7170af7e4dfSMario Kleiner 	int ret = 0;
718ad3543edSMario Kleiner 	unsigned long irqflags;
7190af7e4dfSMario Kleiner 
720fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7210af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7229db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7230af7e4dfSMario Kleiner 		return 0;
7240af7e4dfSMario Kleiner 	}
7250af7e4dfSMario Kleiner 
726c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
72778e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
728c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
729c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
730c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7310af7e4dfSMario Kleiner 
732d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
733d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
734d31faf65SVille Syrjälä 		vbl_end /= 2;
735d31faf65SVille Syrjälä 		vtotal /= 2;
736d31faf65SVille Syrjälä 	}
737d31faf65SVille Syrjälä 
738c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
739c2baf4b7SVille Syrjälä 
740ad3543edSMario Kleiner 	/*
741ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
742ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
743ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
744ad3543edSMario Kleiner 	 */
745ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
746ad3543edSMario Kleiner 
747ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
748ad3543edSMario Kleiner 
749ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
750ad3543edSMario Kleiner 	if (stime)
751ad3543edSMario Kleiner 		*stime = ktime_get();
752ad3543edSMario Kleiner 
7537c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7540af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7550af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7560af7e4dfSMario Kleiner 		 */
757a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
7580af7e4dfSMario Kleiner 	} else {
7590af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
7600af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
7610af7e4dfSMario Kleiner 		 * scanout position.
7620af7e4dfSMario Kleiner 		 */
763ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7640af7e4dfSMario Kleiner 
7653aa18df8SVille Syrjälä 		/* convert to pixel counts */
7663aa18df8SVille Syrjälä 		vbl_start *= htotal;
7673aa18df8SVille Syrjälä 		vbl_end *= htotal;
7683aa18df8SVille Syrjälä 		vtotal *= htotal;
76978e8fc6bSVille Syrjälä 
77078e8fc6bSVille Syrjälä 		/*
7717e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7727e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7737e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7747e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7757e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7767e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7777e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7787e78f1cbSVille Syrjälä 		 */
7797e78f1cbSVille Syrjälä 		if (position >= vtotal)
7807e78f1cbSVille Syrjälä 			position = vtotal - 1;
7817e78f1cbSVille Syrjälä 
7827e78f1cbSVille Syrjälä 		/*
78378e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
78478e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
78578e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
78678e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
78778e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
78878e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
78978e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
79078e8fc6bSVille Syrjälä 		 */
79178e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7923aa18df8SVille Syrjälä 	}
7933aa18df8SVille Syrjälä 
794ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
795ad3543edSMario Kleiner 	if (etime)
796ad3543edSMario Kleiner 		*etime = ktime_get();
797ad3543edSMario Kleiner 
798ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
799ad3543edSMario Kleiner 
800ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
801ad3543edSMario Kleiner 
8023aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8033aa18df8SVille Syrjälä 
8043aa18df8SVille Syrjälä 	/*
8053aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8063aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8073aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8083aa18df8SVille Syrjälä 	 * up since vbl_end.
8093aa18df8SVille Syrjälä 	 */
8103aa18df8SVille Syrjälä 	if (position >= vbl_start)
8113aa18df8SVille Syrjälä 		position -= vbl_end;
8123aa18df8SVille Syrjälä 	else
8133aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8143aa18df8SVille Syrjälä 
8157c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8163aa18df8SVille Syrjälä 		*vpos = position;
8173aa18df8SVille Syrjälä 		*hpos = 0;
8183aa18df8SVille Syrjälä 	} else {
8190af7e4dfSMario Kleiner 		*vpos = position / htotal;
8200af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8210af7e4dfSMario Kleiner 	}
8220af7e4dfSMario Kleiner 
8230af7e4dfSMario Kleiner 	/* In vblank? */
8240af7e4dfSMario Kleiner 	if (in_vbl)
8253d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8260af7e4dfSMario Kleiner 
8270af7e4dfSMario Kleiner 	return ret;
8280af7e4dfSMario Kleiner }
8290af7e4dfSMario Kleiner 
830a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
831a225f079SVille Syrjälä {
832a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
833a225f079SVille Syrjälä 	unsigned long irqflags;
834a225f079SVille Syrjälä 	int position;
835a225f079SVille Syrjälä 
836a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
837a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
838a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
839a225f079SVille Syrjälä 
840a225f079SVille Syrjälä 	return position;
841a225f079SVille Syrjälä }
842a225f079SVille Syrjälä 
843f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8440af7e4dfSMario Kleiner 			      int *max_error,
8450af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8460af7e4dfSMario Kleiner 			      unsigned flags)
8470af7e4dfSMario Kleiner {
8484041b853SChris Wilson 	struct drm_crtc *crtc;
8490af7e4dfSMario Kleiner 
8507eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8514041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8520af7e4dfSMario Kleiner 		return -EINVAL;
8530af7e4dfSMario Kleiner 	}
8540af7e4dfSMario Kleiner 
8550af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
8564041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
8574041b853SChris Wilson 	if (crtc == NULL) {
8584041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8594041b853SChris Wilson 		return -EINVAL;
8604041b853SChris Wilson 	}
8614041b853SChris Wilson 
862fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
8634041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8644041b853SChris Wilson 		return -EBUSY;
8654041b853SChris Wilson 	}
8660af7e4dfSMario Kleiner 
8670af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8684041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8694041b853SChris Wilson 						     vblank_time, flags,
8707da903efSVille Syrjälä 						     crtc,
871fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
8720af7e4dfSMario Kleiner }
8730af7e4dfSMario Kleiner 
874d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
875f97108d1SJesse Barnes {
8762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
877b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8789270388eSDaniel Vetter 	u8 new_delay;
8799270388eSDaniel Vetter 
880d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
881f97108d1SJesse Barnes 
88273edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
88373edd18fSDaniel Vetter 
88420e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8859270388eSDaniel Vetter 
8867648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
887b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
888b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
889f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
890f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
891f97108d1SJesse Barnes 
892f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
893b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
89420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
89520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
89620e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
89720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
898b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
89920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
90020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
90120e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
90220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
903f97108d1SJesse Barnes 	}
904f97108d1SJesse Barnes 
9057648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
90620e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
907f97108d1SJesse Barnes 
908d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9099270388eSDaniel Vetter 
910f97108d1SJesse Barnes 	return;
911f97108d1SJesse Barnes }
912f97108d1SJesse Barnes 
91374cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
914549f7365SChris Wilson {
91593b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
916475553deSChris Wilson 		return;
917475553deSChris Wilson 
918bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9199862e600SChris Wilson 
920549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
921549f7365SChris Wilson }
922549f7365SChris Wilson 
92343cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
92443cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
92531685c25SDeepak S {
92643cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
92743cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
92843cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
92931685c25SDeepak S }
93031685c25SDeepak S 
93143cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
93243cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
93343cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
93443cf3bf0SChris Wilson 			 int threshold)
93531685c25SDeepak S {
93643cf3bf0SChris Wilson 	u64 time, c0;
93731685c25SDeepak S 
93843cf3bf0SChris Wilson 	if (old->cz_clock == 0)
93943cf3bf0SChris Wilson 		return false;
94031685c25SDeepak S 
94143cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
94243cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
94331685c25SDeepak S 
94443cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
94543cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
94643cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
94743cf3bf0SChris Wilson 	 */
94843cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
94943cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
95043cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
95131685c25SDeepak S 
95243cf3bf0SChris Wilson 	return c0 >= time;
95331685c25SDeepak S }
95431685c25SDeepak S 
95543cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
95643cf3bf0SChris Wilson {
95743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
95843cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
95943cf3bf0SChris Wilson }
96043cf3bf0SChris Wilson 
96143cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
96243cf3bf0SChris Wilson {
96343cf3bf0SChris Wilson 	struct intel_rps_ei now;
96443cf3bf0SChris Wilson 	u32 events = 0;
96543cf3bf0SChris Wilson 
9666f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
96743cf3bf0SChris Wilson 		return 0;
96843cf3bf0SChris Wilson 
96943cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
97043cf3bf0SChris Wilson 	if (now.cz_clock == 0)
97143cf3bf0SChris Wilson 		return 0;
97231685c25SDeepak S 
97343cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
97443cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
97543cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
9768fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
97743cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
97843cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
97931685c25SDeepak S 	}
98031685c25SDeepak S 
98143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
98243cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
98343cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
9848fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
98543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
98643cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
98743cf3bf0SChris Wilson 	}
98843cf3bf0SChris Wilson 
98943cf3bf0SChris Wilson 	return events;
99031685c25SDeepak S }
99131685c25SDeepak S 
992f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
993f5a4c67dSChris Wilson {
994f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
995f5a4c67dSChris Wilson 	int i;
996f5a4c67dSChris Wilson 
997f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
998f5a4c67dSChris Wilson 		if (ring->irq_refcount)
999f5a4c67dSChris Wilson 			return true;
1000f5a4c67dSChris Wilson 
1001f5a4c67dSChris Wilson 	return false;
1002f5a4c67dSChris Wilson }
1003f5a4c67dSChris Wilson 
10044912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10053b8d8d91SJesse Barnes {
10062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10072d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10088d3afd7dSChris Wilson 	bool client_boost;
10098d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1010edbfdb45SPaulo Zanoni 	u32 pm_iir;
10113b8d8d91SJesse Barnes 
101259cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1013d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1014d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1015d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1016d4d70aa5SImre Deak 		return;
1017d4d70aa5SImre Deak 	}
1018c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1019c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1020a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1021480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10228d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10238d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
102459cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10254912d041SBen Widawsky 
102660611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1027a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
102860611c13SPaulo Zanoni 
10298d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
10303b8d8d91SJesse Barnes 		return;
10313b8d8d91SJesse Barnes 
10324fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10337b9e0ae6SChris Wilson 
103443cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
103543cf3bf0SChris Wilson 
1036dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1037edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
10388d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
10398d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
10408d3afd7dSChris Wilson 
10418d3afd7dSChris Wilson 	if (client_boost) {
10428d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
10438d3afd7dSChris Wilson 		adj = 0;
10448d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1045dd75fdc8SChris Wilson 		if (adj > 0)
1046dd75fdc8SChris Wilson 			adj *= 2;
1047edcf284bSChris Wilson 		else /* CHV needs even encode values */
1048edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
10497425034aSVille Syrjälä 		/*
10507425034aSVille Syrjälä 		 * For better performance, jump directly
10517425034aSVille Syrjälä 		 * to RPe if we're below it.
10527425034aSVille Syrjälä 		 */
1053edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1054b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1055edcf284bSChris Wilson 			adj = 0;
1056edcf284bSChris Wilson 		}
1057f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1058f5a4c67dSChris Wilson 		adj = 0;
1059dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1060b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1061b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1062dd75fdc8SChris Wilson 		else
1063b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1064dd75fdc8SChris Wilson 		adj = 0;
1065dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1066dd75fdc8SChris Wilson 		if (adj < 0)
1067dd75fdc8SChris Wilson 			adj *= 2;
1068edcf284bSChris Wilson 		else /* CHV needs even encode values */
1069edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1070dd75fdc8SChris Wilson 	} else { /* unknown event */
1071edcf284bSChris Wilson 		adj = 0;
1072dd75fdc8SChris Wilson 	}
10733b8d8d91SJesse Barnes 
1074edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1075edcf284bSChris Wilson 
107679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
107779249636SBen Widawsky 	 * interrupt
107879249636SBen Widawsky 	 */
1079edcf284bSChris Wilson 	new_delay += adj;
10808d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
108127544369SDeepak S 
1082ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
10833b8d8d91SJesse Barnes 
10844fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
10853b8d8d91SJesse Barnes }
10863b8d8d91SJesse Barnes 
1087e3689190SBen Widawsky 
1088e3689190SBen Widawsky /**
1089e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1090e3689190SBen Widawsky  * occurred.
1091e3689190SBen Widawsky  * @work: workqueue struct
1092e3689190SBen Widawsky  *
1093e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1094e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1095e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1096e3689190SBen Widawsky  */
1097e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1098e3689190SBen Widawsky {
10992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11002d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1101e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
110235a85ac6SBen Widawsky 	char *parity_event[6];
1103e3689190SBen Widawsky 	uint32_t misccpctl;
110435a85ac6SBen Widawsky 	uint8_t slice = 0;
1105e3689190SBen Widawsky 
1106e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1107e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1108e3689190SBen Widawsky 	 * any time we access those registers.
1109e3689190SBen Widawsky 	 */
1110e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1111e3689190SBen Widawsky 
111235a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
111335a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
111435a85ac6SBen Widawsky 		goto out;
111535a85ac6SBen Widawsky 
1116e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1117e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1118e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1119e3689190SBen Widawsky 
112035a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
112135a85ac6SBen Widawsky 		u32 reg;
112235a85ac6SBen Widawsky 
112335a85ac6SBen Widawsky 		slice--;
112435a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
112535a85ac6SBen Widawsky 			break;
112635a85ac6SBen Widawsky 
112735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
112835a85ac6SBen Widawsky 
112935a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
113035a85ac6SBen Widawsky 
113135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1132e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1133e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1134e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1135e3689190SBen Widawsky 
113635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
113735a85ac6SBen Widawsky 		POSTING_READ(reg);
1138e3689190SBen Widawsky 
1139cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1140e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1141e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1142e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
114335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
114435a85ac6SBen Widawsky 		parity_event[5] = NULL;
1145e3689190SBen Widawsky 
11465bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1147e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1148e3689190SBen Widawsky 
114935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
115035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1151e3689190SBen Widawsky 
115235a85ac6SBen Widawsky 		kfree(parity_event[4]);
1153e3689190SBen Widawsky 		kfree(parity_event[3]);
1154e3689190SBen Widawsky 		kfree(parity_event[2]);
1155e3689190SBen Widawsky 		kfree(parity_event[1]);
1156e3689190SBen Widawsky 	}
1157e3689190SBen Widawsky 
115835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
115935a85ac6SBen Widawsky 
116035a85ac6SBen Widawsky out:
116135a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
11624cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1163480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
11644cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
116535a85ac6SBen Widawsky 
116635a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
116735a85ac6SBen Widawsky }
116835a85ac6SBen Widawsky 
116935a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1170e3689190SBen Widawsky {
11712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1172e3689190SBen Widawsky 
1173040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1174e3689190SBen Widawsky 		return;
1175e3689190SBen Widawsky 
1176d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1177480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1178d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1179e3689190SBen Widawsky 
118035a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
118135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
118235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
118335a85ac6SBen Widawsky 
118435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
118535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
118635a85ac6SBen Widawsky 
1187a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1188e3689190SBen Widawsky }
1189e3689190SBen Widawsky 
1190f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1191f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1192f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1193f1af8fc1SPaulo Zanoni {
1194f1af8fc1SPaulo Zanoni 	if (gt_iir &
1195f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
119674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1197f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
119874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1199f1af8fc1SPaulo Zanoni }
1200f1af8fc1SPaulo Zanoni 
1201e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1202e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1203e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1204e7b4c6b1SDaniel Vetter {
1205e7b4c6b1SDaniel Vetter 
1206cc609d5dSBen Widawsky 	if (gt_iir &
1207cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
120874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1209cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
121074cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1211cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
121274cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1213e7b4c6b1SDaniel Vetter 
1214cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1215cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1216aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1217aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1218e3689190SBen Widawsky 
121935a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
122035a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1221e7b4c6b1SDaniel Vetter }
1222e7b4c6b1SDaniel Vetter 
122374cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1224abd58f01SBen Widawsky 				       u32 master_ctl)
1225abd58f01SBen Widawsky {
1226abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1227abd58f01SBen Widawsky 
1228abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
122974cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1230abd58f01SBen Widawsky 		if (tmp) {
1231cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1232abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1233e981e7b1SThomas Daniel 
123474cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
123574cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
123674cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
123774cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1238e981e7b1SThomas Daniel 
123974cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
124074cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
124174cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
124274cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1243abd58f01SBen Widawsky 		} else
1244abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1245abd58f01SBen Widawsky 	}
1246abd58f01SBen Widawsky 
124785f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
124874cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1249abd58f01SBen Widawsky 		if (tmp) {
1250cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1251abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1252e981e7b1SThomas Daniel 
125374cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
125474cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
125574cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
125674cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1257e981e7b1SThomas Daniel 
125874cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
125974cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
126074cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
126174cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1262abd58f01SBen Widawsky 		} else
1263abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1264abd58f01SBen Widawsky 	}
1265abd58f01SBen Widawsky 
126674cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
126774cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
126874cdb337SChris Wilson 		if (tmp) {
126974cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
127074cdb337SChris Wilson 			ret = IRQ_HANDLED;
127174cdb337SChris Wilson 
127274cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
127374cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
127474cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
127574cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
127674cdb337SChris Wilson 		} else
127774cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
127874cdb337SChris Wilson 	}
127974cdb337SChris Wilson 
12800961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
128174cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
12820961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1283cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
12840961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
128538cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1286c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
12870961021aSBen Widawsky 		} else
12880961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
12890961021aSBen Widawsky 	}
12900961021aSBen Widawsky 
1291abd58f01SBen Widawsky 	return ret;
1292abd58f01SBen Widawsky }
1293abd58f01SBen Widawsky 
129463c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
129563c88d22SImre Deak {
129663c88d22SImre Deak 	switch (port) {
129763c88d22SImre Deak 	case PORT_A:
1298195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
129963c88d22SImre Deak 	case PORT_B:
130063c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
130163c88d22SImre Deak 	case PORT_C:
130263c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
130363c88d22SImre Deak 	case PORT_D:
130463c88d22SImre Deak 		return val & PORTD_HOTPLUG_LONG_DETECT;
130563c88d22SImre Deak 	default:
130663c88d22SImre Deak 		return false;
130763c88d22SImre Deak 	}
130863c88d22SImre Deak }
130963c88d22SImre Deak 
13106dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13116dbf30ceSVille Syrjälä {
13126dbf30ceSVille Syrjälä 	switch (port) {
13136dbf30ceSVille Syrjälä 	case PORT_E:
13146dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13156dbf30ceSVille Syrjälä 	default:
13166dbf30ceSVille Syrjälä 		return false;
13176dbf30ceSVille Syrjälä 	}
13186dbf30ceSVille Syrjälä }
13196dbf30ceSVille Syrjälä 
132074c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
132174c0b395SVille Syrjälä {
132274c0b395SVille Syrjälä 	switch (port) {
132374c0b395SVille Syrjälä 	case PORT_A:
132474c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
132574c0b395SVille Syrjälä 	case PORT_B:
132674c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
132774c0b395SVille Syrjälä 	case PORT_C:
132874c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
132974c0b395SVille Syrjälä 	case PORT_D:
133074c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
133174c0b395SVille Syrjälä 	default:
133274c0b395SVille Syrjälä 		return false;
133374c0b395SVille Syrjälä 	}
133474c0b395SVille Syrjälä }
133574c0b395SVille Syrjälä 
1336e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1337e4ce95aaSVille Syrjälä {
1338e4ce95aaSVille Syrjälä 	switch (port) {
1339e4ce95aaSVille Syrjälä 	case PORT_A:
1340e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1341e4ce95aaSVille Syrjälä 	default:
1342e4ce95aaSVille Syrjälä 		return false;
1343e4ce95aaSVille Syrjälä 	}
1344e4ce95aaSVille Syrjälä }
1345e4ce95aaSVille Syrjälä 
1346676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
134713cf5504SDave Airlie {
134813cf5504SDave Airlie 	switch (port) {
134913cf5504SDave Airlie 	case PORT_B:
1350676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
135113cf5504SDave Airlie 	case PORT_C:
1352676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
135313cf5504SDave Airlie 	case PORT_D:
1354676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1355676574dfSJani Nikula 	default:
1356676574dfSJani Nikula 		return false;
135713cf5504SDave Airlie 	}
135813cf5504SDave Airlie }
135913cf5504SDave Airlie 
1360676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
136113cf5504SDave Airlie {
136213cf5504SDave Airlie 	switch (port) {
136313cf5504SDave Airlie 	case PORT_B:
1364676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
136513cf5504SDave Airlie 	case PORT_C:
1366676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
136713cf5504SDave Airlie 	case PORT_D:
1368676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1369676574dfSJani Nikula 	default:
1370676574dfSJani Nikula 		return false;
137113cf5504SDave Airlie 	}
137213cf5504SDave Airlie }
137313cf5504SDave Airlie 
137442db67d6SVille Syrjälä /*
137542db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
137642db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
137742db67d6SVille Syrjälä  * hotplug detection results from several registers.
137842db67d6SVille Syrjälä  *
137942db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
138042db67d6SVille Syrjälä  */
1381fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
13828c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1383fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1384fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1385676574dfSJani Nikula {
13868c841e57SJani Nikula 	enum port port;
1387676574dfSJani Nikula 	int i;
1388676574dfSJani Nikula 
1389676574dfSJani Nikula 	for_each_hpd_pin(i) {
13908c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
13918c841e57SJani Nikula 			continue;
13928c841e57SJani Nikula 
1393676574dfSJani Nikula 		*pin_mask |= BIT(i);
1394676574dfSJani Nikula 
1395cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1396cc24fcdcSImre Deak 			continue;
1397cc24fcdcSImre Deak 
1398fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1399676574dfSJani Nikula 			*long_mask |= BIT(i);
1400676574dfSJani Nikula 	}
1401676574dfSJani Nikula 
1402676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1403676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1404676574dfSJani Nikula 
1405676574dfSJani Nikula }
1406676574dfSJani Nikula 
1407515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1408515ac2bbSDaniel Vetter {
14092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
141028c70f16SDaniel Vetter 
141128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1412515ac2bbSDaniel Vetter }
1413515ac2bbSDaniel Vetter 
1414ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1415ce99c256SDaniel Vetter {
14162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
14179ee32feaSDaniel Vetter 
14189ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1419ce99c256SDaniel Vetter }
1420ce99c256SDaniel Vetter 
14218bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1422277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1423eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1424eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14258bc5e955SDaniel Vetter 					 uint32_t crc4)
14268bf1e9f1SShuang He {
14278bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14288bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14298bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1430ac2300d4SDamien Lespiau 	int head, tail;
1431b2c88f5bSDamien Lespiau 
1432d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1433d538bbdfSDamien Lespiau 
14340c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1435d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
143634273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
14370c912c79SDamien Lespiau 		return;
14380c912c79SDamien Lespiau 	}
14390c912c79SDamien Lespiau 
1440d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1441d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1442b2c88f5bSDamien Lespiau 
1443b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1444d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1445b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1446b2c88f5bSDamien Lespiau 		return;
1447b2c88f5bSDamien Lespiau 	}
1448b2c88f5bSDamien Lespiau 
1449b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
14508bf1e9f1SShuang He 
14518bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1452eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1453eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1454eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1455eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1456eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1457b2c88f5bSDamien Lespiau 
1458b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1459d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1460d538bbdfSDamien Lespiau 
1461d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
146207144428SDamien Lespiau 
146307144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
14648bf1e9f1SShuang He }
1465277de95eSDaniel Vetter #else
1466277de95eSDaniel Vetter static inline void
1467277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1468277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1469277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1470277de95eSDaniel Vetter 			     uint32_t crc4) {}
1471277de95eSDaniel Vetter #endif
1472eba94eb9SDaniel Vetter 
1473277de95eSDaniel Vetter 
1474277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14755a69b89fSDaniel Vetter {
14765a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14775a69b89fSDaniel Vetter 
1478277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14795a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
14805a69b89fSDaniel Vetter 				     0, 0, 0, 0);
14815a69b89fSDaniel Vetter }
14825a69b89fSDaniel Vetter 
1483277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1484eba94eb9SDaniel Vetter {
1485eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1486eba94eb9SDaniel Vetter 
1487277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1488eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1489eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1490eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1491eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
14928bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1493eba94eb9SDaniel Vetter }
14945b3a856bSDaniel Vetter 
1495277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14965b3a856bSDaniel Vetter {
14975b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14980b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
14990b5c5ed0SDaniel Vetter 
15000b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15010b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15020b5c5ed0SDaniel Vetter 	else
15030b5c5ed0SDaniel Vetter 		res1 = 0;
15040b5c5ed0SDaniel Vetter 
15050b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
15060b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15070b5c5ed0SDaniel Vetter 	else
15080b5c5ed0SDaniel Vetter 		res2 = 0;
15095b3a856bSDaniel Vetter 
1510277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15110b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15120b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15130b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15140b5c5ed0SDaniel Vetter 				     res1, res2);
15155b3a856bSDaniel Vetter }
15168bf1e9f1SShuang He 
15171403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15181403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15191403c0d4SPaulo Zanoni  * the work queue. */
15201403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1521baf02a1fSBen Widawsky {
1522a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
152359cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1524480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1525d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1526d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
15272adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
152841a05a3aSDaniel Vetter 		}
1529d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1530d4d70aa5SImre Deak 	}
1531baf02a1fSBen Widawsky 
1532c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1533c9a9a268SImre Deak 		return;
1534c9a9a268SImre Deak 
15351403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
153612638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
153774cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
153812638c57SBen Widawsky 
1539aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1540aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
154112638c57SBen Widawsky 	}
15421403c0d4SPaulo Zanoni }
1543baf02a1fSBen Widawsky 
15448d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
15458d7849dbSVille Syrjälä {
15468d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
15478d7849dbSVille Syrjälä 		return false;
15488d7849dbSVille Syrjälä 
15498d7849dbSVille Syrjälä 	return true;
15508d7849dbSVille Syrjälä }
15518d7849dbSVille Syrjälä 
1552c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
15537e231dbeSJesse Barnes {
1554c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
155591d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
15567e231dbeSJesse Barnes 	int pipe;
15577e231dbeSJesse Barnes 
155858ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1559055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
156091d181ddSImre Deak 		int reg;
1561bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
156291d181ddSImre Deak 
1563bbb5eebfSDaniel Vetter 		/*
1564bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1565bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1566bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1567bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1568bbb5eebfSDaniel Vetter 		 * handle.
1569bbb5eebfSDaniel Vetter 		 */
15700f239f4cSDaniel Vetter 
15710f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
15720f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1573bbb5eebfSDaniel Vetter 
1574bbb5eebfSDaniel Vetter 		switch (pipe) {
1575bbb5eebfSDaniel Vetter 		case PIPE_A:
1576bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1577bbb5eebfSDaniel Vetter 			break;
1578bbb5eebfSDaniel Vetter 		case PIPE_B:
1579bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1580bbb5eebfSDaniel Vetter 			break;
15813278f67fSVille Syrjälä 		case PIPE_C:
15823278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
15833278f67fSVille Syrjälä 			break;
1584bbb5eebfSDaniel Vetter 		}
1585bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1586bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1587bbb5eebfSDaniel Vetter 
1588bbb5eebfSDaniel Vetter 		if (!mask)
158991d181ddSImre Deak 			continue;
159091d181ddSImre Deak 
159191d181ddSImre Deak 		reg = PIPESTAT(pipe);
1592bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1593bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
15947e231dbeSJesse Barnes 
15957e231dbeSJesse Barnes 		/*
15967e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
15977e231dbeSJesse Barnes 		 */
159891d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
159991d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16007e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16017e231dbeSJesse Barnes 	}
160258ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16037e231dbeSJesse Barnes 
1604055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1605d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1606d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1607d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
160831acc7f5SJesse Barnes 
1609579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
161031acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
161131acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
161231acc7f5SJesse Barnes 		}
16134356d586SDaniel Vetter 
16144356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1615277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
16162d9d2b0bSVille Syrjälä 
16171f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16181f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
161931acc7f5SJesse Barnes 	}
162031acc7f5SJesse Barnes 
1621c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1622c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1623c1874ed7SImre Deak }
1624c1874ed7SImre Deak 
162516c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
162616c6c56bSVille Syrjälä {
162716c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
162816c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
162942db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
163016c6c56bSVille Syrjälä 
16310d2e4297SJani Nikula 	if (!hotplug_status)
16320d2e4297SJani Nikula 		return;
16330d2e4297SJani Nikula 
16343ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
16353ff60f89SOscar Mateo 	/*
16363ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
16373ff60f89SOscar Mateo 	 * may miss hotplug events.
16383ff60f89SOscar Mateo 	 */
16393ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
16403ff60f89SOscar Mateo 
16414bca26d0SVille Syrjälä 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
164216c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
164316c6c56bSVille Syrjälä 
1644fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1645fd63e2a9SImre Deak 				   hotplug_trigger, hpd_status_g4x,
1646fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
1647676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1648369712e8SJani Nikula 
1649369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1650369712e8SJani Nikula 			dp_aux_irq_handler(dev);
165116c6c56bSVille Syrjälä 	} else {
165216c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
165316c6c56bSVille Syrjälä 
1654fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1655fd63e2a9SImre Deak 				   hotplug_trigger, hpd_status_g4x,
1656fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
1657676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
165816c6c56bSVille Syrjälä 	}
16593ff60f89SOscar Mateo }
166016c6c56bSVille Syrjälä 
1661c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1662c1874ed7SImre Deak {
166345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
16642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1665c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1666c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1667c1874ed7SImre Deak 
16682dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16692dd2a883SImre Deak 		return IRQ_NONE;
16702dd2a883SImre Deak 
1671c1874ed7SImre Deak 	while (true) {
16723ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
16733ff60f89SOscar Mateo 
1674c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
16753ff60f89SOscar Mateo 		if (gt_iir)
16763ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
16773ff60f89SOscar Mateo 
1678c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
16793ff60f89SOscar Mateo 		if (pm_iir)
16803ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
16813ff60f89SOscar Mateo 
16823ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
16833ff60f89SOscar Mateo 		if (iir) {
16843ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
16853ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
16863ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
16873ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
16883ff60f89SOscar Mateo 		}
1689c1874ed7SImre Deak 
1690c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1691c1874ed7SImre Deak 			goto out;
1692c1874ed7SImre Deak 
1693c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1694c1874ed7SImre Deak 
16953ff60f89SOscar Mateo 		if (gt_iir)
1696c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
169760611c13SPaulo Zanoni 		if (pm_iir)
1698d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
16993ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17003ff60f89SOscar Mateo 		 * signalled in iir */
17013ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
17027e231dbeSJesse Barnes 	}
17037e231dbeSJesse Barnes 
17047e231dbeSJesse Barnes out:
17057e231dbeSJesse Barnes 	return ret;
17067e231dbeSJesse Barnes }
17077e231dbeSJesse Barnes 
170843f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
170943f328d7SVille Syrjälä {
171045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
171143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
171243f328d7SVille Syrjälä 	u32 master_ctl, iir;
171343f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
171443f328d7SVille Syrjälä 
17152dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17162dd2a883SImre Deak 		return IRQ_NONE;
17172dd2a883SImre Deak 
17188e5fd599SVille Syrjälä 	for (;;) {
17198e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17203278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
17213278f67fSVille Syrjälä 
17223278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17238e5fd599SVille Syrjälä 			break;
172443f328d7SVille Syrjälä 
172527b6c122SOscar Mateo 		ret = IRQ_HANDLED;
172627b6c122SOscar Mateo 
172743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
172843f328d7SVille Syrjälä 
172927b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
173027b6c122SOscar Mateo 
173127b6c122SOscar Mateo 		if (iir) {
173227b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
173327b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
173427b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
173527b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
173627b6c122SOscar Mateo 		}
173727b6c122SOscar Mateo 
173874cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
173943f328d7SVille Syrjälä 
174027b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
174127b6c122SOscar Mateo 		 * signalled in iir */
17423278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
174343f328d7SVille Syrjälä 
174443f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
174543f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
17468e5fd599SVille Syrjälä 	}
17473278f67fSVille Syrjälä 
174843f328d7SVille Syrjälä 	return ret;
174943f328d7SVille Syrjälä }
175043f328d7SVille Syrjälä 
175123e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1752776ad806SJesse Barnes {
17532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
17549db4a9c7SJesse Barnes 	int pipe;
1755b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1756aaf5ec2eSSonika Jindal 
1757aaf5ec2eSSonika Jindal 	if (hotplug_trigger) {
175842db67d6SVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1759776ad806SJesse Barnes 
176013cf5504SDave Airlie 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
176113cf5504SDave Airlie 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
176213cf5504SDave Airlie 
1763fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1764fd63e2a9SImre Deak 				   dig_hotplug_reg, hpd_ibx,
1765fd63e2a9SImre Deak 				   pch_port_hotplug_long_detect);
1766676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1767aaf5ec2eSSonika Jindal 	}
176891d131d2SDaniel Vetter 
1769cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1770cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1771776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1772cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1773cfc33bf7SVille Syrjälä 				 port_name(port));
1774cfc33bf7SVille Syrjälä 	}
1775776ad806SJesse Barnes 
1776ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1777ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1778ce99c256SDaniel Vetter 
1779776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1780515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1781776ad806SJesse Barnes 
1782776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1783776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1784776ad806SJesse Barnes 
1785776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1786776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1787776ad806SJesse Barnes 
1788776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1789776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1790776ad806SJesse Barnes 
17919db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1792055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
17939db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17949db4a9c7SJesse Barnes 					 pipe_name(pipe),
17959db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1796776ad806SJesse Barnes 
1797776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1798776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1799776ad806SJesse Barnes 
1800776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1801776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1802776ad806SJesse Barnes 
1803776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18041f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18058664281bSPaulo Zanoni 
18068664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
18071f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18088664281bSPaulo Zanoni }
18098664281bSPaulo Zanoni 
18108664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
18118664281bSPaulo Zanoni {
18128664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18138664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
18145a69b89fSDaniel Vetter 	enum pipe pipe;
18158664281bSPaulo Zanoni 
1816de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1817de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1818de032bf4SPaulo Zanoni 
1819055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18201f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18211f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
18228664281bSPaulo Zanoni 
18235a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
18245a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1825277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
18265a69b89fSDaniel Vetter 			else
1827277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
18285a69b89fSDaniel Vetter 		}
18295a69b89fSDaniel Vetter 	}
18308bf1e9f1SShuang He 
18318664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
18328664281bSPaulo Zanoni }
18338664281bSPaulo Zanoni 
18348664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
18358664281bSPaulo Zanoni {
18368664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18378664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
18388664281bSPaulo Zanoni 
1839de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1840de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1841de032bf4SPaulo Zanoni 
18428664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
18431f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18448664281bSPaulo Zanoni 
18458664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
18461f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18478664281bSPaulo Zanoni 
18488664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
18491f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
18508664281bSPaulo Zanoni 
18518664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1852776ad806SJesse Barnes }
1853776ad806SJesse Barnes 
185423e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
185523e81d69SAdam Jackson {
18562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
185723e81d69SAdam Jackson 	int pipe;
18586dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1859aaf5ec2eSSonika Jindal 
1860aaf5ec2eSSonika Jindal 	if (hotplug_trigger) {
186142db67d6SVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
186223e81d69SAdam Jackson 
186313cf5504SDave Airlie 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
186413cf5504SDave Airlie 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
1865fd63e2a9SImre Deak 
186626951cafSXiong Zhang 		intel_get_hpd_pins(&pin_mask, &long_mask,
186726951cafSXiong Zhang 				   hotplug_trigger,
1868fd63e2a9SImre Deak 				   dig_hotplug_reg, hpd_cpt,
1869fd63e2a9SImre Deak 				   pch_port_hotplug_long_detect);
187026951cafSXiong Zhang 
1871676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1872aaf5ec2eSSonika Jindal 	}
187391d131d2SDaniel Vetter 
1874cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1875cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
187623e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1877cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1878cfc33bf7SVille Syrjälä 				 port_name(port));
1879cfc33bf7SVille Syrjälä 	}
188023e81d69SAdam Jackson 
188123e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1882ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
188323e81d69SAdam Jackson 
188423e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1885515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
188623e81d69SAdam Jackson 
188723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
188823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
188923e81d69SAdam Jackson 
189023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
189123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
189223e81d69SAdam Jackson 
189323e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1894055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
189523e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
189623e81d69SAdam Jackson 					 pipe_name(pipe),
189723e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18988664281bSPaulo Zanoni 
18998664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19008664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
190123e81d69SAdam Jackson }
190223e81d69SAdam Jackson 
19036dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
19046dbf30ceSVille Syrjälä {
19056dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
19066dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19076dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19086dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19096dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19106dbf30ceSVille Syrjälä 
19116dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19126dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19136dbf30ceSVille Syrjälä 
19146dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19156dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19166dbf30ceSVille Syrjälä 
19176dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19186dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
191974c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19206dbf30ceSVille Syrjälä 	}
19216dbf30ceSVille Syrjälä 
19226dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19236dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19246dbf30ceSVille Syrjälä 
19256dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19266dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19276dbf30ceSVille Syrjälä 
19286dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
19296dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19306dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19316dbf30ceSVille Syrjälä 	}
19326dbf30ceSVille Syrjälä 
19336dbf30ceSVille Syrjälä 	if (pin_mask)
19346dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
19356dbf30ceSVille Syrjälä 
19366dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
19376dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
19386dbf30ceSVille Syrjälä }
19396dbf30ceSVille Syrjälä 
1940c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1941c008bc6eSPaulo Zanoni {
1942c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
194340da17c2SDaniel Vetter 	enum pipe pipe;
1944e4ce95aaSVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
1945e4ce95aaSVille Syrjälä 
1946e4ce95aaSVille Syrjälä 	if (hotplug_trigger) {
1947e4ce95aaSVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1948e4ce95aaSVille Syrjälä 
1949e4ce95aaSVille Syrjälä 		dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
1950e4ce95aaSVille Syrjälä 		I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
1951e4ce95aaSVille Syrjälä 
1952e4ce95aaSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1953e4ce95aaSVille Syrjälä 				   dig_hotplug_reg, hpd_ilk,
1954e4ce95aaSVille Syrjälä 				   ilk_port_hotplug_long_detect);
1955e4ce95aaSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1956e4ce95aaSVille Syrjälä 	}
1957c008bc6eSPaulo Zanoni 
1958c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1959c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1960c008bc6eSPaulo Zanoni 
1961c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1962c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1963c008bc6eSPaulo Zanoni 
1964c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1965c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1966c008bc6eSPaulo Zanoni 
1967055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1968d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
1969d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1970d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
1971c008bc6eSPaulo Zanoni 
197240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
19731f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1974c008bc6eSPaulo Zanoni 
197540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
197640da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
19775b3a856bSDaniel Vetter 
197840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
197940da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
198040da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
198140da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1982c008bc6eSPaulo Zanoni 		}
1983c008bc6eSPaulo Zanoni 	}
1984c008bc6eSPaulo Zanoni 
1985c008bc6eSPaulo Zanoni 	/* check event from PCH */
1986c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1987c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1988c008bc6eSPaulo Zanoni 
1989c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1990c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1991c008bc6eSPaulo Zanoni 		else
1992c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1993c008bc6eSPaulo Zanoni 
1994c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1995c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1996c008bc6eSPaulo Zanoni 	}
1997c008bc6eSPaulo Zanoni 
1998c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1999c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2000c008bc6eSPaulo Zanoni }
2001c008bc6eSPaulo Zanoni 
20029719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20039719fb98SPaulo Zanoni {
20049719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
200507d27e20SDamien Lespiau 	enum pipe pipe;
200623bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
200723bb4cb5SVille Syrjälä 
200823bb4cb5SVille Syrjälä 	if (hotplug_trigger) {
200923bb4cb5SVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
201023bb4cb5SVille Syrjälä 
201123bb4cb5SVille Syrjälä 		dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
201223bb4cb5SVille Syrjälä 		I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
201323bb4cb5SVille Syrjälä 
201423bb4cb5SVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
201523bb4cb5SVille Syrjälä 				   dig_hotplug_reg, hpd_ivb,
201623bb4cb5SVille Syrjälä 				   ilk_port_hotplug_long_detect);
201723bb4cb5SVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
201823bb4cb5SVille Syrjälä 	}
20199719fb98SPaulo Zanoni 
20209719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20219719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20229719fb98SPaulo Zanoni 
20239719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20249719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20259719fb98SPaulo Zanoni 
20269719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20279719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20289719fb98SPaulo Zanoni 
2029055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2030d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2031d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2032d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
203340da17c2SDaniel Vetter 
203440da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
203507d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
203607d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
203707d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20389719fb98SPaulo Zanoni 		}
20399719fb98SPaulo Zanoni 	}
20409719fb98SPaulo Zanoni 
20419719fb98SPaulo Zanoni 	/* check event from PCH */
20429719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20439719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20449719fb98SPaulo Zanoni 
20459719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20469719fb98SPaulo Zanoni 
20479719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20489719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20499719fb98SPaulo Zanoni 	}
20509719fb98SPaulo Zanoni }
20519719fb98SPaulo Zanoni 
205272c90f62SOscar Mateo /*
205372c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
205472c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
205572c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
205672c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
205772c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
205872c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
205972c90f62SOscar Mateo  */
2060f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2061b1f14ad0SJesse Barnes {
206245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2064f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20650e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2066b1f14ad0SJesse Barnes 
20672dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20682dd2a883SImre Deak 		return IRQ_NONE;
20692dd2a883SImre Deak 
20708664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
20718664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2072907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
20738664281bSPaulo Zanoni 
2074b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2075b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2076b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
207723a78516SPaulo Zanoni 	POSTING_READ(DEIER);
20780e43406bSChris Wilson 
207944498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
208044498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
208144498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
208244498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
208344498aeaSPaulo Zanoni 	 * due to its back queue). */
2084ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
208544498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
208644498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
208744498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2088ab5c608bSBen Widawsky 	}
208944498aeaSPaulo Zanoni 
209072c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
209172c90f62SOscar Mateo 
20920e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
20930e43406bSChris Wilson 	if (gt_iir) {
209472c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
209572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2096d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
20970e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2098d8fc8a47SPaulo Zanoni 		else
2099d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21000e43406bSChris Wilson 	}
2101b1f14ad0SJesse Barnes 
2102b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21030e43406bSChris Wilson 	if (de_iir) {
210472c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
210572c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2106f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21079719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2108f1af8fc1SPaulo Zanoni 		else
2109f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21100e43406bSChris Wilson 	}
21110e43406bSChris Wilson 
2112f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2113f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21140e43406bSChris Wilson 		if (pm_iir) {
2115b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21160e43406bSChris Wilson 			ret = IRQ_HANDLED;
211772c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21180e43406bSChris Wilson 		}
2119f1af8fc1SPaulo Zanoni 	}
2120b1f14ad0SJesse Barnes 
2121b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2122b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2123ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
212444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
212544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2126ab5c608bSBen Widawsky 	}
2127b1f14ad0SJesse Barnes 
2128b1f14ad0SJesse Barnes 	return ret;
2129b1f14ad0SJesse Barnes }
2130b1f14ad0SJesse Barnes 
2131*cebd87a0SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger)
2132d04a492dSShashank Sharma {
2133*cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2134*cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2135d04a492dSShashank Sharma 
2136*cebd87a0SVille Syrjälä 	dig_hotplug_reg = I915_READ(BXT_HOTPLUG_CTL);
2137*cebd87a0SVille Syrjälä 	I915_WRITE(BXT_HOTPLUG_CTL, dig_hotplug_reg);
2138d04a492dSShashank Sharma 
2139*cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
2140*cebd87a0SVille Syrjälä 			   dig_hotplug_reg, hpd_bxt,
2141*cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
2142475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2143d04a492dSShashank Sharma }
2144d04a492dSShashank Sharma 
2145abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2146abd58f01SBen Widawsky {
2147abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2148abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2149abd58f01SBen Widawsky 	u32 master_ctl;
2150abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2151abd58f01SBen Widawsky 	uint32_t tmp = 0;
2152c42664ccSDaniel Vetter 	enum pipe pipe;
215388e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
215488e04703SJesse Barnes 
21552dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21562dd2a883SImre Deak 		return IRQ_NONE;
21572dd2a883SImre Deak 
215888e04703SJesse Barnes 	if (IS_GEN9(dev))
215988e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
216088e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2161abd58f01SBen Widawsky 
2162cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2163abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2164abd58f01SBen Widawsky 	if (!master_ctl)
2165abd58f01SBen Widawsky 		return IRQ_NONE;
2166abd58f01SBen Widawsky 
2167cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2168abd58f01SBen Widawsky 
216938cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
217038cc46d7SOscar Mateo 
217174cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2172abd58f01SBen Widawsky 
2173abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2174abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2175abd58f01SBen Widawsky 		if (tmp) {
2176abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2177abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
217838cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
217938cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
218038cc46d7SOscar Mateo 			else
218138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2182abd58f01SBen Widawsky 		}
218338cc46d7SOscar Mateo 		else
218438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2185abd58f01SBen Widawsky 	}
2186abd58f01SBen Widawsky 
21876d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
21886d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
21896d766f02SDaniel Vetter 		if (tmp) {
2190d04a492dSShashank Sharma 			bool found = false;
2191*cebd87a0SVille Syrjälä 			u32 hotplug_trigger = 0;
2192*cebd87a0SVille Syrjälä 
2193*cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev_priv))
2194*cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & BXT_DE_PORT_HOTPLUG_MASK;
2195*cebd87a0SVille Syrjälä 			else if (IS_BROADWELL(dev_priv))
2196*cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2197d04a492dSShashank Sharma 
21986d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
21996d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
220088e04703SJesse Barnes 
22013a3b3c7dSVille Syrjälä 			if (IS_BROADWELL(dev) && hotplug_trigger) {
22023a3b3c7dSVille Syrjälä 				u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
22033a3b3c7dSVille Syrjälä 
22043a3b3c7dSVille Syrjälä 				dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
22053a3b3c7dSVille Syrjälä 				I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
22063a3b3c7dSVille Syrjälä 
22073a3b3c7dSVille Syrjälä 				intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
22083a3b3c7dSVille Syrjälä 						   dig_hotplug_reg, hpd_bdw,
22093a3b3c7dSVille Syrjälä 						   ilk_port_hotplug_long_detect);
22103a3b3c7dSVille Syrjälä 				intel_hpd_irq_handler(dev, pin_mask, long_mask);
22113a3b3c7dSVille Syrjälä 				found = true;
22123a3b3c7dSVille Syrjälä 			}
22133a3b3c7dSVille Syrjälä 
2214d04a492dSShashank Sharma 			if (tmp & aux_mask) {
221538cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2216d04a492dSShashank Sharma 				found = true;
2217d04a492dSShashank Sharma 			}
2218d04a492dSShashank Sharma 
2219*cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev) && hotplug_trigger) {
2220*cebd87a0SVille Syrjälä 				bxt_hpd_irq_handler(dev, hotplug_trigger);
2221d04a492dSShashank Sharma 				found = true;
2222d04a492dSShashank Sharma 			}
2223d04a492dSShashank Sharma 
22249e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
22259e63743eSShashank Sharma 				gmbus_irq_handler(dev);
22269e63743eSShashank Sharma 				found = true;
22279e63743eSShashank Sharma 			}
22289e63743eSShashank Sharma 
2229d04a492dSShashank Sharma 			if (!found)
223038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22316d766f02SDaniel Vetter 		}
223238cc46d7SOscar Mateo 		else
223338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22346d766f02SDaniel Vetter 	}
22356d766f02SDaniel Vetter 
2236055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2237770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2238abd58f01SBen Widawsky 
2239c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2240c42664ccSDaniel Vetter 			continue;
2241c42664ccSDaniel Vetter 
2242abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
224338cc46d7SOscar Mateo 		if (pipe_iir) {
224438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
224538cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2246770de83dSDamien Lespiau 
2247d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2248d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2249d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2250abd58f01SBen Widawsky 
2251770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2252770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2253770de83dSDamien Lespiau 			else
2254770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2255770de83dSDamien Lespiau 
2256770de83dSDamien Lespiau 			if (flip_done) {
2257abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2258abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2259abd58f01SBen Widawsky 			}
2260abd58f01SBen Widawsky 
22610fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22620fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22630fbe7870SDaniel Vetter 
22641f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22651f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22661f7247c0SDaniel Vetter 								    pipe);
226738d83c96SDaniel Vetter 
2268770de83dSDamien Lespiau 
2269770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2270770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2271770de83dSDamien Lespiau 			else
2272770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2273770de83dSDamien Lespiau 
2274770de83dSDamien Lespiau 			if (fault_errors)
227530100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
227630100f2bSDaniel Vetter 					  pipe_name(pipe),
227730100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2278c42664ccSDaniel Vetter 		} else
2279abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2280abd58f01SBen Widawsky 	}
2281abd58f01SBen Widawsky 
2282266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2283266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
228492d03a80SDaniel Vetter 		/*
228592d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
228692d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
228792d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
228892d03a80SDaniel Vetter 		 */
228992d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
229092d03a80SDaniel Vetter 		if (pch_iir) {
229192d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
229292d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
22936dbf30ceSVille Syrjälä 
22946dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
22956dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
22966dbf30ceSVille Syrjälä 			else
229738cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
229838cc46d7SOscar Mateo 		} else
229938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
230038cc46d7SOscar Mateo 
230192d03a80SDaniel Vetter 	}
230292d03a80SDaniel Vetter 
2303cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2304cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2305abd58f01SBen Widawsky 
2306abd58f01SBen Widawsky 	return ret;
2307abd58f01SBen Widawsky }
2308abd58f01SBen Widawsky 
230917e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
231017e1df07SDaniel Vetter 			       bool reset_completed)
231117e1df07SDaniel Vetter {
2312a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
231317e1df07SDaniel Vetter 	int i;
231417e1df07SDaniel Vetter 
231517e1df07SDaniel Vetter 	/*
231617e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
231717e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
231817e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
231917e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
232017e1df07SDaniel Vetter 	 */
232117e1df07SDaniel Vetter 
232217e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
232317e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
232417e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
232517e1df07SDaniel Vetter 
232617e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
232717e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
232817e1df07SDaniel Vetter 
232917e1df07SDaniel Vetter 	/*
233017e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
233117e1df07SDaniel Vetter 	 * reset state is cleared.
233217e1df07SDaniel Vetter 	 */
233317e1df07SDaniel Vetter 	if (reset_completed)
233417e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
233517e1df07SDaniel Vetter }
233617e1df07SDaniel Vetter 
23378a905236SJesse Barnes /**
2338b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23398a905236SJesse Barnes  *
23408a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23418a905236SJesse Barnes  * was detected.
23428a905236SJesse Barnes  */
2343b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23448a905236SJesse Barnes {
2345b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2346b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2347cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2348cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2349cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
235017e1df07SDaniel Vetter 	int ret;
23518a905236SJesse Barnes 
23525bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23538a905236SJesse Barnes 
23547db0ba24SDaniel Vetter 	/*
23557db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23567db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23577db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23587db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23597db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23607db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23617db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23627db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23637db0ba24SDaniel Vetter 	 */
23647db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
236544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23665bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23677db0ba24SDaniel Vetter 				   reset_event);
23681f83fee0SDaniel Vetter 
236917e1df07SDaniel Vetter 		/*
2370f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2371f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2372f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2373f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2374f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2375f454c694SImre Deak 		 */
2376f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23777514747dSVille Syrjälä 
23787514747dSVille Syrjälä 		intel_prepare_reset(dev);
23797514747dSVille Syrjälä 
2380f454c694SImre Deak 		/*
238117e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
238217e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
238317e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
238417e1df07SDaniel Vetter 		 * deadlocks with the reset work.
238517e1df07SDaniel Vetter 		 */
2386f69061beSDaniel Vetter 		ret = i915_reset(dev);
2387f69061beSDaniel Vetter 
23887514747dSVille Syrjälä 		intel_finish_reset(dev);
238917e1df07SDaniel Vetter 
2390f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2391f454c694SImre Deak 
2392f69061beSDaniel Vetter 		if (ret == 0) {
2393f69061beSDaniel Vetter 			/*
2394f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2395f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2396f69061beSDaniel Vetter 			 * complete.
2397f69061beSDaniel Vetter 			 *
2398f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2399f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2400f69061beSDaniel Vetter 			 * updates before
2401f69061beSDaniel Vetter 			 * the counter increment.
2402f69061beSDaniel Vetter 			 */
24034e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2404f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2405f69061beSDaniel Vetter 
24065bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2407f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24081f83fee0SDaniel Vetter 		} else {
24092ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2410f316a42cSBen Gamari 		}
24111f83fee0SDaniel Vetter 
241217e1df07SDaniel Vetter 		/*
241317e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
241417e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
241517e1df07SDaniel Vetter 		 */
241617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2417f316a42cSBen Gamari 	}
24188a905236SJesse Barnes }
24198a905236SJesse Barnes 
242035aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2421c0e09200SDave Airlie {
24228a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2423bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
242463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2425050ee91fSBen Widawsky 	int pipe, i;
242663eeaf38SJesse Barnes 
242735aed2e6SChris Wilson 	if (!eir)
242835aed2e6SChris Wilson 		return;
242963eeaf38SJesse Barnes 
2430a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24318a905236SJesse Barnes 
2432bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2433bd9854f9SBen Widawsky 
24348a905236SJesse Barnes 	if (IS_G4X(dev)) {
24358a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24368a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24378a905236SJesse Barnes 
2438a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2439a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2440050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2441050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2442a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2443a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24448a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24453143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24468a905236SJesse Barnes 		}
24478a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24488a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2449a70491ccSJoe Perches 			pr_err("page table error\n");
2450a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24518a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24523143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24538a905236SJesse Barnes 		}
24548a905236SJesse Barnes 	}
24558a905236SJesse Barnes 
2456a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
245763eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
245863eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2459a70491ccSJoe Perches 			pr_err("page table error\n");
2460a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
246163eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24623143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
246363eeaf38SJesse Barnes 		}
24648a905236SJesse Barnes 	}
24658a905236SJesse Barnes 
246663eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2467a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2468055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2469a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24709db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
247163eeaf38SJesse Barnes 		/* pipestat has already been acked */
247263eeaf38SJesse Barnes 	}
247363eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2474a70491ccSJoe Perches 		pr_err("instruction error\n");
2475a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2476050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2477050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2478a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
247963eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
248063eeaf38SJesse Barnes 
2481a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2482a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2483a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
248463eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24853143a2bfSChris Wilson 			POSTING_READ(IPEIR);
248663eeaf38SJesse Barnes 		} else {
248763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
248863eeaf38SJesse Barnes 
2489a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2490a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2491a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2492a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
249363eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24943143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
249563eeaf38SJesse Barnes 		}
249663eeaf38SJesse Barnes 	}
249763eeaf38SJesse Barnes 
249863eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24993143a2bfSChris Wilson 	POSTING_READ(EIR);
250063eeaf38SJesse Barnes 	eir = I915_READ(EIR);
250163eeaf38SJesse Barnes 	if (eir) {
250263eeaf38SJesse Barnes 		/*
250363eeaf38SJesse Barnes 		 * some errors might have become stuck,
250463eeaf38SJesse Barnes 		 * mask them.
250563eeaf38SJesse Barnes 		 */
250663eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
250763eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
250863eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
250963eeaf38SJesse Barnes 	}
251035aed2e6SChris Wilson }
251135aed2e6SChris Wilson 
251235aed2e6SChris Wilson /**
2513b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
251435aed2e6SChris Wilson  * @dev: drm device
251535aed2e6SChris Wilson  *
2516b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
251735aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
251835aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
251935aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
252035aed2e6SChris Wilson  * of a ring dump etc.).
252135aed2e6SChris Wilson  */
252258174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
252358174462SMika Kuoppala 		       const char *fmt, ...)
252435aed2e6SChris Wilson {
252535aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
252658174462SMika Kuoppala 	va_list args;
252758174462SMika Kuoppala 	char error_msg[80];
252835aed2e6SChris Wilson 
252958174462SMika Kuoppala 	va_start(args, fmt);
253058174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
253158174462SMika Kuoppala 	va_end(args);
253258174462SMika Kuoppala 
253358174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
253435aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25358a905236SJesse Barnes 
2536ba1234d1SBen Gamari 	if (wedged) {
2537f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2538f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2539ba1234d1SBen Gamari 
254011ed50ecSBen Gamari 		/*
2541b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2542b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2543b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
254417e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
254517e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
254617e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
254717e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
254817e1df07SDaniel Vetter 		 *
254917e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
255017e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
255117e1df07SDaniel Vetter 		 * counter atomic_t.
255211ed50ecSBen Gamari 		 */
255317e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
255411ed50ecSBen Gamari 	}
255511ed50ecSBen Gamari 
2556b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25578a905236SJesse Barnes }
25588a905236SJesse Barnes 
255942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
256042f52ef8SKeith Packard  * we use as a pipe index
256142f52ef8SKeith Packard  */
2562f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25630a3e67a4SJesse Barnes {
25642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2565e9d21d7fSKeith Packard 	unsigned long irqflags;
256671e0ffa5SJesse Barnes 
25671ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2568f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25697c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2570755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25710a3e67a4SJesse Barnes 	else
25727c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2573755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25741ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25758692d00eSChris Wilson 
25760a3e67a4SJesse Barnes 	return 0;
25770a3e67a4SJesse Barnes }
25780a3e67a4SJesse Barnes 
2579f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2580f796cf8fSJesse Barnes {
25812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2582f796cf8fSJesse Barnes 	unsigned long irqflags;
2583b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
258440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2585f796cf8fSJesse Barnes 
2586f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2587b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2588b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2589b1f14ad0SJesse Barnes 
2590b1f14ad0SJesse Barnes 	return 0;
2591b1f14ad0SJesse Barnes }
2592b1f14ad0SJesse Barnes 
25937e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
25947e231dbeSJesse Barnes {
25952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25967e231dbeSJesse Barnes 	unsigned long irqflags;
25977e231dbeSJesse Barnes 
25987e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
259931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2600755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26017e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26027e231dbeSJesse Barnes 
26037e231dbeSJesse Barnes 	return 0;
26047e231dbeSJesse Barnes }
26057e231dbeSJesse Barnes 
2606abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2607abd58f01SBen Widawsky {
2608abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2609abd58f01SBen Widawsky 	unsigned long irqflags;
2610abd58f01SBen Widawsky 
2611abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26127167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26137167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2614abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2615abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2616abd58f01SBen Widawsky 	return 0;
2617abd58f01SBen Widawsky }
2618abd58f01SBen Widawsky 
261942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
262042f52ef8SKeith Packard  * we use as a pipe index
262142f52ef8SKeith Packard  */
2622f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26230a3e67a4SJesse Barnes {
26242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2625e9d21d7fSKeith Packard 	unsigned long irqflags;
26260a3e67a4SJesse Barnes 
26271ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26287c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2629755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2630755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26311ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26320a3e67a4SJesse Barnes }
26330a3e67a4SJesse Barnes 
2634f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2635f796cf8fSJesse Barnes {
26362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2637f796cf8fSJesse Barnes 	unsigned long irqflags;
2638b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
263940da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2640f796cf8fSJesse Barnes 
2641f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2642b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2643b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2644b1f14ad0SJesse Barnes }
2645b1f14ad0SJesse Barnes 
26467e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26477e231dbeSJesse Barnes {
26482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26497e231dbeSJesse Barnes 	unsigned long irqflags;
26507e231dbeSJesse Barnes 
26517e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
265231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2653755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26547e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26557e231dbeSJesse Barnes }
26567e231dbeSJesse Barnes 
2657abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2658abd58f01SBen Widawsky {
2659abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2660abd58f01SBen Widawsky 	unsigned long irqflags;
2661abd58f01SBen Widawsky 
2662abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26637167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26647167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2665abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2666abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2667abd58f01SBen Widawsky }
2668abd58f01SBen Widawsky 
26699107e9d2SChris Wilson static bool
267094f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2671893eead0SChris Wilson {
26729107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
267394f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2674f65d9421SBen Gamari }
2675f65d9421SBen Gamari 
2676a028c4b0SDaniel Vetter static bool
2677a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2678a028c4b0SDaniel Vetter {
2679a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2680a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2681a028c4b0SDaniel Vetter 	} else {
2682a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2683a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2684a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2685a028c4b0SDaniel Vetter 	}
2686a028c4b0SDaniel Vetter }
2687a028c4b0SDaniel Vetter 
2688a4872ba6SOscar Mateo static struct intel_engine_cs *
2689a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2690921d42eaSDaniel Vetter {
2691921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2692a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2693921d42eaSDaniel Vetter 	int i;
2694921d42eaSDaniel Vetter 
2695921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2696a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2697a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2698a6cdb93aSRodrigo Vivi 				continue;
2699a6cdb93aSRodrigo Vivi 
2700a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2701a6cdb93aSRodrigo Vivi 				return signaller;
2702a6cdb93aSRodrigo Vivi 		}
2703921d42eaSDaniel Vetter 	} else {
2704921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2705921d42eaSDaniel Vetter 
2706921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2707921d42eaSDaniel Vetter 			if(ring == signaller)
2708921d42eaSDaniel Vetter 				continue;
2709921d42eaSDaniel Vetter 
2710ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2711921d42eaSDaniel Vetter 				return signaller;
2712921d42eaSDaniel Vetter 		}
2713921d42eaSDaniel Vetter 	}
2714921d42eaSDaniel Vetter 
2715a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2716a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2717921d42eaSDaniel Vetter 
2718921d42eaSDaniel Vetter 	return NULL;
2719921d42eaSDaniel Vetter }
2720921d42eaSDaniel Vetter 
2721a4872ba6SOscar Mateo static struct intel_engine_cs *
2722a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2723a24a11e6SChris Wilson {
2724a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
272588fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2726a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2727a6cdb93aSRodrigo Vivi 	int i, backwards;
2728a24a11e6SChris Wilson 
2729a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2730a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27316274f212SChris Wilson 		return NULL;
2732a24a11e6SChris Wilson 
273388fe429dSDaniel Vetter 	/*
273488fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
273588fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2736a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2737a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
273888fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
273988fe429dSDaniel Vetter 	 * ringbuffer itself.
2740a24a11e6SChris Wilson 	 */
274188fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2742a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
274388fe429dSDaniel Vetter 
2744a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
274588fe429dSDaniel Vetter 		/*
274688fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
274788fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
274888fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
274988fe429dSDaniel Vetter 		 */
2750ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
275188fe429dSDaniel Vetter 
275288fe429dSDaniel Vetter 		/* This here seems to blow up */
2753ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2754a24a11e6SChris Wilson 		if (cmd == ipehr)
2755a24a11e6SChris Wilson 			break;
2756a24a11e6SChris Wilson 
275788fe429dSDaniel Vetter 		head -= 4;
275888fe429dSDaniel Vetter 	}
2759a24a11e6SChris Wilson 
276088fe429dSDaniel Vetter 	if (!i)
276188fe429dSDaniel Vetter 		return NULL;
276288fe429dSDaniel Vetter 
2763ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2764a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2765a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2766a6cdb93aSRodrigo Vivi 		offset <<= 32;
2767a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2768a6cdb93aSRodrigo Vivi 	}
2769a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2770a24a11e6SChris Wilson }
2771a24a11e6SChris Wilson 
2772a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27736274f212SChris Wilson {
27746274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2775a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2776a0d036b0SChris Wilson 	u32 seqno;
27776274f212SChris Wilson 
27784be17381SChris Wilson 	ring->hangcheck.deadlock++;
27796274f212SChris Wilson 
27806274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
27814be17381SChris Wilson 	if (signaller == NULL)
27824be17381SChris Wilson 		return -1;
27834be17381SChris Wilson 
27844be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
27854be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
27866274f212SChris Wilson 		return -1;
27876274f212SChris Wilson 
27884be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
27894be17381SChris Wilson 		return 1;
27904be17381SChris Wilson 
2791a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2792a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2793a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
27944be17381SChris Wilson 		return -1;
27954be17381SChris Wilson 
27964be17381SChris Wilson 	return 0;
27976274f212SChris Wilson }
27986274f212SChris Wilson 
27996274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28006274f212SChris Wilson {
2801a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28026274f212SChris Wilson 	int i;
28036274f212SChris Wilson 
28046274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28054be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28066274f212SChris Wilson }
28076274f212SChris Wilson 
2808ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2809a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28101ec14ad3SChris Wilson {
28111ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28121ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28139107e9d2SChris Wilson 	u32 tmp;
28149107e9d2SChris Wilson 
2815f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2816f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2817f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2818f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2819f260fe7bSMika Kuoppala 		}
2820f260fe7bSMika Kuoppala 
2821f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2822f260fe7bSMika Kuoppala 	}
28236274f212SChris Wilson 
28249107e9d2SChris Wilson 	if (IS_GEN2(dev))
2825f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28269107e9d2SChris Wilson 
28279107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28289107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28299107e9d2SChris Wilson 	 * and break the hang. This should work on
28309107e9d2SChris Wilson 	 * all but the second generation chipsets.
28319107e9d2SChris Wilson 	 */
28329107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28331ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
283458174462SMika Kuoppala 		i915_handle_error(dev, false,
283558174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28361ec14ad3SChris Wilson 				  ring->name);
28371ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2838f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28391ec14ad3SChris Wilson 	}
2840a24a11e6SChris Wilson 
28416274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28426274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28436274f212SChris Wilson 		default:
2844f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28456274f212SChris Wilson 		case 1:
284658174462SMika Kuoppala 			i915_handle_error(dev, false,
284758174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2848a24a11e6SChris Wilson 					  ring->name);
2849a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2850f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28516274f212SChris Wilson 		case 0:
2852f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28536274f212SChris Wilson 		}
28549107e9d2SChris Wilson 	}
28559107e9d2SChris Wilson 
2856f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2857a24a11e6SChris Wilson }
2858d1e61e7fSChris Wilson 
2859737b1506SChris Wilson /*
2860f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
286105407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
286205407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
286305407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
286405407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
286505407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2866f65d9421SBen Gamari  */
2867737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2868f65d9421SBen Gamari {
2869737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2870737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2871737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2872737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2873a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2874b4519513SChris Wilson 	int i;
287505407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28769107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28779107e9d2SChris Wilson #define BUSY 1
28789107e9d2SChris Wilson #define KICK 5
28799107e9d2SChris Wilson #define HUNG 20
2880893eead0SChris Wilson 
2881d330a953SJani Nikula 	if (!i915.enable_hangcheck)
28823e0dc6b0SBen Widawsky 		return;
28833e0dc6b0SBen Widawsky 
2884b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
288550877445SChris Wilson 		u64 acthd;
288650877445SChris Wilson 		u32 seqno;
28879107e9d2SChris Wilson 		bool busy = true;
2888b4519513SChris Wilson 
28896274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
28906274f212SChris Wilson 
289105407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
289205407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
289305407ff8SMika Kuoppala 
289405407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
289594f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2896da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2897da661464SMika Kuoppala 
28989107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
28999107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2900094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2901f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29029107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29039107e9d2SChris Wilson 								  ring->name);
2904f4adcd24SDaniel Vetter 						else
2905f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2906f4adcd24SDaniel Vetter 								 ring->name);
29079107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2908094f9a54SChris Wilson 					}
2909094f9a54SChris Wilson 					/* Safeguard against driver failure */
2910094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29119107e9d2SChris Wilson 				} else
29129107e9d2SChris Wilson 					busy = false;
291305407ff8SMika Kuoppala 			} else {
29146274f212SChris Wilson 				/* We always increment the hangcheck score
29156274f212SChris Wilson 				 * if the ring is busy and still processing
29166274f212SChris Wilson 				 * the same request, so that no single request
29176274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29186274f212SChris Wilson 				 * batches). The only time we do not increment
29196274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29206274f212SChris Wilson 				 * ring is in a legitimate wait for another
29216274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29226274f212SChris Wilson 				 * victim and we want to be sure we catch the
29236274f212SChris Wilson 				 * right culprit. Then every time we do kick
29246274f212SChris Wilson 				 * the ring, add a small increment to the
29256274f212SChris Wilson 				 * score so that we can catch a batch that is
29266274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29276274f212SChris Wilson 				 * for stalling the machine.
29289107e9d2SChris Wilson 				 */
2929ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2930ad8beaeaSMika Kuoppala 								    acthd);
2931ad8beaeaSMika Kuoppala 
2932ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2933da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2934f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2935f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2936f260fe7bSMika Kuoppala 					break;
2937f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2938ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29396274f212SChris Wilson 					break;
2940f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2941ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29426274f212SChris Wilson 					break;
2943f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2944ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29456274f212SChris Wilson 					stuck[i] = true;
29466274f212SChris Wilson 					break;
29476274f212SChris Wilson 				}
294805407ff8SMika Kuoppala 			}
29499107e9d2SChris Wilson 		} else {
2950da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2951da661464SMika Kuoppala 
29529107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29539107e9d2SChris Wilson 			 * attempts across multiple batches.
29549107e9d2SChris Wilson 			 */
29559107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29569107e9d2SChris Wilson 				ring->hangcheck.score--;
2957f260fe7bSMika Kuoppala 
2958f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2959cbb465e7SChris Wilson 		}
2960f65d9421SBen Gamari 
296105407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
296205407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29639107e9d2SChris Wilson 		busy_count += busy;
296405407ff8SMika Kuoppala 	}
296505407ff8SMika Kuoppala 
296605407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2967b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2968b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
296905407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2970a43adf07SChris Wilson 				 ring->name);
2971a43adf07SChris Wilson 			rings_hung++;
297205407ff8SMika Kuoppala 		}
297305407ff8SMika Kuoppala 	}
297405407ff8SMika Kuoppala 
297505407ff8SMika Kuoppala 	if (rings_hung)
297658174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
297705407ff8SMika Kuoppala 
297805407ff8SMika Kuoppala 	if (busy_count)
297905407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
298005407ff8SMika Kuoppala 		 * being added */
298110cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
298210cd45b6SMika Kuoppala }
298310cd45b6SMika Kuoppala 
298410cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
298510cd45b6SMika Kuoppala {
2986737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2987672e7b7cSChris Wilson 
2988d330a953SJani Nikula 	if (!i915.enable_hangcheck)
298910cd45b6SMika Kuoppala 		return;
299010cd45b6SMika Kuoppala 
2991737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2992737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2993737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2994737b1506SChris Wilson 	 */
2995737b1506SChris Wilson 
2996737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
2997737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
2998f65d9421SBen Gamari }
2999f65d9421SBen Gamari 
30001c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
300191738a95SPaulo Zanoni {
300291738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
300391738a95SPaulo Zanoni 
300491738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
300591738a95SPaulo Zanoni 		return;
300691738a95SPaulo Zanoni 
3007f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3008105b122eSPaulo Zanoni 
3009105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3010105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3011622364b6SPaulo Zanoni }
3012105b122eSPaulo Zanoni 
301391738a95SPaulo Zanoni /*
3014622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3015622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3016622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3017622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3018622364b6SPaulo Zanoni  *
3019622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
302091738a95SPaulo Zanoni  */
3021622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3022622364b6SPaulo Zanoni {
3023622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3024622364b6SPaulo Zanoni 
3025622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3026622364b6SPaulo Zanoni 		return;
3027622364b6SPaulo Zanoni 
3028622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
302991738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
303091738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
303191738a95SPaulo Zanoni }
303291738a95SPaulo Zanoni 
30337c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3034d18ea1b5SDaniel Vetter {
3035d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3036d18ea1b5SDaniel Vetter 
3037f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3038a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3039f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3040d18ea1b5SDaniel Vetter }
3041d18ea1b5SDaniel Vetter 
3042c0e09200SDave Airlie /* drm_dma.h hooks
3043c0e09200SDave Airlie */
3044be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3045036a4a7dSZhenyu Wang {
30462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3047036a4a7dSZhenyu Wang 
30480c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3049bdfcdb63SDaniel Vetter 
3050f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3051c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3052c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3053036a4a7dSZhenyu Wang 
30547c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3055c650156aSZhenyu Wang 
30561c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30577d99163dSBen Widawsky }
30587d99163dSBen Widawsky 
305970591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
306070591a41SVille Syrjälä {
306170591a41SVille Syrjälä 	enum pipe pipe;
306270591a41SVille Syrjälä 
306370591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
306470591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
306570591a41SVille Syrjälä 
306670591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
306770591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
306870591a41SVille Syrjälä 
306970591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
307070591a41SVille Syrjälä }
307170591a41SVille Syrjälä 
30727e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30737e231dbeSJesse Barnes {
30742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30757e231dbeSJesse Barnes 
30767e231dbeSJesse Barnes 	/* VLV magic */
30777e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30787e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30797e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30807e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30817e231dbeSJesse Barnes 
30827c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30837e231dbeSJesse Barnes 
30847c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
30857e231dbeSJesse Barnes 
308670591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
30877e231dbeSJesse Barnes }
30887e231dbeSJesse Barnes 
3089d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3090d6e3cca3SDaniel Vetter {
3091d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3092d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3093d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3094d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3095d6e3cca3SDaniel Vetter }
3096d6e3cca3SDaniel Vetter 
3097823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3098abd58f01SBen Widawsky {
3099abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3100abd58f01SBen Widawsky 	int pipe;
3101abd58f01SBen Widawsky 
3102abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3103abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3104abd58f01SBen Widawsky 
3105d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3106abd58f01SBen Widawsky 
3107055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3108f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3109813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3110f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3111abd58f01SBen Widawsky 
3112f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3113f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3114f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3115abd58f01SBen Widawsky 
3116266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31171c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3118abd58f01SBen Widawsky }
3119abd58f01SBen Widawsky 
31204c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31214c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3122d49bdb0eSPaulo Zanoni {
31231180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3124d49bdb0eSPaulo Zanoni 
312513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3126d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3127d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3128d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3129d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31304c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31314c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31324c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31331180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31344c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31354c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31364c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31371180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
313813321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3139d49bdb0eSPaulo Zanoni }
3140d49bdb0eSPaulo Zanoni 
314143f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
314243f328d7SVille Syrjälä {
314343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
314443f328d7SVille Syrjälä 
314543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
314643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
314743f328d7SVille Syrjälä 
3148d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
314943f328d7SVille Syrjälä 
315043f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
315143f328d7SVille Syrjälä 
315243f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
315343f328d7SVille Syrjälä 
315470591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
315543f328d7SVille Syrjälä }
315643f328d7SVille Syrjälä 
315787a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
315887a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
315987a02106SVille Syrjälä {
316087a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
316187a02106SVille Syrjälä 	struct intel_encoder *encoder;
316287a02106SVille Syrjälä 	u32 enabled_irqs = 0;
316387a02106SVille Syrjälä 
316487a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
316587a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
316687a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
316787a02106SVille Syrjälä 
316887a02106SVille Syrjälä 	return enabled_irqs;
316987a02106SVille Syrjälä }
317087a02106SVille Syrjälä 
317182a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
317282a28bcfSDaniel Vetter {
31732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
317487a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
317582a28bcfSDaniel Vetter 
317682a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3177fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
317887a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
317982a28bcfSDaniel Vetter 	} else {
3180fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
318187a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
318282a28bcfSDaniel Vetter 	}
318382a28bcfSDaniel Vetter 
3184fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
318582a28bcfSDaniel Vetter 
31867fe0b973SKeith Packard 	/*
31877fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31886dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
31896dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
31907fe0b973SKeith Packard 	 */
31917fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31927fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31937fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31947fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31957fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31960b2eb33eSVille Syrjälä 	/*
31970b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
31980b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
31990b2eb33eSVille Syrjälä 	 */
32000b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
32010b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
32027fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32036dbf30ceSVille Syrjälä }
320426951cafSXiong Zhang 
32056dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
32066dbf30ceSVille Syrjälä {
32076dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
32086dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
32096dbf30ceSVille Syrjälä 
32106dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
32116dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
32126dbf30ceSVille Syrjälä 
32136dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32146dbf30ceSVille Syrjälä 
32156dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
32166dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32176dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
321874c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
32196dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32206dbf30ceSVille Syrjälä 
322126951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
322226951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
322326951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
322426951cafSXiong Zhang }
32257fe0b973SKeith Packard 
3226e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3227e4ce95aaSVille Syrjälä {
3228e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3229e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3230e4ce95aaSVille Syrjälä 
32313a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
32323a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
32333a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
32343a3b3c7dSVille Syrjälä 
32353a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32363a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
323723bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
323823bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
32393a3b3c7dSVille Syrjälä 
32403a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
324123bb4cb5SVille Syrjälä 	} else {
3242e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3243e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3244e4ce95aaSVille Syrjälä 
3245e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
32463a3b3c7dSVille Syrjälä 	}
3247e4ce95aaSVille Syrjälä 
3248e4ce95aaSVille Syrjälä 	/*
3249e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3250e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
325123bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3252e4ce95aaSVille Syrjälä 	 */
3253e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3254e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3255e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3256e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3257e4ce95aaSVille Syrjälä 
3258e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3259e4ce95aaSVille Syrjälä }
3260e4ce95aaSVille Syrjälä 
3261e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3262e0a20ad7SShashank Sharma {
3263e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
326487a02106SVille Syrjälä 	u32 hotplug_port;
3265e0a20ad7SShashank Sharma 	u32 hotplug_ctrl;
3266e0a20ad7SShashank Sharma 
326787a02106SVille Syrjälä 	hotplug_port = intel_hpd_enabled_irqs(dev, hpd_bxt);
3268e0a20ad7SShashank Sharma 
3269e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(BXT_HOTPLUG_CTL) & ~BXT_HOTPLUG_CTL_MASK;
3270e0a20ad7SShashank Sharma 
32717f3561beSSonika Jindal 	if (hotplug_port & BXT_DE_PORT_HP_DDIA)
32727f3561beSSonika Jindal 		hotplug_ctrl |= BXT_DDIA_HPD_ENABLE;
3273e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIB)
3274e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIB_HPD_ENABLE;
3275e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIC)
3276e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIC_HPD_ENABLE;
3277e0a20ad7SShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hotplug_ctrl);
3278e0a20ad7SShashank Sharma 
3279e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IMR) & ~hotplug_port;
3280e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IMR, hotplug_ctrl);
3281e0a20ad7SShashank Sharma 
3282e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IER) | hotplug_port;
3283e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IER, hotplug_ctrl);
3284e0a20ad7SShashank Sharma 	POSTING_READ(GEN8_DE_PORT_IER);
3285e0a20ad7SShashank Sharma }
3286e0a20ad7SShashank Sharma 
3287d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3288d46da437SPaulo Zanoni {
32892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
329082a28bcfSDaniel Vetter 	u32 mask;
3291d46da437SPaulo Zanoni 
3292692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3293692a04cfSDaniel Vetter 		return;
3294692a04cfSDaniel Vetter 
3295105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32965c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3297105b122eSPaulo Zanoni 	else
32985c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32998664281bSPaulo Zanoni 
3300337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3301d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3302d46da437SPaulo Zanoni }
3303d46da437SPaulo Zanoni 
33040a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
33050a9a8c91SDaniel Vetter {
33060a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
33070a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
33080a9a8c91SDaniel Vetter 
33090a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
33100a9a8c91SDaniel Vetter 
33110a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3312040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
33130a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
331435a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
331535a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
33160a9a8c91SDaniel Vetter 	}
33170a9a8c91SDaniel Vetter 
33180a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33190a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33200a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33210a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33220a9a8c91SDaniel Vetter 	} else {
33230a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33240a9a8c91SDaniel Vetter 	}
33250a9a8c91SDaniel Vetter 
332635079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33270a9a8c91SDaniel Vetter 
33280a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
332978e68d36SImre Deak 		/*
333078e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
333178e68d36SImre Deak 		 * itself is enabled/disabled.
333278e68d36SImre Deak 		 */
33330a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33340a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33350a9a8c91SDaniel Vetter 
3336605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
333735079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33380a9a8c91SDaniel Vetter 	}
33390a9a8c91SDaniel Vetter }
33400a9a8c91SDaniel Vetter 
3341f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3342036a4a7dSZhenyu Wang {
33432d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33448e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33458e76f8dcSPaulo Zanoni 
33468e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33478e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33488e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33498e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33505c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33518e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
335223bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
335323bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33548e76f8dcSPaulo Zanoni 	} else {
33558e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3356ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33575b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33585b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33595b3a856bSDaniel Vetter 				DE_POISON);
3360e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3361e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3362e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33638e76f8dcSPaulo Zanoni 	}
3364036a4a7dSZhenyu Wang 
33651ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3366036a4a7dSZhenyu Wang 
33670c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33680c841212SPaulo Zanoni 
3369622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3370622364b6SPaulo Zanoni 
337135079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3372036a4a7dSZhenyu Wang 
33730a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3374036a4a7dSZhenyu Wang 
3375d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33767fe0b973SKeith Packard 
3377f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33786005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33796005ce42SDaniel Vetter 		 *
33806005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33814bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33824bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3383d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3384f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3385d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3386f97108d1SJesse Barnes 	}
3387f97108d1SJesse Barnes 
3388036a4a7dSZhenyu Wang 	return 0;
3389036a4a7dSZhenyu Wang }
3390036a4a7dSZhenyu Wang 
3391f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3392f8b79e58SImre Deak {
3393f8b79e58SImre Deak 	u32 pipestat_mask;
3394f8b79e58SImre Deak 	u32 iir_mask;
3395120dda4fSVille Syrjälä 	enum pipe pipe;
3396f8b79e58SImre Deak 
3397f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3398f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3399f8b79e58SImre Deak 
3400120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3401120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3402f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3403f8b79e58SImre Deak 
3404f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3405f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3406f8b79e58SImre Deak 
3407120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3408120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3409120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3410f8b79e58SImre Deak 
3411f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3412f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3413f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3414120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3415120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3416f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3417f8b79e58SImre Deak 
3418f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3419f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3420f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
342176e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
342276e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3423f8b79e58SImre Deak }
3424f8b79e58SImre Deak 
3425f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3426f8b79e58SImre Deak {
3427f8b79e58SImre Deak 	u32 pipestat_mask;
3428f8b79e58SImre Deak 	u32 iir_mask;
3429120dda4fSVille Syrjälä 	enum pipe pipe;
3430f8b79e58SImre Deak 
3431f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3432f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
34336c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3434120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3435120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3436f8b79e58SImre Deak 
3437f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3438f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
343976e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3440f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3441f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3442f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3443f8b79e58SImre Deak 
3444f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3445f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3446f8b79e58SImre Deak 
3447120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3448120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3449120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3450f8b79e58SImre Deak 
3451f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3452f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3453120dda4fSVille Syrjälä 
3454120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3455120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3456f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3457f8b79e58SImre Deak }
3458f8b79e58SImre Deak 
3459f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3460f8b79e58SImre Deak {
3461f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3462f8b79e58SImre Deak 
3463f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3464f8b79e58SImre Deak 		return;
3465f8b79e58SImre Deak 
3466f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3467f8b79e58SImre Deak 
3468950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3469f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3470f8b79e58SImre Deak }
3471f8b79e58SImre Deak 
3472f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3473f8b79e58SImre Deak {
3474f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3475f8b79e58SImre Deak 
3476f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3477f8b79e58SImre Deak 		return;
3478f8b79e58SImre Deak 
3479f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3480f8b79e58SImre Deak 
3481950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3482f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3483f8b79e58SImre Deak }
3484f8b79e58SImre Deak 
34850e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
34867e231dbeSJesse Barnes {
3487f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34887e231dbeSJesse Barnes 
348920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
349020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
349120afbda2SDaniel Vetter 
34927e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
349376e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
349476e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
349576e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
349676e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
34977e231dbeSJesse Barnes 
3498b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3499b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3500d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3501f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3502f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3503d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
35040e6c9a9eSVille Syrjälä }
35050e6c9a9eSVille Syrjälä 
35060e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
35070e6c9a9eSVille Syrjälä {
35080e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
35090e6c9a9eSVille Syrjälä 
35100e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
35117e231dbeSJesse Barnes 
35120a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
35137e231dbeSJesse Barnes 
35147e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
35157e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
35167e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
35177e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
35187e231dbeSJesse Barnes #endif
35197e231dbeSJesse Barnes 
35207e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
352120afbda2SDaniel Vetter 
352220afbda2SDaniel Vetter 	return 0;
352320afbda2SDaniel Vetter }
352420afbda2SDaniel Vetter 
3525abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3526abd58f01SBen Widawsky {
3527abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3528abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3529abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
353073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3531abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
353273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
353373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3534abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
353573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
353673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
353773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3538abd58f01SBen Widawsky 		0,
353973d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
354073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3541abd58f01SBen Widawsky 		};
3542abd58f01SBen Widawsky 
35430961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35449a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35459a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
354678e68d36SImre Deak 	/*
354778e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
354878e68d36SImre Deak 	 * is enabled/disabled.
354978e68d36SImre Deak 	 */
355078e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
35519a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3552abd58f01SBen Widawsky }
3553abd58f01SBen Widawsky 
3554abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3555abd58f01SBen Widawsky {
3556770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3557770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
35583a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
35593a3b3c7dSVille Syrjälä 	u32 de_port_enables;
35603a3b3c7dSVille Syrjälä 	enum pipe pipe;
3561770de83dSDamien Lespiau 
356288e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3563770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3564770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
35653a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
356688e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
35679e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
35683a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
35693a3b3c7dSVille Syrjälä 	} else {
3570770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3571770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
35723a3b3c7dSVille Syrjälä 	}
3573770de83dSDamien Lespiau 
3574770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3575770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3576770de83dSDamien Lespiau 
35773a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
35783a3b3c7dSVille Syrjälä 	if (IS_BROADWELL(dev_priv))
35793a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
35803a3b3c7dSVille Syrjälä 
358113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
358213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
358313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3584abd58f01SBen Widawsky 
3585055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3586f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3587813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3588813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3589813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
359035079899SPaulo Zanoni 					  de_pipe_enables);
3591abd58f01SBen Widawsky 
35923a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3593abd58f01SBen Widawsky }
3594abd58f01SBen Widawsky 
3595abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3596abd58f01SBen Widawsky {
3597abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3598abd58f01SBen Widawsky 
3599266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3600622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3601622364b6SPaulo Zanoni 
3602abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3603abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3604abd58f01SBen Widawsky 
3605266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3606abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3607abd58f01SBen Widawsky 
3608abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3609abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3610abd58f01SBen Widawsky 
3611abd58f01SBen Widawsky 	return 0;
3612abd58f01SBen Widawsky }
3613abd58f01SBen Widawsky 
361443f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
361543f328d7SVille Syrjälä {
361643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
361743f328d7SVille Syrjälä 
3618c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
361943f328d7SVille Syrjälä 
362043f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
362143f328d7SVille Syrjälä 
362243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
362343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
362443f328d7SVille Syrjälä 
362543f328d7SVille Syrjälä 	return 0;
362643f328d7SVille Syrjälä }
362743f328d7SVille Syrjälä 
3628abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3629abd58f01SBen Widawsky {
3630abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3631abd58f01SBen Widawsky 
3632abd58f01SBen Widawsky 	if (!dev_priv)
3633abd58f01SBen Widawsky 		return;
3634abd58f01SBen Widawsky 
3635823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3636abd58f01SBen Widawsky }
3637abd58f01SBen Widawsky 
36388ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
36398ea0be4fSVille Syrjälä {
36408ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
36418ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
36428ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36438ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
36448ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
36458ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
36468ea0be4fSVille Syrjälä 
36478ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
36488ea0be4fSVille Syrjälä 
3649c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
36508ea0be4fSVille Syrjälä }
36518ea0be4fSVille Syrjälä 
36527e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
36537e231dbeSJesse Barnes {
36542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36557e231dbeSJesse Barnes 
36567e231dbeSJesse Barnes 	if (!dev_priv)
36577e231dbeSJesse Barnes 		return;
36587e231dbeSJesse Barnes 
3659843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3660843d0e7dSImre Deak 
3661893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3662893fce8eSVille Syrjälä 
36637e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3664f8b79e58SImre Deak 
36658ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
36667e231dbeSJesse Barnes }
36677e231dbeSJesse Barnes 
366843f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
366943f328d7SVille Syrjälä {
367043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
367143f328d7SVille Syrjälä 
367243f328d7SVille Syrjälä 	if (!dev_priv)
367343f328d7SVille Syrjälä 		return;
367443f328d7SVille Syrjälä 
367543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
367643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
367743f328d7SVille Syrjälä 
3678a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
367943f328d7SVille Syrjälä 
3680a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
368143f328d7SVille Syrjälä 
3682c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
368343f328d7SVille Syrjälä }
368443f328d7SVille Syrjälä 
3685f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3686036a4a7dSZhenyu Wang {
36872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36884697995bSJesse Barnes 
36894697995bSJesse Barnes 	if (!dev_priv)
36904697995bSJesse Barnes 		return;
36914697995bSJesse Barnes 
3692be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3693036a4a7dSZhenyu Wang }
3694036a4a7dSZhenyu Wang 
3695c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3696c2798b19SChris Wilson {
36972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3698c2798b19SChris Wilson 	int pipe;
3699c2798b19SChris Wilson 
3700055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3701c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3702c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3703c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3704c2798b19SChris Wilson 	POSTING_READ16(IER);
3705c2798b19SChris Wilson }
3706c2798b19SChris Wilson 
3707c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3708c2798b19SChris Wilson {
37092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3710c2798b19SChris Wilson 
3711c2798b19SChris Wilson 	I915_WRITE16(EMR,
3712c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3713c2798b19SChris Wilson 
3714c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3715c2798b19SChris Wilson 	dev_priv->irq_mask =
3716c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3717c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3718c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
371937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3720c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3721c2798b19SChris Wilson 
3722c2798b19SChris Wilson 	I915_WRITE16(IER,
3723c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3724c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3725c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3726c2798b19SChris Wilson 	POSTING_READ16(IER);
3727c2798b19SChris Wilson 
3728379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3729379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3730d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3731755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3732755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3733d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3734379ef82dSDaniel Vetter 
3735c2798b19SChris Wilson 	return 0;
3736c2798b19SChris Wilson }
3737c2798b19SChris Wilson 
373890a72f87SVille Syrjälä /*
373990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
374090a72f87SVille Syrjälä  */
374190a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37421f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
374390a72f87SVille Syrjälä {
37442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37451f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
374690a72f87SVille Syrjälä 
37478d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
374890a72f87SVille Syrjälä 		return false;
374990a72f87SVille Syrjälä 
375090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3751d6bbafa1SChris Wilson 		goto check_page_flip;
375290a72f87SVille Syrjälä 
375390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
375490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
375590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
375690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
375790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
375890a72f87SVille Syrjälä 	 */
375990a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3760d6bbafa1SChris Wilson 		goto check_page_flip;
376190a72f87SVille Syrjälä 
37627d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
376390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
376490a72f87SVille Syrjälä 	return true;
3765d6bbafa1SChris Wilson 
3766d6bbafa1SChris Wilson check_page_flip:
3767d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3768d6bbafa1SChris Wilson 	return false;
376990a72f87SVille Syrjälä }
377090a72f87SVille Syrjälä 
3771ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3772c2798b19SChris Wilson {
377345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3775c2798b19SChris Wilson 	u16 iir, new_iir;
3776c2798b19SChris Wilson 	u32 pipe_stats[2];
3777c2798b19SChris Wilson 	int pipe;
3778c2798b19SChris Wilson 	u16 flip_mask =
3779c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3780c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3781c2798b19SChris Wilson 
37822dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37832dd2a883SImre Deak 		return IRQ_NONE;
37842dd2a883SImre Deak 
3785c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3786c2798b19SChris Wilson 	if (iir == 0)
3787c2798b19SChris Wilson 		return IRQ_NONE;
3788c2798b19SChris Wilson 
3789c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3790c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3791c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3792c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3793c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3794c2798b19SChris Wilson 		 */
3795222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3796c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3797aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3798c2798b19SChris Wilson 
3799055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3800c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3801c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3802c2798b19SChris Wilson 
3803c2798b19SChris Wilson 			/*
3804c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3805c2798b19SChris Wilson 			 */
38062d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3807c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3808c2798b19SChris Wilson 		}
3809222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3810c2798b19SChris Wilson 
3811c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3812c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3813c2798b19SChris Wilson 
3814c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
381574cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3816c2798b19SChris Wilson 
3817055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38181f1c2e24SVille Syrjälä 			int plane = pipe;
38193a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38201f1c2e24SVille Syrjälä 				plane = !plane;
38211f1c2e24SVille Syrjälä 
38224356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38231f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38241f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3825c2798b19SChris Wilson 
38264356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3827277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38282d9d2b0bSVille Syrjälä 
38291f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38301f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38311f7247c0SDaniel Vetter 								    pipe);
38324356d586SDaniel Vetter 		}
3833c2798b19SChris Wilson 
3834c2798b19SChris Wilson 		iir = new_iir;
3835c2798b19SChris Wilson 	}
3836c2798b19SChris Wilson 
3837c2798b19SChris Wilson 	return IRQ_HANDLED;
3838c2798b19SChris Wilson }
3839c2798b19SChris Wilson 
3840c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3841c2798b19SChris Wilson {
38422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3843c2798b19SChris Wilson 	int pipe;
3844c2798b19SChris Wilson 
3845055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3846c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3847c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3848c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3849c2798b19SChris Wilson 	}
3850c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3851c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3852c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3853c2798b19SChris Wilson }
3854c2798b19SChris Wilson 
3855a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3856a266c7d5SChris Wilson {
38572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3858a266c7d5SChris Wilson 	int pipe;
3859a266c7d5SChris Wilson 
3860a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3861a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3862a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3863a266c7d5SChris Wilson 	}
3864a266c7d5SChris Wilson 
386500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3866055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3867a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3868a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3869a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3870a266c7d5SChris Wilson 	POSTING_READ(IER);
3871a266c7d5SChris Wilson }
3872a266c7d5SChris Wilson 
3873a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3874a266c7d5SChris Wilson {
38752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
387638bde180SChris Wilson 	u32 enable_mask;
3877a266c7d5SChris Wilson 
387838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
387938bde180SChris Wilson 
388038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
388138bde180SChris Wilson 	dev_priv->irq_mask =
388238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
388338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
388438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
388538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
388637ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
388738bde180SChris Wilson 
388838bde180SChris Wilson 	enable_mask =
388938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
389038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
389138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
389238bde180SChris Wilson 		I915_USER_INTERRUPT;
389338bde180SChris Wilson 
3894a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
389520afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
389620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
389720afbda2SDaniel Vetter 
3898a266c7d5SChris Wilson 		/* Enable in IER... */
3899a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3900a266c7d5SChris Wilson 		/* and unmask in IMR */
3901a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3902a266c7d5SChris Wilson 	}
3903a266c7d5SChris Wilson 
3904a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3905a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3906a266c7d5SChris Wilson 	POSTING_READ(IER);
3907a266c7d5SChris Wilson 
3908f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
390920afbda2SDaniel Vetter 
3910379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3911379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3912d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3913755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3914755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3915d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3916379ef82dSDaniel Vetter 
391720afbda2SDaniel Vetter 	return 0;
391820afbda2SDaniel Vetter }
391920afbda2SDaniel Vetter 
392090a72f87SVille Syrjälä /*
392190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
392290a72f87SVille Syrjälä  */
392390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
392490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
392590a72f87SVille Syrjälä {
39262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
392790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
392890a72f87SVille Syrjälä 
39298d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
393090a72f87SVille Syrjälä 		return false;
393190a72f87SVille Syrjälä 
393290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3933d6bbafa1SChris Wilson 		goto check_page_flip;
393490a72f87SVille Syrjälä 
393590a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
393690a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
393790a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
393890a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
393990a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
394090a72f87SVille Syrjälä 	 */
394190a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3942d6bbafa1SChris Wilson 		goto check_page_flip;
394390a72f87SVille Syrjälä 
39447d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
394590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
394690a72f87SVille Syrjälä 	return true;
3947d6bbafa1SChris Wilson 
3948d6bbafa1SChris Wilson check_page_flip:
3949d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3950d6bbafa1SChris Wilson 	return false;
395190a72f87SVille Syrjälä }
395290a72f87SVille Syrjälä 
3953ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3954a266c7d5SChris Wilson {
395545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39578291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
395838bde180SChris Wilson 	u32 flip_mask =
395938bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
396038bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
396138bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3962a266c7d5SChris Wilson 
39632dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39642dd2a883SImre Deak 		return IRQ_NONE;
39652dd2a883SImre Deak 
3966a266c7d5SChris Wilson 	iir = I915_READ(IIR);
396738bde180SChris Wilson 	do {
396838bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39698291ee90SChris Wilson 		bool blc_event = false;
3970a266c7d5SChris Wilson 
3971a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3972a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3973a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3974a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3975a266c7d5SChris Wilson 		 */
3976222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3977a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3978aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3979a266c7d5SChris Wilson 
3980055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3981a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3982a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3983a266c7d5SChris Wilson 
398438bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3985a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3986a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
398738bde180SChris Wilson 				irq_received = true;
3988a266c7d5SChris Wilson 			}
3989a266c7d5SChris Wilson 		}
3990222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3991a266c7d5SChris Wilson 
3992a266c7d5SChris Wilson 		if (!irq_received)
3993a266c7d5SChris Wilson 			break;
3994a266c7d5SChris Wilson 
3995a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
399616c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
399716c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
399816c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3999a266c7d5SChris Wilson 
400038bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4001a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4002a266c7d5SChris Wilson 
4003a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
400474cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4005a266c7d5SChris Wilson 
4006055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
400738bde180SChris Wilson 			int plane = pipe;
40083a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
400938bde180SChris Wilson 				plane = !plane;
40105e2032d4SVille Syrjälä 
401190a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
401290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
401390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4014a266c7d5SChris Wilson 
4015a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4016a266c7d5SChris Wilson 				blc_event = true;
40174356d586SDaniel Vetter 
40184356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4019277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40202d9d2b0bSVille Syrjälä 
40211f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40221f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40231f7247c0SDaniel Vetter 								    pipe);
4024a266c7d5SChris Wilson 		}
4025a266c7d5SChris Wilson 
4026a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4027a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4028a266c7d5SChris Wilson 
4029a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4030a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4031a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4032a266c7d5SChris Wilson 		 * we would never get another interrupt.
4033a266c7d5SChris Wilson 		 *
4034a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4035a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4036a266c7d5SChris Wilson 		 * another one.
4037a266c7d5SChris Wilson 		 *
4038a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4039a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4040a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4041a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4042a266c7d5SChris Wilson 		 * stray interrupts.
4043a266c7d5SChris Wilson 		 */
404438bde180SChris Wilson 		ret = IRQ_HANDLED;
4045a266c7d5SChris Wilson 		iir = new_iir;
404638bde180SChris Wilson 	} while (iir & ~flip_mask);
4047a266c7d5SChris Wilson 
4048a266c7d5SChris Wilson 	return ret;
4049a266c7d5SChris Wilson }
4050a266c7d5SChris Wilson 
4051a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4052a266c7d5SChris Wilson {
40532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4054a266c7d5SChris Wilson 	int pipe;
4055a266c7d5SChris Wilson 
4056a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4057a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4058a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4059a266c7d5SChris Wilson 	}
4060a266c7d5SChris Wilson 
406100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4062055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
406355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4064a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
406555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
406655b39755SChris Wilson 	}
4067a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4068a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4069a266c7d5SChris Wilson 
4070a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4071a266c7d5SChris Wilson }
4072a266c7d5SChris Wilson 
4073a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4074a266c7d5SChris Wilson {
40752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4076a266c7d5SChris Wilson 	int pipe;
4077a266c7d5SChris Wilson 
4078a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4079a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4080a266c7d5SChris Wilson 
4081a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4082055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4083a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4084a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4085a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4086a266c7d5SChris Wilson 	POSTING_READ(IER);
4087a266c7d5SChris Wilson }
4088a266c7d5SChris Wilson 
4089a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4090a266c7d5SChris Wilson {
40912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4092bbba0a97SChris Wilson 	u32 enable_mask;
4093a266c7d5SChris Wilson 	u32 error_mask;
4094a266c7d5SChris Wilson 
4095a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4096bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4097adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4098bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4099bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4100bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4101bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4102bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4103bbba0a97SChris Wilson 
4104bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
410521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
410621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4107bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4108bbba0a97SChris Wilson 
4109bbba0a97SChris Wilson 	if (IS_G4X(dev))
4110bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4111a266c7d5SChris Wilson 
4112b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4113b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4114d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4115755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4116755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4117755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4118d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4119a266c7d5SChris Wilson 
4120a266c7d5SChris Wilson 	/*
4121a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4122a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4123a266c7d5SChris Wilson 	 */
4124a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4125a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4126a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4127a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4128a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4129a266c7d5SChris Wilson 	} else {
4130a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4131a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4132a266c7d5SChris Wilson 	}
4133a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4134a266c7d5SChris Wilson 
4135a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4136a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4137a266c7d5SChris Wilson 	POSTING_READ(IER);
4138a266c7d5SChris Wilson 
413920afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
414020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
414120afbda2SDaniel Vetter 
4142f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
414320afbda2SDaniel Vetter 
414420afbda2SDaniel Vetter 	return 0;
414520afbda2SDaniel Vetter }
414620afbda2SDaniel Vetter 
4147bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
414820afbda2SDaniel Vetter {
41492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
415020afbda2SDaniel Vetter 	u32 hotplug_en;
415120afbda2SDaniel Vetter 
4152b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4153b5ea2d56SDaniel Vetter 
4154bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4155bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4156adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4157e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
415887a02106SVille Syrjälä 	hotplug_en |= intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4159a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4160a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4161a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4162a266c7d5SChris Wilson 	*/
4163a266c7d5SChris Wilson 	if (IS_G4X(dev))
4164a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
416585fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4166a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4167a266c7d5SChris Wilson 
4168a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4169a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4170a266c7d5SChris Wilson }
4171a266c7d5SChris Wilson 
4172ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4173a266c7d5SChris Wilson {
417445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4176a266c7d5SChris Wilson 	u32 iir, new_iir;
4177a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4178a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
417921ad8330SVille Syrjälä 	u32 flip_mask =
418021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
418121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4182a266c7d5SChris Wilson 
41832dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41842dd2a883SImre Deak 		return IRQ_NONE;
41852dd2a883SImre Deak 
4186a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4187a266c7d5SChris Wilson 
4188a266c7d5SChris Wilson 	for (;;) {
4189501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41902c8ba29fSChris Wilson 		bool blc_event = false;
41912c8ba29fSChris Wilson 
4192a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4193a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4194a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4195a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4196a266c7d5SChris Wilson 		 */
4197222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4198a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4199aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4200a266c7d5SChris Wilson 
4201055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4202a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4203a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4204a266c7d5SChris Wilson 
4205a266c7d5SChris Wilson 			/*
4206a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4207a266c7d5SChris Wilson 			 */
4208a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4209a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4210501e01d7SVille Syrjälä 				irq_received = true;
4211a266c7d5SChris Wilson 			}
4212a266c7d5SChris Wilson 		}
4213222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4214a266c7d5SChris Wilson 
4215a266c7d5SChris Wilson 		if (!irq_received)
4216a266c7d5SChris Wilson 			break;
4217a266c7d5SChris Wilson 
4218a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4219a266c7d5SChris Wilson 
4220a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
422116c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
422216c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4223a266c7d5SChris Wilson 
422421ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4225a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4226a266c7d5SChris Wilson 
4227a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
422874cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4229a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
423074cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4231a266c7d5SChris Wilson 
4232055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42332c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
423490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
423590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4236a266c7d5SChris Wilson 
4237a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4238a266c7d5SChris Wilson 				blc_event = true;
42394356d586SDaniel Vetter 
42404356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4241277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4242a266c7d5SChris Wilson 
42431f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42441f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42452d9d2b0bSVille Syrjälä 		}
4246a266c7d5SChris Wilson 
4247a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4248a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4249a266c7d5SChris Wilson 
4250515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4251515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4252515ac2bbSDaniel Vetter 
4253a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4254a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4255a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4256a266c7d5SChris Wilson 		 * we would never get another interrupt.
4257a266c7d5SChris Wilson 		 *
4258a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4259a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4260a266c7d5SChris Wilson 		 * another one.
4261a266c7d5SChris Wilson 		 *
4262a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4263a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4264a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4265a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4266a266c7d5SChris Wilson 		 * stray interrupts.
4267a266c7d5SChris Wilson 		 */
4268a266c7d5SChris Wilson 		iir = new_iir;
4269a266c7d5SChris Wilson 	}
4270a266c7d5SChris Wilson 
4271a266c7d5SChris Wilson 	return ret;
4272a266c7d5SChris Wilson }
4273a266c7d5SChris Wilson 
4274a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4275a266c7d5SChris Wilson {
42762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4277a266c7d5SChris Wilson 	int pipe;
4278a266c7d5SChris Wilson 
4279a266c7d5SChris Wilson 	if (!dev_priv)
4280a266c7d5SChris Wilson 		return;
4281a266c7d5SChris Wilson 
4282a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4283a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4284a266c7d5SChris Wilson 
4285a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4286055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4287a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4288a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4289a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4290a266c7d5SChris Wilson 
4291055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4292a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4293a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4294a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4295a266c7d5SChris Wilson }
4296a266c7d5SChris Wilson 
4297fca52a55SDaniel Vetter /**
4298fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4299fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4300fca52a55SDaniel Vetter  *
4301fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4302fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4303fca52a55SDaniel Vetter  */
4304b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4305f71d4af4SJesse Barnes {
4306b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43078b2e326dSChris Wilson 
430877913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
430977913b39SJani Nikula 
4310c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4311a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43128b2e326dSChris Wilson 
4313a6706b45SDeepak S 	/* Let's track the enabled rps events */
4314b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43156c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43166f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
431731685c25SDeepak S 	else
4318a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4319a6706b45SDeepak S 
4320737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4321737b1506SChris Wilson 			  i915_hangcheck_elapsed);
432261bac78eSDaniel Vetter 
432397a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43249ee32feaSDaniel Vetter 
4325b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43264cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43274cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4328b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4329f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4330f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4331391f75e2SVille Syrjälä 	} else {
4332391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4333391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4334f71d4af4SJesse Barnes 	}
4335f71d4af4SJesse Barnes 
433621da2700SVille Syrjälä 	/*
433721da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
433821da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
433921da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
434021da2700SVille Syrjälä 	 */
4341b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
434221da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
434321da2700SVille Syrjälä 
4344f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4345f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4346f71d4af4SJesse Barnes 
4347b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
434843f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
434943f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
435043f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
435143f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
435243f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
435343f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
435443f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4355b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43567e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43577e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43587e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43597e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43607e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43617e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4362fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4363b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4364abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4365723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4366abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4367abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4368abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4369abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
43706dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4371e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
43726dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
43736dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
43746dbf30ceSVille Syrjälä 		else
43753a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4376f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4377f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4378723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4379f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4380f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4381f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4382f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4383e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4384f71d4af4SJesse Barnes 	} else {
4385b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4386c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4387c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4388c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4389c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4390b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4391a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4392a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4393a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4394a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4395c2798b19SChris Wilson 		} else {
4396a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4397a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4398a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4399a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4400c2798b19SChris Wilson 		}
4401778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4402778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4403f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4404f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4405f71d4af4SJesse Barnes 	}
4406f71d4af4SJesse Barnes }
440720afbda2SDaniel Vetter 
4408fca52a55SDaniel Vetter /**
4409fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4410fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4411fca52a55SDaniel Vetter  *
4412fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4413fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4414fca52a55SDaniel Vetter  *
4415fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4416fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4417fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4418fca52a55SDaniel Vetter  */
44192aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44202aeb7d3aSDaniel Vetter {
44212aeb7d3aSDaniel Vetter 	/*
44222aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44232aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44242aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44252aeb7d3aSDaniel Vetter 	 */
44262aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44272aeb7d3aSDaniel Vetter 
44282aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44292aeb7d3aSDaniel Vetter }
44302aeb7d3aSDaniel Vetter 
4431fca52a55SDaniel Vetter /**
4432fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4433fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4434fca52a55SDaniel Vetter  *
4435fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4436fca52a55SDaniel Vetter  * resources acquired in the init functions.
4437fca52a55SDaniel Vetter  */
44382aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44392aeb7d3aSDaniel Vetter {
44402aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44412aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44422aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44432aeb7d3aSDaniel Vetter }
44442aeb7d3aSDaniel Vetter 
4445fca52a55SDaniel Vetter /**
4446fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4447fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4448fca52a55SDaniel Vetter  *
4449fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4450fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4451fca52a55SDaniel Vetter  */
4452b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4453c67a470bSPaulo Zanoni {
4454b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44552aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44562dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4457c67a470bSPaulo Zanoni }
4458c67a470bSPaulo Zanoni 
4459fca52a55SDaniel Vetter /**
4460fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4461fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4462fca52a55SDaniel Vetter  *
4463fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4464fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4465fca52a55SDaniel Vetter  */
4466b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4467c67a470bSPaulo Zanoni {
44682aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4469b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4470b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4471c67a470bSPaulo Zanoni }
4472