xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 3a3b3c7da37b35955c7c6a36a1f059d02f17eafb)
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ä 
56*3a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
57*3a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
58*3a3b3c7dSVille Syrjälä };
59*3a3b3c7dSVille 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] = {
7726951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7826951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8126951cafSXiong Zhang };
8226951cafSXiong Zhang 
837c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
84e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
85e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
90e5868a31SEgbert Eich };
91e5868a31SEgbert Eich 
927c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
93e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
94e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
95e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
97e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
99e5868a31SEgbert Eich };
100e5868a31SEgbert Eich 
1014bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
102e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
103e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
104e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
106e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
108e5868a31SEgbert Eich };
109e5868a31SEgbert Eich 
110e0a20ad7SShashank Sharma /* BXT hpd list */
111e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1127f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
113e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
114e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
115e0a20ad7SShashank Sharma };
116e0a20ad7SShashank Sharma 
1175c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
118f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1195c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1205c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1215c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1235c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1245c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1255c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1265c502442SPaulo Zanoni } while (0)
1275c502442SPaulo Zanoni 
128f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
129a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1305c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
131a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1325c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1335c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1345c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1355c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
136a9d356a6SPaulo Zanoni } while (0)
137a9d356a6SPaulo Zanoni 
138337ba017SPaulo Zanoni /*
139337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
140337ba017SPaulo Zanoni  */
141337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
142337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
143337ba017SPaulo Zanoni 	if (val) { \
144337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
145337ba017SPaulo Zanoni 		     (reg), val); \
146337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
147337ba017SPaulo Zanoni 		POSTING_READ(reg); \
148337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
149337ba017SPaulo Zanoni 		POSTING_READ(reg); \
150337ba017SPaulo Zanoni 	} \
151337ba017SPaulo Zanoni } while (0)
152337ba017SPaulo Zanoni 
15335079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
154337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
15535079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1567d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1577d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
15835079899SPaulo Zanoni } while (0)
15935079899SPaulo Zanoni 
16035079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
161337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
16235079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1637d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1647d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
16535079899SPaulo Zanoni } while (0)
16635079899SPaulo Zanoni 
167c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
168c9a9a268SImre Deak 
169d9dc34f1SVille Syrjälä /**
170d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
171d9dc34f1SVille Syrjälä  * @dev_priv: driver private
172d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
173d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
174d9dc34f1SVille Syrjälä  */
175d9dc34f1SVille Syrjälä static void ilk_update_display_irq(struct drm_i915_private *dev_priv,
176d9dc34f1SVille Syrjälä 				   uint32_t interrupt_mask,
177d9dc34f1SVille Syrjälä 				   uint32_t enabled_irq_mask)
178036a4a7dSZhenyu Wang {
179d9dc34f1SVille Syrjälä 	uint32_t new_val;
180d9dc34f1SVille Syrjälä 
1814bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1824bc9d430SDaniel Vetter 
183d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
184d9dc34f1SVille Syrjälä 
1859df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
186c67a470bSPaulo Zanoni 		return;
187c67a470bSPaulo Zanoni 
188d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
189d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
190d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
191d9dc34f1SVille Syrjälä 
192d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
193d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
1941ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1953143a2bfSChris Wilson 		POSTING_READ(DEIMR);
196036a4a7dSZhenyu Wang 	}
197036a4a7dSZhenyu Wang }
198036a4a7dSZhenyu Wang 
19947339cd9SDaniel Vetter void
200d9dc34f1SVille Syrjälä ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
201d9dc34f1SVille Syrjälä {
202d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, mask);
203d9dc34f1SVille Syrjälä }
204d9dc34f1SVille Syrjälä 
205d9dc34f1SVille Syrjälä void
2062d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
207036a4a7dSZhenyu Wang {
208d9dc34f1SVille Syrjälä 	ilk_update_display_irq(dev_priv, mask, 0);
209036a4a7dSZhenyu Wang }
210036a4a7dSZhenyu Wang 
21143eaea13SPaulo Zanoni /**
21243eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
21343eaea13SPaulo Zanoni  * @dev_priv: driver private
21443eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
21543eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
21643eaea13SPaulo Zanoni  */
21743eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
21843eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
21943eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
22043eaea13SPaulo Zanoni {
22143eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
22243eaea13SPaulo Zanoni 
22315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
22415a17aaeSDaniel Vetter 
2259df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
226c67a470bSPaulo Zanoni 		return;
227c67a470bSPaulo Zanoni 
22843eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
22943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
23043eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
23143eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
23243eaea13SPaulo Zanoni }
23343eaea13SPaulo Zanoni 
234480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
23543eaea13SPaulo Zanoni {
23643eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
23743eaea13SPaulo Zanoni }
23843eaea13SPaulo Zanoni 
239480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
24043eaea13SPaulo Zanoni {
24143eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
24243eaea13SPaulo Zanoni }
24343eaea13SPaulo Zanoni 
244b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
245b900b949SImre Deak {
246b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
247b900b949SImre Deak }
248b900b949SImre Deak 
249a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
250a72fbc3aSImre Deak {
251a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
252a72fbc3aSImre Deak }
253a72fbc3aSImre Deak 
254b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
255b900b949SImre Deak {
256b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
257b900b949SImre Deak }
258b900b949SImre Deak 
259edbfdb45SPaulo Zanoni /**
260edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
261edbfdb45SPaulo Zanoni   * @dev_priv: driver private
262edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
263edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
264edbfdb45SPaulo Zanoni   */
265edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
266edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
267edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
268edbfdb45SPaulo Zanoni {
269605cd25bSPaulo Zanoni 	uint32_t new_val;
270edbfdb45SPaulo Zanoni 
27115a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
27215a17aaeSDaniel Vetter 
273edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
274edbfdb45SPaulo Zanoni 
275605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
276f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
277f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
278f52ecbcfSPaulo Zanoni 
279605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
280605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
281a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
282a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
283edbfdb45SPaulo Zanoni 	}
284f52ecbcfSPaulo Zanoni }
285edbfdb45SPaulo Zanoni 
286480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
287edbfdb45SPaulo Zanoni {
2889939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2899939fba2SImre Deak 		return;
2909939fba2SImre Deak 
291edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
292edbfdb45SPaulo Zanoni }
293edbfdb45SPaulo Zanoni 
2949939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
2959939fba2SImre Deak 				  uint32_t mask)
2969939fba2SImre Deak {
2979939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
2989939fba2SImre Deak }
2999939fba2SImre Deak 
300480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
301edbfdb45SPaulo Zanoni {
3029939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3039939fba2SImre Deak 		return;
3049939fba2SImre Deak 
3059939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
306edbfdb45SPaulo Zanoni }
307edbfdb45SPaulo Zanoni 
3083cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3093cc134e3SImre Deak {
3103cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
3113cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
3123cc134e3SImre Deak 
3133cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3143cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3153cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3163cc134e3SImre Deak 	POSTING_READ(reg);
317096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3183cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3193cc134e3SImre Deak }
3203cc134e3SImre Deak 
321b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
322b900b949SImre Deak {
323b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
324b900b949SImre Deak 
325b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
32678e68d36SImre Deak 
327b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3283cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
329d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
33078e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
33178e68d36SImre Deak 				dev_priv->pm_rps_events);
332b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
33378e68d36SImre Deak 
334b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
335b900b949SImre Deak }
336b900b949SImre Deak 
33759d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
33859d02a1fSImre Deak {
33959d02a1fSImre Deak 	/*
340f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
34159d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
342f24eeb19SImre Deak 	 *
343f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
34459d02a1fSImre Deak 	 */
34559d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
34659d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
34759d02a1fSImre Deak 
34859d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
34959d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
35059d02a1fSImre Deak 
35159d02a1fSImre Deak 	return mask;
35259d02a1fSImre Deak }
35359d02a1fSImre Deak 
354b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
355b900b949SImre Deak {
356b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
357b900b949SImre Deak 
358d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
359d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
360d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
361d4d70aa5SImre Deak 
362d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
363d4d70aa5SImre Deak 
3649939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3659939fba2SImre Deak 
36659d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3679939fba2SImre Deak 
3689939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
369b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
370b900b949SImre Deak 				~dev_priv->pm_rps_events);
37158072ccbSImre Deak 
37258072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
37358072ccbSImre Deak 
37458072ccbSImre Deak 	synchronize_irq(dev->irq);
375b900b949SImre Deak }
376b900b949SImre Deak 
3770961021aSBen Widawsky /**
378*3a3b3c7dSVille Syrjälä   * bdw_update_port_irq - update DE port interrupt
379*3a3b3c7dSVille Syrjälä   * @dev_priv: driver private
380*3a3b3c7dSVille Syrjälä   * @interrupt_mask: mask of interrupt bits to update
381*3a3b3c7dSVille Syrjälä   * @enabled_irq_mask: mask of interrupt bits to enable
382*3a3b3c7dSVille Syrjälä   */
383*3a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
384*3a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
385*3a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
386*3a3b3c7dSVille Syrjälä {
387*3a3b3c7dSVille Syrjälä 	uint32_t new_val;
388*3a3b3c7dSVille Syrjälä 	uint32_t old_val;
389*3a3b3c7dSVille Syrjälä 
390*3a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
391*3a3b3c7dSVille Syrjälä 
392*3a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
393*3a3b3c7dSVille Syrjälä 
394*3a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
395*3a3b3c7dSVille Syrjälä 		return;
396*3a3b3c7dSVille Syrjälä 
397*3a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
398*3a3b3c7dSVille Syrjälä 
399*3a3b3c7dSVille Syrjälä 	new_val = old_val;
400*3a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
401*3a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
402*3a3b3c7dSVille Syrjälä 
403*3a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
404*3a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
405*3a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
406*3a3b3c7dSVille Syrjälä 	}
407*3a3b3c7dSVille Syrjälä }
408*3a3b3c7dSVille Syrjälä 
409*3a3b3c7dSVille Syrjälä /**
410fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
411fee884edSDaniel Vetter  * @dev_priv: driver private
412fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
413fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
414fee884edSDaniel Vetter  */
41547339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
416fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
417fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
418fee884edSDaniel Vetter {
419fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
420fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
421fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
422fee884edSDaniel Vetter 
42315a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
42415a17aaeSDaniel Vetter 
425fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
426fee884edSDaniel Vetter 
4279df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
428c67a470bSPaulo Zanoni 		return;
429c67a470bSPaulo Zanoni 
430fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
431fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
432fee884edSDaniel Vetter }
4338664281bSPaulo Zanoni 
434b5ea642aSDaniel Vetter static void
435755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
436755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4377c463586SKeith Packard {
4389db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
439755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4407c463586SKeith Packard 
441b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
442d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
443b79480baSDaniel Vetter 
44404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
44504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
44604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
44704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
448755e9019SImre Deak 		return;
449755e9019SImre Deak 
450755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
45146c06a30SVille Syrjälä 		return;
45246c06a30SVille Syrjälä 
45391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
45491d181ddSImre Deak 
4557c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
456755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
45746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4583143a2bfSChris Wilson 	POSTING_READ(reg);
4597c463586SKeith Packard }
4607c463586SKeith Packard 
461b5ea642aSDaniel Vetter static void
462755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
463755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
4647c463586SKeith Packard {
4659db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
466755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4677c463586SKeith Packard 
468b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
469d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
470b79480baSDaniel Vetter 
47104feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
47204feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
47304feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
47404feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
47546c06a30SVille Syrjälä 		return;
47646c06a30SVille Syrjälä 
477755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
478755e9019SImre Deak 		return;
479755e9019SImre Deak 
48091d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
48191d181ddSImre Deak 
482755e9019SImre Deak 	pipestat &= ~enable_mask;
48346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4843143a2bfSChris Wilson 	POSTING_READ(reg);
4857c463586SKeith Packard }
4867c463586SKeith Packard 
48710c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
48810c59c51SImre Deak {
48910c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
49010c59c51SImre Deak 
49110c59c51SImre Deak 	/*
492724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
493724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
49410c59c51SImre Deak 	 */
49510c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
49610c59c51SImre Deak 		return 0;
497724a6905SVille Syrjälä 	/*
498724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
499724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
500724a6905SVille Syrjälä 	 */
501724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
502724a6905SVille Syrjälä 		return 0;
50310c59c51SImre Deak 
50410c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
50510c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
50610c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
50710c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
50810c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
50910c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
51010c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
51110c59c51SImre Deak 
51210c59c51SImre Deak 	return enable_mask;
51310c59c51SImre Deak }
51410c59c51SImre Deak 
515755e9019SImre Deak void
516755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
517755e9019SImre Deak 		     u32 status_mask)
518755e9019SImre Deak {
519755e9019SImre Deak 	u32 enable_mask;
520755e9019SImre Deak 
52110c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
52210c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
52310c59c51SImre Deak 							   status_mask);
52410c59c51SImre Deak 	else
525755e9019SImre Deak 		enable_mask = status_mask << 16;
526755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
527755e9019SImre Deak }
528755e9019SImre Deak 
529755e9019SImre Deak void
530755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
531755e9019SImre Deak 		      u32 status_mask)
532755e9019SImre Deak {
533755e9019SImre Deak 	u32 enable_mask;
534755e9019SImre Deak 
53510c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
53610c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
53710c59c51SImre Deak 							   status_mask);
53810c59c51SImre Deak 	else
539755e9019SImre Deak 		enable_mask = status_mask << 16;
540755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
541755e9019SImre Deak }
542755e9019SImre Deak 
543c0e09200SDave Airlie /**
544f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
54501c66889SZhao Yakui  */
546f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
54701c66889SZhao Yakui {
5482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5491ec14ad3SChris Wilson 
550f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
551f49e38ddSJani Nikula 		return;
552f49e38ddSJani Nikula 
55313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
55401c66889SZhao Yakui 
555755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
556a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
5573b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
558755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
5591ec14ad3SChris Wilson 
56013321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
56101c66889SZhao Yakui }
56201c66889SZhao Yakui 
563f75f3746SVille Syrjälä /*
564f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
565f75f3746SVille Syrjälä  * around the vertical blanking period.
566f75f3746SVille Syrjälä  *
567f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
568f75f3746SVille Syrjälä  *  vblank_start >= 3
569f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
570f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
571f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
572f75f3746SVille Syrjälä  *
573f75f3746SVille Syrjälä  *           start of vblank:
574f75f3746SVille Syrjälä  *           latch double buffered registers
575f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
576f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
577f75f3746SVille Syrjälä  *           |
578f75f3746SVille Syrjälä  *           |          frame start:
579f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
580f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
581f75f3746SVille Syrjälä  *           |          |
582f75f3746SVille Syrjälä  *           |          |  start of vsync:
583f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
584f75f3746SVille Syrjälä  *           |          |  |
585f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
586f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
587f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
588f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
589f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
590f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
591f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
592f75f3746SVille Syrjälä  *       |          |                                         |
593f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
594f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
595f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
596f75f3746SVille Syrjälä  *
597f75f3746SVille Syrjälä  * x  = horizontal active
598f75f3746SVille Syrjälä  * _  = horizontal blanking
599f75f3746SVille Syrjälä  * hs = horizontal sync
600f75f3746SVille Syrjälä  * va = vertical active
601f75f3746SVille Syrjälä  * vb = vertical blanking
602f75f3746SVille Syrjälä  * vs = vertical sync
603f75f3746SVille Syrjälä  * vbs = vblank_start (number)
604f75f3746SVille Syrjälä  *
605f75f3746SVille Syrjälä  * Summary:
606f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
607f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
608f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
609f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
610f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
611f75f3746SVille Syrjälä  */
612f75f3746SVille Syrjälä 
6134cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
6144cdb83ecSVille Syrjälä {
6154cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6164cdb83ecSVille Syrjälä 	return 0;
6174cdb83ecSVille Syrjälä }
6184cdb83ecSVille Syrjälä 
61942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
62042f52ef8SKeith Packard  * we use as a pipe index
62142f52ef8SKeith Packard  */
622f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
6230a3e67a4SJesse Barnes {
6242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6250a3e67a4SJesse Barnes 	unsigned long high_frame;
6260a3e67a4SJesse Barnes 	unsigned long low_frame;
6270b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
628391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
629391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
630fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
631391f75e2SVille Syrjälä 
6320b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6330b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6340b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6350b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6360b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
637391f75e2SVille Syrjälä 
6380b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6390b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6400b2a8e09SVille Syrjälä 
6410b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6420b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6430b2a8e09SVille Syrjälä 
6449db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6459db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6465eddb70bSChris Wilson 
6470a3e67a4SJesse Barnes 	/*
6480a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6490a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6500a3e67a4SJesse Barnes 	 * register.
6510a3e67a4SJesse Barnes 	 */
6520a3e67a4SJesse Barnes 	do {
6535eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
654391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
6555eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
6560a3e67a4SJesse Barnes 	} while (high1 != high2);
6570a3e67a4SJesse Barnes 
6585eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
659391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
6605eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
661391f75e2SVille Syrjälä 
662391f75e2SVille Syrjälä 	/*
663391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
664391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
665391f75e2SVille Syrjälä 	 * counter against vblank start.
666391f75e2SVille Syrjälä 	 */
667edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6680a3e67a4SJesse Barnes }
6690a3e67a4SJesse Barnes 
670f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6719880b7a5SJesse Barnes {
6722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6739db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6749880b7a5SJesse Barnes 
6759880b7a5SJesse Barnes 	return I915_READ(reg);
6769880b7a5SJesse Barnes }
6779880b7a5SJesse Barnes 
678ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
679ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
680ad3543edSMario Kleiner 
681a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
682a225f079SVille Syrjälä {
683a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
684a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
685fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
686a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
68780715b2fSVille Syrjälä 	int position, vtotal;
688a225f079SVille Syrjälä 
68980715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
690a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
691a225f079SVille Syrjälä 		vtotal /= 2;
692a225f079SVille Syrjälä 
693a225f079SVille Syrjälä 	if (IS_GEN2(dev))
694a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
695a225f079SVille Syrjälä 	else
696a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
697a225f079SVille Syrjälä 
698a225f079SVille Syrjälä 	/*
69980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
70080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
701a225f079SVille Syrjälä 	 */
70280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
703a225f079SVille Syrjälä }
704a225f079SVille Syrjälä 
705f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
706abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
707abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
7080af7e4dfSMario Kleiner {
709c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
710c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
711c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
712fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
7133aa18df8SVille Syrjälä 	int position;
71478e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7150af7e4dfSMario Kleiner 	bool in_vbl = true;
7160af7e4dfSMario Kleiner 	int ret = 0;
717ad3543edSMario Kleiner 	unsigned long irqflags;
7180af7e4dfSMario Kleiner 
719fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7200af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7219db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7220af7e4dfSMario Kleiner 		return 0;
7230af7e4dfSMario Kleiner 	}
7240af7e4dfSMario Kleiner 
725c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
72678e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
727c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
728c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
729c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7300af7e4dfSMario Kleiner 
731d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
732d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
733d31faf65SVille Syrjälä 		vbl_end /= 2;
734d31faf65SVille Syrjälä 		vtotal /= 2;
735d31faf65SVille Syrjälä 	}
736d31faf65SVille Syrjälä 
737c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
738c2baf4b7SVille Syrjälä 
739ad3543edSMario Kleiner 	/*
740ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
741ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
742ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
743ad3543edSMario Kleiner 	 */
744ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
745ad3543edSMario Kleiner 
746ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
747ad3543edSMario Kleiner 
748ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
749ad3543edSMario Kleiner 	if (stime)
750ad3543edSMario Kleiner 		*stime = ktime_get();
751ad3543edSMario Kleiner 
7527c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7530af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
7540af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
7550af7e4dfSMario Kleiner 		 */
756a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
7570af7e4dfSMario Kleiner 	} else {
7580af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
7590af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
7600af7e4dfSMario Kleiner 		 * scanout position.
7610af7e4dfSMario Kleiner 		 */
762ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7630af7e4dfSMario Kleiner 
7643aa18df8SVille Syrjälä 		/* convert to pixel counts */
7653aa18df8SVille Syrjälä 		vbl_start *= htotal;
7663aa18df8SVille Syrjälä 		vbl_end *= htotal;
7673aa18df8SVille Syrjälä 		vtotal *= htotal;
76878e8fc6bSVille Syrjälä 
76978e8fc6bSVille Syrjälä 		/*
7707e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7717e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7727e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7737e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7747e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7757e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7767e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7777e78f1cbSVille Syrjälä 		 */
7787e78f1cbSVille Syrjälä 		if (position >= vtotal)
7797e78f1cbSVille Syrjälä 			position = vtotal - 1;
7807e78f1cbSVille Syrjälä 
7817e78f1cbSVille Syrjälä 		/*
78278e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
78378e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
78478e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
78578e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
78678e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
78778e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
78878e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
78978e8fc6bSVille Syrjälä 		 */
79078e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7913aa18df8SVille Syrjälä 	}
7923aa18df8SVille Syrjälä 
793ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
794ad3543edSMario Kleiner 	if (etime)
795ad3543edSMario Kleiner 		*etime = ktime_get();
796ad3543edSMario Kleiner 
797ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
798ad3543edSMario Kleiner 
799ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
800ad3543edSMario Kleiner 
8013aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8023aa18df8SVille Syrjälä 
8033aa18df8SVille Syrjälä 	/*
8043aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8053aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8063aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8073aa18df8SVille Syrjälä 	 * up since vbl_end.
8083aa18df8SVille Syrjälä 	 */
8093aa18df8SVille Syrjälä 	if (position >= vbl_start)
8103aa18df8SVille Syrjälä 		position -= vbl_end;
8113aa18df8SVille Syrjälä 	else
8123aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8133aa18df8SVille Syrjälä 
8147c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8153aa18df8SVille Syrjälä 		*vpos = position;
8163aa18df8SVille Syrjälä 		*hpos = 0;
8173aa18df8SVille Syrjälä 	} else {
8180af7e4dfSMario Kleiner 		*vpos = position / htotal;
8190af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8200af7e4dfSMario Kleiner 	}
8210af7e4dfSMario Kleiner 
8220af7e4dfSMario Kleiner 	/* In vblank? */
8230af7e4dfSMario Kleiner 	if (in_vbl)
8243d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8250af7e4dfSMario Kleiner 
8260af7e4dfSMario Kleiner 	return ret;
8270af7e4dfSMario Kleiner }
8280af7e4dfSMario Kleiner 
829a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
830a225f079SVille Syrjälä {
831a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
832a225f079SVille Syrjälä 	unsigned long irqflags;
833a225f079SVille Syrjälä 	int position;
834a225f079SVille Syrjälä 
835a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
836a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
837a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
838a225f079SVille Syrjälä 
839a225f079SVille Syrjälä 	return position;
840a225f079SVille Syrjälä }
841a225f079SVille Syrjälä 
842f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
8430af7e4dfSMario Kleiner 			      int *max_error,
8440af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
8450af7e4dfSMario Kleiner 			      unsigned flags)
8460af7e4dfSMario Kleiner {
8474041b853SChris Wilson 	struct drm_crtc *crtc;
8480af7e4dfSMario Kleiner 
8497eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
8504041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8510af7e4dfSMario Kleiner 		return -EINVAL;
8520af7e4dfSMario Kleiner 	}
8530af7e4dfSMario Kleiner 
8540af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
8554041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
8564041b853SChris Wilson 	if (crtc == NULL) {
8574041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
8584041b853SChris Wilson 		return -EINVAL;
8594041b853SChris Wilson 	}
8604041b853SChris Wilson 
861fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
8624041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8634041b853SChris Wilson 		return -EBUSY;
8644041b853SChris Wilson 	}
8650af7e4dfSMario Kleiner 
8660af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8674041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8684041b853SChris Wilson 						     vblank_time, flags,
8697da903efSVille Syrjälä 						     crtc,
870fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
8710af7e4dfSMario Kleiner }
8720af7e4dfSMario Kleiner 
873d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
874f97108d1SJesse Barnes {
8752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
876b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
8779270388eSDaniel Vetter 	u8 new_delay;
8789270388eSDaniel Vetter 
879d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
880f97108d1SJesse Barnes 
88173edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
88273edd18fSDaniel Vetter 
88320e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
8849270388eSDaniel Vetter 
8857648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
886b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
887b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
888f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
889f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
890f97108d1SJesse Barnes 
891f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
892b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
89320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
89420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
89520e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
89620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
897b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
89820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
89920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
90020e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
90120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
902f97108d1SJesse Barnes 	}
903f97108d1SJesse Barnes 
9047648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
90520e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
906f97108d1SJesse Barnes 
907d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9089270388eSDaniel Vetter 
909f97108d1SJesse Barnes 	return;
910f97108d1SJesse Barnes }
911f97108d1SJesse Barnes 
91274cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
913549f7365SChris Wilson {
91493b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
915475553deSChris Wilson 		return;
916475553deSChris Wilson 
917bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9189862e600SChris Wilson 
919549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
920549f7365SChris Wilson }
921549f7365SChris Wilson 
92243cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
92343cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
92431685c25SDeepak S {
92543cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
92643cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
92743cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
92831685c25SDeepak S }
92931685c25SDeepak S 
93043cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
93143cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
93243cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
93343cf3bf0SChris Wilson 			 int threshold)
93431685c25SDeepak S {
93543cf3bf0SChris Wilson 	u64 time, c0;
93631685c25SDeepak S 
93743cf3bf0SChris Wilson 	if (old->cz_clock == 0)
93843cf3bf0SChris Wilson 		return false;
93931685c25SDeepak S 
94043cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
94143cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
94231685c25SDeepak S 
94343cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
94443cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
94543cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
94643cf3bf0SChris Wilson 	 */
94743cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
94843cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
94943cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
95031685c25SDeepak S 
95143cf3bf0SChris Wilson 	return c0 >= time;
95231685c25SDeepak S }
95331685c25SDeepak S 
95443cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
95543cf3bf0SChris Wilson {
95643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
95743cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
95843cf3bf0SChris Wilson }
95943cf3bf0SChris Wilson 
96043cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
96143cf3bf0SChris Wilson {
96243cf3bf0SChris Wilson 	struct intel_rps_ei now;
96343cf3bf0SChris Wilson 	u32 events = 0;
96443cf3bf0SChris Wilson 
9656f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
96643cf3bf0SChris Wilson 		return 0;
96743cf3bf0SChris Wilson 
96843cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
96943cf3bf0SChris Wilson 	if (now.cz_clock == 0)
97043cf3bf0SChris Wilson 		return 0;
97131685c25SDeepak S 
97243cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
97343cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
97443cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
9758fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
97643cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
97743cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
97831685c25SDeepak S 	}
97931685c25SDeepak S 
98043cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
98143cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
98243cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
9838fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
98443cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
98543cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
98643cf3bf0SChris Wilson 	}
98743cf3bf0SChris Wilson 
98843cf3bf0SChris Wilson 	return events;
98931685c25SDeepak S }
99031685c25SDeepak S 
991f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
992f5a4c67dSChris Wilson {
993f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
994f5a4c67dSChris Wilson 	int i;
995f5a4c67dSChris Wilson 
996f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
997f5a4c67dSChris Wilson 		if (ring->irq_refcount)
998f5a4c67dSChris Wilson 			return true;
999f5a4c67dSChris Wilson 
1000f5a4c67dSChris Wilson 	return false;
1001f5a4c67dSChris Wilson }
1002f5a4c67dSChris Wilson 
10034912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10043b8d8d91SJesse Barnes {
10052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10062d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10078d3afd7dSChris Wilson 	bool client_boost;
10088d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1009edbfdb45SPaulo Zanoni 	u32 pm_iir;
10103b8d8d91SJesse Barnes 
101159cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1012d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1013d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1014d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1015d4d70aa5SImre Deak 		return;
1016d4d70aa5SImre Deak 	}
1017c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1018c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1019a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1020480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10218d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10228d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
102359cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10244912d041SBen Widawsky 
102560611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1026a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
102760611c13SPaulo Zanoni 
10288d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
10293b8d8d91SJesse Barnes 		return;
10303b8d8d91SJesse Barnes 
10314fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10327b9e0ae6SChris Wilson 
103343cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
103443cf3bf0SChris Wilson 
1035dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1036edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
10378d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
10388d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
10398d3afd7dSChris Wilson 
10408d3afd7dSChris Wilson 	if (client_boost) {
10418d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
10428d3afd7dSChris Wilson 		adj = 0;
10438d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1044dd75fdc8SChris Wilson 		if (adj > 0)
1045dd75fdc8SChris Wilson 			adj *= 2;
1046edcf284bSChris Wilson 		else /* CHV needs even encode values */
1047edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
10487425034aSVille Syrjälä 		/*
10497425034aSVille Syrjälä 		 * For better performance, jump directly
10507425034aSVille Syrjälä 		 * to RPe if we're below it.
10517425034aSVille Syrjälä 		 */
1052edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1053b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1054edcf284bSChris Wilson 			adj = 0;
1055edcf284bSChris Wilson 		}
1056f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1057f5a4c67dSChris Wilson 		adj = 0;
1058dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1059b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1060b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1061dd75fdc8SChris Wilson 		else
1062b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1063dd75fdc8SChris Wilson 		adj = 0;
1064dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1065dd75fdc8SChris Wilson 		if (adj < 0)
1066dd75fdc8SChris Wilson 			adj *= 2;
1067edcf284bSChris Wilson 		else /* CHV needs even encode values */
1068edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1069dd75fdc8SChris Wilson 	} else { /* unknown event */
1070edcf284bSChris Wilson 		adj = 0;
1071dd75fdc8SChris Wilson 	}
10723b8d8d91SJesse Barnes 
1073edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1074edcf284bSChris Wilson 
107579249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
107679249636SBen Widawsky 	 * interrupt
107779249636SBen Widawsky 	 */
1078edcf284bSChris Wilson 	new_delay += adj;
10798d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
108027544369SDeepak S 
1081ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
10823b8d8d91SJesse Barnes 
10834fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
10843b8d8d91SJesse Barnes }
10853b8d8d91SJesse Barnes 
1086e3689190SBen Widawsky 
1087e3689190SBen Widawsky /**
1088e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1089e3689190SBen Widawsky  * occurred.
1090e3689190SBen Widawsky  * @work: workqueue struct
1091e3689190SBen Widawsky  *
1092e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1093e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1094e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1095e3689190SBen Widawsky  */
1096e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1097e3689190SBen Widawsky {
10982d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10992d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1100e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
110135a85ac6SBen Widawsky 	char *parity_event[6];
1102e3689190SBen Widawsky 	uint32_t misccpctl;
110335a85ac6SBen Widawsky 	uint8_t slice = 0;
1104e3689190SBen Widawsky 
1105e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1106e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1107e3689190SBen Widawsky 	 * any time we access those registers.
1108e3689190SBen Widawsky 	 */
1109e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1110e3689190SBen Widawsky 
111135a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
111235a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
111335a85ac6SBen Widawsky 		goto out;
111435a85ac6SBen Widawsky 
1115e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1116e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1117e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1118e3689190SBen Widawsky 
111935a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
112035a85ac6SBen Widawsky 		u32 reg;
112135a85ac6SBen Widawsky 
112235a85ac6SBen Widawsky 		slice--;
112335a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
112435a85ac6SBen Widawsky 			break;
112535a85ac6SBen Widawsky 
112635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
112735a85ac6SBen Widawsky 
112835a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
112935a85ac6SBen Widawsky 
113035a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1131e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1132e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1133e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1134e3689190SBen Widawsky 
113535a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
113635a85ac6SBen Widawsky 		POSTING_READ(reg);
1137e3689190SBen Widawsky 
1138cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1139e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1140e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1141e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
114235a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
114335a85ac6SBen Widawsky 		parity_event[5] = NULL;
1144e3689190SBen Widawsky 
11455bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1146e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1147e3689190SBen Widawsky 
114835a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
114935a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1150e3689190SBen Widawsky 
115135a85ac6SBen Widawsky 		kfree(parity_event[4]);
1152e3689190SBen Widawsky 		kfree(parity_event[3]);
1153e3689190SBen Widawsky 		kfree(parity_event[2]);
1154e3689190SBen Widawsky 		kfree(parity_event[1]);
1155e3689190SBen Widawsky 	}
1156e3689190SBen Widawsky 
115735a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
115835a85ac6SBen Widawsky 
115935a85ac6SBen Widawsky out:
116035a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
11614cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1162480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
11634cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
116435a85ac6SBen Widawsky 
116535a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
116635a85ac6SBen Widawsky }
116735a85ac6SBen Widawsky 
116835a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1169e3689190SBen Widawsky {
11702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1171e3689190SBen Widawsky 
1172040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1173e3689190SBen Widawsky 		return;
1174e3689190SBen Widawsky 
1175d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1176480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1177d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1178e3689190SBen Widawsky 
117935a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
118035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
118135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
118235a85ac6SBen Widawsky 
118335a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
118435a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
118535a85ac6SBen Widawsky 
1186a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1187e3689190SBen Widawsky }
1188e3689190SBen Widawsky 
1189f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1190f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1191f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1192f1af8fc1SPaulo Zanoni {
1193f1af8fc1SPaulo Zanoni 	if (gt_iir &
1194f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
119574cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1196f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
119774cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1198f1af8fc1SPaulo Zanoni }
1199f1af8fc1SPaulo Zanoni 
1200e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1201e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1202e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1203e7b4c6b1SDaniel Vetter {
1204e7b4c6b1SDaniel Vetter 
1205cc609d5dSBen Widawsky 	if (gt_iir &
1206cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
120774cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1208cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
120974cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1210cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
121174cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1212e7b4c6b1SDaniel Vetter 
1213cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1214cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1215aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1216aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1217e3689190SBen Widawsky 
121835a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
121935a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1220e7b4c6b1SDaniel Vetter }
1221e7b4c6b1SDaniel Vetter 
122274cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1223abd58f01SBen Widawsky 				       u32 master_ctl)
1224abd58f01SBen Widawsky {
1225abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1226abd58f01SBen Widawsky 
1227abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
122874cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1229abd58f01SBen Widawsky 		if (tmp) {
1230cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1231abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1232e981e7b1SThomas Daniel 
123374cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
123474cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
123574cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
123674cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1237e981e7b1SThomas Daniel 
123874cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
123974cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
124074cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
124174cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1242abd58f01SBen Widawsky 		} else
1243abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1244abd58f01SBen Widawsky 	}
1245abd58f01SBen Widawsky 
124685f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
124774cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1248abd58f01SBen Widawsky 		if (tmp) {
1249cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1250abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1251e981e7b1SThomas Daniel 
125274cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
125374cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
125474cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
125574cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1256e981e7b1SThomas Daniel 
125774cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
125874cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
125974cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
126074cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1261abd58f01SBen Widawsky 		} else
1262abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1263abd58f01SBen Widawsky 	}
1264abd58f01SBen Widawsky 
126574cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
126674cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
126774cdb337SChris Wilson 		if (tmp) {
126874cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
126974cdb337SChris Wilson 			ret = IRQ_HANDLED;
127074cdb337SChris Wilson 
127174cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
127274cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
127374cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
127474cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
127574cdb337SChris Wilson 		} else
127674cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
127774cdb337SChris Wilson 	}
127874cdb337SChris Wilson 
12790961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
128074cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
12810961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1282cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
12830961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
128438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1285c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
12860961021aSBen Widawsky 		} else
12870961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
12880961021aSBen Widawsky 	}
12890961021aSBen Widawsky 
1290abd58f01SBen Widawsky 	return ret;
1291abd58f01SBen Widawsky }
1292abd58f01SBen Widawsky 
129363c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
129463c88d22SImre Deak {
129563c88d22SImre Deak 	switch (port) {
129663c88d22SImre Deak 	case PORT_A:
1297195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
129863c88d22SImre Deak 	case PORT_B:
129963c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
130063c88d22SImre Deak 	case PORT_C:
130163c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
130263c88d22SImre Deak 	case PORT_D:
130363c88d22SImre Deak 		return val & PORTD_HOTPLUG_LONG_DETECT;
130463c88d22SImre Deak 	default:
130563c88d22SImre Deak 		return false;
130663c88d22SImre Deak 	}
130763c88d22SImre Deak }
130863c88d22SImre Deak 
13096dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13106dbf30ceSVille Syrjälä {
13116dbf30ceSVille Syrjälä 	switch (port) {
13126dbf30ceSVille Syrjälä 	case PORT_E:
13136dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13146dbf30ceSVille Syrjälä 	default:
13156dbf30ceSVille Syrjälä 		return false;
13166dbf30ceSVille Syrjälä 	}
13176dbf30ceSVille Syrjälä }
13186dbf30ceSVille Syrjälä 
1319e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1320e4ce95aaSVille Syrjälä {
1321e4ce95aaSVille Syrjälä 	switch (port) {
1322e4ce95aaSVille Syrjälä 	case PORT_A:
1323e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1324e4ce95aaSVille Syrjälä 	default:
1325e4ce95aaSVille Syrjälä 		return false;
1326e4ce95aaSVille Syrjälä 	}
1327e4ce95aaSVille Syrjälä }
1328e4ce95aaSVille Syrjälä 
1329676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
133013cf5504SDave Airlie {
133113cf5504SDave Airlie 	switch (port) {
133213cf5504SDave Airlie 	case PORT_B:
1333676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
133413cf5504SDave Airlie 	case PORT_C:
1335676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
133613cf5504SDave Airlie 	case PORT_D:
1337676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1338676574dfSJani Nikula 	default:
1339676574dfSJani Nikula 		return false;
134013cf5504SDave Airlie 	}
134113cf5504SDave Airlie }
134213cf5504SDave Airlie 
1343676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
134413cf5504SDave Airlie {
134513cf5504SDave Airlie 	switch (port) {
134613cf5504SDave Airlie 	case PORT_B:
1347676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
134813cf5504SDave Airlie 	case PORT_C:
1349676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
135013cf5504SDave Airlie 	case PORT_D:
1351676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1352676574dfSJani Nikula 	default:
1353676574dfSJani Nikula 		return false;
135413cf5504SDave Airlie 	}
135513cf5504SDave Airlie }
135613cf5504SDave Airlie 
135742db67d6SVille Syrjälä /*
135842db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
135942db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
136042db67d6SVille Syrjälä  * hotplug detection results from several registers.
136142db67d6SVille Syrjälä  *
136242db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
136342db67d6SVille Syrjälä  */
1364fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
13658c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1366fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1367fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1368676574dfSJani Nikula {
13698c841e57SJani Nikula 	enum port port;
1370676574dfSJani Nikula 	int i;
1371676574dfSJani Nikula 
1372676574dfSJani Nikula 	for_each_hpd_pin(i) {
13738c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
13748c841e57SJani Nikula 			continue;
13758c841e57SJani Nikula 
1376676574dfSJani Nikula 		*pin_mask |= BIT(i);
1377676574dfSJani Nikula 
1378cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1379cc24fcdcSImre Deak 			continue;
1380cc24fcdcSImre Deak 
1381fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1382676574dfSJani Nikula 			*long_mask |= BIT(i);
1383676574dfSJani Nikula 	}
1384676574dfSJani Nikula 
1385676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1386676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1387676574dfSJani Nikula 
1388676574dfSJani Nikula }
1389676574dfSJani Nikula 
1390515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1391515ac2bbSDaniel Vetter {
13922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
139328c70f16SDaniel Vetter 
139428c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1395515ac2bbSDaniel Vetter }
1396515ac2bbSDaniel Vetter 
1397ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1398ce99c256SDaniel Vetter {
13992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
14009ee32feaSDaniel Vetter 
14019ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1402ce99c256SDaniel Vetter }
1403ce99c256SDaniel Vetter 
14048bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1405277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1406eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1407eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14088bc5e955SDaniel Vetter 					 uint32_t crc4)
14098bf1e9f1SShuang He {
14108bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
14118bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14128bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1413ac2300d4SDamien Lespiau 	int head, tail;
1414b2c88f5bSDamien Lespiau 
1415d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1416d538bbdfSDamien Lespiau 
14170c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1418d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
141934273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
14200c912c79SDamien Lespiau 		return;
14210c912c79SDamien Lespiau 	}
14220c912c79SDamien Lespiau 
1423d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1424d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1425b2c88f5bSDamien Lespiau 
1426b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1427d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1428b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1429b2c88f5bSDamien Lespiau 		return;
1430b2c88f5bSDamien Lespiau 	}
1431b2c88f5bSDamien Lespiau 
1432b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
14338bf1e9f1SShuang He 
14348bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1435eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1436eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1437eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1438eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1439eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1440b2c88f5bSDamien Lespiau 
1441b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1442d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1443d538bbdfSDamien Lespiau 
1444d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
144507144428SDamien Lespiau 
144607144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
14478bf1e9f1SShuang He }
1448277de95eSDaniel Vetter #else
1449277de95eSDaniel Vetter static inline void
1450277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1451277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1452277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1453277de95eSDaniel Vetter 			     uint32_t crc4) {}
1454277de95eSDaniel Vetter #endif
1455eba94eb9SDaniel Vetter 
1456277de95eSDaniel Vetter 
1457277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14585a69b89fSDaniel Vetter {
14595a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14605a69b89fSDaniel Vetter 
1461277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14625a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
14635a69b89fSDaniel Vetter 				     0, 0, 0, 0);
14645a69b89fSDaniel Vetter }
14655a69b89fSDaniel Vetter 
1466277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1467eba94eb9SDaniel Vetter {
1468eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1469eba94eb9SDaniel Vetter 
1470277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1471eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1472eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1473eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1474eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
14758bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1476eba94eb9SDaniel Vetter }
14775b3a856bSDaniel Vetter 
1478277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
14795b3a856bSDaniel Vetter {
14805b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
14810b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
14820b5c5ed0SDaniel Vetter 
14830b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
14840b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
14850b5c5ed0SDaniel Vetter 	else
14860b5c5ed0SDaniel Vetter 		res1 = 0;
14870b5c5ed0SDaniel Vetter 
14880b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
14890b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
14900b5c5ed0SDaniel Vetter 	else
14910b5c5ed0SDaniel Vetter 		res2 = 0;
14925b3a856bSDaniel Vetter 
1493277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
14940b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
14950b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
14960b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
14970b5c5ed0SDaniel Vetter 				     res1, res2);
14985b3a856bSDaniel Vetter }
14998bf1e9f1SShuang He 
15001403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15011403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15021403c0d4SPaulo Zanoni  * the work queue. */
15031403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1504baf02a1fSBen Widawsky {
1505a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
150659cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1507480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1508d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1509d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
15102adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
151141a05a3aSDaniel Vetter 		}
1512d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1513d4d70aa5SImre Deak 	}
1514baf02a1fSBen Widawsky 
1515c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1516c9a9a268SImre Deak 		return;
1517c9a9a268SImre Deak 
15181403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
151912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
152074cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
152112638c57SBen Widawsky 
1522aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1523aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
152412638c57SBen Widawsky 	}
15251403c0d4SPaulo Zanoni }
1526baf02a1fSBen Widawsky 
15278d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
15288d7849dbSVille Syrjälä {
15298d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
15308d7849dbSVille Syrjälä 		return false;
15318d7849dbSVille Syrjälä 
15328d7849dbSVille Syrjälä 	return true;
15338d7849dbSVille Syrjälä }
15348d7849dbSVille Syrjälä 
1535c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
15367e231dbeSJesse Barnes {
1537c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
153891d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
15397e231dbeSJesse Barnes 	int pipe;
15407e231dbeSJesse Barnes 
154158ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1542055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
154391d181ddSImre Deak 		int reg;
1544bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
154591d181ddSImre Deak 
1546bbb5eebfSDaniel Vetter 		/*
1547bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1548bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1549bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1550bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1551bbb5eebfSDaniel Vetter 		 * handle.
1552bbb5eebfSDaniel Vetter 		 */
15530f239f4cSDaniel Vetter 
15540f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
15550f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1556bbb5eebfSDaniel Vetter 
1557bbb5eebfSDaniel Vetter 		switch (pipe) {
1558bbb5eebfSDaniel Vetter 		case PIPE_A:
1559bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1560bbb5eebfSDaniel Vetter 			break;
1561bbb5eebfSDaniel Vetter 		case PIPE_B:
1562bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1563bbb5eebfSDaniel Vetter 			break;
15643278f67fSVille Syrjälä 		case PIPE_C:
15653278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
15663278f67fSVille Syrjälä 			break;
1567bbb5eebfSDaniel Vetter 		}
1568bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1569bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1570bbb5eebfSDaniel Vetter 
1571bbb5eebfSDaniel Vetter 		if (!mask)
157291d181ddSImre Deak 			continue;
157391d181ddSImre Deak 
157491d181ddSImre Deak 		reg = PIPESTAT(pipe);
1575bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1576bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
15777e231dbeSJesse Barnes 
15787e231dbeSJesse Barnes 		/*
15797e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
15807e231dbeSJesse Barnes 		 */
158191d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
158291d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
15837e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
15847e231dbeSJesse Barnes 	}
158558ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
15867e231dbeSJesse Barnes 
1587055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1588d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1589d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1590d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
159131acc7f5SJesse Barnes 
1592579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
159331acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
159431acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
159531acc7f5SJesse Barnes 		}
15964356d586SDaniel Vetter 
15974356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1598277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
15992d9d2b0bSVille Syrjälä 
16001f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16011f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
160231acc7f5SJesse Barnes 	}
160331acc7f5SJesse Barnes 
1604c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1605c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1606c1874ed7SImre Deak }
1607c1874ed7SImre Deak 
160816c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
160916c6c56bSVille Syrjälä {
161016c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
161116c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
161242db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
161316c6c56bSVille Syrjälä 
16140d2e4297SJani Nikula 	if (!hotplug_status)
16150d2e4297SJani Nikula 		return;
16160d2e4297SJani Nikula 
16173ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
16183ff60f89SOscar Mateo 	/*
16193ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
16203ff60f89SOscar Mateo 	 * may miss hotplug events.
16213ff60f89SOscar Mateo 	 */
16223ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
16233ff60f89SOscar Mateo 
16244bca26d0SVille Syrjälä 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
162516c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
162616c6c56bSVille Syrjälä 
1627fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1628fd63e2a9SImre Deak 				   hotplug_trigger, hpd_status_g4x,
1629fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
1630676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1631369712e8SJani Nikula 
1632369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1633369712e8SJani Nikula 			dp_aux_irq_handler(dev);
163416c6c56bSVille Syrjälä 	} else {
163516c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
163616c6c56bSVille Syrjälä 
1637fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1638fd63e2a9SImre Deak 				   hotplug_trigger, hpd_status_g4x,
1639fd63e2a9SImre Deak 				   i9xx_port_hotplug_long_detect);
1640676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
164116c6c56bSVille Syrjälä 	}
16423ff60f89SOscar Mateo }
164316c6c56bSVille Syrjälä 
1644c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1645c1874ed7SImre Deak {
164645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
16472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1648c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1649c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1650c1874ed7SImre Deak 
16512dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16522dd2a883SImre Deak 		return IRQ_NONE;
16532dd2a883SImre Deak 
1654c1874ed7SImre Deak 	while (true) {
16553ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
16563ff60f89SOscar Mateo 
1657c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
16583ff60f89SOscar Mateo 		if (gt_iir)
16593ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
16603ff60f89SOscar Mateo 
1661c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
16623ff60f89SOscar Mateo 		if (pm_iir)
16633ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
16643ff60f89SOscar Mateo 
16653ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
16663ff60f89SOscar Mateo 		if (iir) {
16673ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
16683ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
16693ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
16703ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
16713ff60f89SOscar Mateo 		}
1672c1874ed7SImre Deak 
1673c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1674c1874ed7SImre Deak 			goto out;
1675c1874ed7SImre Deak 
1676c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1677c1874ed7SImre Deak 
16783ff60f89SOscar Mateo 		if (gt_iir)
1679c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
168060611c13SPaulo Zanoni 		if (pm_iir)
1681d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
16823ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
16833ff60f89SOscar Mateo 		 * signalled in iir */
16843ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
16857e231dbeSJesse Barnes 	}
16867e231dbeSJesse Barnes 
16877e231dbeSJesse Barnes out:
16887e231dbeSJesse Barnes 	return ret;
16897e231dbeSJesse Barnes }
16907e231dbeSJesse Barnes 
169143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
169243f328d7SVille Syrjälä {
169345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
169443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
169543f328d7SVille Syrjälä 	u32 master_ctl, iir;
169643f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
169743f328d7SVille Syrjälä 
16982dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
16992dd2a883SImre Deak 		return IRQ_NONE;
17002dd2a883SImre Deak 
17018e5fd599SVille Syrjälä 	for (;;) {
17028e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
17033278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
17043278f67fSVille Syrjälä 
17053278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
17068e5fd599SVille Syrjälä 			break;
170743f328d7SVille Syrjälä 
170827b6c122SOscar Mateo 		ret = IRQ_HANDLED;
170927b6c122SOscar Mateo 
171043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
171143f328d7SVille Syrjälä 
171227b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
171327b6c122SOscar Mateo 
171427b6c122SOscar Mateo 		if (iir) {
171527b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
171627b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
171727b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
171827b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
171927b6c122SOscar Mateo 		}
172027b6c122SOscar Mateo 
172174cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
172243f328d7SVille Syrjälä 
172327b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
172427b6c122SOscar Mateo 		 * signalled in iir */
17253278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
172643f328d7SVille Syrjälä 
172743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
172843f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
17298e5fd599SVille Syrjälä 	}
17303278f67fSVille Syrjälä 
173143f328d7SVille Syrjälä 	return ret;
173243f328d7SVille Syrjälä }
173343f328d7SVille Syrjälä 
173423e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1735776ad806SJesse Barnes {
17362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
17379db4a9c7SJesse Barnes 	int pipe;
1738b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1739aaf5ec2eSSonika Jindal 
1740aaf5ec2eSSonika Jindal 	if (hotplug_trigger) {
174142db67d6SVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1742776ad806SJesse Barnes 
174313cf5504SDave Airlie 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
174413cf5504SDave Airlie 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
174513cf5504SDave Airlie 
1746fd63e2a9SImre Deak 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1747fd63e2a9SImre Deak 				   dig_hotplug_reg, hpd_ibx,
1748fd63e2a9SImre Deak 				   pch_port_hotplug_long_detect);
1749676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1750aaf5ec2eSSonika Jindal 	}
175191d131d2SDaniel Vetter 
1752cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1753cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1754776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1755cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1756cfc33bf7SVille Syrjälä 				 port_name(port));
1757cfc33bf7SVille Syrjälä 	}
1758776ad806SJesse Barnes 
1759ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1760ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1761ce99c256SDaniel Vetter 
1762776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1763515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1764776ad806SJesse Barnes 
1765776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1766776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1767776ad806SJesse Barnes 
1768776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1769776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1770776ad806SJesse Barnes 
1771776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1772776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1773776ad806SJesse Barnes 
17749db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1775055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
17769db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
17779db4a9c7SJesse Barnes 					 pipe_name(pipe),
17789db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1779776ad806SJesse Barnes 
1780776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1781776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1782776ad806SJesse Barnes 
1783776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1784776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1785776ad806SJesse Barnes 
1786776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
17871f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
17888664281bSPaulo Zanoni 
17898664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
17901f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
17918664281bSPaulo Zanoni }
17928664281bSPaulo Zanoni 
17938664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
17948664281bSPaulo Zanoni {
17958664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
17968664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
17975a69b89fSDaniel Vetter 	enum pipe pipe;
17988664281bSPaulo Zanoni 
1799de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1800de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1801de032bf4SPaulo Zanoni 
1802055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
18031f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
18041f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
18058664281bSPaulo Zanoni 
18065a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
18075a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1808277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
18095a69b89fSDaniel Vetter 			else
1810277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
18115a69b89fSDaniel Vetter 		}
18125a69b89fSDaniel Vetter 	}
18138bf1e9f1SShuang He 
18148664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
18158664281bSPaulo Zanoni }
18168664281bSPaulo Zanoni 
18178664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
18188664281bSPaulo Zanoni {
18198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
18208664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
18218664281bSPaulo Zanoni 
1822de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1823de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1824de032bf4SPaulo Zanoni 
18258664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
18261f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18278664281bSPaulo Zanoni 
18288664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
18291f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18308664281bSPaulo Zanoni 
18318664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
18321f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
18338664281bSPaulo Zanoni 
18348664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1835776ad806SJesse Barnes }
1836776ad806SJesse Barnes 
183723e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
183823e81d69SAdam Jackson {
18392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
184023e81d69SAdam Jackson 	int pipe;
18416dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1842aaf5ec2eSSonika Jindal 
1843aaf5ec2eSSonika Jindal 	if (hotplug_trigger) {
184442db67d6SVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
184523e81d69SAdam Jackson 
184613cf5504SDave Airlie 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
184713cf5504SDave Airlie 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
1848fd63e2a9SImre Deak 
184926951cafSXiong Zhang 		intel_get_hpd_pins(&pin_mask, &long_mask,
185026951cafSXiong Zhang 				   hotplug_trigger,
1851fd63e2a9SImre Deak 				   dig_hotplug_reg, hpd_cpt,
1852fd63e2a9SImre Deak 				   pch_port_hotplug_long_detect);
185326951cafSXiong Zhang 
1854676574dfSJani Nikula 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1855aaf5ec2eSSonika Jindal 	}
185691d131d2SDaniel Vetter 
1857cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1858cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
185923e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1860cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1861cfc33bf7SVille Syrjälä 				 port_name(port));
1862cfc33bf7SVille Syrjälä 	}
186323e81d69SAdam Jackson 
186423e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1865ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
186623e81d69SAdam Jackson 
186723e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1868515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
186923e81d69SAdam Jackson 
187023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
187123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
187223e81d69SAdam Jackson 
187323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
187423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
187523e81d69SAdam Jackson 
187623e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1877055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
187823e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
187923e81d69SAdam Jackson 					 pipe_name(pipe),
188023e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
18818664281bSPaulo Zanoni 
18828664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
18838664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
188423e81d69SAdam Jackson }
188523e81d69SAdam Jackson 
18866dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
18876dbf30ceSVille Syrjälä {
18886dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
18896dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
18906dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
18916dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
18926dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
18936dbf30ceSVille Syrjälä 
18946dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
18956dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
18966dbf30ceSVille Syrjälä 
18976dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
18986dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
18996dbf30ceSVille Syrjälä 
19006dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
19016dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19026dbf30ceSVille Syrjälä 				   pch_port_hotplug_long_detect);
19036dbf30ceSVille Syrjälä 	}
19046dbf30ceSVille Syrjälä 
19056dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
19066dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
19076dbf30ceSVille Syrjälä 
19086dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
19096dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
19106dbf30ceSVille Syrjälä 
19116dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
19126dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
19136dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
19146dbf30ceSVille Syrjälä 	}
19156dbf30ceSVille Syrjälä 
19166dbf30ceSVille Syrjälä 	if (pin_mask)
19176dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
19186dbf30ceSVille Syrjälä 
19196dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
19206dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
19216dbf30ceSVille Syrjälä }
19226dbf30ceSVille Syrjälä 
1923c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1924c008bc6eSPaulo Zanoni {
1925c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
192640da17c2SDaniel Vetter 	enum pipe pipe;
1927e4ce95aaSVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
1928e4ce95aaSVille Syrjälä 
1929e4ce95aaSVille Syrjälä 	if (hotplug_trigger) {
1930e4ce95aaSVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1931e4ce95aaSVille Syrjälä 
1932e4ce95aaSVille Syrjälä 		dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
1933e4ce95aaSVille Syrjälä 		I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
1934e4ce95aaSVille Syrjälä 
1935e4ce95aaSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1936e4ce95aaSVille Syrjälä 				   dig_hotplug_reg, hpd_ilk,
1937e4ce95aaSVille Syrjälä 				   ilk_port_hotplug_long_detect);
1938e4ce95aaSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
1939e4ce95aaSVille Syrjälä 	}
1940c008bc6eSPaulo Zanoni 
1941c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1942c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1943c008bc6eSPaulo Zanoni 
1944c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1945c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1946c008bc6eSPaulo Zanoni 
1947c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1948c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1949c008bc6eSPaulo Zanoni 
1950055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1951d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
1952d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1953d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
1954c008bc6eSPaulo Zanoni 
195540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
19561f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
1957c008bc6eSPaulo Zanoni 
195840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
195940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
19605b3a856bSDaniel Vetter 
196140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
196240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
196340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
196440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
1965c008bc6eSPaulo Zanoni 		}
1966c008bc6eSPaulo Zanoni 	}
1967c008bc6eSPaulo Zanoni 
1968c008bc6eSPaulo Zanoni 	/* check event from PCH */
1969c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
1970c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
1971c008bc6eSPaulo Zanoni 
1972c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
1973c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
1974c008bc6eSPaulo Zanoni 		else
1975c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
1976c008bc6eSPaulo Zanoni 
1977c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
1978c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
1979c008bc6eSPaulo Zanoni 	}
1980c008bc6eSPaulo Zanoni 
1981c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
1982c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
1983c008bc6eSPaulo Zanoni }
1984c008bc6eSPaulo Zanoni 
19859719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
19869719fb98SPaulo Zanoni {
19879719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
198807d27e20SDamien Lespiau 	enum pipe pipe;
198923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
199023bb4cb5SVille Syrjälä 
199123bb4cb5SVille Syrjälä 	if (hotplug_trigger) {
199223bb4cb5SVille Syrjälä 		u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
199323bb4cb5SVille Syrjälä 
199423bb4cb5SVille Syrjälä 		dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
199523bb4cb5SVille Syrjälä 		I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
199623bb4cb5SVille Syrjälä 
199723bb4cb5SVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
199823bb4cb5SVille Syrjälä 				   dig_hotplug_reg, hpd_ivb,
199923bb4cb5SVille Syrjälä 				   ilk_port_hotplug_long_detect);
200023bb4cb5SVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
200123bb4cb5SVille Syrjälä 	}
20029719fb98SPaulo Zanoni 
20039719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20049719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20059719fb98SPaulo Zanoni 
20069719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20079719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20089719fb98SPaulo Zanoni 
20099719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20109719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20119719fb98SPaulo Zanoni 
2012055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2013d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2014d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2015d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
201640da17c2SDaniel Vetter 
201740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
201807d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
201907d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
202007d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20219719fb98SPaulo Zanoni 		}
20229719fb98SPaulo Zanoni 	}
20239719fb98SPaulo Zanoni 
20249719fb98SPaulo Zanoni 	/* check event from PCH */
20259719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20269719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20279719fb98SPaulo Zanoni 
20289719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20299719fb98SPaulo Zanoni 
20309719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20319719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20329719fb98SPaulo Zanoni 	}
20339719fb98SPaulo Zanoni }
20349719fb98SPaulo Zanoni 
203572c90f62SOscar Mateo /*
203672c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
203772c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
203872c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
203972c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
204072c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
204172c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
204272c90f62SOscar Mateo  */
2043f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2044b1f14ad0SJesse Barnes {
204545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2047f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20480e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2049b1f14ad0SJesse Barnes 
20502dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20512dd2a883SImre Deak 		return IRQ_NONE;
20522dd2a883SImre Deak 
20538664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
20548664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2055907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
20568664281bSPaulo Zanoni 
2057b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2058b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2059b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
206023a78516SPaulo Zanoni 	POSTING_READ(DEIER);
20610e43406bSChris Wilson 
206244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
206344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
206444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
206544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
206644498aeaSPaulo Zanoni 	 * due to its back queue). */
2067ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
206844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
206944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
207044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2071ab5c608bSBen Widawsky 	}
207244498aeaSPaulo Zanoni 
207372c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
207472c90f62SOscar Mateo 
20750e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
20760e43406bSChris Wilson 	if (gt_iir) {
207772c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
207872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2079d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
20800e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2081d8fc8a47SPaulo Zanoni 		else
2082d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
20830e43406bSChris Wilson 	}
2084b1f14ad0SJesse Barnes 
2085b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
20860e43406bSChris Wilson 	if (de_iir) {
208772c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
208872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2089f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
20909719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2091f1af8fc1SPaulo Zanoni 		else
2092f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
20930e43406bSChris Wilson 	}
20940e43406bSChris Wilson 
2095f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2096f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
20970e43406bSChris Wilson 		if (pm_iir) {
2098b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
20990e43406bSChris Wilson 			ret = IRQ_HANDLED;
210072c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21010e43406bSChris Wilson 		}
2102f1af8fc1SPaulo Zanoni 	}
2103b1f14ad0SJesse Barnes 
2104b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2105b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2106ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
210744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
210844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2109ab5c608bSBen Widawsky 	}
2110b1f14ad0SJesse Barnes 
2111b1f14ad0SJesse Barnes 	return ret;
2112b1f14ad0SJesse Barnes }
2113b1f14ad0SJesse Barnes 
2114d04a492dSShashank Sharma static void bxt_hpd_handler(struct drm_device *dev, uint32_t iir_status)
2115d04a492dSShashank Sharma {
2116d04a492dSShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
2117676574dfSJani Nikula 	u32 hp_control, hp_trigger;
211842db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
2119d04a492dSShashank Sharma 
2120d04a492dSShashank Sharma 	/* Get the status */
2121d04a492dSShashank Sharma 	hp_trigger = iir_status & BXT_DE_PORT_HOTPLUG_MASK;
2122d04a492dSShashank Sharma 	hp_control = I915_READ(BXT_HOTPLUG_CTL);
2123d04a492dSShashank Sharma 
2124d04a492dSShashank Sharma 	/* Hotplug not enabled ? */
2125d04a492dSShashank Sharma 	if (!(hp_control & BXT_HOTPLUG_CTL_MASK)) {
2126d04a492dSShashank Sharma 		DRM_ERROR("Interrupt when HPD disabled\n");
2127d04a492dSShashank Sharma 		return;
2128d04a492dSShashank Sharma 	}
2129d04a492dSShashank Sharma 
2130d04a492dSShashank Sharma 	/* Clear sticky bits in hpd status */
2131d04a492dSShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hp_control);
2132475c2e3bSJani Nikula 
2133fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hp_trigger, hp_control,
213463c88d22SImre Deak 			   hpd_bxt, bxt_port_hotplug_long_detect);
2135475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2136d04a492dSShashank Sharma }
2137d04a492dSShashank Sharma 
2138abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2139abd58f01SBen Widawsky {
2140abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2141abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2142abd58f01SBen Widawsky 	u32 master_ctl;
2143abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2144abd58f01SBen Widawsky 	uint32_t tmp = 0;
2145c42664ccSDaniel Vetter 	enum pipe pipe;
214688e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
214788e04703SJesse Barnes 
21482dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21492dd2a883SImre Deak 		return IRQ_NONE;
21502dd2a883SImre Deak 
215188e04703SJesse Barnes 	if (IS_GEN9(dev))
215288e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
215388e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2154abd58f01SBen Widawsky 
2155cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2156abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2157abd58f01SBen Widawsky 	if (!master_ctl)
2158abd58f01SBen Widawsky 		return IRQ_NONE;
2159abd58f01SBen Widawsky 
2160cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2161abd58f01SBen Widawsky 
216238cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
216338cc46d7SOscar Mateo 
216474cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2165abd58f01SBen Widawsky 
2166abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2167abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2168abd58f01SBen Widawsky 		if (tmp) {
2169abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2170abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
217138cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
217238cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
217338cc46d7SOscar Mateo 			else
217438cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2175abd58f01SBen Widawsky 		}
217638cc46d7SOscar Mateo 		else
217738cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2178abd58f01SBen Widawsky 	}
2179abd58f01SBen Widawsky 
21806d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
21816d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
21826d766f02SDaniel Vetter 		if (tmp) {
2183d04a492dSShashank Sharma 			bool found = false;
2184*3a3b3c7dSVille Syrjälä 			u32 hotplug_trigger = tmp & GEN8_PORT_DP_A_HOTPLUG;
2185d04a492dSShashank Sharma 
21866d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
21876d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
218888e04703SJesse Barnes 
2189*3a3b3c7dSVille Syrjälä 			if (IS_BROADWELL(dev) && hotplug_trigger) {
2190*3a3b3c7dSVille Syrjälä 				u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2191*3a3b3c7dSVille Syrjälä 
2192*3a3b3c7dSVille Syrjälä 				dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2193*3a3b3c7dSVille Syrjälä 				I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2194*3a3b3c7dSVille Syrjälä 
2195*3a3b3c7dSVille Syrjälä 				intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
2196*3a3b3c7dSVille Syrjälä 						   dig_hotplug_reg, hpd_bdw,
2197*3a3b3c7dSVille Syrjälä 						   ilk_port_hotplug_long_detect);
2198*3a3b3c7dSVille Syrjälä 				intel_hpd_irq_handler(dev, pin_mask, long_mask);
2199*3a3b3c7dSVille Syrjälä 				found = true;
2200*3a3b3c7dSVille Syrjälä 			}
2201*3a3b3c7dSVille Syrjälä 
2202d04a492dSShashank Sharma 			if (tmp & aux_mask) {
220338cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2204d04a492dSShashank Sharma 				found = true;
2205d04a492dSShashank Sharma 			}
2206d04a492dSShashank Sharma 
2207d04a492dSShashank Sharma 			if (IS_BROXTON(dev) && tmp & BXT_DE_PORT_HOTPLUG_MASK) {
2208d04a492dSShashank Sharma 				bxt_hpd_handler(dev, tmp);
2209d04a492dSShashank Sharma 				found = true;
2210d04a492dSShashank Sharma 			}
2211d04a492dSShashank Sharma 
22129e63743eSShashank Sharma 			if (IS_BROXTON(dev) && (tmp & BXT_DE_PORT_GMBUS)) {
22139e63743eSShashank Sharma 				gmbus_irq_handler(dev);
22149e63743eSShashank Sharma 				found = true;
22159e63743eSShashank Sharma 			}
22169e63743eSShashank Sharma 
2217d04a492dSShashank Sharma 			if (!found)
221838cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22196d766f02SDaniel Vetter 		}
222038cc46d7SOscar Mateo 		else
222138cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22226d766f02SDaniel Vetter 	}
22236d766f02SDaniel Vetter 
2224055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2225770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2226abd58f01SBen Widawsky 
2227c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2228c42664ccSDaniel Vetter 			continue;
2229c42664ccSDaniel Vetter 
2230abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
223138cc46d7SOscar Mateo 		if (pipe_iir) {
223238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
223338cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2234770de83dSDamien Lespiau 
2235d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2236d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2237d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2238abd58f01SBen Widawsky 
2239770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2240770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2241770de83dSDamien Lespiau 			else
2242770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2243770de83dSDamien Lespiau 
2244770de83dSDamien Lespiau 			if (flip_done) {
2245abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2246abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2247abd58f01SBen Widawsky 			}
2248abd58f01SBen Widawsky 
22490fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22500fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22510fbe7870SDaniel Vetter 
22521f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22531f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22541f7247c0SDaniel Vetter 								    pipe);
225538d83c96SDaniel Vetter 
2256770de83dSDamien Lespiau 
2257770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2258770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2259770de83dSDamien Lespiau 			else
2260770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2261770de83dSDamien Lespiau 
2262770de83dSDamien Lespiau 			if (fault_errors)
226330100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
226430100f2bSDaniel Vetter 					  pipe_name(pipe),
226530100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2266c42664ccSDaniel Vetter 		} else
2267abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2268abd58f01SBen Widawsky 	}
2269abd58f01SBen Widawsky 
2270266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2271266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
227292d03a80SDaniel Vetter 		/*
227392d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
227492d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
227592d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
227692d03a80SDaniel Vetter 		 */
227792d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
227892d03a80SDaniel Vetter 		if (pch_iir) {
227992d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
228092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
22816dbf30ceSVille Syrjälä 
22826dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
22836dbf30ceSVille Syrjälä 				spt_irq_handler(dev, pch_iir);
22846dbf30ceSVille Syrjälä 			else
228538cc46d7SOscar Mateo 				cpt_irq_handler(dev, pch_iir);
228638cc46d7SOscar Mateo 		} else
228738cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
228838cc46d7SOscar Mateo 
228992d03a80SDaniel Vetter 	}
229092d03a80SDaniel Vetter 
2291cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2292cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2293abd58f01SBen Widawsky 
2294abd58f01SBen Widawsky 	return ret;
2295abd58f01SBen Widawsky }
2296abd58f01SBen Widawsky 
229717e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
229817e1df07SDaniel Vetter 			       bool reset_completed)
229917e1df07SDaniel Vetter {
2300a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
230117e1df07SDaniel Vetter 	int i;
230217e1df07SDaniel Vetter 
230317e1df07SDaniel Vetter 	/*
230417e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
230517e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
230617e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
230717e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
230817e1df07SDaniel Vetter 	 */
230917e1df07SDaniel Vetter 
231017e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
231117e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
231217e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
231317e1df07SDaniel Vetter 
231417e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
231517e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
231617e1df07SDaniel Vetter 
231717e1df07SDaniel Vetter 	/*
231817e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
231917e1df07SDaniel Vetter 	 * reset state is cleared.
232017e1df07SDaniel Vetter 	 */
232117e1df07SDaniel Vetter 	if (reset_completed)
232217e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
232317e1df07SDaniel Vetter }
232417e1df07SDaniel Vetter 
23258a905236SJesse Barnes /**
2326b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23278a905236SJesse Barnes  *
23288a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23298a905236SJesse Barnes  * was detected.
23308a905236SJesse Barnes  */
2331b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23328a905236SJesse Barnes {
2333b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2334b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2335cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2336cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2337cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
233817e1df07SDaniel Vetter 	int ret;
23398a905236SJesse Barnes 
23405bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23418a905236SJesse Barnes 
23427db0ba24SDaniel Vetter 	/*
23437db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23447db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23457db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23467db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23477db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23487db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23497db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23507db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23517db0ba24SDaniel Vetter 	 */
23527db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
235344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23545bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23557db0ba24SDaniel Vetter 				   reset_event);
23561f83fee0SDaniel Vetter 
235717e1df07SDaniel Vetter 		/*
2358f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2359f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2360f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2361f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2362f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2363f454c694SImre Deak 		 */
2364f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23657514747dSVille Syrjälä 
23667514747dSVille Syrjälä 		intel_prepare_reset(dev);
23677514747dSVille Syrjälä 
2368f454c694SImre Deak 		/*
236917e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
237017e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
237117e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
237217e1df07SDaniel Vetter 		 * deadlocks with the reset work.
237317e1df07SDaniel Vetter 		 */
2374f69061beSDaniel Vetter 		ret = i915_reset(dev);
2375f69061beSDaniel Vetter 
23767514747dSVille Syrjälä 		intel_finish_reset(dev);
237717e1df07SDaniel Vetter 
2378f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2379f454c694SImre Deak 
2380f69061beSDaniel Vetter 		if (ret == 0) {
2381f69061beSDaniel Vetter 			/*
2382f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2383f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2384f69061beSDaniel Vetter 			 * complete.
2385f69061beSDaniel Vetter 			 *
2386f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2387f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2388f69061beSDaniel Vetter 			 * updates before
2389f69061beSDaniel Vetter 			 * the counter increment.
2390f69061beSDaniel Vetter 			 */
23914e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2392f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2393f69061beSDaniel Vetter 
23945bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2395f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
23961f83fee0SDaniel Vetter 		} else {
23972ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2398f316a42cSBen Gamari 		}
23991f83fee0SDaniel Vetter 
240017e1df07SDaniel Vetter 		/*
240117e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
240217e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
240317e1df07SDaniel Vetter 		 */
240417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2405f316a42cSBen Gamari 	}
24068a905236SJesse Barnes }
24078a905236SJesse Barnes 
240835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2409c0e09200SDave Airlie {
24108a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2411bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
241263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2413050ee91fSBen Widawsky 	int pipe, i;
241463eeaf38SJesse Barnes 
241535aed2e6SChris Wilson 	if (!eir)
241635aed2e6SChris Wilson 		return;
241763eeaf38SJesse Barnes 
2418a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24198a905236SJesse Barnes 
2420bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2421bd9854f9SBen Widawsky 
24228a905236SJesse Barnes 	if (IS_G4X(dev)) {
24238a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24248a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24258a905236SJesse Barnes 
2426a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2427a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2428050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2429050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2430a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2431a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24328a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24333143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24348a905236SJesse Barnes 		}
24358a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24368a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2437a70491ccSJoe Perches 			pr_err("page table error\n");
2438a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24398a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24403143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24418a905236SJesse Barnes 		}
24428a905236SJesse Barnes 	}
24438a905236SJesse Barnes 
2444a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
244563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
244663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2447a70491ccSJoe Perches 			pr_err("page table error\n");
2448a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
244963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24503143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
245163eeaf38SJesse Barnes 		}
24528a905236SJesse Barnes 	}
24538a905236SJesse Barnes 
245463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2455a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2456055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2457a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24589db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
245963eeaf38SJesse Barnes 		/* pipestat has already been acked */
246063eeaf38SJesse Barnes 	}
246163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2462a70491ccSJoe Perches 		pr_err("instruction error\n");
2463a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2464050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2465050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2466a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
246763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
246863eeaf38SJesse Barnes 
2469a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2470a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2471a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
247263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24733143a2bfSChris Wilson 			POSTING_READ(IPEIR);
247463eeaf38SJesse Barnes 		} else {
247563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
247663eeaf38SJesse Barnes 
2477a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2478a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2479a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2480a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
248163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24823143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
248363eeaf38SJesse Barnes 		}
248463eeaf38SJesse Barnes 	}
248563eeaf38SJesse Barnes 
248663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24873143a2bfSChris Wilson 	POSTING_READ(EIR);
248863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
248963eeaf38SJesse Barnes 	if (eir) {
249063eeaf38SJesse Barnes 		/*
249163eeaf38SJesse Barnes 		 * some errors might have become stuck,
249263eeaf38SJesse Barnes 		 * mask them.
249363eeaf38SJesse Barnes 		 */
249463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
249563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
249663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
249763eeaf38SJesse Barnes 	}
249835aed2e6SChris Wilson }
249935aed2e6SChris Wilson 
250035aed2e6SChris Wilson /**
2501b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
250235aed2e6SChris Wilson  * @dev: drm device
250335aed2e6SChris Wilson  *
2504b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
250535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
250635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
250735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
250835aed2e6SChris Wilson  * of a ring dump etc.).
250935aed2e6SChris Wilson  */
251058174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
251158174462SMika Kuoppala 		       const char *fmt, ...)
251235aed2e6SChris Wilson {
251335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
251458174462SMika Kuoppala 	va_list args;
251558174462SMika Kuoppala 	char error_msg[80];
251635aed2e6SChris Wilson 
251758174462SMika Kuoppala 	va_start(args, fmt);
251858174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
251958174462SMika Kuoppala 	va_end(args);
252058174462SMika Kuoppala 
252158174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
252235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25238a905236SJesse Barnes 
2524ba1234d1SBen Gamari 	if (wedged) {
2525f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2526f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2527ba1234d1SBen Gamari 
252811ed50ecSBen Gamari 		/*
2529b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2530b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2531b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
253217e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
253317e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
253417e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
253517e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
253617e1df07SDaniel Vetter 		 *
253717e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
253817e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
253917e1df07SDaniel Vetter 		 * counter atomic_t.
254011ed50ecSBen Gamari 		 */
254117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
254211ed50ecSBen Gamari 	}
254311ed50ecSBen Gamari 
2544b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25458a905236SJesse Barnes }
25468a905236SJesse Barnes 
254742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
254842f52ef8SKeith Packard  * we use as a pipe index
254942f52ef8SKeith Packard  */
2550f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25510a3e67a4SJesse Barnes {
25522d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2553e9d21d7fSKeith Packard 	unsigned long irqflags;
255471e0ffa5SJesse Barnes 
25551ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2556f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25577c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2558755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25590a3e67a4SJesse Barnes 	else
25607c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2561755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25621ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25638692d00eSChris Wilson 
25640a3e67a4SJesse Barnes 	return 0;
25650a3e67a4SJesse Barnes }
25660a3e67a4SJesse Barnes 
2567f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2568f796cf8fSJesse Barnes {
25692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2570f796cf8fSJesse Barnes 	unsigned long irqflags;
2571b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
257240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2573f796cf8fSJesse Barnes 
2574f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2575b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2576b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2577b1f14ad0SJesse Barnes 
2578b1f14ad0SJesse Barnes 	return 0;
2579b1f14ad0SJesse Barnes }
2580b1f14ad0SJesse Barnes 
25817e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
25827e231dbeSJesse Barnes {
25832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25847e231dbeSJesse Barnes 	unsigned long irqflags;
25857e231dbeSJesse Barnes 
25867e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
258731acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2588755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25897e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25907e231dbeSJesse Barnes 
25917e231dbeSJesse Barnes 	return 0;
25927e231dbeSJesse Barnes }
25937e231dbeSJesse Barnes 
2594abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2595abd58f01SBen Widawsky {
2596abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2597abd58f01SBen Widawsky 	unsigned long irqflags;
2598abd58f01SBen Widawsky 
2599abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26007167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26017167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2602abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2603abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2604abd58f01SBen Widawsky 	return 0;
2605abd58f01SBen Widawsky }
2606abd58f01SBen Widawsky 
260742f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
260842f52ef8SKeith Packard  * we use as a pipe index
260942f52ef8SKeith Packard  */
2610f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26110a3e67a4SJesse Barnes {
26122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2613e9d21d7fSKeith Packard 	unsigned long irqflags;
26140a3e67a4SJesse Barnes 
26151ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26167c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2617755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2618755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26191ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26200a3e67a4SJesse Barnes }
26210a3e67a4SJesse Barnes 
2622f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2623f796cf8fSJesse Barnes {
26242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2625f796cf8fSJesse Barnes 	unsigned long irqflags;
2626b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
262740da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2628f796cf8fSJesse Barnes 
2629f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2630b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2631b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2632b1f14ad0SJesse Barnes }
2633b1f14ad0SJesse Barnes 
26347e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26357e231dbeSJesse Barnes {
26362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26377e231dbeSJesse Barnes 	unsigned long irqflags;
26387e231dbeSJesse Barnes 
26397e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
264031acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2641755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26427e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26437e231dbeSJesse Barnes }
26447e231dbeSJesse Barnes 
2645abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2646abd58f01SBen Widawsky {
2647abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2648abd58f01SBen Widawsky 	unsigned long irqflags;
2649abd58f01SBen Widawsky 
2650abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26517167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26527167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2653abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2654abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2655abd58f01SBen Widawsky }
2656abd58f01SBen Widawsky 
26579107e9d2SChris Wilson static bool
265894f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2659893eead0SChris Wilson {
26609107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
266194f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2662f65d9421SBen Gamari }
2663f65d9421SBen Gamari 
2664a028c4b0SDaniel Vetter static bool
2665a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2666a028c4b0SDaniel Vetter {
2667a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2668a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2669a028c4b0SDaniel Vetter 	} else {
2670a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2671a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2672a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2673a028c4b0SDaniel Vetter 	}
2674a028c4b0SDaniel Vetter }
2675a028c4b0SDaniel Vetter 
2676a4872ba6SOscar Mateo static struct intel_engine_cs *
2677a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2678921d42eaSDaniel Vetter {
2679921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2680a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2681921d42eaSDaniel Vetter 	int i;
2682921d42eaSDaniel Vetter 
2683921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2684a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2685a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2686a6cdb93aSRodrigo Vivi 				continue;
2687a6cdb93aSRodrigo Vivi 
2688a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2689a6cdb93aSRodrigo Vivi 				return signaller;
2690a6cdb93aSRodrigo Vivi 		}
2691921d42eaSDaniel Vetter 	} else {
2692921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2693921d42eaSDaniel Vetter 
2694921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2695921d42eaSDaniel Vetter 			if(ring == signaller)
2696921d42eaSDaniel Vetter 				continue;
2697921d42eaSDaniel Vetter 
2698ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2699921d42eaSDaniel Vetter 				return signaller;
2700921d42eaSDaniel Vetter 		}
2701921d42eaSDaniel Vetter 	}
2702921d42eaSDaniel Vetter 
2703a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2704a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2705921d42eaSDaniel Vetter 
2706921d42eaSDaniel Vetter 	return NULL;
2707921d42eaSDaniel Vetter }
2708921d42eaSDaniel Vetter 
2709a4872ba6SOscar Mateo static struct intel_engine_cs *
2710a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2711a24a11e6SChris Wilson {
2712a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
271388fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2714a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2715a6cdb93aSRodrigo Vivi 	int i, backwards;
2716a24a11e6SChris Wilson 
2717a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2718a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27196274f212SChris Wilson 		return NULL;
2720a24a11e6SChris Wilson 
272188fe429dSDaniel Vetter 	/*
272288fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
272388fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2724a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2725a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
272688fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
272788fe429dSDaniel Vetter 	 * ringbuffer itself.
2728a24a11e6SChris Wilson 	 */
272988fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2730a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
273188fe429dSDaniel Vetter 
2732a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
273388fe429dSDaniel Vetter 		/*
273488fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
273588fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
273688fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
273788fe429dSDaniel Vetter 		 */
2738ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
273988fe429dSDaniel Vetter 
274088fe429dSDaniel Vetter 		/* This here seems to blow up */
2741ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2742a24a11e6SChris Wilson 		if (cmd == ipehr)
2743a24a11e6SChris Wilson 			break;
2744a24a11e6SChris Wilson 
274588fe429dSDaniel Vetter 		head -= 4;
274688fe429dSDaniel Vetter 	}
2747a24a11e6SChris Wilson 
274888fe429dSDaniel Vetter 	if (!i)
274988fe429dSDaniel Vetter 		return NULL;
275088fe429dSDaniel Vetter 
2751ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2752a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2753a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2754a6cdb93aSRodrigo Vivi 		offset <<= 32;
2755a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2756a6cdb93aSRodrigo Vivi 	}
2757a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2758a24a11e6SChris Wilson }
2759a24a11e6SChris Wilson 
2760a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27616274f212SChris Wilson {
27626274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2763a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2764a0d036b0SChris Wilson 	u32 seqno;
27656274f212SChris Wilson 
27664be17381SChris Wilson 	ring->hangcheck.deadlock++;
27676274f212SChris Wilson 
27686274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
27694be17381SChris Wilson 	if (signaller == NULL)
27704be17381SChris Wilson 		return -1;
27714be17381SChris Wilson 
27724be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
27734be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
27746274f212SChris Wilson 		return -1;
27756274f212SChris Wilson 
27764be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
27774be17381SChris Wilson 		return 1;
27784be17381SChris Wilson 
2779a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2780a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2781a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
27824be17381SChris Wilson 		return -1;
27834be17381SChris Wilson 
27844be17381SChris Wilson 	return 0;
27856274f212SChris Wilson }
27866274f212SChris Wilson 
27876274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
27886274f212SChris Wilson {
2789a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
27906274f212SChris Wilson 	int i;
27916274f212SChris Wilson 
27926274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
27934be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
27946274f212SChris Wilson }
27956274f212SChris Wilson 
2796ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2797a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
27981ec14ad3SChris Wilson {
27991ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28001ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28019107e9d2SChris Wilson 	u32 tmp;
28029107e9d2SChris Wilson 
2803f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2804f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2805f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2806f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2807f260fe7bSMika Kuoppala 		}
2808f260fe7bSMika Kuoppala 
2809f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2810f260fe7bSMika Kuoppala 	}
28116274f212SChris Wilson 
28129107e9d2SChris Wilson 	if (IS_GEN2(dev))
2813f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28149107e9d2SChris Wilson 
28159107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28169107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28179107e9d2SChris Wilson 	 * and break the hang. This should work on
28189107e9d2SChris Wilson 	 * all but the second generation chipsets.
28199107e9d2SChris Wilson 	 */
28209107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28211ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
282258174462SMika Kuoppala 		i915_handle_error(dev, false,
282358174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28241ec14ad3SChris Wilson 				  ring->name);
28251ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2826f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28271ec14ad3SChris Wilson 	}
2828a24a11e6SChris Wilson 
28296274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28306274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28316274f212SChris Wilson 		default:
2832f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28336274f212SChris Wilson 		case 1:
283458174462SMika Kuoppala 			i915_handle_error(dev, false,
283558174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2836a24a11e6SChris Wilson 					  ring->name);
2837a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2838f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28396274f212SChris Wilson 		case 0:
2840f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28416274f212SChris Wilson 		}
28429107e9d2SChris Wilson 	}
28439107e9d2SChris Wilson 
2844f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2845a24a11e6SChris Wilson }
2846d1e61e7fSChris Wilson 
2847737b1506SChris Wilson /*
2848f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
284905407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
285005407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
285105407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
285205407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
285305407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2854f65d9421SBen Gamari  */
2855737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2856f65d9421SBen Gamari {
2857737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2858737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2859737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2860737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2861a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2862b4519513SChris Wilson 	int i;
286305407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28649107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28659107e9d2SChris Wilson #define BUSY 1
28669107e9d2SChris Wilson #define KICK 5
28679107e9d2SChris Wilson #define HUNG 20
2868893eead0SChris Wilson 
2869d330a953SJani Nikula 	if (!i915.enable_hangcheck)
28703e0dc6b0SBen Widawsky 		return;
28713e0dc6b0SBen Widawsky 
2872b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
287350877445SChris Wilson 		u64 acthd;
287450877445SChris Wilson 		u32 seqno;
28759107e9d2SChris Wilson 		bool busy = true;
2876b4519513SChris Wilson 
28776274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
28786274f212SChris Wilson 
287905407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
288005407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
288105407ff8SMika Kuoppala 
288205407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
288394f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
2884da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2885da661464SMika Kuoppala 
28869107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
28879107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2888094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2889f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
28909107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
28919107e9d2SChris Wilson 								  ring->name);
2892f4adcd24SDaniel Vetter 						else
2893f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2894f4adcd24SDaniel Vetter 								 ring->name);
28959107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2896094f9a54SChris Wilson 					}
2897094f9a54SChris Wilson 					/* Safeguard against driver failure */
2898094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
28999107e9d2SChris Wilson 				} else
29009107e9d2SChris Wilson 					busy = false;
290105407ff8SMika Kuoppala 			} else {
29026274f212SChris Wilson 				/* We always increment the hangcheck score
29036274f212SChris Wilson 				 * if the ring is busy and still processing
29046274f212SChris Wilson 				 * the same request, so that no single request
29056274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29066274f212SChris Wilson 				 * batches). The only time we do not increment
29076274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29086274f212SChris Wilson 				 * ring is in a legitimate wait for another
29096274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29106274f212SChris Wilson 				 * victim and we want to be sure we catch the
29116274f212SChris Wilson 				 * right culprit. Then every time we do kick
29126274f212SChris Wilson 				 * the ring, add a small increment to the
29136274f212SChris Wilson 				 * score so that we can catch a batch that is
29146274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29156274f212SChris Wilson 				 * for stalling the machine.
29169107e9d2SChris Wilson 				 */
2917ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2918ad8beaeaSMika Kuoppala 								    acthd);
2919ad8beaeaSMika Kuoppala 
2920ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2921da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2922f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2923f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2924f260fe7bSMika Kuoppala 					break;
2925f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2926ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29276274f212SChris Wilson 					break;
2928f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2929ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29306274f212SChris Wilson 					break;
2931f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2932ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29336274f212SChris Wilson 					stuck[i] = true;
29346274f212SChris Wilson 					break;
29356274f212SChris Wilson 				}
293605407ff8SMika Kuoppala 			}
29379107e9d2SChris Wilson 		} else {
2938da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2939da661464SMika Kuoppala 
29409107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29419107e9d2SChris Wilson 			 * attempts across multiple batches.
29429107e9d2SChris Wilson 			 */
29439107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29449107e9d2SChris Wilson 				ring->hangcheck.score--;
2945f260fe7bSMika Kuoppala 
2946f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2947cbb465e7SChris Wilson 		}
2948f65d9421SBen Gamari 
294905407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
295005407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29519107e9d2SChris Wilson 		busy_count += busy;
295205407ff8SMika Kuoppala 	}
295305407ff8SMika Kuoppala 
295405407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2955b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2956b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
295705407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2958a43adf07SChris Wilson 				 ring->name);
2959a43adf07SChris Wilson 			rings_hung++;
296005407ff8SMika Kuoppala 		}
296105407ff8SMika Kuoppala 	}
296205407ff8SMika Kuoppala 
296305407ff8SMika Kuoppala 	if (rings_hung)
296458174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
296505407ff8SMika Kuoppala 
296605407ff8SMika Kuoppala 	if (busy_count)
296705407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
296805407ff8SMika Kuoppala 		 * being added */
296910cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
297010cd45b6SMika Kuoppala }
297110cd45b6SMika Kuoppala 
297210cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
297310cd45b6SMika Kuoppala {
2974737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2975672e7b7cSChris Wilson 
2976d330a953SJani Nikula 	if (!i915.enable_hangcheck)
297710cd45b6SMika Kuoppala 		return;
297810cd45b6SMika Kuoppala 
2979737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2980737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2981737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2982737b1506SChris Wilson 	 */
2983737b1506SChris Wilson 
2984737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
2985737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
2986f65d9421SBen Gamari }
2987f65d9421SBen Gamari 
29881c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
298991738a95SPaulo Zanoni {
299091738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
299191738a95SPaulo Zanoni 
299291738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
299391738a95SPaulo Zanoni 		return;
299491738a95SPaulo Zanoni 
2995f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2996105b122eSPaulo Zanoni 
2997105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2998105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2999622364b6SPaulo Zanoni }
3000105b122eSPaulo Zanoni 
300191738a95SPaulo Zanoni /*
3002622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3003622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3004622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3005622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3006622364b6SPaulo Zanoni  *
3007622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
300891738a95SPaulo Zanoni  */
3009622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3010622364b6SPaulo Zanoni {
3011622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3012622364b6SPaulo Zanoni 
3013622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3014622364b6SPaulo Zanoni 		return;
3015622364b6SPaulo Zanoni 
3016622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
301791738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
301891738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
301991738a95SPaulo Zanoni }
302091738a95SPaulo Zanoni 
30217c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3022d18ea1b5SDaniel Vetter {
3023d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3024d18ea1b5SDaniel Vetter 
3025f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3026a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3027f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3028d18ea1b5SDaniel Vetter }
3029d18ea1b5SDaniel Vetter 
3030c0e09200SDave Airlie /* drm_dma.h hooks
3031c0e09200SDave Airlie */
3032be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3033036a4a7dSZhenyu Wang {
30342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3035036a4a7dSZhenyu Wang 
30360c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3037bdfcdb63SDaniel Vetter 
3038f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3039c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3040c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3041036a4a7dSZhenyu Wang 
30427c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3043c650156aSZhenyu Wang 
30441c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30457d99163dSBen Widawsky }
30467d99163dSBen Widawsky 
304770591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
304870591a41SVille Syrjälä {
304970591a41SVille Syrjälä 	enum pipe pipe;
305070591a41SVille Syrjälä 
305170591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
305270591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
305370591a41SVille Syrjälä 
305470591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
305570591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
305670591a41SVille Syrjälä 
305770591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
305870591a41SVille Syrjälä }
305970591a41SVille Syrjälä 
30607e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30617e231dbeSJesse Barnes {
30622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30637e231dbeSJesse Barnes 
30647e231dbeSJesse Barnes 	/* VLV magic */
30657e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30667e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30677e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30687e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30697e231dbeSJesse Barnes 
30707c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30717e231dbeSJesse Barnes 
30727c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
30737e231dbeSJesse Barnes 
307470591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
30757e231dbeSJesse Barnes }
30767e231dbeSJesse Barnes 
3077d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3078d6e3cca3SDaniel Vetter {
3079d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3080d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3081d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3082d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3083d6e3cca3SDaniel Vetter }
3084d6e3cca3SDaniel Vetter 
3085823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3086abd58f01SBen Widawsky {
3087abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3088abd58f01SBen Widawsky 	int pipe;
3089abd58f01SBen Widawsky 
3090abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3091abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3092abd58f01SBen Widawsky 
3093d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3094abd58f01SBen Widawsky 
3095055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3096f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3097813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3098f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3099abd58f01SBen Widawsky 
3100f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3101f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3102f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3103abd58f01SBen Widawsky 
3104266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31051c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3106abd58f01SBen Widawsky }
3107abd58f01SBen Widawsky 
31084c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31094c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3110d49bdb0eSPaulo Zanoni {
31111180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3112d49bdb0eSPaulo Zanoni 
311313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3114d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3115d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3116d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3117d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31184c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31194c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31204c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31211180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31224c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31234c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31244c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31251180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
312613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3127d49bdb0eSPaulo Zanoni }
3128d49bdb0eSPaulo Zanoni 
312943f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
313043f328d7SVille Syrjälä {
313143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
313243f328d7SVille Syrjälä 
313343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
313443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
313543f328d7SVille Syrjälä 
3136d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
313743f328d7SVille Syrjälä 
313843f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
313943f328d7SVille Syrjälä 
314043f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
314143f328d7SVille Syrjälä 
314270591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
314343f328d7SVille Syrjälä }
314443f328d7SVille Syrjälä 
314587a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
314687a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
314787a02106SVille Syrjälä {
314887a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
314987a02106SVille Syrjälä 	struct intel_encoder *encoder;
315087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
315187a02106SVille Syrjälä 
315287a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
315387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
315487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
315587a02106SVille Syrjälä 
315687a02106SVille Syrjälä 	return enabled_irqs;
315787a02106SVille Syrjälä }
315887a02106SVille Syrjälä 
315982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
316082a28bcfSDaniel Vetter {
31612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
316287a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
316382a28bcfSDaniel Vetter 
316482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3165fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
316687a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
316782a28bcfSDaniel Vetter 	} else {
3168fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
316987a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
317082a28bcfSDaniel Vetter 	}
317182a28bcfSDaniel Vetter 
3172fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
317382a28bcfSDaniel Vetter 
31747fe0b973SKeith Packard 	/*
31757fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31766dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
31776dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
31787fe0b973SKeith Packard 	 */
31797fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31807fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31817fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31827fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31837fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31840b2eb33eSVille Syrjälä 	/*
31850b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
31860b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
31870b2eb33eSVille Syrjälä 	 */
31880b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
31890b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
31907fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31916dbf30ceSVille Syrjälä }
319226951cafSXiong Zhang 
31936dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
31946dbf30ceSVille Syrjälä {
31956dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
31966dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
31976dbf30ceSVille Syrjälä 
31986dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
31996dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
32006dbf30ceSVille Syrjälä 
32016dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
32026dbf30ceSVille Syrjälä 
32036dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
32046dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32056dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
32066dbf30ceSVille Syrjälä 		PORTB_HOTPLUG_ENABLE;
32076dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32086dbf30ceSVille Syrjälä 
320926951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
321026951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
321126951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
321226951cafSXiong Zhang }
32137fe0b973SKeith Packard 
3214e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3215e4ce95aaSVille Syrjälä {
3216e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3217e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3218e4ce95aaSVille Syrjälä 
3219*3a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
3220*3a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
3221*3a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
3222*3a3b3c7dSVille Syrjälä 
3223*3a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3224*3a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
322523bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
322623bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
3227*3a3b3c7dSVille Syrjälä 
3228*3a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
322923bb4cb5SVille Syrjälä 	} else {
3230e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3231e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3232e4ce95aaSVille Syrjälä 
3233e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
3234*3a3b3c7dSVille Syrjälä 	}
3235e4ce95aaSVille Syrjälä 
3236e4ce95aaSVille Syrjälä 	/*
3237e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3238e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
323923bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3240e4ce95aaSVille Syrjälä 	 */
3241e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3242e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3243e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3244e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3245e4ce95aaSVille Syrjälä 
3246e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3247e4ce95aaSVille Syrjälä }
3248e4ce95aaSVille Syrjälä 
3249e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3250e0a20ad7SShashank Sharma {
3251e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
325287a02106SVille Syrjälä 	u32 hotplug_port;
3253e0a20ad7SShashank Sharma 	u32 hotplug_ctrl;
3254e0a20ad7SShashank Sharma 
325587a02106SVille Syrjälä 	hotplug_port = intel_hpd_enabled_irqs(dev, hpd_bxt);
3256e0a20ad7SShashank Sharma 
3257e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(BXT_HOTPLUG_CTL) & ~BXT_HOTPLUG_CTL_MASK;
3258e0a20ad7SShashank Sharma 
32597f3561beSSonika Jindal 	if (hotplug_port & BXT_DE_PORT_HP_DDIA)
32607f3561beSSonika Jindal 		hotplug_ctrl |= BXT_DDIA_HPD_ENABLE;
3261e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIB)
3262e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIB_HPD_ENABLE;
3263e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIC)
3264e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIC_HPD_ENABLE;
3265e0a20ad7SShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hotplug_ctrl);
3266e0a20ad7SShashank Sharma 
3267e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IMR) & ~hotplug_port;
3268e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IMR, hotplug_ctrl);
3269e0a20ad7SShashank Sharma 
3270e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IER) | hotplug_port;
3271e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IER, hotplug_ctrl);
3272e0a20ad7SShashank Sharma 	POSTING_READ(GEN8_DE_PORT_IER);
3273e0a20ad7SShashank Sharma }
3274e0a20ad7SShashank Sharma 
3275d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3276d46da437SPaulo Zanoni {
32772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
327882a28bcfSDaniel Vetter 	u32 mask;
3279d46da437SPaulo Zanoni 
3280692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3281692a04cfSDaniel Vetter 		return;
3282692a04cfSDaniel Vetter 
3283105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32845c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3285105b122eSPaulo Zanoni 	else
32865c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32878664281bSPaulo Zanoni 
3288337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3289d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3290d46da437SPaulo Zanoni }
3291d46da437SPaulo Zanoni 
32920a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32930a9a8c91SDaniel Vetter {
32940a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32950a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32960a9a8c91SDaniel Vetter 
32970a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32980a9a8c91SDaniel Vetter 
32990a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3300040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
33010a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
330235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
330335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
33040a9a8c91SDaniel Vetter 	}
33050a9a8c91SDaniel Vetter 
33060a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
33070a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33080a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33090a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33100a9a8c91SDaniel Vetter 	} else {
33110a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33120a9a8c91SDaniel Vetter 	}
33130a9a8c91SDaniel Vetter 
331435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33150a9a8c91SDaniel Vetter 
33160a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
331778e68d36SImre Deak 		/*
331878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
331978e68d36SImre Deak 		 * itself is enabled/disabled.
332078e68d36SImre Deak 		 */
33210a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33220a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33230a9a8c91SDaniel Vetter 
3324605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
332535079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33260a9a8c91SDaniel Vetter 	}
33270a9a8c91SDaniel Vetter }
33280a9a8c91SDaniel Vetter 
3329f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3330036a4a7dSZhenyu Wang {
33312d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33328e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33338e76f8dcSPaulo Zanoni 
33348e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33358e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33368e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33378e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33385c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33398e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
334023bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
334123bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
33428e76f8dcSPaulo Zanoni 	} else {
33438e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3344ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33455b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33465b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33475b3a856bSDaniel Vetter 				DE_POISON);
3348e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3349e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3350e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
33518e76f8dcSPaulo Zanoni 	}
3352036a4a7dSZhenyu Wang 
33531ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3354036a4a7dSZhenyu Wang 
33550c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33560c841212SPaulo Zanoni 
3357622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3358622364b6SPaulo Zanoni 
335935079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3360036a4a7dSZhenyu Wang 
33610a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3362036a4a7dSZhenyu Wang 
3363d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33647fe0b973SKeith Packard 
3365f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33666005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33676005ce42SDaniel Vetter 		 *
33686005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33694bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33704bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3371d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3372f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3373d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3374f97108d1SJesse Barnes 	}
3375f97108d1SJesse Barnes 
3376036a4a7dSZhenyu Wang 	return 0;
3377036a4a7dSZhenyu Wang }
3378036a4a7dSZhenyu Wang 
3379f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3380f8b79e58SImre Deak {
3381f8b79e58SImre Deak 	u32 pipestat_mask;
3382f8b79e58SImre Deak 	u32 iir_mask;
3383120dda4fSVille Syrjälä 	enum pipe pipe;
3384f8b79e58SImre Deak 
3385f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3386f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3387f8b79e58SImre Deak 
3388120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3389120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3390f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3391f8b79e58SImre Deak 
3392f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3393f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3394f8b79e58SImre Deak 
3395120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3396120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3397120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3398f8b79e58SImre Deak 
3399f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3400f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3401f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3402120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3403120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3404f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3405f8b79e58SImre Deak 
3406f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3407f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3408f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
340976e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
341076e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3411f8b79e58SImre Deak }
3412f8b79e58SImre Deak 
3413f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3414f8b79e58SImre Deak {
3415f8b79e58SImre Deak 	u32 pipestat_mask;
3416f8b79e58SImre Deak 	u32 iir_mask;
3417120dda4fSVille Syrjälä 	enum pipe pipe;
3418f8b79e58SImre Deak 
3419f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3420f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
34216c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3422120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3423120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3424f8b79e58SImre Deak 
3425f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3426f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
342776e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3428f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3429f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3430f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3431f8b79e58SImre Deak 
3432f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3433f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3434f8b79e58SImre Deak 
3435120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3436120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3437120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3438f8b79e58SImre Deak 
3439f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3440f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3441120dda4fSVille Syrjälä 
3442120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3443120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3444f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3445f8b79e58SImre Deak }
3446f8b79e58SImre Deak 
3447f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3448f8b79e58SImre Deak {
3449f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3450f8b79e58SImre Deak 
3451f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3452f8b79e58SImre Deak 		return;
3453f8b79e58SImre Deak 
3454f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3455f8b79e58SImre Deak 
3456950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3457f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3458f8b79e58SImre Deak }
3459f8b79e58SImre Deak 
3460f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3461f8b79e58SImre Deak {
3462f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3463f8b79e58SImre Deak 
3464f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3465f8b79e58SImre Deak 		return;
3466f8b79e58SImre Deak 
3467f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3468f8b79e58SImre Deak 
3469950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3470f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3471f8b79e58SImre Deak }
3472f8b79e58SImre Deak 
34730e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
34747e231dbeSJesse Barnes {
3475f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34767e231dbeSJesse Barnes 
347720afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
347820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
347920afbda2SDaniel Vetter 
34807e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
348176e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
348276e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
348376e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
348476e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
34857e231dbeSJesse Barnes 
3486b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3487b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3488d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3489f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3490f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3491d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
34920e6c9a9eSVille Syrjälä }
34930e6c9a9eSVille Syrjälä 
34940e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34950e6c9a9eSVille Syrjälä {
34960e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34970e6c9a9eSVille Syrjälä 
34980e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
34997e231dbeSJesse Barnes 
35000a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
35017e231dbeSJesse Barnes 
35027e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
35037e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
35047e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
35057e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
35067e231dbeSJesse Barnes #endif
35077e231dbeSJesse Barnes 
35087e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
350920afbda2SDaniel Vetter 
351020afbda2SDaniel Vetter 	return 0;
351120afbda2SDaniel Vetter }
351220afbda2SDaniel Vetter 
3513abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3514abd58f01SBen Widawsky {
3515abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3516abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3517abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
351873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3519abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
352073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
352173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3522abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
352373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
352473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
352573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3526abd58f01SBen Widawsky 		0,
352773d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
352873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3529abd58f01SBen Widawsky 		};
3530abd58f01SBen Widawsky 
35310961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35329a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35339a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
353478e68d36SImre Deak 	/*
353578e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
353678e68d36SImre Deak 	 * is enabled/disabled.
353778e68d36SImre Deak 	 */
353878e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
35399a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3540abd58f01SBen Widawsky }
3541abd58f01SBen Widawsky 
3542abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3543abd58f01SBen Widawsky {
3544770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3545770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3546*3a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
3547*3a3b3c7dSVille Syrjälä 	u32 de_port_enables;
3548*3a3b3c7dSVille Syrjälä 	enum pipe pipe;
3549770de83dSDamien Lespiau 
355088e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3551770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3552770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
3553*3a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
355488e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
35559e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
3556*3a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
3557*3a3b3c7dSVille Syrjälä 	} else {
3558770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3559770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3560*3a3b3c7dSVille Syrjälä 	}
3561770de83dSDamien Lespiau 
3562770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3563770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3564770de83dSDamien Lespiau 
3565*3a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3566*3a3b3c7dSVille Syrjälä 	if (IS_BROADWELL(dev_priv))
3567*3a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
3568*3a3b3c7dSVille Syrjälä 
356913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
357013b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
357113b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3572abd58f01SBen Widawsky 
3573055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3574f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3575813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3576813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3577813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
357835079899SPaulo Zanoni 					  de_pipe_enables);
3579abd58f01SBen Widawsky 
3580*3a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3581abd58f01SBen Widawsky }
3582abd58f01SBen Widawsky 
3583abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3584abd58f01SBen Widawsky {
3585abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3586abd58f01SBen Widawsky 
3587266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3588622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3589622364b6SPaulo Zanoni 
3590abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3591abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3592abd58f01SBen Widawsky 
3593266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3594abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3595abd58f01SBen Widawsky 
3596abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3597abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3598abd58f01SBen Widawsky 
3599abd58f01SBen Widawsky 	return 0;
3600abd58f01SBen Widawsky }
3601abd58f01SBen Widawsky 
360243f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
360343f328d7SVille Syrjälä {
360443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
360543f328d7SVille Syrjälä 
3606c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
360743f328d7SVille Syrjälä 
360843f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
360943f328d7SVille Syrjälä 
361043f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
361143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
361243f328d7SVille Syrjälä 
361343f328d7SVille Syrjälä 	return 0;
361443f328d7SVille Syrjälä }
361543f328d7SVille Syrjälä 
3616abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3617abd58f01SBen Widawsky {
3618abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3619abd58f01SBen Widawsky 
3620abd58f01SBen Widawsky 	if (!dev_priv)
3621abd58f01SBen Widawsky 		return;
3622abd58f01SBen Widawsky 
3623823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3624abd58f01SBen Widawsky }
3625abd58f01SBen Widawsky 
36268ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
36278ea0be4fSVille Syrjälä {
36288ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
36298ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
36308ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
36318ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
36328ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
36338ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
36348ea0be4fSVille Syrjälä 
36358ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
36368ea0be4fSVille Syrjälä 
3637c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
36388ea0be4fSVille Syrjälä }
36398ea0be4fSVille Syrjälä 
36407e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
36417e231dbeSJesse Barnes {
36422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36437e231dbeSJesse Barnes 
36447e231dbeSJesse Barnes 	if (!dev_priv)
36457e231dbeSJesse Barnes 		return;
36467e231dbeSJesse Barnes 
3647843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3648843d0e7dSImre Deak 
3649893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3650893fce8eSVille Syrjälä 
36517e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3652f8b79e58SImre Deak 
36538ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
36547e231dbeSJesse Barnes }
36557e231dbeSJesse Barnes 
365643f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
365743f328d7SVille Syrjälä {
365843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
365943f328d7SVille Syrjälä 
366043f328d7SVille Syrjälä 	if (!dev_priv)
366143f328d7SVille Syrjälä 		return;
366243f328d7SVille Syrjälä 
366343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
366443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
366543f328d7SVille Syrjälä 
3666a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
366743f328d7SVille Syrjälä 
3668a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
366943f328d7SVille Syrjälä 
3670c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
367143f328d7SVille Syrjälä }
367243f328d7SVille Syrjälä 
3673f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3674036a4a7dSZhenyu Wang {
36752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36764697995bSJesse Barnes 
36774697995bSJesse Barnes 	if (!dev_priv)
36784697995bSJesse Barnes 		return;
36794697995bSJesse Barnes 
3680be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3681036a4a7dSZhenyu Wang }
3682036a4a7dSZhenyu Wang 
3683c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3684c2798b19SChris Wilson {
36852d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3686c2798b19SChris Wilson 	int pipe;
3687c2798b19SChris Wilson 
3688055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3689c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3690c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3691c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3692c2798b19SChris Wilson 	POSTING_READ16(IER);
3693c2798b19SChris Wilson }
3694c2798b19SChris Wilson 
3695c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3696c2798b19SChris Wilson {
36972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3698c2798b19SChris Wilson 
3699c2798b19SChris Wilson 	I915_WRITE16(EMR,
3700c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3701c2798b19SChris Wilson 
3702c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3703c2798b19SChris Wilson 	dev_priv->irq_mask =
3704c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3705c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3706c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
370737ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3708c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3709c2798b19SChris Wilson 
3710c2798b19SChris Wilson 	I915_WRITE16(IER,
3711c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3712c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3713c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3714c2798b19SChris Wilson 	POSTING_READ16(IER);
3715c2798b19SChris Wilson 
3716379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3717379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3718d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3719755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3720755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3721d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3722379ef82dSDaniel Vetter 
3723c2798b19SChris Wilson 	return 0;
3724c2798b19SChris Wilson }
3725c2798b19SChris Wilson 
372690a72f87SVille Syrjälä /*
372790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
372890a72f87SVille Syrjälä  */
372990a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37301f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
373190a72f87SVille Syrjälä {
37322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37331f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
373490a72f87SVille Syrjälä 
37358d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
373690a72f87SVille Syrjälä 		return false;
373790a72f87SVille Syrjälä 
373890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3739d6bbafa1SChris Wilson 		goto check_page_flip;
374090a72f87SVille Syrjälä 
374190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
374290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
374390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
374490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
374590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
374690a72f87SVille Syrjälä 	 */
374790a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3748d6bbafa1SChris Wilson 		goto check_page_flip;
374990a72f87SVille Syrjälä 
37507d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
375190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
375290a72f87SVille Syrjälä 	return true;
3753d6bbafa1SChris Wilson 
3754d6bbafa1SChris Wilson check_page_flip:
3755d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3756d6bbafa1SChris Wilson 	return false;
375790a72f87SVille Syrjälä }
375890a72f87SVille Syrjälä 
3759ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3760c2798b19SChris Wilson {
376145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3763c2798b19SChris Wilson 	u16 iir, new_iir;
3764c2798b19SChris Wilson 	u32 pipe_stats[2];
3765c2798b19SChris Wilson 	int pipe;
3766c2798b19SChris Wilson 	u16 flip_mask =
3767c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3768c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3769c2798b19SChris Wilson 
37702dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37712dd2a883SImre Deak 		return IRQ_NONE;
37722dd2a883SImre Deak 
3773c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3774c2798b19SChris Wilson 	if (iir == 0)
3775c2798b19SChris Wilson 		return IRQ_NONE;
3776c2798b19SChris Wilson 
3777c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3778c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3779c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3780c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3781c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3782c2798b19SChris Wilson 		 */
3783222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3784c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3785aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3786c2798b19SChris Wilson 
3787055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3788c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3789c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3790c2798b19SChris Wilson 
3791c2798b19SChris Wilson 			/*
3792c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3793c2798b19SChris Wilson 			 */
37942d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3795c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3796c2798b19SChris Wilson 		}
3797222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3798c2798b19SChris Wilson 
3799c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3800c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3801c2798b19SChris Wilson 
3802c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
380374cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3804c2798b19SChris Wilson 
3805055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38061f1c2e24SVille Syrjälä 			int plane = pipe;
38073a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38081f1c2e24SVille Syrjälä 				plane = !plane;
38091f1c2e24SVille Syrjälä 
38104356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38111f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38121f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3813c2798b19SChris Wilson 
38144356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3815277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38162d9d2b0bSVille Syrjälä 
38171f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38181f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38191f7247c0SDaniel Vetter 								    pipe);
38204356d586SDaniel Vetter 		}
3821c2798b19SChris Wilson 
3822c2798b19SChris Wilson 		iir = new_iir;
3823c2798b19SChris Wilson 	}
3824c2798b19SChris Wilson 
3825c2798b19SChris Wilson 	return IRQ_HANDLED;
3826c2798b19SChris Wilson }
3827c2798b19SChris Wilson 
3828c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3829c2798b19SChris Wilson {
38302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3831c2798b19SChris Wilson 	int pipe;
3832c2798b19SChris Wilson 
3833055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3834c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3835c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3836c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3837c2798b19SChris Wilson 	}
3838c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3839c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3840c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3841c2798b19SChris Wilson }
3842c2798b19SChris Wilson 
3843a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3844a266c7d5SChris Wilson {
38452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3846a266c7d5SChris Wilson 	int pipe;
3847a266c7d5SChris Wilson 
3848a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3849a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3850a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3851a266c7d5SChris Wilson 	}
3852a266c7d5SChris Wilson 
385300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3854055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3855a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3856a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3857a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3858a266c7d5SChris Wilson 	POSTING_READ(IER);
3859a266c7d5SChris Wilson }
3860a266c7d5SChris Wilson 
3861a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3862a266c7d5SChris Wilson {
38632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
386438bde180SChris Wilson 	u32 enable_mask;
3865a266c7d5SChris Wilson 
386638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
386738bde180SChris Wilson 
386838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
386938bde180SChris Wilson 	dev_priv->irq_mask =
387038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
387138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
387238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
387338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
387437ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
387538bde180SChris Wilson 
387638bde180SChris Wilson 	enable_mask =
387738bde180SChris Wilson 		I915_ASLE_INTERRUPT |
387838bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
387938bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
388038bde180SChris Wilson 		I915_USER_INTERRUPT;
388138bde180SChris Wilson 
3882a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
388320afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
388420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
388520afbda2SDaniel Vetter 
3886a266c7d5SChris Wilson 		/* Enable in IER... */
3887a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3888a266c7d5SChris Wilson 		/* and unmask in IMR */
3889a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3890a266c7d5SChris Wilson 	}
3891a266c7d5SChris Wilson 
3892a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3893a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3894a266c7d5SChris Wilson 	POSTING_READ(IER);
3895a266c7d5SChris Wilson 
3896f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
389720afbda2SDaniel Vetter 
3898379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3899379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3900d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3901755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3902755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3903d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3904379ef82dSDaniel Vetter 
390520afbda2SDaniel Vetter 	return 0;
390620afbda2SDaniel Vetter }
390720afbda2SDaniel Vetter 
390890a72f87SVille Syrjälä /*
390990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
391090a72f87SVille Syrjälä  */
391190a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
391290a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
391390a72f87SVille Syrjälä {
39142d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
391590a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
391690a72f87SVille Syrjälä 
39178d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
391890a72f87SVille Syrjälä 		return false;
391990a72f87SVille Syrjälä 
392090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3921d6bbafa1SChris Wilson 		goto check_page_flip;
392290a72f87SVille Syrjälä 
392390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
392490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
392590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
392690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
392790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
392890a72f87SVille Syrjälä 	 */
392990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3930d6bbafa1SChris Wilson 		goto check_page_flip;
393190a72f87SVille Syrjälä 
39327d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
393390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
393490a72f87SVille Syrjälä 	return true;
3935d6bbafa1SChris Wilson 
3936d6bbafa1SChris Wilson check_page_flip:
3937d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3938d6bbafa1SChris Wilson 	return false;
393990a72f87SVille Syrjälä }
394090a72f87SVille Syrjälä 
3941ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3942a266c7d5SChris Wilson {
394345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39458291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
394638bde180SChris Wilson 	u32 flip_mask =
394738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
394838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
394938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3950a266c7d5SChris Wilson 
39512dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39522dd2a883SImre Deak 		return IRQ_NONE;
39532dd2a883SImre Deak 
3954a266c7d5SChris Wilson 	iir = I915_READ(IIR);
395538bde180SChris Wilson 	do {
395638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39578291ee90SChris Wilson 		bool blc_event = false;
3958a266c7d5SChris Wilson 
3959a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3960a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3961a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3962a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3963a266c7d5SChris Wilson 		 */
3964222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3965a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3966aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3967a266c7d5SChris Wilson 
3968055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3969a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3970a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3971a266c7d5SChris Wilson 
397238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3973a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3974a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
397538bde180SChris Wilson 				irq_received = true;
3976a266c7d5SChris Wilson 			}
3977a266c7d5SChris Wilson 		}
3978222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3979a266c7d5SChris Wilson 
3980a266c7d5SChris Wilson 		if (!irq_received)
3981a266c7d5SChris Wilson 			break;
3982a266c7d5SChris Wilson 
3983a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
398416c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
398516c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
398616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3987a266c7d5SChris Wilson 
398838bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3989a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3990a266c7d5SChris Wilson 
3991a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
399274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3993a266c7d5SChris Wilson 
3994055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
399538bde180SChris Wilson 			int plane = pipe;
39963a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
399738bde180SChris Wilson 				plane = !plane;
39985e2032d4SVille Syrjälä 
399990a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
400090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
400190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4002a266c7d5SChris Wilson 
4003a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4004a266c7d5SChris Wilson 				blc_event = true;
40054356d586SDaniel Vetter 
40064356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4007277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40082d9d2b0bSVille Syrjälä 
40091f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40101f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40111f7247c0SDaniel Vetter 								    pipe);
4012a266c7d5SChris Wilson 		}
4013a266c7d5SChris Wilson 
4014a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4015a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4016a266c7d5SChris Wilson 
4017a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4018a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4019a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4020a266c7d5SChris Wilson 		 * we would never get another interrupt.
4021a266c7d5SChris Wilson 		 *
4022a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4023a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4024a266c7d5SChris Wilson 		 * another one.
4025a266c7d5SChris Wilson 		 *
4026a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4027a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4028a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4029a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4030a266c7d5SChris Wilson 		 * stray interrupts.
4031a266c7d5SChris Wilson 		 */
403238bde180SChris Wilson 		ret = IRQ_HANDLED;
4033a266c7d5SChris Wilson 		iir = new_iir;
403438bde180SChris Wilson 	} while (iir & ~flip_mask);
4035a266c7d5SChris Wilson 
4036a266c7d5SChris Wilson 	return ret;
4037a266c7d5SChris Wilson }
4038a266c7d5SChris Wilson 
4039a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4040a266c7d5SChris Wilson {
40412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4042a266c7d5SChris Wilson 	int pipe;
4043a266c7d5SChris Wilson 
4044a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4045a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4046a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4047a266c7d5SChris Wilson 	}
4048a266c7d5SChris Wilson 
404900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4050055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
405155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4052a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
405355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
405455b39755SChris Wilson 	}
4055a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4056a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4057a266c7d5SChris Wilson 
4058a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4059a266c7d5SChris Wilson }
4060a266c7d5SChris Wilson 
4061a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4062a266c7d5SChris Wilson {
40632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4064a266c7d5SChris Wilson 	int pipe;
4065a266c7d5SChris Wilson 
4066a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4067a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4068a266c7d5SChris Wilson 
4069a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4070055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4071a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4072a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4073a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4074a266c7d5SChris Wilson 	POSTING_READ(IER);
4075a266c7d5SChris Wilson }
4076a266c7d5SChris Wilson 
4077a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4078a266c7d5SChris Wilson {
40792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4080bbba0a97SChris Wilson 	u32 enable_mask;
4081a266c7d5SChris Wilson 	u32 error_mask;
4082a266c7d5SChris Wilson 
4083a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4084bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4085adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4086bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4087bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4088bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4089bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4090bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4091bbba0a97SChris Wilson 
4092bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
409321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
409421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4095bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4096bbba0a97SChris Wilson 
4097bbba0a97SChris Wilson 	if (IS_G4X(dev))
4098bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4099a266c7d5SChris Wilson 
4100b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4101b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4102d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4103755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4104755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4105755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4106d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4107a266c7d5SChris Wilson 
4108a266c7d5SChris Wilson 	/*
4109a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4110a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4111a266c7d5SChris Wilson 	 */
4112a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4113a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4114a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4115a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4116a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4117a266c7d5SChris Wilson 	} else {
4118a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4119a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4120a266c7d5SChris Wilson 	}
4121a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4122a266c7d5SChris Wilson 
4123a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4124a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4125a266c7d5SChris Wilson 	POSTING_READ(IER);
4126a266c7d5SChris Wilson 
412720afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
412820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
412920afbda2SDaniel Vetter 
4130f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
413120afbda2SDaniel Vetter 
413220afbda2SDaniel Vetter 	return 0;
413320afbda2SDaniel Vetter }
413420afbda2SDaniel Vetter 
4135bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
413620afbda2SDaniel Vetter {
41372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
413820afbda2SDaniel Vetter 	u32 hotplug_en;
413920afbda2SDaniel Vetter 
4140b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4141b5ea2d56SDaniel Vetter 
4142bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4143bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4144adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4145e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
414687a02106SVille Syrjälä 	hotplug_en |= intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4147a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4148a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4149a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4150a266c7d5SChris Wilson 	*/
4151a266c7d5SChris Wilson 	if (IS_G4X(dev))
4152a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
415385fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4154a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4155a266c7d5SChris Wilson 
4156a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4157a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4158a266c7d5SChris Wilson }
4159a266c7d5SChris Wilson 
4160ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4161a266c7d5SChris Wilson {
416245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4164a266c7d5SChris Wilson 	u32 iir, new_iir;
4165a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4166a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
416721ad8330SVille Syrjälä 	u32 flip_mask =
416821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
416921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4170a266c7d5SChris Wilson 
41712dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41722dd2a883SImre Deak 		return IRQ_NONE;
41732dd2a883SImre Deak 
4174a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4175a266c7d5SChris Wilson 
4176a266c7d5SChris Wilson 	for (;;) {
4177501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41782c8ba29fSChris Wilson 		bool blc_event = false;
41792c8ba29fSChris Wilson 
4180a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4181a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4182a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4183a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4184a266c7d5SChris Wilson 		 */
4185222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4186a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4187aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4188a266c7d5SChris Wilson 
4189055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4190a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4191a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4192a266c7d5SChris Wilson 
4193a266c7d5SChris Wilson 			/*
4194a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4195a266c7d5SChris Wilson 			 */
4196a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4197a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4198501e01d7SVille Syrjälä 				irq_received = true;
4199a266c7d5SChris Wilson 			}
4200a266c7d5SChris Wilson 		}
4201222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4202a266c7d5SChris Wilson 
4203a266c7d5SChris Wilson 		if (!irq_received)
4204a266c7d5SChris Wilson 			break;
4205a266c7d5SChris Wilson 
4206a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4207a266c7d5SChris Wilson 
4208a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
420916c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
421016c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4211a266c7d5SChris Wilson 
421221ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4213a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4214a266c7d5SChris Wilson 
4215a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
421674cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4217a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
421874cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4219a266c7d5SChris Wilson 
4220055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42212c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
422290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
422390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4224a266c7d5SChris Wilson 
4225a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4226a266c7d5SChris Wilson 				blc_event = true;
42274356d586SDaniel Vetter 
42284356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4229277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4230a266c7d5SChris Wilson 
42311f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42321f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42332d9d2b0bSVille Syrjälä 		}
4234a266c7d5SChris Wilson 
4235a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4236a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4237a266c7d5SChris Wilson 
4238515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4239515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4240515ac2bbSDaniel Vetter 
4241a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4242a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4243a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4244a266c7d5SChris Wilson 		 * we would never get another interrupt.
4245a266c7d5SChris Wilson 		 *
4246a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4247a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4248a266c7d5SChris Wilson 		 * another one.
4249a266c7d5SChris Wilson 		 *
4250a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4251a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4252a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4253a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4254a266c7d5SChris Wilson 		 * stray interrupts.
4255a266c7d5SChris Wilson 		 */
4256a266c7d5SChris Wilson 		iir = new_iir;
4257a266c7d5SChris Wilson 	}
4258a266c7d5SChris Wilson 
4259a266c7d5SChris Wilson 	return ret;
4260a266c7d5SChris Wilson }
4261a266c7d5SChris Wilson 
4262a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4263a266c7d5SChris Wilson {
42642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4265a266c7d5SChris Wilson 	int pipe;
4266a266c7d5SChris Wilson 
4267a266c7d5SChris Wilson 	if (!dev_priv)
4268a266c7d5SChris Wilson 		return;
4269a266c7d5SChris Wilson 
4270a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4271a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4272a266c7d5SChris Wilson 
4273a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4274055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4275a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4276a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4277a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4278a266c7d5SChris Wilson 
4279055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4280a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4281a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4282a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4283a266c7d5SChris Wilson }
4284a266c7d5SChris Wilson 
4285fca52a55SDaniel Vetter /**
4286fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4287fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4288fca52a55SDaniel Vetter  *
4289fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4290fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4291fca52a55SDaniel Vetter  */
4292b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4293f71d4af4SJesse Barnes {
4294b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
42958b2e326dSChris Wilson 
429677913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
429777913b39SJani Nikula 
4298c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4299a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43008b2e326dSChris Wilson 
4301a6706b45SDeepak S 	/* Let's track the enabled rps events */
4302b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43036c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43046f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
430531685c25SDeepak S 	else
4306a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4307a6706b45SDeepak S 
4308737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4309737b1506SChris Wilson 			  i915_hangcheck_elapsed);
431061bac78eSDaniel Vetter 
431197a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43129ee32feaSDaniel Vetter 
4313b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43144cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43154cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4316b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4317f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4318f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4319391f75e2SVille Syrjälä 	} else {
4320391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4321391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4322f71d4af4SJesse Barnes 	}
4323f71d4af4SJesse Barnes 
432421da2700SVille Syrjälä 	/*
432521da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
432621da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
432721da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
432821da2700SVille Syrjälä 	 */
4329b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
433021da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
433121da2700SVille Syrjälä 
4332f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4333f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4334f71d4af4SJesse Barnes 
4335b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
433643f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
433743f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
433843f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
433943f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
434043f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
434143f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
434243f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4343b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43447e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43457e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43467e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43477e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43487e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43497e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4350fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4351b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4352abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4353723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4354abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4355abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4356abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4357abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
43586dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4359e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
43606dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
43616dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
43626dbf30ceSVille Syrjälä 		else
4363*3a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4364f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4365f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4366723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4367f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4368f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4369f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4370f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4371e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4372f71d4af4SJesse Barnes 	} else {
4373b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4374c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4375c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4376c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4377c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4378b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4379a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4380a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4381a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4382a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4383c2798b19SChris Wilson 		} else {
4384a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4385a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4386a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4387a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4388c2798b19SChris Wilson 		}
4389778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4390778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4391f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4392f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4393f71d4af4SJesse Barnes 	}
4394f71d4af4SJesse Barnes }
439520afbda2SDaniel Vetter 
4396fca52a55SDaniel Vetter /**
4397fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4398fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4399fca52a55SDaniel Vetter  *
4400fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4401fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4402fca52a55SDaniel Vetter  *
4403fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4404fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4405fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4406fca52a55SDaniel Vetter  */
44072aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44082aeb7d3aSDaniel Vetter {
44092aeb7d3aSDaniel Vetter 	/*
44102aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44112aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44122aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44132aeb7d3aSDaniel Vetter 	 */
44142aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44152aeb7d3aSDaniel Vetter 
44162aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44172aeb7d3aSDaniel Vetter }
44182aeb7d3aSDaniel Vetter 
4419fca52a55SDaniel Vetter /**
4420fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4421fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4422fca52a55SDaniel Vetter  *
4423fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4424fca52a55SDaniel Vetter  * resources acquired in the init functions.
4425fca52a55SDaniel Vetter  */
44262aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44272aeb7d3aSDaniel Vetter {
44282aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44292aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44302aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44312aeb7d3aSDaniel Vetter }
44322aeb7d3aSDaniel Vetter 
4433fca52a55SDaniel Vetter /**
4434fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4435fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4436fca52a55SDaniel Vetter  *
4437fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4438fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4439fca52a55SDaniel Vetter  */
4440b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4441c67a470bSPaulo Zanoni {
4442b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44432aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44442dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4445c67a470bSPaulo Zanoni }
4446c67a470bSPaulo Zanoni 
4447fca52a55SDaniel Vetter /**
4448fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4449fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4450fca52a55SDaniel Vetter  *
4451fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4452fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4453fca52a55SDaniel Vetter  */
4454b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4455c67a470bSPaulo Zanoni {
44562aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4457b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4458b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4459c67a470bSPaulo Zanoni }
4460