xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 40e5641086f6f64acb13ff6893cf82c4fb30a6d7)
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ä 
1751*40e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
1752*40e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1753776ad806SJesse Barnes {
1754*40e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
175542db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1756776ad806SJesse Barnes 
175713cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
175813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
175913cf5504SDave Airlie 
1760fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1761*40e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1762fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
1763*40e56410SVille Syrjälä 
1764676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1765aaf5ec2eSSonika Jindal }
176691d131d2SDaniel Vetter 
1767*40e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1768*40e56410SVille Syrjälä {
1769*40e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
1770*40e56410SVille Syrjälä 	int pipe;
1771*40e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1772*40e56410SVille Syrjälä 
1773*40e56410SVille Syrjälä 	if (hotplug_trigger)
1774*40e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
1775*40e56410SVille Syrjälä 
1776cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1777cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1778776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1779cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1780cfc33bf7SVille Syrjälä 				 port_name(port));
1781cfc33bf7SVille Syrjälä 	}
1782776ad806SJesse Barnes 
1783ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1784ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1785ce99c256SDaniel Vetter 
1786776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1787515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1788776ad806SJesse Barnes 
1789776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1790776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1791776ad806SJesse Barnes 
1792776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1793776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1794776ad806SJesse Barnes 
1795776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1796776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1797776ad806SJesse Barnes 
17989db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1799055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
18009db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18019db4a9c7SJesse Barnes 					 pipe_name(pipe),
18029db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1803776ad806SJesse Barnes 
1804776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1805776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1806776ad806SJesse Barnes 
1807776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1808776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1809776ad806SJesse Barnes 
1810776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18111f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18128664281bSPaulo Zanoni 
18138664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
18141f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18158664281bSPaulo Zanoni }
18168664281bSPaulo Zanoni 
18178664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
18188664281bSPaulo Zanoni {
18198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18208664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
18215a69b89fSDaniel Vetter 	enum pipe pipe;
18228664281bSPaulo Zanoni 
1823de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1824de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1825de032bf4SPaulo Zanoni 
1826055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18271f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18281f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
18298664281bSPaulo Zanoni 
18305a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
18315a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1832277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
18335a69b89fSDaniel Vetter 			else
1834277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
18355a69b89fSDaniel Vetter 		}
18365a69b89fSDaniel Vetter 	}
18378bf1e9f1SShuang He 
18388664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
18398664281bSPaulo Zanoni }
18408664281bSPaulo Zanoni 
18418664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
18428664281bSPaulo Zanoni {
18438664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18448664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
18458664281bSPaulo Zanoni 
1846de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1847de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1848de032bf4SPaulo Zanoni 
18498664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
18501f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18518664281bSPaulo Zanoni 
18528664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
18531f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18548664281bSPaulo Zanoni 
18558664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
18561f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
18578664281bSPaulo Zanoni 
18588664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1859776ad806SJesse Barnes }
1860776ad806SJesse Barnes 
186123e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
186223e81d69SAdam Jackson {
18632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
186423e81d69SAdam Jackson 	int pipe;
18656dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1866aaf5ec2eSSonika Jindal 
1867*40e56410SVille Syrjälä 	if (hotplug_trigger)
1868*40e56410SVille Syrjälä 		ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
186991d131d2SDaniel Vetter 
1870cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1871cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
187223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1873cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1874cfc33bf7SVille Syrjälä 				 port_name(port));
1875cfc33bf7SVille Syrjälä 	}
187623e81d69SAdam Jackson 
187723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1878ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
187923e81d69SAdam Jackson 
188023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1881515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
188223e81d69SAdam Jackson 
188323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
188423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
188523e81d69SAdam Jackson 
188623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
188723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
188823e81d69SAdam Jackson 
188923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1890055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
189123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
189223e81d69SAdam Jackson 					 pipe_name(pipe),
189323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18948664281bSPaulo Zanoni 
18958664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
18968664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
189723e81d69SAdam Jackson }
189823e81d69SAdam Jackson 
18996dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
19006dbf30ceSVille Syrjälä {
19016dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
19026dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
19036dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
19046dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
19056dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
19066dbf30ceSVille Syrjälä 
19076dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
19086dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19096dbf30ceSVille Syrjälä 
19106dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19116dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19126dbf30ceSVille Syrjälä 
19136dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19146dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
191574c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
19166dbf30ceSVille Syrjälä 	}
19176dbf30ceSVille Syrjälä 
19186dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19196dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19206dbf30ceSVille Syrjälä 
19216dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19226dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19236dbf30ceSVille Syrjälä 
19246dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
19256dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19266dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19276dbf30ceSVille Syrjälä 	}
19286dbf30ceSVille Syrjälä 
19296dbf30ceSVille Syrjälä 	if (pin_mask)
19306dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
19316dbf30ceSVille Syrjälä 
19326dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
19336dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
19346dbf30ceSVille Syrjälä }
19356dbf30ceSVille Syrjälä 
1936*40e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
1937*40e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1938c008bc6eSPaulo Zanoni {
1939*40e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
1940e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1941e4ce95aaSVille Syrjälä 
1942e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
1943e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
1944e4ce95aaSVille Syrjälä 
1945e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1946*40e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1947e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
1948*40e56410SVille Syrjälä 
1949e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1950e4ce95aaSVille Syrjälä }
1951c008bc6eSPaulo Zanoni 
1952*40e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1953*40e56410SVille Syrjälä {
1954*40e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
1955*40e56410SVille Syrjälä 	enum pipe pipe;
1956*40e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
1957*40e56410SVille Syrjälä 
1958*40e56410SVille Syrjälä 	if (hotplug_trigger)
1959*40e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
1960*40e56410SVille Syrjälä 
1961c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1962c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1963c008bc6eSPaulo Zanoni 
1964c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1965c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1966c008bc6eSPaulo Zanoni 
1967c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1968c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1969c008bc6eSPaulo Zanoni 
1970055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1971d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
1972d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1973d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
1974c008bc6eSPaulo Zanoni 
197540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
19761f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1977c008bc6eSPaulo Zanoni 
197840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
197940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
19805b3a856bSDaniel Vetter 
198140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
198240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
198340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
198440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1985c008bc6eSPaulo Zanoni 		}
1986c008bc6eSPaulo Zanoni 	}
1987c008bc6eSPaulo Zanoni 
1988c008bc6eSPaulo Zanoni 	/* check event from PCH */
1989c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1990c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1991c008bc6eSPaulo Zanoni 
1992c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1993c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1994c008bc6eSPaulo Zanoni 		else
1995c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1996c008bc6eSPaulo Zanoni 
1997c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1998c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1999c008bc6eSPaulo Zanoni 	}
2000c008bc6eSPaulo Zanoni 
2001c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2002c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2003c008bc6eSPaulo Zanoni }
2004c008bc6eSPaulo Zanoni 
20059719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20069719fb98SPaulo Zanoni {
20079719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
200807d27e20SDamien Lespiau 	enum pipe pipe;
200923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
201023bb4cb5SVille Syrjälä 
2011*40e56410SVille Syrjälä 	if (hotplug_trigger)
2012*40e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
20139719fb98SPaulo Zanoni 
20149719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20159719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20169719fb98SPaulo Zanoni 
20179719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20189719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20199719fb98SPaulo Zanoni 
20209719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20219719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20229719fb98SPaulo Zanoni 
2023055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2024d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2025d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2026d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
202740da17c2SDaniel Vetter 
202840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
202907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
203007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
203107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20329719fb98SPaulo Zanoni 		}
20339719fb98SPaulo Zanoni 	}
20349719fb98SPaulo Zanoni 
20359719fb98SPaulo Zanoni 	/* check event from PCH */
20369719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20379719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20389719fb98SPaulo Zanoni 
20399719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20409719fb98SPaulo Zanoni 
20419719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20429719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20439719fb98SPaulo Zanoni 	}
20449719fb98SPaulo Zanoni }
20459719fb98SPaulo Zanoni 
204672c90f62SOscar Mateo /*
204772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
204872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
204972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
205072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
205172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
205272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
205372c90f62SOscar Mateo  */
2054f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2055b1f14ad0SJesse Barnes {
205645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2058f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20590e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2060b1f14ad0SJesse Barnes 
20612dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20622dd2a883SImre Deak 		return IRQ_NONE;
20632dd2a883SImre Deak 
20648664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
20658664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2066907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
20678664281bSPaulo Zanoni 
2068b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2069b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2070b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
207123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
20720e43406bSChris Wilson 
207344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
207444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
207544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
207644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
207744498aeaSPaulo Zanoni 	 * due to its back queue). */
2078ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
207944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
208044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
208144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2082ab5c608bSBen Widawsky 	}
208344498aeaSPaulo Zanoni 
208472c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
208572c90f62SOscar Mateo 
20860e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
20870e43406bSChris Wilson 	if (gt_iir) {
208872c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
208972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2090d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
20910e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2092d8fc8a47SPaulo Zanoni 		else
2093d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
20940e43406bSChris Wilson 	}
2095b1f14ad0SJesse Barnes 
2096b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
20970e43406bSChris Wilson 	if (de_iir) {
209872c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
209972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2100f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21019719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2102f1af8fc1SPaulo Zanoni 		else
2103f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21040e43406bSChris Wilson 	}
21050e43406bSChris Wilson 
2106f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2107f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21080e43406bSChris Wilson 		if (pm_iir) {
2109b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21100e43406bSChris Wilson 			ret = IRQ_HANDLED;
211172c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21120e43406bSChris Wilson 		}
2113f1af8fc1SPaulo Zanoni 	}
2114b1f14ad0SJesse Barnes 
2115b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2116b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2117ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
211844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
211944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2120ab5c608bSBen Widawsky 	}
2121b1f14ad0SJesse Barnes 
2122b1f14ad0SJesse Barnes 	return ret;
2123b1f14ad0SJesse Barnes }
2124b1f14ad0SJesse Barnes 
2125*40e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
2126*40e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2127d04a492dSShashank Sharma {
2128cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2129cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2130d04a492dSShashank Sharma 
2131cebd87a0SVille Syrjälä 	dig_hotplug_reg = I915_READ(BXT_HOTPLUG_CTL);
2132cebd87a0SVille Syrjälä 	I915_WRITE(BXT_HOTPLUG_CTL, dig_hotplug_reg);
2133d04a492dSShashank Sharma 
2134cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
2135*40e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2136cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
2137*40e56410SVille Syrjälä 
2138475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2139d04a492dSShashank Sharma }
2140d04a492dSShashank Sharma 
2141abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2142abd58f01SBen Widawsky {
2143abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2144abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2145abd58f01SBen Widawsky 	u32 master_ctl;
2146abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2147abd58f01SBen Widawsky 	uint32_t tmp = 0;
2148c42664ccSDaniel Vetter 	enum pipe pipe;
214988e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
215088e04703SJesse Barnes 
21512dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21522dd2a883SImre Deak 		return IRQ_NONE;
21532dd2a883SImre Deak 
215488e04703SJesse Barnes 	if (IS_GEN9(dev))
215588e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
215688e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2157abd58f01SBen Widawsky 
2158cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2159abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2160abd58f01SBen Widawsky 	if (!master_ctl)
2161abd58f01SBen Widawsky 		return IRQ_NONE;
2162abd58f01SBen Widawsky 
2163cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2164abd58f01SBen Widawsky 
216538cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
216638cc46d7SOscar Mateo 
216774cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2168abd58f01SBen Widawsky 
2169abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2170abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2171abd58f01SBen Widawsky 		if (tmp) {
2172abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2173abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
217438cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
217538cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
217638cc46d7SOscar Mateo 			else
217738cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2178abd58f01SBen Widawsky 		}
217938cc46d7SOscar Mateo 		else
218038cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2181abd58f01SBen Widawsky 	}
2182abd58f01SBen Widawsky 
21836d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
21846d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
21856d766f02SDaniel Vetter 		if (tmp) {
2186d04a492dSShashank Sharma 			bool found = false;
2187cebd87a0SVille Syrjälä 			u32 hotplug_trigger = 0;
2188cebd87a0SVille Syrjälä 
2189cebd87a0SVille Syrjälä 			if (IS_BROXTON(dev_priv))
2190cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & BXT_DE_PORT_HOTPLUG_MASK;
2191cebd87a0SVille Syrjälä 			else if (IS_BROADWELL(dev_priv))
2192cebd87a0SVille Syrjälä 				hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2193d04a492dSShashank Sharma 
21946d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
21956d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
219688e04703SJesse Barnes 
2197d04a492dSShashank Sharma 			if (tmp & aux_mask) {
219838cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2199d04a492dSShashank Sharma 				found = true;
2200d04a492dSShashank Sharma 			}
2201d04a492dSShashank Sharma 
2202*40e56410SVille Syrjälä 			if (hotplug_trigger) {
2203*40e56410SVille Syrjälä 				if (IS_BROXTON(dev))
2204*40e56410SVille Syrjälä 					bxt_hpd_irq_handler(dev, hotplug_trigger, hpd_bxt);
2205*40e56410SVille Syrjälä 				else
2206*40e56410SVille Syrjälä 					ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_bdw);
2207d04a492dSShashank Sharma 				found = true;
2208d04a492dSShashank Sharma 			}
2209d04a492dSShashank Sharma 
22109e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
22119e63743eSShashank Sharma 				gmbus_irq_handler(dev);
22129e63743eSShashank Sharma 				found = true;
22139e63743eSShashank Sharma 			}
22149e63743eSShashank Sharma 
2215d04a492dSShashank Sharma 			if (!found)
221638cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22176d766f02SDaniel Vetter 		}
221838cc46d7SOscar Mateo 		else
221938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22206d766f02SDaniel Vetter 	}
22216d766f02SDaniel Vetter 
2222055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2223770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2224abd58f01SBen Widawsky 
2225c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2226c42664ccSDaniel Vetter 			continue;
2227c42664ccSDaniel Vetter 
2228abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
222938cc46d7SOscar Mateo 		if (pipe_iir) {
223038cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
223138cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2232770de83dSDamien Lespiau 
2233d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2234d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2235d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2236abd58f01SBen Widawsky 
2237770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2238770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2239770de83dSDamien Lespiau 			else
2240770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2241770de83dSDamien Lespiau 
2242770de83dSDamien Lespiau 			if (flip_done) {
2243abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2244abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2245abd58f01SBen Widawsky 			}
2246abd58f01SBen Widawsky 
22470fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22480fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22490fbe7870SDaniel Vetter 
22501f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22511f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22521f7247c0SDaniel Vetter 								    pipe);
225338d83c96SDaniel Vetter 
2254770de83dSDamien Lespiau 
2255770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2256770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2257770de83dSDamien Lespiau 			else
2258770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2259770de83dSDamien Lespiau 
2260770de83dSDamien Lespiau 			if (fault_errors)
226130100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
226230100f2bSDaniel Vetter 					  pipe_name(pipe),
226330100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2264c42664ccSDaniel Vetter 		} else
2265abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2266abd58f01SBen Widawsky 	}
2267abd58f01SBen Widawsky 
2268266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2269266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
227092d03a80SDaniel Vetter 		/*
227192d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
227292d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
227392d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
227492d03a80SDaniel Vetter 		 */
227592d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
227692d03a80SDaniel Vetter 		if (pch_iir) {
227792d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
227892d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
22796dbf30ceSVille Syrjälä 
22806dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
22816dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
22826dbf30ceSVille Syrjälä 			else
228338cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
228438cc46d7SOscar Mateo 		} else
228538cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
228638cc46d7SOscar Mateo 
228792d03a80SDaniel Vetter 	}
228892d03a80SDaniel Vetter 
2289cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2290cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2291abd58f01SBen Widawsky 
2292abd58f01SBen Widawsky 	return ret;
2293abd58f01SBen Widawsky }
2294abd58f01SBen Widawsky 
229517e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
229617e1df07SDaniel Vetter 			       bool reset_completed)
229717e1df07SDaniel Vetter {
2298a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
229917e1df07SDaniel Vetter 	int i;
230017e1df07SDaniel Vetter 
230117e1df07SDaniel Vetter 	/*
230217e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
230317e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
230417e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
230517e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
230617e1df07SDaniel Vetter 	 */
230717e1df07SDaniel Vetter 
230817e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
230917e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
231017e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
231117e1df07SDaniel Vetter 
231217e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
231317e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
231417e1df07SDaniel Vetter 
231517e1df07SDaniel Vetter 	/*
231617e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
231717e1df07SDaniel Vetter 	 * reset state is cleared.
231817e1df07SDaniel Vetter 	 */
231917e1df07SDaniel Vetter 	if (reset_completed)
232017e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
232117e1df07SDaniel Vetter }
232217e1df07SDaniel Vetter 
23238a905236SJesse Barnes /**
2324b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23258a905236SJesse Barnes  *
23268a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23278a905236SJesse Barnes  * was detected.
23288a905236SJesse Barnes  */
2329b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23308a905236SJesse Barnes {
2331b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2332b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2333cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2334cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2335cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
233617e1df07SDaniel Vetter 	int ret;
23378a905236SJesse Barnes 
23385bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23398a905236SJesse Barnes 
23407db0ba24SDaniel Vetter 	/*
23417db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23427db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23437db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23447db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23457db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23467db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23477db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23487db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23497db0ba24SDaniel Vetter 	 */
23507db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
235144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23525bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23537db0ba24SDaniel Vetter 				   reset_event);
23541f83fee0SDaniel Vetter 
235517e1df07SDaniel Vetter 		/*
2356f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2357f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2358f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2359f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2360f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2361f454c694SImre Deak 		 */
2362f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23637514747dSVille Syrjälä 
23647514747dSVille Syrjälä 		intel_prepare_reset(dev);
23657514747dSVille Syrjälä 
2366f454c694SImre Deak 		/*
236717e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
236817e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
236917e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
237017e1df07SDaniel Vetter 		 * deadlocks with the reset work.
237117e1df07SDaniel Vetter 		 */
2372f69061beSDaniel Vetter 		ret = i915_reset(dev);
2373f69061beSDaniel Vetter 
23747514747dSVille Syrjälä 		intel_finish_reset(dev);
237517e1df07SDaniel Vetter 
2376f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2377f454c694SImre Deak 
2378f69061beSDaniel Vetter 		if (ret == 0) {
2379f69061beSDaniel Vetter 			/*
2380f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2381f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2382f69061beSDaniel Vetter 			 * complete.
2383f69061beSDaniel Vetter 			 *
2384f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2385f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2386f69061beSDaniel Vetter 			 * updates before
2387f69061beSDaniel Vetter 			 * the counter increment.
2388f69061beSDaniel Vetter 			 */
23894e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2390f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2391f69061beSDaniel Vetter 
23925bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2393f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
23941f83fee0SDaniel Vetter 		} else {
23952ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2396f316a42cSBen Gamari 		}
23971f83fee0SDaniel Vetter 
239817e1df07SDaniel Vetter 		/*
239917e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
240017e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
240117e1df07SDaniel Vetter 		 */
240217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2403f316a42cSBen Gamari 	}
24048a905236SJesse Barnes }
24058a905236SJesse Barnes 
240635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2407c0e09200SDave Airlie {
24088a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2409bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
241063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2411050ee91fSBen Widawsky 	int pipe, i;
241263eeaf38SJesse Barnes 
241335aed2e6SChris Wilson 	if (!eir)
241435aed2e6SChris Wilson 		return;
241563eeaf38SJesse Barnes 
2416a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24178a905236SJesse Barnes 
2418bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2419bd9854f9SBen Widawsky 
24208a905236SJesse Barnes 	if (IS_G4X(dev)) {
24218a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24228a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24238a905236SJesse Barnes 
2424a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2425a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2426050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2427050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2428a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2429a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24308a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24313143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24328a905236SJesse Barnes 		}
24338a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24348a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2435a70491ccSJoe Perches 			pr_err("page table error\n");
2436a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24378a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24383143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24398a905236SJesse Barnes 		}
24408a905236SJesse Barnes 	}
24418a905236SJesse Barnes 
2442a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
244363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
244463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2445a70491ccSJoe Perches 			pr_err("page table error\n");
2446a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
244763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24483143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
244963eeaf38SJesse Barnes 		}
24508a905236SJesse Barnes 	}
24518a905236SJesse Barnes 
245263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2453a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2454055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2455a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24569db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
245763eeaf38SJesse Barnes 		/* pipestat has already been acked */
245863eeaf38SJesse Barnes 	}
245963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2460a70491ccSJoe Perches 		pr_err("instruction error\n");
2461a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2462050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2463050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2464a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
246563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
246663eeaf38SJesse Barnes 
2467a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2468a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2469a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
247063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24713143a2bfSChris Wilson 			POSTING_READ(IPEIR);
247263eeaf38SJesse Barnes 		} else {
247363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
247463eeaf38SJesse Barnes 
2475a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2476a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2477a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2478a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
247963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24803143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
248163eeaf38SJesse Barnes 		}
248263eeaf38SJesse Barnes 	}
248363eeaf38SJesse Barnes 
248463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24853143a2bfSChris Wilson 	POSTING_READ(EIR);
248663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
248763eeaf38SJesse Barnes 	if (eir) {
248863eeaf38SJesse Barnes 		/*
248963eeaf38SJesse Barnes 		 * some errors might have become stuck,
249063eeaf38SJesse Barnes 		 * mask them.
249163eeaf38SJesse Barnes 		 */
249263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
249363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
249463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
249563eeaf38SJesse Barnes 	}
249635aed2e6SChris Wilson }
249735aed2e6SChris Wilson 
249835aed2e6SChris Wilson /**
2499b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
250035aed2e6SChris Wilson  * @dev: drm device
250135aed2e6SChris Wilson  *
2502b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
250335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
250435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
250535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
250635aed2e6SChris Wilson  * of a ring dump etc.).
250735aed2e6SChris Wilson  */
250858174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
250958174462SMika Kuoppala 		       const char *fmt, ...)
251035aed2e6SChris Wilson {
251135aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
251258174462SMika Kuoppala 	va_list args;
251358174462SMika Kuoppala 	char error_msg[80];
251435aed2e6SChris Wilson 
251558174462SMika Kuoppala 	va_start(args, fmt);
251658174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
251758174462SMika Kuoppala 	va_end(args);
251858174462SMika Kuoppala 
251958174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
252035aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25218a905236SJesse Barnes 
2522ba1234d1SBen Gamari 	if (wedged) {
2523f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2524f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2525ba1234d1SBen Gamari 
252611ed50ecSBen Gamari 		/*
2527b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2528b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2529b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
253017e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
253117e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
253217e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
253317e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
253417e1df07SDaniel Vetter 		 *
253517e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
253617e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
253717e1df07SDaniel Vetter 		 * counter atomic_t.
253811ed50ecSBen Gamari 		 */
253917e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
254011ed50ecSBen Gamari 	}
254111ed50ecSBen Gamari 
2542b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25438a905236SJesse Barnes }
25448a905236SJesse Barnes 
254542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
254642f52ef8SKeith Packard  * we use as a pipe index
254742f52ef8SKeith Packard  */
2548f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25490a3e67a4SJesse Barnes {
25502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2551e9d21d7fSKeith Packard 	unsigned long irqflags;
255271e0ffa5SJesse Barnes 
25531ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2554f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25557c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2556755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25570a3e67a4SJesse Barnes 	else
25587c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2559755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25601ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25618692d00eSChris Wilson 
25620a3e67a4SJesse Barnes 	return 0;
25630a3e67a4SJesse Barnes }
25640a3e67a4SJesse Barnes 
2565f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2566f796cf8fSJesse Barnes {
25672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2568f796cf8fSJesse Barnes 	unsigned long irqflags;
2569b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
257040da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2571f796cf8fSJesse Barnes 
2572f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2573b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2574b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2575b1f14ad0SJesse Barnes 
2576b1f14ad0SJesse Barnes 	return 0;
2577b1f14ad0SJesse Barnes }
2578b1f14ad0SJesse Barnes 
25797e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
25807e231dbeSJesse Barnes {
25812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25827e231dbeSJesse Barnes 	unsigned long irqflags;
25837e231dbeSJesse Barnes 
25847e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
258531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2586755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25877e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25887e231dbeSJesse Barnes 
25897e231dbeSJesse Barnes 	return 0;
25907e231dbeSJesse Barnes }
25917e231dbeSJesse Barnes 
2592abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2593abd58f01SBen Widawsky {
2594abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2595abd58f01SBen Widawsky 	unsigned long irqflags;
2596abd58f01SBen Widawsky 
2597abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25987167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
25997167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2600abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2601abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2602abd58f01SBen Widawsky 	return 0;
2603abd58f01SBen Widawsky }
2604abd58f01SBen Widawsky 
260542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
260642f52ef8SKeith Packard  * we use as a pipe index
260742f52ef8SKeith Packard  */
2608f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26090a3e67a4SJesse Barnes {
26102d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2611e9d21d7fSKeith Packard 	unsigned long irqflags;
26120a3e67a4SJesse Barnes 
26131ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26147c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2615755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2616755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26171ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26180a3e67a4SJesse Barnes }
26190a3e67a4SJesse Barnes 
2620f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2621f796cf8fSJesse Barnes {
26222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2623f796cf8fSJesse Barnes 	unsigned long irqflags;
2624b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
262540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2626f796cf8fSJesse Barnes 
2627f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2628b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2629b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2630b1f14ad0SJesse Barnes }
2631b1f14ad0SJesse Barnes 
26327e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26337e231dbeSJesse Barnes {
26342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26357e231dbeSJesse Barnes 	unsigned long irqflags;
26367e231dbeSJesse Barnes 
26377e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
263831acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2639755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26407e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26417e231dbeSJesse Barnes }
26427e231dbeSJesse Barnes 
2643abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2644abd58f01SBen Widawsky {
2645abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2646abd58f01SBen Widawsky 	unsigned long irqflags;
2647abd58f01SBen Widawsky 
2648abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26497167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26507167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2651abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2652abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2653abd58f01SBen Widawsky }
2654abd58f01SBen Widawsky 
26559107e9d2SChris Wilson static bool
265694f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2657893eead0SChris Wilson {
26589107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
265994f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2660f65d9421SBen Gamari }
2661f65d9421SBen Gamari 
2662a028c4b0SDaniel Vetter static bool
2663a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2664a028c4b0SDaniel Vetter {
2665a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2666a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2667a028c4b0SDaniel Vetter 	} else {
2668a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2669a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2670a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2671a028c4b0SDaniel Vetter 	}
2672a028c4b0SDaniel Vetter }
2673a028c4b0SDaniel Vetter 
2674a4872ba6SOscar Mateo static struct intel_engine_cs *
2675a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2676921d42eaSDaniel Vetter {
2677921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2678a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2679921d42eaSDaniel Vetter 	int i;
2680921d42eaSDaniel Vetter 
2681921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2682a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2683a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2684a6cdb93aSRodrigo Vivi 				continue;
2685a6cdb93aSRodrigo Vivi 
2686a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2687a6cdb93aSRodrigo Vivi 				return signaller;
2688a6cdb93aSRodrigo Vivi 		}
2689921d42eaSDaniel Vetter 	} else {
2690921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2691921d42eaSDaniel Vetter 
2692921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2693921d42eaSDaniel Vetter 			if(ring == signaller)
2694921d42eaSDaniel Vetter 				continue;
2695921d42eaSDaniel Vetter 
2696ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2697921d42eaSDaniel Vetter 				return signaller;
2698921d42eaSDaniel Vetter 		}
2699921d42eaSDaniel Vetter 	}
2700921d42eaSDaniel Vetter 
2701a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2702a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2703921d42eaSDaniel Vetter 
2704921d42eaSDaniel Vetter 	return NULL;
2705921d42eaSDaniel Vetter }
2706921d42eaSDaniel Vetter 
2707a4872ba6SOscar Mateo static struct intel_engine_cs *
2708a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2709a24a11e6SChris Wilson {
2710a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
271188fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2712a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2713a6cdb93aSRodrigo Vivi 	int i, backwards;
2714a24a11e6SChris Wilson 
2715a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2716a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27176274f212SChris Wilson 		return NULL;
2718a24a11e6SChris Wilson 
271988fe429dSDaniel Vetter 	/*
272088fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
272188fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2722a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2723a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
272488fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
272588fe429dSDaniel Vetter 	 * ringbuffer itself.
2726a24a11e6SChris Wilson 	 */
272788fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2728a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
272988fe429dSDaniel Vetter 
2730a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
273188fe429dSDaniel Vetter 		/*
273288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
273388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
273488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
273588fe429dSDaniel Vetter 		 */
2736ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
273788fe429dSDaniel Vetter 
273888fe429dSDaniel Vetter 		/* This here seems to blow up */
2739ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2740a24a11e6SChris Wilson 		if (cmd == ipehr)
2741a24a11e6SChris Wilson 			break;
2742a24a11e6SChris Wilson 
274388fe429dSDaniel Vetter 		head -= 4;
274488fe429dSDaniel Vetter 	}
2745a24a11e6SChris Wilson 
274688fe429dSDaniel Vetter 	if (!i)
274788fe429dSDaniel Vetter 		return NULL;
274888fe429dSDaniel Vetter 
2749ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2750a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2751a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2752a6cdb93aSRodrigo Vivi 		offset <<= 32;
2753a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2754a6cdb93aSRodrigo Vivi 	}
2755a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2756a24a11e6SChris Wilson }
2757a24a11e6SChris Wilson 
2758a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27596274f212SChris Wilson {
27606274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2761a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2762a0d036b0SChris Wilson 	u32 seqno;
27636274f212SChris Wilson 
27644be17381SChris Wilson 	ring->hangcheck.deadlock++;
27656274f212SChris Wilson 
27666274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
27674be17381SChris Wilson 	if (signaller == NULL)
27684be17381SChris Wilson 		return -1;
27694be17381SChris Wilson 
27704be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
27714be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
27726274f212SChris Wilson 		return -1;
27736274f212SChris Wilson 
27744be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
27754be17381SChris Wilson 		return 1;
27764be17381SChris Wilson 
2777a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2778a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2779a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
27804be17381SChris Wilson 		return -1;
27814be17381SChris Wilson 
27824be17381SChris Wilson 	return 0;
27836274f212SChris Wilson }
27846274f212SChris Wilson 
27856274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
27866274f212SChris Wilson {
2787a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
27886274f212SChris Wilson 	int i;
27896274f212SChris Wilson 
27906274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
27914be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
27926274f212SChris Wilson }
27936274f212SChris Wilson 
2794ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2795a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
27961ec14ad3SChris Wilson {
27971ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
27981ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
27999107e9d2SChris Wilson 	u32 tmp;
28009107e9d2SChris Wilson 
2801f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2802f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2803f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2804f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2805f260fe7bSMika Kuoppala 		}
2806f260fe7bSMika Kuoppala 
2807f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2808f260fe7bSMika Kuoppala 	}
28096274f212SChris Wilson 
28109107e9d2SChris Wilson 	if (IS_GEN2(dev))
2811f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28129107e9d2SChris Wilson 
28139107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28149107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28159107e9d2SChris Wilson 	 * and break the hang. This should work on
28169107e9d2SChris Wilson 	 * all but the second generation chipsets.
28179107e9d2SChris Wilson 	 */
28189107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28191ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
282058174462SMika Kuoppala 		i915_handle_error(dev, false,
282158174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28221ec14ad3SChris Wilson 				  ring->name);
28231ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2824f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28251ec14ad3SChris Wilson 	}
2826a24a11e6SChris Wilson 
28276274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28286274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28296274f212SChris Wilson 		default:
2830f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28316274f212SChris Wilson 		case 1:
283258174462SMika Kuoppala 			i915_handle_error(dev, false,
283358174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2834a24a11e6SChris Wilson 					  ring->name);
2835a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2836f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28376274f212SChris Wilson 		case 0:
2838f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28396274f212SChris Wilson 		}
28409107e9d2SChris Wilson 	}
28419107e9d2SChris Wilson 
2842f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2843a24a11e6SChris Wilson }
2844d1e61e7fSChris Wilson 
2845737b1506SChris Wilson /*
2846f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
284705407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
284805407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
284905407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
285005407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
285105407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2852f65d9421SBen Gamari  */
2853737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2854f65d9421SBen Gamari {
2855737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2856737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2857737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2858737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2859a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2860b4519513SChris Wilson 	int i;
286105407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28629107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28639107e9d2SChris Wilson #define BUSY 1
28649107e9d2SChris Wilson #define KICK 5
28659107e9d2SChris Wilson #define HUNG 20
2866893eead0SChris Wilson 
2867d330a953SJani Nikula 	if (!i915.enable_hangcheck)
28683e0dc6b0SBen Widawsky 		return;
28693e0dc6b0SBen Widawsky 
2870b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
287150877445SChris Wilson 		u64 acthd;
287250877445SChris Wilson 		u32 seqno;
28739107e9d2SChris Wilson 		bool busy = true;
2874b4519513SChris Wilson 
28756274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
28766274f212SChris Wilson 
287705407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
287805407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
287905407ff8SMika Kuoppala 
288005407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
288194f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2882da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2883da661464SMika Kuoppala 
28849107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
28859107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2886094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2887f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
28889107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
28899107e9d2SChris Wilson 								  ring->name);
2890f4adcd24SDaniel Vetter 						else
2891f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2892f4adcd24SDaniel Vetter 								 ring->name);
28939107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2894094f9a54SChris Wilson 					}
2895094f9a54SChris Wilson 					/* Safeguard against driver failure */
2896094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
28979107e9d2SChris Wilson 				} else
28989107e9d2SChris Wilson 					busy = false;
289905407ff8SMika Kuoppala 			} else {
29006274f212SChris Wilson 				/* We always increment the hangcheck score
29016274f212SChris Wilson 				 * if the ring is busy and still processing
29026274f212SChris Wilson 				 * the same request, so that no single request
29036274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29046274f212SChris Wilson 				 * batches). The only time we do not increment
29056274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29066274f212SChris Wilson 				 * ring is in a legitimate wait for another
29076274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29086274f212SChris Wilson 				 * victim and we want to be sure we catch the
29096274f212SChris Wilson 				 * right culprit. Then every time we do kick
29106274f212SChris Wilson 				 * the ring, add a small increment to the
29116274f212SChris Wilson 				 * score so that we can catch a batch that is
29126274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29136274f212SChris Wilson 				 * for stalling the machine.
29149107e9d2SChris Wilson 				 */
2915ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2916ad8beaeaSMika Kuoppala 								    acthd);
2917ad8beaeaSMika Kuoppala 
2918ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2919da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2920f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2921f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2922f260fe7bSMika Kuoppala 					break;
2923f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2924ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29256274f212SChris Wilson 					break;
2926f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2927ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29286274f212SChris Wilson 					break;
2929f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2930ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29316274f212SChris Wilson 					stuck[i] = true;
29326274f212SChris Wilson 					break;
29336274f212SChris Wilson 				}
293405407ff8SMika Kuoppala 			}
29359107e9d2SChris Wilson 		} else {
2936da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2937da661464SMika Kuoppala 
29389107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29399107e9d2SChris Wilson 			 * attempts across multiple batches.
29409107e9d2SChris Wilson 			 */
29419107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29429107e9d2SChris Wilson 				ring->hangcheck.score--;
2943f260fe7bSMika Kuoppala 
2944f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2945cbb465e7SChris Wilson 		}
2946f65d9421SBen Gamari 
294705407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
294805407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29499107e9d2SChris Wilson 		busy_count += busy;
295005407ff8SMika Kuoppala 	}
295105407ff8SMika Kuoppala 
295205407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2953b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2954b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
295505407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2956a43adf07SChris Wilson 				 ring->name);
2957a43adf07SChris Wilson 			rings_hung++;
295805407ff8SMika Kuoppala 		}
295905407ff8SMika Kuoppala 	}
296005407ff8SMika Kuoppala 
296105407ff8SMika Kuoppala 	if (rings_hung)
296258174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
296305407ff8SMika Kuoppala 
296405407ff8SMika Kuoppala 	if (busy_count)
296505407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
296605407ff8SMika Kuoppala 		 * being added */
296710cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
296810cd45b6SMika Kuoppala }
296910cd45b6SMika Kuoppala 
297010cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
297110cd45b6SMika Kuoppala {
2972737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2973672e7b7cSChris Wilson 
2974d330a953SJani Nikula 	if (!i915.enable_hangcheck)
297510cd45b6SMika Kuoppala 		return;
297610cd45b6SMika Kuoppala 
2977737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2978737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2979737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2980737b1506SChris Wilson 	 */
2981737b1506SChris Wilson 
2982737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
2983737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
2984f65d9421SBen Gamari }
2985f65d9421SBen Gamari 
29861c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
298791738a95SPaulo Zanoni {
298891738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
298991738a95SPaulo Zanoni 
299091738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
299191738a95SPaulo Zanoni 		return;
299291738a95SPaulo Zanoni 
2993f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2994105b122eSPaulo Zanoni 
2995105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2996105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2997622364b6SPaulo Zanoni }
2998105b122eSPaulo Zanoni 
299991738a95SPaulo Zanoni /*
3000622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3001622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3002622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3003622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3004622364b6SPaulo Zanoni  *
3005622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
300691738a95SPaulo Zanoni  */
3007622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3008622364b6SPaulo Zanoni {
3009622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3010622364b6SPaulo Zanoni 
3011622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3012622364b6SPaulo Zanoni 		return;
3013622364b6SPaulo Zanoni 
3014622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
301591738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
301691738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
301791738a95SPaulo Zanoni }
301891738a95SPaulo Zanoni 
30197c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3020d18ea1b5SDaniel Vetter {
3021d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3022d18ea1b5SDaniel Vetter 
3023f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3024a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3025f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3026d18ea1b5SDaniel Vetter }
3027d18ea1b5SDaniel Vetter 
3028c0e09200SDave Airlie /* drm_dma.h hooks
3029c0e09200SDave Airlie */
3030be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3031036a4a7dSZhenyu Wang {
30322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3033036a4a7dSZhenyu Wang 
30340c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3035bdfcdb63SDaniel Vetter 
3036f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3037c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3038c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3039036a4a7dSZhenyu Wang 
30407c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3041c650156aSZhenyu Wang 
30421c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30437d99163dSBen Widawsky }
30447d99163dSBen Widawsky 
304570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
304670591a41SVille Syrjälä {
304770591a41SVille Syrjälä 	enum pipe pipe;
304870591a41SVille Syrjälä 
304970591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
305070591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
305170591a41SVille Syrjälä 
305270591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
305370591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
305470591a41SVille Syrjälä 
305570591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
305670591a41SVille Syrjälä }
305770591a41SVille Syrjälä 
30587e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30597e231dbeSJesse Barnes {
30602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30617e231dbeSJesse Barnes 
30627e231dbeSJesse Barnes 	/* VLV magic */
30637e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30647e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30657e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30667e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30677e231dbeSJesse Barnes 
30687c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30697e231dbeSJesse Barnes 
30707c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
30717e231dbeSJesse Barnes 
307270591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
30737e231dbeSJesse Barnes }
30747e231dbeSJesse Barnes 
3075d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3076d6e3cca3SDaniel Vetter {
3077d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3078d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3079d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3080d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3081d6e3cca3SDaniel Vetter }
3082d6e3cca3SDaniel Vetter 
3083823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3084abd58f01SBen Widawsky {
3085abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3086abd58f01SBen Widawsky 	int pipe;
3087abd58f01SBen Widawsky 
3088abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3089abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3090abd58f01SBen Widawsky 
3091d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3092abd58f01SBen Widawsky 
3093055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3094f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3095813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3096f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3097abd58f01SBen Widawsky 
3098f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3099f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3100f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3101abd58f01SBen Widawsky 
3102266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31031c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3104abd58f01SBen Widawsky }
3105abd58f01SBen Widawsky 
31064c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31074c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3108d49bdb0eSPaulo Zanoni {
31091180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3110d49bdb0eSPaulo Zanoni 
311113321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3112d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3113d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3114d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3115d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31164c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31174c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31184c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31191180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31204c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31214c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31224c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31231180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
312413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3125d49bdb0eSPaulo Zanoni }
3126d49bdb0eSPaulo Zanoni 
312743f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
312843f328d7SVille Syrjälä {
312943f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
313043f328d7SVille Syrjälä 
313143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
313243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
313343f328d7SVille Syrjälä 
3134d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
313543f328d7SVille Syrjälä 
313643f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
313743f328d7SVille Syrjälä 
313843f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
313943f328d7SVille Syrjälä 
314070591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
314143f328d7SVille Syrjälä }
314243f328d7SVille Syrjälä 
314387a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
314487a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
314587a02106SVille Syrjälä {
314687a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
314787a02106SVille Syrjälä 	struct intel_encoder *encoder;
314887a02106SVille Syrjälä 	u32 enabled_irqs = 0;
314987a02106SVille Syrjälä 
315087a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
315187a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
315287a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
315387a02106SVille Syrjälä 
315487a02106SVille Syrjälä 	return enabled_irqs;
315587a02106SVille Syrjälä }
315687a02106SVille Syrjälä 
315782a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
315882a28bcfSDaniel Vetter {
31592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
316087a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
316182a28bcfSDaniel Vetter 
316282a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3163fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
316487a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
316582a28bcfSDaniel Vetter 	} else {
3166fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
316787a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
316882a28bcfSDaniel Vetter 	}
316982a28bcfSDaniel Vetter 
3170fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
317182a28bcfSDaniel Vetter 
31727fe0b973SKeith Packard 	/*
31737fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31746dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
31756dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
31767fe0b973SKeith Packard 	 */
31777fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31787fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31797fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31807fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31817fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31820b2eb33eSVille Syrjälä 	/*
31830b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
31840b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
31850b2eb33eSVille Syrjälä 	 */
31860b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
31870b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
31887fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31896dbf30ceSVille Syrjälä }
319026951cafSXiong Zhang 
31916dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
31926dbf30ceSVille Syrjälä {
31936dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
31946dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
31956dbf30ceSVille Syrjälä 
31966dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
31976dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
31986dbf30ceSVille Syrjälä 
31996dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32006dbf30ceSVille Syrjälä 
32016dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
32026dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32036dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
320474c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
32056dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32066dbf30ceSVille Syrjälä 
320726951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
320826951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
320926951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
321026951cafSXiong Zhang }
32117fe0b973SKeith Packard 
3212e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3213e4ce95aaSVille Syrjälä {
3214e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3215e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3216e4ce95aaSVille Syrjälä 
32173a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
32183a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
32193a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
32203a3b3c7dSVille Syrjälä 
32213a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
32223a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
322323bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
322423bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
32253a3b3c7dSVille Syrjälä 
32263a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
322723bb4cb5SVille Syrjälä 	} else {
3228e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3229e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3230e4ce95aaSVille Syrjälä 
3231e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
32323a3b3c7dSVille Syrjälä 	}
3233e4ce95aaSVille Syrjälä 
3234e4ce95aaSVille Syrjälä 	/*
3235e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3236e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
323723bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3238e4ce95aaSVille Syrjälä 	 */
3239e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3240e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3241e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3242e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3243e4ce95aaSVille Syrjälä 
3244e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3245e4ce95aaSVille Syrjälä }
3246e4ce95aaSVille Syrjälä 
3247e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3248e0a20ad7SShashank Sharma {
3249e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
325087a02106SVille Syrjälä 	u32 hotplug_port;
3251e0a20ad7SShashank Sharma 	u32 hotplug_ctrl;
3252e0a20ad7SShashank Sharma 
325387a02106SVille Syrjälä 	hotplug_port = intel_hpd_enabled_irqs(dev, hpd_bxt);
3254e0a20ad7SShashank Sharma 
3255e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(BXT_HOTPLUG_CTL) & ~BXT_HOTPLUG_CTL_MASK;
3256e0a20ad7SShashank Sharma 
32577f3561beSSonika Jindal 	if (hotplug_port & BXT_DE_PORT_HP_DDIA)
32587f3561beSSonika Jindal 		hotplug_ctrl |= BXT_DDIA_HPD_ENABLE;
3259e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIB)
3260e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIB_HPD_ENABLE;
3261e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIC)
3262e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIC_HPD_ENABLE;
3263e0a20ad7SShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hotplug_ctrl);
3264e0a20ad7SShashank Sharma 
3265e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IMR) & ~hotplug_port;
3266e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IMR, hotplug_ctrl);
3267e0a20ad7SShashank Sharma 
3268e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IER) | hotplug_port;
3269e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IER, hotplug_ctrl);
3270e0a20ad7SShashank Sharma 	POSTING_READ(GEN8_DE_PORT_IER);
3271e0a20ad7SShashank Sharma }
3272e0a20ad7SShashank Sharma 
3273d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3274d46da437SPaulo Zanoni {
32752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
327682a28bcfSDaniel Vetter 	u32 mask;
3277d46da437SPaulo Zanoni 
3278692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3279692a04cfSDaniel Vetter 		return;
3280692a04cfSDaniel Vetter 
3281105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32825c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3283105b122eSPaulo Zanoni 	else
32845c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32858664281bSPaulo Zanoni 
3286337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3287d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3288d46da437SPaulo Zanoni }
3289d46da437SPaulo Zanoni 
32900a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32910a9a8c91SDaniel Vetter {
32920a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32930a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32940a9a8c91SDaniel Vetter 
32950a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32960a9a8c91SDaniel Vetter 
32970a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3298040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32990a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
330035a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
330135a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
33020a9a8c91SDaniel Vetter 	}
33030a9a8c91SDaniel Vetter 
33040a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33050a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33060a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33070a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33080a9a8c91SDaniel Vetter 	} else {
33090a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33100a9a8c91SDaniel Vetter 	}
33110a9a8c91SDaniel Vetter 
331235079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33130a9a8c91SDaniel Vetter 
33140a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
331578e68d36SImre Deak 		/*
331678e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
331778e68d36SImre Deak 		 * itself is enabled/disabled.
331878e68d36SImre Deak 		 */
33190a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33200a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33210a9a8c91SDaniel Vetter 
3322605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
332335079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33240a9a8c91SDaniel Vetter 	}
33250a9a8c91SDaniel Vetter }
33260a9a8c91SDaniel Vetter 
3327f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3328036a4a7dSZhenyu Wang {
33292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33308e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33318e76f8dcSPaulo Zanoni 
33328e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33338e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33348e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33358e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33365c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33378e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
333823bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
333923bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33408e76f8dcSPaulo Zanoni 	} else {
33418e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3342ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33435b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33445b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33455b3a856bSDaniel Vetter 				DE_POISON);
3346e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3347e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3348e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33498e76f8dcSPaulo Zanoni 	}
3350036a4a7dSZhenyu Wang 
33511ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3352036a4a7dSZhenyu Wang 
33530c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33540c841212SPaulo Zanoni 
3355622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3356622364b6SPaulo Zanoni 
335735079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3358036a4a7dSZhenyu Wang 
33590a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3360036a4a7dSZhenyu Wang 
3361d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33627fe0b973SKeith Packard 
3363f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33646005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33656005ce42SDaniel Vetter 		 *
33666005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33674bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33684bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3369d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3370f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3371d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3372f97108d1SJesse Barnes 	}
3373f97108d1SJesse Barnes 
3374036a4a7dSZhenyu Wang 	return 0;
3375036a4a7dSZhenyu Wang }
3376036a4a7dSZhenyu Wang 
3377f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3378f8b79e58SImre Deak {
3379f8b79e58SImre Deak 	u32 pipestat_mask;
3380f8b79e58SImre Deak 	u32 iir_mask;
3381120dda4fSVille Syrjälä 	enum pipe pipe;
3382f8b79e58SImre Deak 
3383f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3384f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3385f8b79e58SImre Deak 
3386120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3387120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3388f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3389f8b79e58SImre Deak 
3390f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3391f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3392f8b79e58SImre Deak 
3393120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3394120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3395120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3396f8b79e58SImre Deak 
3397f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3398f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3399f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3400120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3401120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3402f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3403f8b79e58SImre Deak 
3404f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3405f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3406f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
340776e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
340876e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3409f8b79e58SImre Deak }
3410f8b79e58SImre Deak 
3411f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3412f8b79e58SImre Deak {
3413f8b79e58SImre Deak 	u32 pipestat_mask;
3414f8b79e58SImre Deak 	u32 iir_mask;
3415120dda4fSVille Syrjälä 	enum pipe pipe;
3416f8b79e58SImre Deak 
3417f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3418f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
34196c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3420120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3421120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3422f8b79e58SImre Deak 
3423f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3424f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
342576e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3426f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3427f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3428f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3429f8b79e58SImre Deak 
3430f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3431f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3432f8b79e58SImre Deak 
3433120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3434120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3435120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3436f8b79e58SImre Deak 
3437f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3438f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3439120dda4fSVille Syrjälä 
3440120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3441120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3442f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3443f8b79e58SImre Deak }
3444f8b79e58SImre Deak 
3445f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3446f8b79e58SImre Deak {
3447f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3448f8b79e58SImre Deak 
3449f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3450f8b79e58SImre Deak 		return;
3451f8b79e58SImre Deak 
3452f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3453f8b79e58SImre Deak 
3454950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3455f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3456f8b79e58SImre Deak }
3457f8b79e58SImre Deak 
3458f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3459f8b79e58SImre Deak {
3460f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3461f8b79e58SImre Deak 
3462f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3463f8b79e58SImre Deak 		return;
3464f8b79e58SImre Deak 
3465f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3466f8b79e58SImre Deak 
3467950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3468f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3469f8b79e58SImre Deak }
3470f8b79e58SImre Deak 
34710e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
34727e231dbeSJesse Barnes {
3473f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34747e231dbeSJesse Barnes 
347520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
347620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
347720afbda2SDaniel Vetter 
34787e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
347976e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
348076e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
348176e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
348276e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
34837e231dbeSJesse Barnes 
3484b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3485b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3486d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3487f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3488f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3489d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
34900e6c9a9eSVille Syrjälä }
34910e6c9a9eSVille Syrjälä 
34920e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34930e6c9a9eSVille Syrjälä {
34940e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34950e6c9a9eSVille Syrjälä 
34960e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
34977e231dbeSJesse Barnes 
34980a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34997e231dbeSJesse Barnes 
35007e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
35017e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
35027e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
35037e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
35047e231dbeSJesse Barnes #endif
35057e231dbeSJesse Barnes 
35067e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
350720afbda2SDaniel Vetter 
350820afbda2SDaniel Vetter 	return 0;
350920afbda2SDaniel Vetter }
351020afbda2SDaniel Vetter 
3511abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3512abd58f01SBen Widawsky {
3513abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3514abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3515abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
351673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3517abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
351873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
351973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3520abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
352173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
352273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
352373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3524abd58f01SBen Widawsky 		0,
352573d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
352673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3527abd58f01SBen Widawsky 		};
3528abd58f01SBen Widawsky 
35290961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35309a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35319a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
353278e68d36SImre Deak 	/*
353378e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
353478e68d36SImre Deak 	 * is enabled/disabled.
353578e68d36SImre Deak 	 */
353678e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
35379a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3538abd58f01SBen Widawsky }
3539abd58f01SBen Widawsky 
3540abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3541abd58f01SBen Widawsky {
3542770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3543770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
35443a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
35453a3b3c7dSVille Syrjälä 	u32 de_port_enables;
35463a3b3c7dSVille Syrjälä 	enum pipe pipe;
3547770de83dSDamien Lespiau 
354888e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3549770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3550770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
35513a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
355288e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
35539e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
35543a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
35553a3b3c7dSVille Syrjälä 	} else {
3556770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3557770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
35583a3b3c7dSVille Syrjälä 	}
3559770de83dSDamien Lespiau 
3560770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3561770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3562770de83dSDamien Lespiau 
35633a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
35643a3b3c7dSVille Syrjälä 	if (IS_BROADWELL(dev_priv))
35653a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
35663a3b3c7dSVille Syrjälä 
356713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
356813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
356913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3570abd58f01SBen Widawsky 
3571055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3572f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3573813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3574813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3575813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
357635079899SPaulo Zanoni 					  de_pipe_enables);
3577abd58f01SBen Widawsky 
35783a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3579abd58f01SBen Widawsky }
3580abd58f01SBen Widawsky 
3581abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3582abd58f01SBen Widawsky {
3583abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3584abd58f01SBen Widawsky 
3585266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3586622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3587622364b6SPaulo Zanoni 
3588abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3589abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3590abd58f01SBen Widawsky 
3591266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3592abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3593abd58f01SBen Widawsky 
3594abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3595abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3596abd58f01SBen Widawsky 
3597abd58f01SBen Widawsky 	return 0;
3598abd58f01SBen Widawsky }
3599abd58f01SBen Widawsky 
360043f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
360143f328d7SVille Syrjälä {
360243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
360343f328d7SVille Syrjälä 
3604c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
360543f328d7SVille Syrjälä 
360643f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
360743f328d7SVille Syrjälä 
360843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
360943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
361043f328d7SVille Syrjälä 
361143f328d7SVille Syrjälä 	return 0;
361243f328d7SVille Syrjälä }
361343f328d7SVille Syrjälä 
3614abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3615abd58f01SBen Widawsky {
3616abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3617abd58f01SBen Widawsky 
3618abd58f01SBen Widawsky 	if (!dev_priv)
3619abd58f01SBen Widawsky 		return;
3620abd58f01SBen Widawsky 
3621823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3622abd58f01SBen Widawsky }
3623abd58f01SBen Widawsky 
36248ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
36258ea0be4fSVille Syrjälä {
36268ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
36278ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
36288ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36298ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
36308ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
36318ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
36328ea0be4fSVille Syrjälä 
36338ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
36348ea0be4fSVille Syrjälä 
3635c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
36368ea0be4fSVille Syrjälä }
36378ea0be4fSVille Syrjälä 
36387e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
36397e231dbeSJesse Barnes {
36402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36417e231dbeSJesse Barnes 
36427e231dbeSJesse Barnes 	if (!dev_priv)
36437e231dbeSJesse Barnes 		return;
36447e231dbeSJesse Barnes 
3645843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3646843d0e7dSImre Deak 
3647893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3648893fce8eSVille Syrjälä 
36497e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3650f8b79e58SImre Deak 
36518ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
36527e231dbeSJesse Barnes }
36537e231dbeSJesse Barnes 
365443f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
365543f328d7SVille Syrjälä {
365643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
365743f328d7SVille Syrjälä 
365843f328d7SVille Syrjälä 	if (!dev_priv)
365943f328d7SVille Syrjälä 		return;
366043f328d7SVille Syrjälä 
366143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
366243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
366343f328d7SVille Syrjälä 
3664a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
366543f328d7SVille Syrjälä 
3666a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
366743f328d7SVille Syrjälä 
3668c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
366943f328d7SVille Syrjälä }
367043f328d7SVille Syrjälä 
3671f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3672036a4a7dSZhenyu Wang {
36732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36744697995bSJesse Barnes 
36754697995bSJesse Barnes 	if (!dev_priv)
36764697995bSJesse Barnes 		return;
36774697995bSJesse Barnes 
3678be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3679036a4a7dSZhenyu Wang }
3680036a4a7dSZhenyu Wang 
3681c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3682c2798b19SChris Wilson {
36832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3684c2798b19SChris Wilson 	int pipe;
3685c2798b19SChris Wilson 
3686055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3687c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3688c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3689c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3690c2798b19SChris Wilson 	POSTING_READ16(IER);
3691c2798b19SChris Wilson }
3692c2798b19SChris Wilson 
3693c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3694c2798b19SChris Wilson {
36952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3696c2798b19SChris Wilson 
3697c2798b19SChris Wilson 	I915_WRITE16(EMR,
3698c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3699c2798b19SChris Wilson 
3700c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3701c2798b19SChris Wilson 	dev_priv->irq_mask =
3702c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3703c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3704c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
370537ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3706c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3707c2798b19SChris Wilson 
3708c2798b19SChris Wilson 	I915_WRITE16(IER,
3709c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3710c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3711c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3712c2798b19SChris Wilson 	POSTING_READ16(IER);
3713c2798b19SChris Wilson 
3714379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3715379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3716d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3717755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3718755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3719d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3720379ef82dSDaniel Vetter 
3721c2798b19SChris Wilson 	return 0;
3722c2798b19SChris Wilson }
3723c2798b19SChris Wilson 
372490a72f87SVille Syrjälä /*
372590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
372690a72f87SVille Syrjälä  */
372790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37281f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
372990a72f87SVille Syrjälä {
37302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37311f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
373290a72f87SVille Syrjälä 
37338d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
373490a72f87SVille Syrjälä 		return false;
373590a72f87SVille Syrjälä 
373690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3737d6bbafa1SChris Wilson 		goto check_page_flip;
373890a72f87SVille Syrjälä 
373990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
374090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
374190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
374290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
374390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
374490a72f87SVille Syrjälä 	 */
374590a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3746d6bbafa1SChris Wilson 		goto check_page_flip;
374790a72f87SVille Syrjälä 
37487d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
374990a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
375090a72f87SVille Syrjälä 	return true;
3751d6bbafa1SChris Wilson 
3752d6bbafa1SChris Wilson check_page_flip:
3753d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3754d6bbafa1SChris Wilson 	return false;
375590a72f87SVille Syrjälä }
375690a72f87SVille Syrjälä 
3757ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3758c2798b19SChris Wilson {
375945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3761c2798b19SChris Wilson 	u16 iir, new_iir;
3762c2798b19SChris Wilson 	u32 pipe_stats[2];
3763c2798b19SChris Wilson 	int pipe;
3764c2798b19SChris Wilson 	u16 flip_mask =
3765c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3766c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3767c2798b19SChris Wilson 
37682dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37692dd2a883SImre Deak 		return IRQ_NONE;
37702dd2a883SImre Deak 
3771c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3772c2798b19SChris Wilson 	if (iir == 0)
3773c2798b19SChris Wilson 		return IRQ_NONE;
3774c2798b19SChris Wilson 
3775c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3776c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3777c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3778c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3779c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3780c2798b19SChris Wilson 		 */
3781222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3782c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3783aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3784c2798b19SChris Wilson 
3785055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3786c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3787c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3788c2798b19SChris Wilson 
3789c2798b19SChris Wilson 			/*
3790c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3791c2798b19SChris Wilson 			 */
37922d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3793c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3794c2798b19SChris Wilson 		}
3795222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3796c2798b19SChris Wilson 
3797c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3798c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3799c2798b19SChris Wilson 
3800c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
380174cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3802c2798b19SChris Wilson 
3803055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38041f1c2e24SVille Syrjälä 			int plane = pipe;
38053a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38061f1c2e24SVille Syrjälä 				plane = !plane;
38071f1c2e24SVille Syrjälä 
38084356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38091f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38101f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3811c2798b19SChris Wilson 
38124356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3813277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38142d9d2b0bSVille Syrjälä 
38151f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38161f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38171f7247c0SDaniel Vetter 								    pipe);
38184356d586SDaniel Vetter 		}
3819c2798b19SChris Wilson 
3820c2798b19SChris Wilson 		iir = new_iir;
3821c2798b19SChris Wilson 	}
3822c2798b19SChris Wilson 
3823c2798b19SChris Wilson 	return IRQ_HANDLED;
3824c2798b19SChris Wilson }
3825c2798b19SChris Wilson 
3826c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3827c2798b19SChris Wilson {
38282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3829c2798b19SChris Wilson 	int pipe;
3830c2798b19SChris Wilson 
3831055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3832c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3833c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3834c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3835c2798b19SChris Wilson 	}
3836c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3837c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3838c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3839c2798b19SChris Wilson }
3840c2798b19SChris Wilson 
3841a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3842a266c7d5SChris Wilson {
38432d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3844a266c7d5SChris Wilson 	int pipe;
3845a266c7d5SChris Wilson 
3846a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3847a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3848a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3849a266c7d5SChris Wilson 	}
3850a266c7d5SChris Wilson 
385100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3852055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3853a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3854a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3855a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3856a266c7d5SChris Wilson 	POSTING_READ(IER);
3857a266c7d5SChris Wilson }
3858a266c7d5SChris Wilson 
3859a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3860a266c7d5SChris Wilson {
38612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
386238bde180SChris Wilson 	u32 enable_mask;
3863a266c7d5SChris Wilson 
386438bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
386538bde180SChris Wilson 
386638bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
386738bde180SChris Wilson 	dev_priv->irq_mask =
386838bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
386938bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
387038bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
387138bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
387237ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
387338bde180SChris Wilson 
387438bde180SChris Wilson 	enable_mask =
387538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
387638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
387738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
387838bde180SChris Wilson 		I915_USER_INTERRUPT;
387938bde180SChris Wilson 
3880a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
388120afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
388220afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
388320afbda2SDaniel Vetter 
3884a266c7d5SChris Wilson 		/* Enable in IER... */
3885a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3886a266c7d5SChris Wilson 		/* and unmask in IMR */
3887a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3888a266c7d5SChris Wilson 	}
3889a266c7d5SChris Wilson 
3890a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3891a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3892a266c7d5SChris Wilson 	POSTING_READ(IER);
3893a266c7d5SChris Wilson 
3894f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
389520afbda2SDaniel Vetter 
3896379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3897379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3898d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3899755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3900755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3901d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3902379ef82dSDaniel Vetter 
390320afbda2SDaniel Vetter 	return 0;
390420afbda2SDaniel Vetter }
390520afbda2SDaniel Vetter 
390690a72f87SVille Syrjälä /*
390790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
390890a72f87SVille Syrjälä  */
390990a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
391090a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
391190a72f87SVille Syrjälä {
39122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
391390a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
391490a72f87SVille Syrjälä 
39158d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
391690a72f87SVille Syrjälä 		return false;
391790a72f87SVille Syrjälä 
391890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3919d6bbafa1SChris Wilson 		goto check_page_flip;
392090a72f87SVille Syrjälä 
392190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
392290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
392390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
392490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
392590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
392690a72f87SVille Syrjälä 	 */
392790a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3928d6bbafa1SChris Wilson 		goto check_page_flip;
392990a72f87SVille Syrjälä 
39307d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
393190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
393290a72f87SVille Syrjälä 	return true;
3933d6bbafa1SChris Wilson 
3934d6bbafa1SChris Wilson check_page_flip:
3935d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3936d6bbafa1SChris Wilson 	return false;
393790a72f87SVille Syrjälä }
393890a72f87SVille Syrjälä 
3939ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3940a266c7d5SChris Wilson {
394145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39438291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
394438bde180SChris Wilson 	u32 flip_mask =
394538bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
394638bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
394738bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3948a266c7d5SChris Wilson 
39492dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39502dd2a883SImre Deak 		return IRQ_NONE;
39512dd2a883SImre Deak 
3952a266c7d5SChris Wilson 	iir = I915_READ(IIR);
395338bde180SChris Wilson 	do {
395438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39558291ee90SChris Wilson 		bool blc_event = false;
3956a266c7d5SChris Wilson 
3957a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3958a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3959a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3960a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3961a266c7d5SChris Wilson 		 */
3962222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3963a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3964aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3965a266c7d5SChris Wilson 
3966055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3967a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3968a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3969a266c7d5SChris Wilson 
397038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3971a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3972a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
397338bde180SChris Wilson 				irq_received = true;
3974a266c7d5SChris Wilson 			}
3975a266c7d5SChris Wilson 		}
3976222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3977a266c7d5SChris Wilson 
3978a266c7d5SChris Wilson 		if (!irq_received)
3979a266c7d5SChris Wilson 			break;
3980a266c7d5SChris Wilson 
3981a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
398216c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
398316c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
398416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3985a266c7d5SChris Wilson 
398638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3987a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3988a266c7d5SChris Wilson 
3989a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
399074cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3991a266c7d5SChris Wilson 
3992055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
399338bde180SChris Wilson 			int plane = pipe;
39943a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
399538bde180SChris Wilson 				plane = !plane;
39965e2032d4SVille Syrjälä 
399790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
399890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
399990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4000a266c7d5SChris Wilson 
4001a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4002a266c7d5SChris Wilson 				blc_event = true;
40034356d586SDaniel Vetter 
40044356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4005277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40062d9d2b0bSVille Syrjälä 
40071f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40081f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40091f7247c0SDaniel Vetter 								    pipe);
4010a266c7d5SChris Wilson 		}
4011a266c7d5SChris Wilson 
4012a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4013a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4014a266c7d5SChris Wilson 
4015a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4016a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4017a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4018a266c7d5SChris Wilson 		 * we would never get another interrupt.
4019a266c7d5SChris Wilson 		 *
4020a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4021a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4022a266c7d5SChris Wilson 		 * another one.
4023a266c7d5SChris Wilson 		 *
4024a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4025a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4026a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4027a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4028a266c7d5SChris Wilson 		 * stray interrupts.
4029a266c7d5SChris Wilson 		 */
403038bde180SChris Wilson 		ret = IRQ_HANDLED;
4031a266c7d5SChris Wilson 		iir = new_iir;
403238bde180SChris Wilson 	} while (iir & ~flip_mask);
4033a266c7d5SChris Wilson 
4034a266c7d5SChris Wilson 	return ret;
4035a266c7d5SChris Wilson }
4036a266c7d5SChris Wilson 
4037a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4038a266c7d5SChris Wilson {
40392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4040a266c7d5SChris Wilson 	int pipe;
4041a266c7d5SChris Wilson 
4042a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4043a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4044a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4045a266c7d5SChris Wilson 	}
4046a266c7d5SChris Wilson 
404700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4048055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
404955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4050a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
405155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
405255b39755SChris Wilson 	}
4053a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4054a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4055a266c7d5SChris Wilson 
4056a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4057a266c7d5SChris Wilson }
4058a266c7d5SChris Wilson 
4059a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4060a266c7d5SChris Wilson {
40612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4062a266c7d5SChris Wilson 	int pipe;
4063a266c7d5SChris Wilson 
4064a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4065a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4066a266c7d5SChris Wilson 
4067a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4068055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4069a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4070a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4071a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4072a266c7d5SChris Wilson 	POSTING_READ(IER);
4073a266c7d5SChris Wilson }
4074a266c7d5SChris Wilson 
4075a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4076a266c7d5SChris Wilson {
40772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4078bbba0a97SChris Wilson 	u32 enable_mask;
4079a266c7d5SChris Wilson 	u32 error_mask;
4080a266c7d5SChris Wilson 
4081a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4082bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4083adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4084bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4085bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4086bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4087bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4088bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4089bbba0a97SChris Wilson 
4090bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
409121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
409221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4093bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4094bbba0a97SChris Wilson 
4095bbba0a97SChris Wilson 	if (IS_G4X(dev))
4096bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4097a266c7d5SChris Wilson 
4098b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4099b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4100d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4101755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4102755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4103755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4104d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson 	/*
4107a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4108a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4109a266c7d5SChris Wilson 	 */
4110a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4111a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4112a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4113a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4114a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4115a266c7d5SChris Wilson 	} else {
4116a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4117a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4118a266c7d5SChris Wilson 	}
4119a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4120a266c7d5SChris Wilson 
4121a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4122a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4123a266c7d5SChris Wilson 	POSTING_READ(IER);
4124a266c7d5SChris Wilson 
412520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
412620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
412720afbda2SDaniel Vetter 
4128f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
412920afbda2SDaniel Vetter 
413020afbda2SDaniel Vetter 	return 0;
413120afbda2SDaniel Vetter }
413220afbda2SDaniel Vetter 
4133bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
413420afbda2SDaniel Vetter {
41352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
413620afbda2SDaniel Vetter 	u32 hotplug_en;
413720afbda2SDaniel Vetter 
4138b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4139b5ea2d56SDaniel Vetter 
4140bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4141bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4142adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4143e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
414487a02106SVille Syrjälä 	hotplug_en |= intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4145a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4146a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4147a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4148a266c7d5SChris Wilson 	*/
4149a266c7d5SChris Wilson 	if (IS_G4X(dev))
4150a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
415185fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4152a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4153a266c7d5SChris Wilson 
4154a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4155a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4156a266c7d5SChris Wilson }
4157a266c7d5SChris Wilson 
4158ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4159a266c7d5SChris Wilson {
416045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4162a266c7d5SChris Wilson 	u32 iir, new_iir;
4163a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4164a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
416521ad8330SVille Syrjälä 	u32 flip_mask =
416621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
416721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4168a266c7d5SChris Wilson 
41692dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41702dd2a883SImre Deak 		return IRQ_NONE;
41712dd2a883SImre Deak 
4172a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4173a266c7d5SChris Wilson 
4174a266c7d5SChris Wilson 	for (;;) {
4175501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41762c8ba29fSChris Wilson 		bool blc_event = false;
41772c8ba29fSChris Wilson 
4178a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4179a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4180a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4181a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4182a266c7d5SChris Wilson 		 */
4183222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4184a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4185aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4186a266c7d5SChris Wilson 
4187055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4188a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4189a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4190a266c7d5SChris Wilson 
4191a266c7d5SChris Wilson 			/*
4192a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4193a266c7d5SChris Wilson 			 */
4194a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4195a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4196501e01d7SVille Syrjälä 				irq_received = true;
4197a266c7d5SChris Wilson 			}
4198a266c7d5SChris Wilson 		}
4199222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4200a266c7d5SChris Wilson 
4201a266c7d5SChris Wilson 		if (!irq_received)
4202a266c7d5SChris Wilson 			break;
4203a266c7d5SChris Wilson 
4204a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4205a266c7d5SChris Wilson 
4206a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
420716c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
420816c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4209a266c7d5SChris Wilson 
421021ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4211a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4212a266c7d5SChris Wilson 
4213a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
421474cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4215a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
421674cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4217a266c7d5SChris Wilson 
4218055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42192c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
422090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
422190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4222a266c7d5SChris Wilson 
4223a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4224a266c7d5SChris Wilson 				blc_event = true;
42254356d586SDaniel Vetter 
42264356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4227277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4228a266c7d5SChris Wilson 
42291f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42301f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42312d9d2b0bSVille Syrjälä 		}
4232a266c7d5SChris Wilson 
4233a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4234a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4235a266c7d5SChris Wilson 
4236515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4237515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4238515ac2bbSDaniel Vetter 
4239a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4240a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4241a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4242a266c7d5SChris Wilson 		 * we would never get another interrupt.
4243a266c7d5SChris Wilson 		 *
4244a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4245a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4246a266c7d5SChris Wilson 		 * another one.
4247a266c7d5SChris Wilson 		 *
4248a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4249a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4250a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4251a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4252a266c7d5SChris Wilson 		 * stray interrupts.
4253a266c7d5SChris Wilson 		 */
4254a266c7d5SChris Wilson 		iir = new_iir;
4255a266c7d5SChris Wilson 	}
4256a266c7d5SChris Wilson 
4257a266c7d5SChris Wilson 	return ret;
4258a266c7d5SChris Wilson }
4259a266c7d5SChris Wilson 
4260a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4261a266c7d5SChris Wilson {
42622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4263a266c7d5SChris Wilson 	int pipe;
4264a266c7d5SChris Wilson 
4265a266c7d5SChris Wilson 	if (!dev_priv)
4266a266c7d5SChris Wilson 		return;
4267a266c7d5SChris Wilson 
4268a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4269a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4270a266c7d5SChris Wilson 
4271a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4272055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4273a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4274a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4275a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4276a266c7d5SChris Wilson 
4277055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4278a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4279a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4280a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4281a266c7d5SChris Wilson }
4282a266c7d5SChris Wilson 
4283fca52a55SDaniel Vetter /**
4284fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4285fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4286fca52a55SDaniel Vetter  *
4287fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4288fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4289fca52a55SDaniel Vetter  */
4290b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4291f71d4af4SJesse Barnes {
4292b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
42938b2e326dSChris Wilson 
429477913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
429577913b39SJani Nikula 
4296c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4297a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42988b2e326dSChris Wilson 
4299a6706b45SDeepak S 	/* Let's track the enabled rps events */
4300b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43016c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43026f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
430331685c25SDeepak S 	else
4304a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4305a6706b45SDeepak S 
4306737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4307737b1506SChris Wilson 			  i915_hangcheck_elapsed);
430861bac78eSDaniel Vetter 
430997a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43109ee32feaSDaniel Vetter 
4311b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43124cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43134cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4314b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4315f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4316f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4317391f75e2SVille Syrjälä 	} else {
4318391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4319391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4320f71d4af4SJesse Barnes 	}
4321f71d4af4SJesse Barnes 
432221da2700SVille Syrjälä 	/*
432321da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
432421da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
432521da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
432621da2700SVille Syrjälä 	 */
4327b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
432821da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
432921da2700SVille Syrjälä 
4330f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4331f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4332f71d4af4SJesse Barnes 
4333b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
433443f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
433543f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
433643f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
433743f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
433843f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
433943f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
434043f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4341b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43427e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43437e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43447e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43457e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43467e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43477e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4348fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4349b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4350abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4351723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4352abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4353abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4354abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4355abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
43566dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4357e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
43586dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
43596dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
43606dbf30ceSVille Syrjälä 		else
43613a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4362f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4363f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4364723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4365f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4366f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4367f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4368f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4369e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4370f71d4af4SJesse Barnes 	} else {
4371b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4372c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4373c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4374c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4375c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4376b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4377a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4378a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4379a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4380a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4381c2798b19SChris Wilson 		} else {
4382a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4383a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4384a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4385a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4386c2798b19SChris Wilson 		}
4387778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4388778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4389f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4390f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4391f71d4af4SJesse Barnes 	}
4392f71d4af4SJesse Barnes }
439320afbda2SDaniel Vetter 
4394fca52a55SDaniel Vetter /**
4395fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4396fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4397fca52a55SDaniel Vetter  *
4398fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4399fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4400fca52a55SDaniel Vetter  *
4401fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4402fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4403fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4404fca52a55SDaniel Vetter  */
44052aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44062aeb7d3aSDaniel Vetter {
44072aeb7d3aSDaniel Vetter 	/*
44082aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44092aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44102aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44112aeb7d3aSDaniel Vetter 	 */
44122aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44132aeb7d3aSDaniel Vetter 
44142aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44152aeb7d3aSDaniel Vetter }
44162aeb7d3aSDaniel Vetter 
4417fca52a55SDaniel Vetter /**
4418fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4419fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4420fca52a55SDaniel Vetter  *
4421fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4422fca52a55SDaniel Vetter  * resources acquired in the init functions.
4423fca52a55SDaniel Vetter  */
44242aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44252aeb7d3aSDaniel Vetter {
44262aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44272aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44282aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44292aeb7d3aSDaniel Vetter }
44302aeb7d3aSDaniel Vetter 
4431fca52a55SDaniel Vetter /**
4432fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4433fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4434fca52a55SDaniel Vetter  *
4435fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4436fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4437fca52a55SDaniel Vetter  */
4438b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4439c67a470bSPaulo Zanoni {
4440b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44412aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44422dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4443c67a470bSPaulo Zanoni }
4444c67a470bSPaulo Zanoni 
4445fca52a55SDaniel Vetter /**
4446fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4447fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4448fca52a55SDaniel Vetter  *
4449fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4450fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4451fca52a55SDaniel Vetter  */
4452b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4453c67a470bSPaulo Zanoni {
44542aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4455b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4456b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4457c67a470bSPaulo Zanoni }
4458