xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 6e266956a57f878a6d648aed8e0380688e6f48c5)
1c0e09200SDave Airlie /* i915_irq.c -- IRQ support for the I915 -*- linux-c -*-
2c0e09200SDave Airlie  */
3c0e09200SDave Airlie /*
4c0e09200SDave Airlie  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5c0e09200SDave Airlie  * All Rights Reserved.
6c0e09200SDave Airlie  *
7c0e09200SDave Airlie  * Permission is hereby granted, free of charge, to any person obtaining a
8c0e09200SDave Airlie  * copy of this software and associated documentation files (the
9c0e09200SDave Airlie  * "Software"), to deal in the Software without restriction, including
10c0e09200SDave Airlie  * without limitation the rights to use, copy, modify, merge, publish,
11c0e09200SDave Airlie  * distribute, sub license, and/or sell copies of the Software, and to
12c0e09200SDave Airlie  * permit persons to whom the Software is furnished to do so, subject to
13c0e09200SDave Airlie  * the following conditions:
14c0e09200SDave Airlie  *
15c0e09200SDave Airlie  * The above copyright notice and this permission notice (including the
16c0e09200SDave Airlie  * next paragraph) shall be included in all copies or substantial portions
17c0e09200SDave Airlie  * of the Software.
18c0e09200SDave Airlie  *
19c0e09200SDave Airlie  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20c0e09200SDave Airlie  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21c0e09200SDave Airlie  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22c0e09200SDave Airlie  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23c0e09200SDave Airlie  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24c0e09200SDave Airlie  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25c0e09200SDave Airlie  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26c0e09200SDave Airlie  *
27c0e09200SDave Airlie  */
28c0e09200SDave Airlie 
29a70491ccSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30a70491ccSJoe Perches 
3163eeaf38SJesse Barnes #include <linux/sysrq.h>
325a0e3ad6STejun Heo #include <linux/slab.h>
33b2c88f5bSDamien Lespiau #include <linux/circ_buf.h>
34760285e7SDavid Howells #include <drm/drmP.h>
35760285e7SDavid Howells #include <drm/i915_drm.h>
36c0e09200SDave Airlie #include "i915_drv.h"
371c5d22f7SChris Wilson #include "i915_trace.h"
3879e53945SJesse Barnes #include "intel_drv.h"
39c0e09200SDave Airlie 
40fca52a55SDaniel Vetter /**
41fca52a55SDaniel Vetter  * DOC: interrupt handling
42fca52a55SDaniel Vetter  *
43fca52a55SDaniel Vetter  * These functions provide the basic support for enabling and disabling the
44fca52a55SDaniel Vetter  * interrupt handling support. There's a lot more functionality in i915_irq.c
45fca52a55SDaniel Vetter  * and related files, but that will be described in separate chapters.
46fca52a55SDaniel Vetter  */
47fca52a55SDaniel Vetter 
48e4ce95aaSVille Syrjälä static const u32 hpd_ilk[HPD_NUM_PINS] = {
49e4ce95aaSVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG,
50e4ce95aaSVille Syrjälä };
51e4ce95aaSVille Syrjälä 
5223bb4cb5SVille Syrjälä static const u32 hpd_ivb[HPD_NUM_PINS] = {
5323bb4cb5SVille Syrjälä 	[HPD_PORT_A] = DE_DP_A_HOTPLUG_IVB,
5423bb4cb5SVille Syrjälä };
5523bb4cb5SVille Syrjälä 
563a3b3c7dSVille Syrjälä static const u32 hpd_bdw[HPD_NUM_PINS] = {
573a3b3c7dSVille Syrjälä 	[HPD_PORT_A] = GEN8_PORT_DP_A_HOTPLUG,
583a3b3c7dSVille Syrjälä };
593a3b3c7dSVille Syrjälä 
607c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
61e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
62e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
63e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
64e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
65e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
66e5868a31SEgbert Eich };
67e5868a31SEgbert Eich 
687c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
69e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
7073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
71e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
72e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
73e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
74e5868a31SEgbert Eich };
75e5868a31SEgbert Eich 
7626951cafSXiong Zhang static const u32 hpd_spt[HPD_NUM_PINS] = {
7774c0b395SVille Syrjälä 	[HPD_PORT_A] = SDE_PORTA_HOTPLUG_SPT,
7826951cafSXiong Zhang 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
7926951cafSXiong Zhang 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
8026951cafSXiong Zhang 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT,
8126951cafSXiong Zhang 	[HPD_PORT_E] = SDE_PORTE_HOTPLUG_SPT
8226951cafSXiong Zhang };
8326951cafSXiong Zhang 
847c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
85e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
86e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
87e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
88e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
89e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
90e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
91e5868a31SEgbert Eich };
92e5868a31SEgbert Eich 
937c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
94e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
95e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
96e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
97e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
98e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
99e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
100e5868a31SEgbert Eich };
101e5868a31SEgbert Eich 
1024bca26d0SVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = {
103e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
104e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
105e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
106e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
107e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
108e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
109e5868a31SEgbert Eich };
110e5868a31SEgbert Eich 
111e0a20ad7SShashank Sharma /* BXT hpd list */
112e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
1137f3561beSSonika Jindal 	[HPD_PORT_A] = BXT_DE_PORT_HP_DDIA,
114e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
115e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
116e0a20ad7SShashank Sharma };
117e0a20ad7SShashank Sharma 
1185c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
119f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
1205c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1215c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1225c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1235c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1245c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1255c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1265c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1275c502442SPaulo Zanoni } while (0)
1285c502442SPaulo Zanoni 
129f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
130a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1315c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
132a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1335c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1345c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1355c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1365c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
137a9d356a6SPaulo Zanoni } while (0)
138a9d356a6SPaulo Zanoni 
139337ba017SPaulo Zanoni /*
140337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
141337ba017SPaulo Zanoni  */
142f0f59a00SVille Syrjälä static void gen5_assert_iir_is_zero(struct drm_i915_private *dev_priv,
143f0f59a00SVille Syrjälä 				    i915_reg_t reg)
144b51a2842SVille Syrjälä {
145b51a2842SVille Syrjälä 	u32 val = I915_READ(reg);
146b51a2842SVille Syrjälä 
147b51a2842SVille Syrjälä 	if (val == 0)
148b51a2842SVille Syrjälä 		return;
149b51a2842SVille Syrjälä 
150b51a2842SVille Syrjälä 	WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n",
151f0f59a00SVille Syrjälä 	     i915_mmio_reg_offset(reg), val);
152b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
153b51a2842SVille Syrjälä 	POSTING_READ(reg);
154b51a2842SVille Syrjälä 	I915_WRITE(reg, 0xffffffff);
155b51a2842SVille Syrjälä 	POSTING_READ(reg);
156b51a2842SVille Syrjälä }
157337ba017SPaulo Zanoni 
15835079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
159b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, GEN8_##type##_IIR(which)); \
16035079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1617d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1627d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
16335079899SPaulo Zanoni } while (0)
16435079899SPaulo Zanoni 
16535079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
166b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, type##IIR); \
16735079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1687d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1697d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
17035079899SPaulo Zanoni } while (0)
17135079899SPaulo Zanoni 
172c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
173c9a9a268SImre Deak 
1740706f17cSEgbert Eich /* For display hotplug interrupt */
1750706f17cSEgbert Eich static inline void
1760706f17cSEgbert Eich i915_hotplug_interrupt_update_locked(struct drm_i915_private *dev_priv,
1770706f17cSEgbert Eich 				     uint32_t mask,
1780706f17cSEgbert Eich 				     uint32_t bits)
1790706f17cSEgbert Eich {
1800706f17cSEgbert Eich 	uint32_t val;
1810706f17cSEgbert Eich 
1820706f17cSEgbert Eich 	assert_spin_locked(&dev_priv->irq_lock);
1830706f17cSEgbert Eich 	WARN_ON(bits & ~mask);
1840706f17cSEgbert Eich 
1850706f17cSEgbert Eich 	val = I915_READ(PORT_HOTPLUG_EN);
1860706f17cSEgbert Eich 	val &= ~mask;
1870706f17cSEgbert Eich 	val |= bits;
1880706f17cSEgbert Eich 	I915_WRITE(PORT_HOTPLUG_EN, val);
1890706f17cSEgbert Eich }
1900706f17cSEgbert Eich 
1910706f17cSEgbert Eich /**
1920706f17cSEgbert Eich  * i915_hotplug_interrupt_update - update hotplug interrupt enable
1930706f17cSEgbert Eich  * @dev_priv: driver private
1940706f17cSEgbert Eich  * @mask: bits to update
1950706f17cSEgbert Eich  * @bits: bits to enable
1960706f17cSEgbert Eich  * NOTE: the HPD enable bits are modified both inside and outside
1970706f17cSEgbert Eich  * of an interrupt context. To avoid that read-modify-write cycles
1980706f17cSEgbert Eich  * interfer, these bits are protected by a spinlock. Since this
1990706f17cSEgbert Eich  * function is usually not called from a context where the lock is
2000706f17cSEgbert Eich  * held already, this function acquires the lock itself. A non-locking
2010706f17cSEgbert Eich  * version is also available.
2020706f17cSEgbert Eich  */
2030706f17cSEgbert Eich void i915_hotplug_interrupt_update(struct drm_i915_private *dev_priv,
2040706f17cSEgbert Eich 				   uint32_t mask,
2050706f17cSEgbert Eich 				   uint32_t bits)
2060706f17cSEgbert Eich {
2070706f17cSEgbert Eich 	spin_lock_irq(&dev_priv->irq_lock);
2080706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv, mask, bits);
2090706f17cSEgbert Eich 	spin_unlock_irq(&dev_priv->irq_lock);
2100706f17cSEgbert Eich }
2110706f17cSEgbert Eich 
212d9dc34f1SVille Syrjälä /**
213d9dc34f1SVille Syrjälä  * ilk_update_display_irq - update DEIMR
214d9dc34f1SVille Syrjälä  * @dev_priv: driver private
215d9dc34f1SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
216d9dc34f1SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
217d9dc34f1SVille Syrjälä  */
218fbdedaeaSVille Syrjälä void ilk_update_display_irq(struct drm_i915_private *dev_priv,
219d9dc34f1SVille Syrjälä 			    uint32_t interrupt_mask,
220d9dc34f1SVille Syrjälä 			    uint32_t enabled_irq_mask)
221036a4a7dSZhenyu Wang {
222d9dc34f1SVille Syrjälä 	uint32_t new_val;
223d9dc34f1SVille Syrjälä 
2244bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2254bc9d430SDaniel Vetter 
226d9dc34f1SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
227d9dc34f1SVille Syrjälä 
2289df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
229c67a470bSPaulo Zanoni 		return;
230c67a470bSPaulo Zanoni 
231d9dc34f1SVille Syrjälä 	new_val = dev_priv->irq_mask;
232d9dc34f1SVille Syrjälä 	new_val &= ~interrupt_mask;
233d9dc34f1SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
234d9dc34f1SVille Syrjälä 
235d9dc34f1SVille Syrjälä 	if (new_val != dev_priv->irq_mask) {
236d9dc34f1SVille Syrjälä 		dev_priv->irq_mask = new_val;
2371ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
2383143a2bfSChris Wilson 		POSTING_READ(DEIMR);
239036a4a7dSZhenyu Wang 	}
240036a4a7dSZhenyu Wang }
241036a4a7dSZhenyu Wang 
24243eaea13SPaulo Zanoni /**
24343eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
24443eaea13SPaulo Zanoni  * @dev_priv: driver private
24543eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
24643eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
24743eaea13SPaulo Zanoni  */
24843eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
24943eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
25043eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
25143eaea13SPaulo Zanoni {
25243eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
25343eaea13SPaulo Zanoni 
25415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
25515a17aaeSDaniel Vetter 
2569df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
257c67a470bSPaulo Zanoni 		return;
258c67a470bSPaulo Zanoni 
25943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
26043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
26143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
26243eaea13SPaulo Zanoni }
26343eaea13SPaulo Zanoni 
264480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26543eaea13SPaulo Zanoni {
26643eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
26731bb59ccSChris Wilson 	POSTING_READ_FW(GTIMR);
26843eaea13SPaulo Zanoni }
26943eaea13SPaulo Zanoni 
270480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
27143eaea13SPaulo Zanoni {
27243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
27343eaea13SPaulo Zanoni }
27443eaea13SPaulo Zanoni 
275f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_iir(struct drm_i915_private *dev_priv)
276b900b949SImre Deak {
277b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
278b900b949SImre Deak }
279b900b949SImre Deak 
280f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_imr(struct drm_i915_private *dev_priv)
281a72fbc3aSImre Deak {
282a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
283a72fbc3aSImre Deak }
284a72fbc3aSImre Deak 
285f0f59a00SVille Syrjälä static i915_reg_t gen6_pm_ier(struct drm_i915_private *dev_priv)
286b900b949SImre Deak {
287b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
288b900b949SImre Deak }
289b900b949SImre Deak 
290edbfdb45SPaulo Zanoni /**
291edbfdb45SPaulo Zanoni  * snb_update_pm_irq - update GEN6_PMIMR
292edbfdb45SPaulo Zanoni  * @dev_priv: driver private
293edbfdb45SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
294edbfdb45SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
295edbfdb45SPaulo Zanoni  */
296edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
297edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
298edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
299edbfdb45SPaulo Zanoni {
300605cd25bSPaulo Zanoni 	uint32_t new_val;
301edbfdb45SPaulo Zanoni 
30215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
30315a17aaeSDaniel Vetter 
304edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
305edbfdb45SPaulo Zanoni 
306605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
307f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
308f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
309f52ecbcfSPaulo Zanoni 
310605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
311605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
312a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
313a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
314edbfdb45SPaulo Zanoni 	}
315f52ecbcfSPaulo Zanoni }
316edbfdb45SPaulo Zanoni 
317480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
318edbfdb45SPaulo Zanoni {
3199939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3209939fba2SImre Deak 		return;
3219939fba2SImre Deak 
322edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
323edbfdb45SPaulo Zanoni }
324edbfdb45SPaulo Zanoni 
3259939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
3269939fba2SImre Deak 				  uint32_t mask)
3279939fba2SImre Deak {
3289939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
3299939fba2SImre Deak }
3309939fba2SImre Deak 
331480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
332edbfdb45SPaulo Zanoni {
3339939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
3349939fba2SImre Deak 		return;
3359939fba2SImre Deak 
3369939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
337edbfdb45SPaulo Zanoni }
338edbfdb45SPaulo Zanoni 
339dc97997aSChris Wilson void gen6_reset_rps_interrupts(struct drm_i915_private *dev_priv)
3403cc134e3SImre Deak {
341f0f59a00SVille Syrjälä 	i915_reg_t reg = gen6_pm_iir(dev_priv);
3423cc134e3SImre Deak 
3433cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3443cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3453cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3463cc134e3SImre Deak 	POSTING_READ(reg);
347096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3483cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3493cc134e3SImre Deak }
3503cc134e3SImre Deak 
35191d14251STvrtko Ursulin void gen6_enable_rps_interrupts(struct drm_i915_private *dev_priv)
352b900b949SImre Deak {
353f2a91d1aSChris Wilson 	if (READ_ONCE(dev_priv->rps.interrupts_enabled))
354f2a91d1aSChris Wilson 		return;
355f2a91d1aSChris Wilson 
356b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
357c33d247dSChris Wilson 	WARN_ON_ONCE(dev_priv->rps.pm_iir);
358c33d247dSChris Wilson 	WARN_ON_ONCE(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
359d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
36078e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
36178e68d36SImre Deak 				dev_priv->pm_rps_events);
362b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
36378e68d36SImre Deak 
364b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
365b900b949SImre Deak }
366b900b949SImre Deak 
36759d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
36859d02a1fSImre Deak {
3691800ad25SSagar Arun Kamble 	return (mask & ~dev_priv->rps.pm_intr_keep);
37059d02a1fSImre Deak }
37159d02a1fSImre Deak 
37291d14251STvrtko Ursulin void gen6_disable_rps_interrupts(struct drm_i915_private *dev_priv)
373b900b949SImre Deak {
374f2a91d1aSChris Wilson 	if (!READ_ONCE(dev_priv->rps.interrupts_enabled))
375f2a91d1aSChris Wilson 		return;
376f2a91d1aSChris Wilson 
377d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
378d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
3799939fba2SImre Deak 
380b20e3cfeSDave Gordon 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0u));
3819939fba2SImre Deak 
3829939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
383b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
384b900b949SImre Deak 				~dev_priv->pm_rps_events);
38558072ccbSImre Deak 
38658072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
38791c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
388c33d247dSChris Wilson 
389c33d247dSChris Wilson 	/* Now that we will not be generating any more work, flush any
390c33d247dSChris Wilson 	 * outsanding tasks. As we are called on the RPS idle path,
391c33d247dSChris Wilson 	 * we will reset the GPU to minimum frequencies, so the current
392c33d247dSChris Wilson 	 * state of the worker can be discarded.
393c33d247dSChris Wilson 	 */
394c33d247dSChris Wilson 	cancel_work_sync(&dev_priv->rps.work);
395c33d247dSChris Wilson 	gen6_reset_rps_interrupts(dev_priv);
396b900b949SImre Deak }
397b900b949SImre Deak 
3980961021aSBen Widawsky /**
3993a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4003a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4013a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4023a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4033a3b3c7dSVille Syrjälä  */
4043a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4053a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4063a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4073a3b3c7dSVille Syrjälä {
4083a3b3c7dSVille Syrjälä 	uint32_t new_val;
4093a3b3c7dSVille Syrjälä 	uint32_t old_val;
4103a3b3c7dSVille Syrjälä 
4113a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4123a3b3c7dSVille Syrjälä 
4133a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4143a3b3c7dSVille Syrjälä 
4153a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4163a3b3c7dSVille Syrjälä 		return;
4173a3b3c7dSVille Syrjälä 
4183a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4193a3b3c7dSVille Syrjälä 
4203a3b3c7dSVille Syrjälä 	new_val = old_val;
4213a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4223a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4233a3b3c7dSVille Syrjälä 
4243a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4253a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4263a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4273a3b3c7dSVille Syrjälä 	}
4283a3b3c7dSVille Syrjälä }
4293a3b3c7dSVille Syrjälä 
4303a3b3c7dSVille Syrjälä /**
431013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
432013d3752SVille Syrjälä  * @dev_priv: driver private
433013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
434013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
435013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
436013d3752SVille Syrjälä  */
437013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
438013d3752SVille Syrjälä 			 enum pipe pipe,
439013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
440013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
441013d3752SVille Syrjälä {
442013d3752SVille Syrjälä 	uint32_t new_val;
443013d3752SVille Syrjälä 
444013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
445013d3752SVille Syrjälä 
446013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
447013d3752SVille Syrjälä 
448013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
449013d3752SVille Syrjälä 		return;
450013d3752SVille Syrjälä 
451013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
452013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
453013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
454013d3752SVille Syrjälä 
455013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
456013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
457013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
458013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
459013d3752SVille Syrjälä 	}
460013d3752SVille Syrjälä }
461013d3752SVille Syrjälä 
462013d3752SVille Syrjälä /**
463fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
464fee884edSDaniel Vetter  * @dev_priv: driver private
465fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
466fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
467fee884edSDaniel Vetter  */
46847339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
469fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
470fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
471fee884edSDaniel Vetter {
472fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
473fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
474fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
475fee884edSDaniel Vetter 
47615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
47715a17aaeSDaniel Vetter 
478fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
479fee884edSDaniel Vetter 
4809df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
481c67a470bSPaulo Zanoni 		return;
482c67a470bSPaulo Zanoni 
483fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
484fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
485fee884edSDaniel Vetter }
4868664281bSPaulo Zanoni 
487b5ea642aSDaniel Vetter static void
488755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
489755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
4907c463586SKeith Packard {
491f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
492755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4937c463586SKeith Packard 
494b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
495d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
496b79480baSDaniel Vetter 
49704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
49804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
49904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
50004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
501755e9019SImre Deak 		return;
502755e9019SImre Deak 
503755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
50446c06a30SVille Syrjälä 		return;
50546c06a30SVille Syrjälä 
50691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
50791d181ddSImre Deak 
5087c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
509755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
51046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5113143a2bfSChris Wilson 	POSTING_READ(reg);
5127c463586SKeith Packard }
5137c463586SKeith Packard 
514b5ea642aSDaniel Vetter static void
515755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
516755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5177c463586SKeith Packard {
518f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
519755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5207c463586SKeith Packard 
521b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
522d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
523b79480baSDaniel Vetter 
52404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
52504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
52604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
52704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
52846c06a30SVille Syrjälä 		return;
52946c06a30SVille Syrjälä 
530755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
531755e9019SImre Deak 		return;
532755e9019SImre Deak 
53391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
53491d181ddSImre Deak 
535755e9019SImre Deak 	pipestat &= ~enable_mask;
53646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5373143a2bfSChris Wilson 	POSTING_READ(reg);
5387c463586SKeith Packard }
5397c463586SKeith Packard 
54010c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
54110c59c51SImre Deak {
54210c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
54310c59c51SImre Deak 
54410c59c51SImre Deak 	/*
545724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
546724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
54710c59c51SImre Deak 	 */
54810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
54910c59c51SImre Deak 		return 0;
550724a6905SVille Syrjälä 	/*
551724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
552724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
553724a6905SVille Syrjälä 	 */
554724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
555724a6905SVille Syrjälä 		return 0;
55610c59c51SImre Deak 
55710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
55810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
55910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
56010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
56110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
56210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
56310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
56410c59c51SImre Deak 
56510c59c51SImre Deak 	return enable_mask;
56610c59c51SImre Deak }
56710c59c51SImre Deak 
568755e9019SImre Deak void
569755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
570755e9019SImre Deak 		     u32 status_mask)
571755e9019SImre Deak {
572755e9019SImre Deak 	u32 enable_mask;
573755e9019SImre Deak 
574666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
57591c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
57610c59c51SImre Deak 							   status_mask);
57710c59c51SImre Deak 	else
578755e9019SImre Deak 		enable_mask = status_mask << 16;
579755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
580755e9019SImre Deak }
581755e9019SImre Deak 
582755e9019SImre Deak void
583755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
584755e9019SImre Deak 		      u32 status_mask)
585755e9019SImre Deak {
586755e9019SImre Deak 	u32 enable_mask;
587755e9019SImre Deak 
588666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
58991c8a326SChris Wilson 		enable_mask = vlv_get_pipestat_enable_mask(&dev_priv->drm,
59010c59c51SImre Deak 							   status_mask);
59110c59c51SImre Deak 	else
592755e9019SImre Deak 		enable_mask = status_mask << 16;
593755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
594755e9019SImre Deak }
595755e9019SImre Deak 
596c0e09200SDave Airlie /**
597f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
59814bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
59901c66889SZhao Yakui  */
60091d14251STvrtko Ursulin static void i915_enable_asle_pipestat(struct drm_i915_private *dev_priv)
60101c66889SZhao Yakui {
60291d14251STvrtko Ursulin 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev_priv))
603f49e38ddSJani Nikula 		return;
604f49e38ddSJani Nikula 
60513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
60601c66889SZhao Yakui 
607755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
60891d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 4)
6093b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
610755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6111ec14ad3SChris Wilson 
61213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
61301c66889SZhao Yakui }
61401c66889SZhao Yakui 
615f75f3746SVille Syrjälä /*
616f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
617f75f3746SVille Syrjälä  * around the vertical blanking period.
618f75f3746SVille Syrjälä  *
619f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
620f75f3746SVille Syrjälä  *  vblank_start >= 3
621f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
622f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
623f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
624f75f3746SVille Syrjälä  *
625f75f3746SVille Syrjälä  *           start of vblank:
626f75f3746SVille Syrjälä  *           latch double buffered registers
627f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
628f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
629f75f3746SVille Syrjälä  *           |
630f75f3746SVille Syrjälä  *           |          frame start:
631f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
632f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
633f75f3746SVille Syrjälä  *           |          |
634f75f3746SVille Syrjälä  *           |          |  start of vsync:
635f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
636f75f3746SVille Syrjälä  *           |          |  |
637f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
638f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
639f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
640f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
641f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
642f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
643f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
644f75f3746SVille Syrjälä  *       |          |                                         |
645f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
646f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
647f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
648f75f3746SVille Syrjälä  *
649f75f3746SVille Syrjälä  * x  = horizontal active
650f75f3746SVille Syrjälä  * _  = horizontal blanking
651f75f3746SVille Syrjälä  * hs = horizontal sync
652f75f3746SVille Syrjälä  * va = vertical active
653f75f3746SVille Syrjälä  * vb = vertical blanking
654f75f3746SVille Syrjälä  * vs = vertical sync
655f75f3746SVille Syrjälä  * vbs = vblank_start (number)
656f75f3746SVille Syrjälä  *
657f75f3746SVille Syrjälä  * Summary:
658f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
659f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
660f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
661f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
662f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
663f75f3746SVille Syrjälä  */
664f75f3746SVille Syrjälä 
66542f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
66642f52ef8SKeith Packard  * we use as a pipe index
66742f52ef8SKeith Packard  */
66888e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6690a3e67a4SJesse Barnes {
670fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
671f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6720b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
673391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
674391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
675fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
676391f75e2SVille Syrjälä 
6770b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6780b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6790b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6800b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6810b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
682391f75e2SVille Syrjälä 
6830b2a8e09SVille Syrjälä 	/* Convert to pixel count */
6840b2a8e09SVille Syrjälä 	vbl_start *= htotal;
6850b2a8e09SVille Syrjälä 
6860b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
6870b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
6880b2a8e09SVille Syrjälä 
6899db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
6909db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
6915eddb70bSChris Wilson 
6920a3e67a4SJesse Barnes 	/*
6930a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
6940a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
6950a3e67a4SJesse Barnes 	 * register.
6960a3e67a4SJesse Barnes 	 */
6970a3e67a4SJesse Barnes 	do {
6985eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
699391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7005eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7010a3e67a4SJesse Barnes 	} while (high1 != high2);
7020a3e67a4SJesse Barnes 
7035eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
704391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7055eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
706391f75e2SVille Syrjälä 
707391f75e2SVille Syrjälä 	/*
708391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
709391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
710391f75e2SVille Syrjälä 	 * counter against vblank start.
711391f75e2SVille Syrjälä 	 */
712edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7130a3e67a4SJesse Barnes }
7140a3e67a4SJesse Barnes 
715974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7169880b7a5SJesse Barnes {
717fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
7189880b7a5SJesse Barnes 
719649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7209880b7a5SJesse Barnes }
7219880b7a5SJesse Barnes 
72275aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
723a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
724a225f079SVille Syrjälä {
725a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
726fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
727fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
728a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
72980715b2fSVille Syrjälä 	int position, vtotal;
730a225f079SVille Syrjälä 
73180715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
732a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
733a225f079SVille Syrjälä 		vtotal /= 2;
734a225f079SVille Syrjälä 
73591d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv))
73675aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
737a225f079SVille Syrjälä 	else
73875aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
739a225f079SVille Syrjälä 
740a225f079SVille Syrjälä 	/*
74141b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
74241b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
74341b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
74441b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
74541b578fbSJesse Barnes 	 *
74641b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
74741b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
74841b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
74941b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
75041b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
75141b578fbSJesse Barnes 	 */
75291d14251STvrtko Ursulin 	if (HAS_DDI(dev_priv) && !position) {
75341b578fbSJesse Barnes 		int i, temp;
75441b578fbSJesse Barnes 
75541b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
75641b578fbSJesse Barnes 			udelay(1);
75741b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
75841b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
75941b578fbSJesse Barnes 			if (temp != position) {
76041b578fbSJesse Barnes 				position = temp;
76141b578fbSJesse Barnes 				break;
76241b578fbSJesse Barnes 			}
76341b578fbSJesse Barnes 		}
76441b578fbSJesse Barnes 	}
76541b578fbSJesse Barnes 
76641b578fbSJesse Barnes 	/*
76780715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
76880715b2fSVille Syrjälä 	 * scanline_offset adjustment.
769a225f079SVille Syrjälä 	 */
77080715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
771a225f079SVille Syrjälä }
772a225f079SVille Syrjälä 
77388e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
774abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7753bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7763bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7770af7e4dfSMario Kleiner {
778fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
779c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
780c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7813aa18df8SVille Syrjälä 	int position;
78278e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
7830af7e4dfSMario Kleiner 	bool in_vbl = true;
7840af7e4dfSMario Kleiner 	int ret = 0;
785ad3543edSMario Kleiner 	unsigned long irqflags;
7860af7e4dfSMario Kleiner 
787fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
7880af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
7899db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
7900af7e4dfSMario Kleiner 		return 0;
7910af7e4dfSMario Kleiner 	}
7920af7e4dfSMario Kleiner 
793c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
79478e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
795c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
796c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
797c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
7980af7e4dfSMario Kleiner 
799d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
800d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
801d31faf65SVille Syrjälä 		vbl_end /= 2;
802d31faf65SVille Syrjälä 		vtotal /= 2;
803d31faf65SVille Syrjälä 	}
804d31faf65SVille Syrjälä 
805c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
806c2baf4b7SVille Syrjälä 
807ad3543edSMario Kleiner 	/*
808ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
809ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
810ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
811ad3543edSMario Kleiner 	 */
812ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
813ad3543edSMario Kleiner 
814ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
815ad3543edSMario Kleiner 
816ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
817ad3543edSMario Kleiner 	if (stime)
818ad3543edSMario Kleiner 		*stime = ktime_get();
819ad3543edSMario Kleiner 
82091d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8210af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8220af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8230af7e4dfSMario Kleiner 		 */
824a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8250af7e4dfSMario Kleiner 	} else {
8260af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8270af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8280af7e4dfSMario Kleiner 		 * scanout position.
8290af7e4dfSMario Kleiner 		 */
83075aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8310af7e4dfSMario Kleiner 
8323aa18df8SVille Syrjälä 		/* convert to pixel counts */
8333aa18df8SVille Syrjälä 		vbl_start *= htotal;
8343aa18df8SVille Syrjälä 		vbl_end *= htotal;
8353aa18df8SVille Syrjälä 		vtotal *= htotal;
83678e8fc6bSVille Syrjälä 
83778e8fc6bSVille Syrjälä 		/*
8387e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8397e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8407e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8417e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8427e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8437e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8447e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8457e78f1cbSVille Syrjälä 		 */
8467e78f1cbSVille Syrjälä 		if (position >= vtotal)
8477e78f1cbSVille Syrjälä 			position = vtotal - 1;
8487e78f1cbSVille Syrjälä 
8497e78f1cbSVille Syrjälä 		/*
85078e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
85178e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
85278e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
85378e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
85478e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
85578e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
85678e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
85778e8fc6bSVille Syrjälä 		 */
85878e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8593aa18df8SVille Syrjälä 	}
8603aa18df8SVille Syrjälä 
861ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
862ad3543edSMario Kleiner 	if (etime)
863ad3543edSMario Kleiner 		*etime = ktime_get();
864ad3543edSMario Kleiner 
865ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
866ad3543edSMario Kleiner 
867ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
868ad3543edSMario Kleiner 
8693aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8703aa18df8SVille Syrjälä 
8713aa18df8SVille Syrjälä 	/*
8723aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8733aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8743aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8753aa18df8SVille Syrjälä 	 * up since vbl_end.
8763aa18df8SVille Syrjälä 	 */
8773aa18df8SVille Syrjälä 	if (position >= vbl_start)
8783aa18df8SVille Syrjälä 		position -= vbl_end;
8793aa18df8SVille Syrjälä 	else
8803aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8813aa18df8SVille Syrjälä 
88291d14251STvrtko Ursulin 	if (IS_GEN2(dev_priv) || IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5) {
8833aa18df8SVille Syrjälä 		*vpos = position;
8843aa18df8SVille Syrjälä 		*hpos = 0;
8853aa18df8SVille Syrjälä 	} else {
8860af7e4dfSMario Kleiner 		*vpos = position / htotal;
8870af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
8880af7e4dfSMario Kleiner 	}
8890af7e4dfSMario Kleiner 
8900af7e4dfSMario Kleiner 	/* In vblank? */
8910af7e4dfSMario Kleiner 	if (in_vbl)
8923d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
8930af7e4dfSMario Kleiner 
8940af7e4dfSMario Kleiner 	return ret;
8950af7e4dfSMario Kleiner }
8960af7e4dfSMario Kleiner 
897a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
898a225f079SVille Syrjälä {
899fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
900a225f079SVille Syrjälä 	unsigned long irqflags;
901a225f079SVille Syrjälä 	int position;
902a225f079SVille Syrjälä 
903a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
904a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
905a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
906a225f079SVille Syrjälä 
907a225f079SVille Syrjälä 	return position;
908a225f079SVille Syrjälä }
909a225f079SVille Syrjälä 
91088e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9110af7e4dfSMario Kleiner 			      int *max_error,
9120af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9130af7e4dfSMario Kleiner 			      unsigned flags)
9140af7e4dfSMario Kleiner {
9154041b853SChris Wilson 	struct drm_crtc *crtc;
9160af7e4dfSMario Kleiner 
91788e72717SThierry Reding 	if (pipe >= INTEL_INFO(dev)->num_pipes) {
91888e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9190af7e4dfSMario Kleiner 		return -EINVAL;
9200af7e4dfSMario Kleiner 	}
9210af7e4dfSMario Kleiner 
9220af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9234041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9244041b853SChris Wilson 	if (crtc == NULL) {
92588e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9264041b853SChris Wilson 		return -EINVAL;
9274041b853SChris Wilson 	}
9284041b853SChris Wilson 
929fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
93088e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9314041b853SChris Wilson 		return -EBUSY;
9324041b853SChris Wilson 	}
9330af7e4dfSMario Kleiner 
9340af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9354041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9364041b853SChris Wilson 						     vblank_time, flags,
937fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9380af7e4dfSMario Kleiner }
9390af7e4dfSMario Kleiner 
94091d14251STvrtko Ursulin static void ironlake_rps_change_irq_handler(struct drm_i915_private *dev_priv)
941f97108d1SJesse Barnes {
942b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9439270388eSDaniel Vetter 	u8 new_delay;
9449270388eSDaniel Vetter 
945d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
946f97108d1SJesse Barnes 
94773edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94873edd18fSDaniel Vetter 
94920e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9509270388eSDaniel Vetter 
9517648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
952b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
953b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
954f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
955f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
956f97108d1SJesse Barnes 
957f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
958b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
96020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
96120e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
96220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
963b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96620e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
968f97108d1SJesse Barnes 	}
969f97108d1SJesse Barnes 
97091d14251STvrtko Ursulin 	if (ironlake_set_drps(dev_priv, new_delay))
97120e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
972f97108d1SJesse Barnes 
973d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9749270388eSDaniel Vetter 
975f97108d1SJesse Barnes 	return;
976f97108d1SJesse Barnes }
977f97108d1SJesse Barnes 
9780bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
979549f7365SChris Wilson {
980aca34b6eSChris Wilson 	smp_store_mb(engine->breadcrumbs.irq_posted, true);
98183348ba8SChris Wilson 	if (intel_engine_wakeup(engine))
9820bc40be8STvrtko Ursulin 		trace_i915_gem_request_notify(engine);
983549f7365SChris Wilson }
984549f7365SChris Wilson 
98543cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
98643cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
98731685c25SDeepak S {
98843cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
98943cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
99043cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
99131685c25SDeepak S }
99231685c25SDeepak S 
99343cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
99443cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
99543cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
99643cf3bf0SChris Wilson 			 int threshold)
99731685c25SDeepak S {
99843cf3bf0SChris Wilson 	u64 time, c0;
9997bad74d5SVille Syrjälä 	unsigned int mul = 100;
100031685c25SDeepak S 
100143cf3bf0SChris Wilson 	if (old->cz_clock == 0)
100243cf3bf0SChris Wilson 		return false;
100331685c25SDeepak S 
10047bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10057bad74d5SVille Syrjälä 		mul <<= 8;
10067bad74d5SVille Syrjälä 
100743cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10087bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
100931685c25SDeepak S 
101043cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
101143cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
101243cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
101343cf3bf0SChris Wilson 	 */
101443cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
101543cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10167bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
101731685c25SDeepak S 
101843cf3bf0SChris Wilson 	return c0 >= time;
101931685c25SDeepak S }
102031685c25SDeepak S 
102143cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
102243cf3bf0SChris Wilson {
102343cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
102443cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
102543cf3bf0SChris Wilson }
102643cf3bf0SChris Wilson 
102743cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
102843cf3bf0SChris Wilson {
102943cf3bf0SChris Wilson 	struct intel_rps_ei now;
103043cf3bf0SChris Wilson 	u32 events = 0;
103143cf3bf0SChris Wilson 
10326f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
103343cf3bf0SChris Wilson 		return 0;
103443cf3bf0SChris Wilson 
103543cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
103643cf3bf0SChris Wilson 	if (now.cz_clock == 0)
103743cf3bf0SChris Wilson 		return 0;
103831685c25SDeepak S 
103943cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
104043cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
104143cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10428fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
104343cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
104443cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
104531685c25SDeepak S 	}
104631685c25SDeepak S 
104743cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
104843cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
104943cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10508fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
105143cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
105243cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
105343cf3bf0SChris Wilson 	}
105443cf3bf0SChris Wilson 
105543cf3bf0SChris Wilson 	return events;
105631685c25SDeepak S }
105731685c25SDeepak S 
1058f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1059f5a4c67dSChris Wilson {
1060e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
10613b3f1650SAkash Goel 	enum intel_engine_id id;
1062f5a4c67dSChris Wilson 
10633b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
1064688e6c72SChris Wilson 		if (intel_engine_has_waiter(engine))
1065f5a4c67dSChris Wilson 			return true;
1066f5a4c67dSChris Wilson 
1067f5a4c67dSChris Wilson 	return false;
1068f5a4c67dSChris Wilson }
1069f5a4c67dSChris Wilson 
10704912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10713b8d8d91SJesse Barnes {
10722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10732d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10748d3afd7dSChris Wilson 	bool client_boost;
10758d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1076edbfdb45SPaulo Zanoni 	u32 pm_iir;
10773b8d8d91SJesse Barnes 
107859cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1079d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1080d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1081d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1082d4d70aa5SImre Deak 		return;
1083d4d70aa5SImre Deak 	}
10841f814dacSImre Deak 
1085c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1086c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1087a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1088480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
10898d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
10908d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
109159cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10924912d041SBen Widawsky 
109360611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1094a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
109560611c13SPaulo Zanoni 
10968d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
1097c33d247dSChris Wilson 		return;
10983b8d8d91SJesse Barnes 
10994fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11007b9e0ae6SChris Wilson 
110143cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
110243cf3bf0SChris Wilson 
1103dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1104edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11058d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11068d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
110729ecd78dSChris Wilson 	if (client_boost || any_waiters(dev_priv))
110829ecd78dSChris Wilson 		max = dev_priv->rps.max_freq;
110929ecd78dSChris Wilson 	if (client_boost && new_delay < dev_priv->rps.boost_freq) {
111029ecd78dSChris Wilson 		new_delay = dev_priv->rps.boost_freq;
11118d3afd7dSChris Wilson 		adj = 0;
11128d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1113dd75fdc8SChris Wilson 		if (adj > 0)
1114dd75fdc8SChris Wilson 			adj *= 2;
1115edcf284bSChris Wilson 		else /* CHV needs even encode values */
1116edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11177425034aSVille Syrjälä 		/*
11187425034aSVille Syrjälä 		 * For better performance, jump directly
11197425034aSVille Syrjälä 		 * to RPe if we're below it.
11207425034aSVille Syrjälä 		 */
1121edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1122b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1123edcf284bSChris Wilson 			adj = 0;
1124edcf284bSChris Wilson 		}
112529ecd78dSChris Wilson 	} else if (client_boost || any_waiters(dev_priv)) {
1126f5a4c67dSChris Wilson 		adj = 0;
1127dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1128b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1129b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1130dd75fdc8SChris Wilson 		else
1131b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1132dd75fdc8SChris Wilson 		adj = 0;
1133dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1134dd75fdc8SChris Wilson 		if (adj < 0)
1135dd75fdc8SChris Wilson 			adj *= 2;
1136edcf284bSChris Wilson 		else /* CHV needs even encode values */
1137edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1138dd75fdc8SChris Wilson 	} else { /* unknown event */
1139edcf284bSChris Wilson 		adj = 0;
1140dd75fdc8SChris Wilson 	}
11413b8d8d91SJesse Barnes 
1142edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1143edcf284bSChris Wilson 
114479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
114579249636SBen Widawsky 	 * interrupt
114679249636SBen Widawsky 	 */
1147edcf284bSChris Wilson 	new_delay += adj;
11488d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
114927544369SDeepak S 
1150dc97997aSChris Wilson 	intel_set_rps(dev_priv, new_delay);
11513b8d8d91SJesse Barnes 
11524fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11533b8d8d91SJesse Barnes }
11543b8d8d91SJesse Barnes 
1155e3689190SBen Widawsky 
1156e3689190SBen Widawsky /**
1157e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1158e3689190SBen Widawsky  * occurred.
1159e3689190SBen Widawsky  * @work: workqueue struct
1160e3689190SBen Widawsky  *
1161e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1162e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1163e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1164e3689190SBen Widawsky  */
1165e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1166e3689190SBen Widawsky {
11672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11682d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1169e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
117035a85ac6SBen Widawsky 	char *parity_event[6];
1171e3689190SBen Widawsky 	uint32_t misccpctl;
117235a85ac6SBen Widawsky 	uint8_t slice = 0;
1173e3689190SBen Widawsky 
1174e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1175e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1176e3689190SBen Widawsky 	 * any time we access those registers.
1177e3689190SBen Widawsky 	 */
117891c8a326SChris Wilson 	mutex_lock(&dev_priv->drm.struct_mutex);
1179e3689190SBen Widawsky 
118035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
118135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
118235a85ac6SBen Widawsky 		goto out;
118335a85ac6SBen Widawsky 
1184e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1185e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1186e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1187e3689190SBen Widawsky 
118835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1189f0f59a00SVille Syrjälä 		i915_reg_t reg;
119035a85ac6SBen Widawsky 
119135a85ac6SBen Widawsky 		slice--;
11922d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
119335a85ac6SBen Widawsky 			break;
119435a85ac6SBen Widawsky 
119535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
119635a85ac6SBen Widawsky 
11976fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
119835a85ac6SBen Widawsky 
119935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1200e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1201e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1202e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1203e3689190SBen Widawsky 
120435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
120535a85ac6SBen Widawsky 		POSTING_READ(reg);
1206e3689190SBen Widawsky 
1207cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1208e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1209e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1210e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
121135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
121235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1213e3689190SBen Widawsky 
121491c8a326SChris Wilson 		kobject_uevent_env(&dev_priv->drm.primary->kdev->kobj,
1215e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1216e3689190SBen Widawsky 
121735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
121835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1219e3689190SBen Widawsky 
122035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1221e3689190SBen Widawsky 		kfree(parity_event[3]);
1222e3689190SBen Widawsky 		kfree(parity_event[2]);
1223e3689190SBen Widawsky 		kfree(parity_event[1]);
1224e3689190SBen Widawsky 	}
1225e3689190SBen Widawsky 
122635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
122735a85ac6SBen Widawsky 
122835a85ac6SBen Widawsky out:
122935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12304cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12312d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12324cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
123335a85ac6SBen Widawsky 
123491c8a326SChris Wilson 	mutex_unlock(&dev_priv->drm.struct_mutex);
123535a85ac6SBen Widawsky }
123635a85ac6SBen Widawsky 
1237261e40b8SVille Syrjälä static void ivybridge_parity_error_irq_handler(struct drm_i915_private *dev_priv,
1238261e40b8SVille Syrjälä 					       u32 iir)
1239e3689190SBen Widawsky {
1240261e40b8SVille Syrjälä 	if (!HAS_L3_DPF(dev_priv))
1241e3689190SBen Widawsky 		return;
1242e3689190SBen Widawsky 
1243d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1244261e40b8SVille Syrjälä 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
1245d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1246e3689190SBen Widawsky 
1247261e40b8SVille Syrjälä 	iir &= GT_PARITY_ERROR(dev_priv);
124835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
124935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
125035a85ac6SBen Widawsky 
125135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
125235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
125335a85ac6SBen Widawsky 
1254a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1255e3689190SBen Widawsky }
1256e3689190SBen Widawsky 
1257261e40b8SVille Syrjälä static void ilk_gt_irq_handler(struct drm_i915_private *dev_priv,
1258f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1259f1af8fc1SPaulo Zanoni {
1260f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
12613b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1262f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
12633b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1264f1af8fc1SPaulo Zanoni }
1265f1af8fc1SPaulo Zanoni 
1266261e40b8SVille Syrjälä static void snb_gt_irq_handler(struct drm_i915_private *dev_priv,
1267e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1268e7b4c6b1SDaniel Vetter {
1269f8973c21SChris Wilson 	if (gt_iir & GT_RENDER_USER_INTERRUPT)
12703b3f1650SAkash Goel 		notify_ring(dev_priv->engine[RCS]);
1271cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
12723b3f1650SAkash Goel 		notify_ring(dev_priv->engine[VCS]);
1273cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
12743b3f1650SAkash Goel 		notify_ring(dev_priv->engine[BCS]);
1275e7b4c6b1SDaniel Vetter 
1276cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1277cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1278aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1279aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1280e3689190SBen Widawsky 
1281261e40b8SVille Syrjälä 	if (gt_iir & GT_PARITY_ERROR(dev_priv))
1282261e40b8SVille Syrjälä 		ivybridge_parity_error_irq_handler(dev_priv, gt_iir);
1283e7b4c6b1SDaniel Vetter }
1284e7b4c6b1SDaniel Vetter 
1285fbcc1a0cSNick Hoath static __always_inline void
12860bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1287fbcc1a0cSNick Hoath {
1288fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
12890bc40be8STvrtko Ursulin 		notify_ring(engine);
1290fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
129127af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1292fbcc1a0cSNick Hoath }
1293fbcc1a0cSNick Hoath 
1294e30e251aSVille Syrjälä static irqreturn_t gen8_gt_irq_ack(struct drm_i915_private *dev_priv,
1295e30e251aSVille Syrjälä 				   u32 master_ctl,
1296e30e251aSVille Syrjälä 				   u32 gt_iir[4])
1297abd58f01SBen Widawsky {
1298abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1299abd58f01SBen Widawsky 
1300abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1301e30e251aSVille Syrjälä 		gt_iir[0] = I915_READ_FW(GEN8_GT_IIR(0));
1302e30e251aSVille Syrjälä 		if (gt_iir[0]) {
1303e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(0), gt_iir[0]);
1304abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1305abd58f01SBen Widawsky 		} else
1306abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1307abd58f01SBen Widawsky 	}
1308abd58f01SBen Widawsky 
130985f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1310e30e251aSVille Syrjälä 		gt_iir[1] = I915_READ_FW(GEN8_GT_IIR(1));
1311e30e251aSVille Syrjälä 		if (gt_iir[1]) {
1312e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(1), gt_iir[1]);
1313abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1314abd58f01SBen Widawsky 		} else
1315abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1316abd58f01SBen Widawsky 	}
1317abd58f01SBen Widawsky 
131874cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1319e30e251aSVille Syrjälä 		gt_iir[3] = I915_READ_FW(GEN8_GT_IIR(3));
1320e30e251aSVille Syrjälä 		if (gt_iir[3]) {
1321e30e251aSVille Syrjälä 			I915_WRITE_FW(GEN8_GT_IIR(3), gt_iir[3]);
132274cdb337SChris Wilson 			ret = IRQ_HANDLED;
132374cdb337SChris Wilson 		} else
132474cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
132574cdb337SChris Wilson 	}
132674cdb337SChris Wilson 
13270961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
1328e30e251aSVille Syrjälä 		gt_iir[2] = I915_READ_FW(GEN8_GT_IIR(2));
1329e30e251aSVille Syrjälä 		if (gt_iir[2] & dev_priv->pm_rps_events) {
1330cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
1331e30e251aSVille Syrjälä 				      gt_iir[2] & dev_priv->pm_rps_events);
133238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13330961021aSBen Widawsky 		} else
13340961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13350961021aSBen Widawsky 	}
13360961021aSBen Widawsky 
1337abd58f01SBen Widawsky 	return ret;
1338abd58f01SBen Widawsky }
1339abd58f01SBen Widawsky 
1340e30e251aSVille Syrjälä static void gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1341e30e251aSVille Syrjälä 				u32 gt_iir[4])
1342e30e251aSVille Syrjälä {
1343e30e251aSVille Syrjälä 	if (gt_iir[0]) {
13443b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[RCS],
1345e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_RCS_IRQ_SHIFT);
13463b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[BCS],
1347e30e251aSVille Syrjälä 				    gt_iir[0], GEN8_BCS_IRQ_SHIFT);
1348e30e251aSVille Syrjälä 	}
1349e30e251aSVille Syrjälä 
1350e30e251aSVille Syrjälä 	if (gt_iir[1]) {
13513b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS],
1352e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS1_IRQ_SHIFT);
13533b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VCS2],
1354e30e251aSVille Syrjälä 				    gt_iir[1], GEN8_VCS2_IRQ_SHIFT);
1355e30e251aSVille Syrjälä 	}
1356e30e251aSVille Syrjälä 
1357e30e251aSVille Syrjälä 	if (gt_iir[3])
13583b3f1650SAkash Goel 		gen8_cs_irq_handler(dev_priv->engine[VECS],
1359e30e251aSVille Syrjälä 				    gt_iir[3], GEN8_VECS_IRQ_SHIFT);
1360e30e251aSVille Syrjälä 
1361e30e251aSVille Syrjälä 	if (gt_iir[2] & dev_priv->pm_rps_events)
1362e30e251aSVille Syrjälä 		gen6_rps_irq_handler(dev_priv, gt_iir[2]);
1363e30e251aSVille Syrjälä }
1364e30e251aSVille Syrjälä 
136563c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
136663c88d22SImre Deak {
136763c88d22SImre Deak 	switch (port) {
136863c88d22SImre Deak 	case PORT_A:
1369195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
137063c88d22SImre Deak 	case PORT_B:
137163c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
137263c88d22SImre Deak 	case PORT_C:
137363c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
137463c88d22SImre Deak 	default:
137563c88d22SImre Deak 		return false;
137663c88d22SImre Deak 	}
137763c88d22SImre Deak }
137863c88d22SImre Deak 
13796dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
13806dbf30ceSVille Syrjälä {
13816dbf30ceSVille Syrjälä 	switch (port) {
13826dbf30ceSVille Syrjälä 	case PORT_E:
13836dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
13846dbf30ceSVille Syrjälä 	default:
13856dbf30ceSVille Syrjälä 		return false;
13866dbf30ceSVille Syrjälä 	}
13876dbf30ceSVille Syrjälä }
13886dbf30ceSVille Syrjälä 
138974c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
139074c0b395SVille Syrjälä {
139174c0b395SVille Syrjälä 	switch (port) {
139274c0b395SVille Syrjälä 	case PORT_A:
139374c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
139474c0b395SVille Syrjälä 	case PORT_B:
139574c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
139674c0b395SVille Syrjälä 	case PORT_C:
139774c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
139874c0b395SVille Syrjälä 	case PORT_D:
139974c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
140074c0b395SVille Syrjälä 	default:
140174c0b395SVille Syrjälä 		return false;
140274c0b395SVille Syrjälä 	}
140374c0b395SVille Syrjälä }
140474c0b395SVille Syrjälä 
1405e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1406e4ce95aaSVille Syrjälä {
1407e4ce95aaSVille Syrjälä 	switch (port) {
1408e4ce95aaSVille Syrjälä 	case PORT_A:
1409e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1410e4ce95aaSVille Syrjälä 	default:
1411e4ce95aaSVille Syrjälä 		return false;
1412e4ce95aaSVille Syrjälä 	}
1413e4ce95aaSVille Syrjälä }
1414e4ce95aaSVille Syrjälä 
1415676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
141613cf5504SDave Airlie {
141713cf5504SDave Airlie 	switch (port) {
141813cf5504SDave Airlie 	case PORT_B:
1419676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
142013cf5504SDave Airlie 	case PORT_C:
1421676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
142213cf5504SDave Airlie 	case PORT_D:
1423676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1424676574dfSJani Nikula 	default:
1425676574dfSJani Nikula 		return false;
142613cf5504SDave Airlie 	}
142713cf5504SDave Airlie }
142813cf5504SDave Airlie 
1429676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
143013cf5504SDave Airlie {
143113cf5504SDave Airlie 	switch (port) {
143213cf5504SDave Airlie 	case PORT_B:
1433676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
143413cf5504SDave Airlie 	case PORT_C:
1435676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
143613cf5504SDave Airlie 	case PORT_D:
1437676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1438676574dfSJani Nikula 	default:
1439676574dfSJani Nikula 		return false;
144013cf5504SDave Airlie 	}
144113cf5504SDave Airlie }
144213cf5504SDave Airlie 
144342db67d6SVille Syrjälä /*
144442db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
144542db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
144642db67d6SVille Syrjälä  * hotplug detection results from several registers.
144742db67d6SVille Syrjälä  *
144842db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
144942db67d6SVille Syrjälä  */
1450fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14518c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1452fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1453fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1454676574dfSJani Nikula {
14558c841e57SJani Nikula 	enum port port;
1456676574dfSJani Nikula 	int i;
1457676574dfSJani Nikula 
1458676574dfSJani Nikula 	for_each_hpd_pin(i) {
14598c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14608c841e57SJani Nikula 			continue;
14618c841e57SJani Nikula 
1462676574dfSJani Nikula 		*pin_mask |= BIT(i);
1463676574dfSJani Nikula 
1464cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1465cc24fcdcSImre Deak 			continue;
1466cc24fcdcSImre Deak 
1467fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1468676574dfSJani Nikula 			*long_mask |= BIT(i);
1469676574dfSJani Nikula 	}
1470676574dfSJani Nikula 
1471676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1472676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1473676574dfSJani Nikula 
1474676574dfSJani Nikula }
1475676574dfSJani Nikula 
147691d14251STvrtko Ursulin static void gmbus_irq_handler(struct drm_i915_private *dev_priv)
1477515ac2bbSDaniel Vetter {
147828c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1479515ac2bbSDaniel Vetter }
1480515ac2bbSDaniel Vetter 
148191d14251STvrtko Ursulin static void dp_aux_irq_handler(struct drm_i915_private *dev_priv)
1482ce99c256SDaniel Vetter {
14839ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1484ce99c256SDaniel Vetter }
1485ce99c256SDaniel Vetter 
14868bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
148791d14251STvrtko Ursulin static void display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
148891d14251STvrtko Ursulin 					 enum pipe pipe,
1489eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1490eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
14918bc5e955SDaniel Vetter 					 uint32_t crc4)
14928bf1e9f1SShuang He {
14938bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
14948bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1495ac2300d4SDamien Lespiau 	int head, tail;
1496b2c88f5bSDamien Lespiau 
1497d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1498d538bbdfSDamien Lespiau 
14990c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1500d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
150134273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15020c912c79SDamien Lespiau 		return;
15030c912c79SDamien Lespiau 	}
15040c912c79SDamien Lespiau 
1505d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1506d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1507b2c88f5bSDamien Lespiau 
1508b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1509d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1510b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1511b2c88f5bSDamien Lespiau 		return;
1512b2c88f5bSDamien Lespiau 	}
1513b2c88f5bSDamien Lespiau 
1514b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15158bf1e9f1SShuang He 
151691c8a326SChris Wilson 	entry->frame = dev_priv->drm.driver->get_vblank_counter(&dev_priv->drm,
151791d14251STvrtko Ursulin 								 pipe);
1518eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1519eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1520eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1521eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1522eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1523b2c88f5bSDamien Lespiau 
1524b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1525d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1526d538bbdfSDamien Lespiau 
1527d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
152807144428SDamien Lespiau 
152907144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15308bf1e9f1SShuang He }
1531277de95eSDaniel Vetter #else
1532277de95eSDaniel Vetter static inline void
153391d14251STvrtko Ursulin display_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
153491d14251STvrtko Ursulin 			     enum pipe pipe,
1535277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1536277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1537277de95eSDaniel Vetter 			     uint32_t crc4) {}
1538277de95eSDaniel Vetter #endif
1539eba94eb9SDaniel Vetter 
1540277de95eSDaniel Vetter 
154191d14251STvrtko Ursulin static void hsw_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
154291d14251STvrtko Ursulin 				     enum pipe pipe)
15435a69b89fSDaniel Vetter {
154491d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
15455a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15465a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15475a69b89fSDaniel Vetter }
15485a69b89fSDaniel Vetter 
154991d14251STvrtko Ursulin static void ivb_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
155091d14251STvrtko Ursulin 				     enum pipe pipe)
1551eba94eb9SDaniel Vetter {
155291d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
1553eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1554eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1555eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1556eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15578bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1558eba94eb9SDaniel Vetter }
15595b3a856bSDaniel Vetter 
156091d14251STvrtko Ursulin static void i9xx_pipe_crc_irq_handler(struct drm_i915_private *dev_priv,
156191d14251STvrtko Ursulin 				      enum pipe pipe)
15625b3a856bSDaniel Vetter {
15630b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15640b5c5ed0SDaniel Vetter 
156591d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 3)
15660b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15670b5c5ed0SDaniel Vetter 	else
15680b5c5ed0SDaniel Vetter 		res1 = 0;
15690b5c5ed0SDaniel Vetter 
157091d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
15710b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
15720b5c5ed0SDaniel Vetter 	else
15730b5c5ed0SDaniel Vetter 		res2 = 0;
15745b3a856bSDaniel Vetter 
157591d14251STvrtko Ursulin 	display_pipe_crc_irq_handler(dev_priv, pipe,
15760b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
15770b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
15780b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
15790b5c5ed0SDaniel Vetter 				     res1, res2);
15805b3a856bSDaniel Vetter }
15818bf1e9f1SShuang He 
15821403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
15831403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
15841403c0d4SPaulo Zanoni  * the work queue. */
15851403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1586baf02a1fSBen Widawsky {
1587a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
158859cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1589480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1590d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1591d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1592c33d247dSChris Wilson 			schedule_work(&dev_priv->rps.work);
159341a05a3aSDaniel Vetter 		}
1594d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1595d4d70aa5SImre Deak 	}
1596baf02a1fSBen Widawsky 
1597c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1598c9a9a268SImre Deak 		return;
1599c9a9a268SImre Deak 
16002d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
160112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16023b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VECS]);
160312638c57SBen Widawsky 
1604aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1605aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
160612638c57SBen Widawsky 	}
16071403c0d4SPaulo Zanoni }
1608baf02a1fSBen Widawsky 
16095a21b665SDaniel Vetter static bool intel_pipe_handle_vblank(struct drm_i915_private *dev_priv,
161091d14251STvrtko Ursulin 				     enum pipe pipe)
16118d7849dbSVille Syrjälä {
16125a21b665SDaniel Vetter 	bool ret;
16135a21b665SDaniel Vetter 
161491c8a326SChris Wilson 	ret = drm_handle_vblank(&dev_priv->drm, pipe);
16155a21b665SDaniel Vetter 	if (ret)
161651cbaf01SMaarten Lankhorst 		intel_finish_page_flip_mmio(dev_priv, pipe);
16175a21b665SDaniel Vetter 
16185a21b665SDaniel Vetter 	return ret;
16198d7849dbSVille Syrjälä }
16208d7849dbSVille Syrjälä 
162191d14251STvrtko Ursulin static void valleyview_pipestat_irq_ack(struct drm_i915_private *dev_priv,
162291d14251STvrtko Ursulin 					u32 iir, u32 pipe_stats[I915_MAX_PIPES])
16237e231dbeSJesse Barnes {
16247e231dbeSJesse Barnes 	int pipe;
16257e231dbeSJesse Barnes 
162658ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
16271ca993d2SVille Syrjälä 
16281ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
16291ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
16301ca993d2SVille Syrjälä 		return;
16311ca993d2SVille Syrjälä 	}
16321ca993d2SVille Syrjälä 
1633055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1634f0f59a00SVille Syrjälä 		i915_reg_t reg;
1635bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
163691d181ddSImre Deak 
1637bbb5eebfSDaniel Vetter 		/*
1638bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1639bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1640bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1641bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1642bbb5eebfSDaniel Vetter 		 * handle.
1643bbb5eebfSDaniel Vetter 		 */
16440f239f4cSDaniel Vetter 
16450f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16460f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1647bbb5eebfSDaniel Vetter 
1648bbb5eebfSDaniel Vetter 		switch (pipe) {
1649bbb5eebfSDaniel Vetter 		case PIPE_A:
1650bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1651bbb5eebfSDaniel Vetter 			break;
1652bbb5eebfSDaniel Vetter 		case PIPE_B:
1653bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1654bbb5eebfSDaniel Vetter 			break;
16553278f67fSVille Syrjälä 		case PIPE_C:
16563278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16573278f67fSVille Syrjälä 			break;
1658bbb5eebfSDaniel Vetter 		}
1659bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1660bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1661bbb5eebfSDaniel Vetter 
1662bbb5eebfSDaniel Vetter 		if (!mask)
166391d181ddSImre Deak 			continue;
166491d181ddSImre Deak 
166591d181ddSImre Deak 		reg = PIPESTAT(pipe);
1666bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1667bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16687e231dbeSJesse Barnes 
16697e231dbeSJesse Barnes 		/*
16707e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16717e231dbeSJesse Barnes 		 */
167291d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
167391d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
16747e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
16757e231dbeSJesse Barnes 	}
167658ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
16772ecb8ca4SVille Syrjälä }
16782ecb8ca4SVille Syrjälä 
167991d14251STvrtko Ursulin static void valleyview_pipestat_irq_handler(struct drm_i915_private *dev_priv,
16802ecb8ca4SVille Syrjälä 					    u32 pipe_stats[I915_MAX_PIPES])
16812ecb8ca4SVille Syrjälä {
16822ecb8ca4SVille Syrjälä 	enum pipe pipe;
16837e231dbeSJesse Barnes 
1684055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
16855a21b665SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
16865a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
16875a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
168831acc7f5SJesse Barnes 
16895251f04eSMaarten Lankhorst 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV)
169051cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
16914356d586SDaniel Vetter 
16924356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
169391d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
16942d9d2b0bSVille Syrjälä 
16951f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
16961f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
169731acc7f5SJesse Barnes 	}
169831acc7f5SJesse Barnes 
1699c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
170091d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1701c1874ed7SImre Deak }
1702c1874ed7SImre Deak 
17031ae3c34cSVille Syrjälä static u32 i9xx_hpd_irq_ack(struct drm_i915_private *dev_priv)
170416c6c56bSVille Syrjälä {
170516c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
170616c6c56bSVille Syrjälä 
17071ae3c34cSVille Syrjälä 	if (hotplug_status)
17083ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17091ae3c34cSVille Syrjälä 
17101ae3c34cSVille Syrjälä 	return hotplug_status;
17111ae3c34cSVille Syrjälä }
17121ae3c34cSVille Syrjälä 
171391d14251STvrtko Ursulin static void i9xx_hpd_irq_handler(struct drm_i915_private *dev_priv,
17141ae3c34cSVille Syrjälä 				 u32 hotplug_status)
17151ae3c34cSVille Syrjälä {
17161ae3c34cSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
17173ff60f89SOscar Mateo 
171891d14251STvrtko Ursulin 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
171991d14251STvrtko Ursulin 	    IS_CHERRYVIEW(dev_priv)) {
172016c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
172116c6c56bSVille Syrjälä 
172258f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1723fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1724fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1725fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
172658f2cf24SVille Syrjälä 
172791d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
172858f2cf24SVille Syrjälä 		}
1729369712e8SJani Nikula 
1730369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
173191d14251STvrtko Ursulin 			dp_aux_irq_handler(dev_priv);
173216c6c56bSVille Syrjälä 	} else {
173316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
173416c6c56bSVille Syrjälä 
173558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1736fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17374e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1738fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
173991d14251STvrtko Ursulin 			intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
174016c6c56bSVille Syrjälä 		}
17413ff60f89SOscar Mateo 	}
174258f2cf24SVille Syrjälä }
174316c6c56bSVille Syrjälä 
1744c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1745c1874ed7SImre Deak {
174645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1747fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
1748c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1749c1874ed7SImre Deak 
17502dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17512dd2a883SImre Deak 		return IRQ_NONE;
17522dd2a883SImre Deak 
17531f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17541f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
17551f814dacSImre Deak 
17561e1cace9SVille Syrjälä 	do {
17576e814800SVille Syrjälä 		u32 iir, gt_iir, pm_iir;
17582ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
17591ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1760a5e485a9SVille Syrjälä 		u32 ier = 0;
17613ff60f89SOscar Mateo 
1762c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1763c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17643ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
1765c1874ed7SImre Deak 
1766c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
17671e1cace9SVille Syrjälä 			break;
1768c1874ed7SImre Deak 
1769c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1770c1874ed7SImre Deak 
1771a5e485a9SVille Syrjälä 		/*
1772a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1773a5e485a9SVille Syrjälä 		 *
1774a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1775a5e485a9SVille Syrjälä 		 *      (((GT_IIR & GT_IER) || (GEN6_PMIIR & GEN6_PMIER)) &&
1776a5e485a9SVille Syrjälä 		 *       (VLV_MASTER_IER & MASTER_INTERRUPT_ENABLE)));
1777a5e485a9SVille Syrjälä 		 *
1778a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1779a5e485a9SVille Syrjälä 		 * Hence we clear MASTER_INTERRUPT_ENABLE and VLV_IER to
1780a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1781a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR, GT_IIR, GEN6_PMIIR
1782a5e485a9SVille Syrjälä 		 * bits this time around.
1783a5e485a9SVille Syrjälä 		 */
17844a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, 0);
1785a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1786a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
17874a0a0202SVille Syrjälä 
17884a0a0202SVille Syrjälä 		if (gt_iir)
17894a0a0202SVille Syrjälä 			I915_WRITE(GTIIR, gt_iir);
17904a0a0202SVille Syrjälä 		if (pm_iir)
17914a0a0202SVille Syrjälä 			I915_WRITE(GEN6_PMIIR, pm_iir);
17924a0a0202SVille Syrjälä 
17937ce4d1f2SVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
17941ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
17957ce4d1f2SVille Syrjälä 
17963ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17973ff60f89SOscar Mateo 		 * signalled in iir */
179891d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
17997ce4d1f2SVille Syrjälä 
18007ce4d1f2SVille Syrjälä 		/*
18017ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18027ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18037ce4d1f2SVille Syrjälä 		 */
18047ce4d1f2SVille Syrjälä 		if (iir)
18057ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
18064a0a0202SVille Syrjälä 
1807a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
18084a0a0202SVille Syrjälä 		I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
18094a0a0202SVille Syrjälä 		POSTING_READ(VLV_MASTER_IER);
18101ae3c34cSVille Syrjälä 
181152894874SVille Syrjälä 		if (gt_iir)
1812261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
181352894874SVille Syrjälä 		if (pm_iir)
181452894874SVille Syrjälä 			gen6_rps_irq_handler(dev_priv, pm_iir);
181552894874SVille Syrjälä 
18161ae3c34cSVille Syrjälä 		if (hotplug_status)
181791d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
18182ecb8ca4SVille Syrjälä 
181991d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
18201e1cace9SVille Syrjälä 	} while (0);
18217e231dbeSJesse Barnes 
18221f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18231f814dacSImre Deak 
18247e231dbeSJesse Barnes 	return ret;
18257e231dbeSJesse Barnes }
18267e231dbeSJesse Barnes 
182743f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
182843f328d7SVille Syrjälä {
182945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
1830fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
183143f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
183243f328d7SVille Syrjälä 
18332dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18342dd2a883SImre Deak 		return IRQ_NONE;
18352dd2a883SImre Deak 
18361f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18371f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18381f814dacSImre Deak 
1839579de73bSChris Wilson 	do {
18406e814800SVille Syrjälä 		u32 master_ctl, iir;
1841e30e251aSVille Syrjälä 		u32 gt_iir[4] = {};
18422ecb8ca4SVille Syrjälä 		u32 pipe_stats[I915_MAX_PIPES] = {};
18431ae3c34cSVille Syrjälä 		u32 hotplug_status = 0;
1844a5e485a9SVille Syrjälä 		u32 ier = 0;
1845a5e485a9SVille Syrjälä 
18468e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18473278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18483278f67fSVille Syrjälä 
18493278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18508e5fd599SVille Syrjälä 			break;
185143f328d7SVille Syrjälä 
185227b6c122SOscar Mateo 		ret = IRQ_HANDLED;
185327b6c122SOscar Mateo 
1854a5e485a9SVille Syrjälä 		/*
1855a5e485a9SVille Syrjälä 		 * Theory on interrupt generation, based on empirical evidence:
1856a5e485a9SVille Syrjälä 		 *
1857a5e485a9SVille Syrjälä 		 * x = ((VLV_IIR & VLV_IER) ||
1858a5e485a9SVille Syrjälä 		 *      ((GEN8_MASTER_IRQ & ~GEN8_MASTER_IRQ_CONTROL) &&
1859a5e485a9SVille Syrjälä 		 *       (GEN8_MASTER_IRQ & GEN8_MASTER_IRQ_CONTROL)));
1860a5e485a9SVille Syrjälä 		 *
1861a5e485a9SVille Syrjälä 		 * A CPU interrupt will only be raised when 'x' has a 0->1 edge.
1862a5e485a9SVille Syrjälä 		 * Hence we clear GEN8_MASTER_IRQ_CONTROL and VLV_IER to
1863a5e485a9SVille Syrjälä 		 * guarantee the CPU interrupt will be raised again even if we
1864a5e485a9SVille Syrjälä 		 * don't end up clearing all the VLV_IIR and GEN8_MASTER_IRQ_CONTROL
1865a5e485a9SVille Syrjälä 		 * bits this time around.
1866a5e485a9SVille Syrjälä 		 */
186743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
1868a5e485a9SVille Syrjälä 		ier = I915_READ(VLV_IER);
1869a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, 0);
187043f328d7SVille Syrjälä 
1871e30e251aSVille Syrjälä 		gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
187227b6c122SOscar Mateo 
187327b6c122SOscar Mateo 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
18741ae3c34cSVille Syrjälä 			hotplug_status = i9xx_hpd_irq_ack(dev_priv);
187543f328d7SVille Syrjälä 
187627b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
187727b6c122SOscar Mateo 		 * signalled in iir */
187891d14251STvrtko Ursulin 		valleyview_pipestat_irq_ack(dev_priv, iir, pipe_stats);
187943f328d7SVille Syrjälä 
18807ce4d1f2SVille Syrjälä 		/*
18817ce4d1f2SVille Syrjälä 		 * VLV_IIR is single buffered, and reflects the level
18827ce4d1f2SVille Syrjälä 		 * from PIPESTAT/PORT_HOTPLUG_STAT, hence clear it last.
18837ce4d1f2SVille Syrjälä 		 */
18847ce4d1f2SVille Syrjälä 		if (iir)
18857ce4d1f2SVille Syrjälä 			I915_WRITE(VLV_IIR, iir);
18867ce4d1f2SVille Syrjälä 
1887a5e485a9SVille Syrjälä 		I915_WRITE(VLV_IER, ier);
1888e5328c43SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
188943f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18901ae3c34cSVille Syrjälä 
1891e30e251aSVille Syrjälä 		gen8_gt_irq_handler(dev_priv, gt_iir);
1892e30e251aSVille Syrjälä 
18931ae3c34cSVille Syrjälä 		if (hotplug_status)
189491d14251STvrtko Ursulin 			i9xx_hpd_irq_handler(dev_priv, hotplug_status);
18952ecb8ca4SVille Syrjälä 
189691d14251STvrtko Ursulin 		valleyview_pipestat_irq_handler(dev_priv, pipe_stats);
1897579de73bSChris Wilson 	} while (0);
18983278f67fSVille Syrjälä 
18991f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
19001f814dacSImre Deak 
190143f328d7SVille Syrjälä 	return ret;
190243f328d7SVille Syrjälä }
190343f328d7SVille Syrjälä 
190491d14251STvrtko Ursulin static void ibx_hpd_irq_handler(struct drm_i915_private *dev_priv,
190591d14251STvrtko Ursulin 				u32 hotplug_trigger,
190640e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1907776ad806SJesse Barnes {
190842db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1909776ad806SJesse Barnes 
19106a39d7c9SJani Nikula 	/*
19116a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
19126a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
19136a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
19146a39d7c9SJani Nikula 	 * errors.
19156a39d7c9SJani Nikula 	 */
191613cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
19176a39d7c9SJani Nikula 	if (!hotplug_trigger) {
19186a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
19196a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
19206a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
19216a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
19226a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
19236a39d7c9SJani Nikula 	}
19246a39d7c9SJani Nikula 
192513cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
19266a39d7c9SJani Nikula 	if (!hotplug_trigger)
19276a39d7c9SJani Nikula 		return;
192813cf5504SDave Airlie 
1929fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
193040e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1931fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
193240e56410SVille Syrjälä 
193391d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
1934aaf5ec2eSSonika Jindal }
193591d131d2SDaniel Vetter 
193691d14251STvrtko Ursulin static void ibx_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
193740e56410SVille Syrjälä {
193840e56410SVille Syrjälä 	int pipe;
193940e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
194040e56410SVille Syrjälä 
194191d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ibx);
194240e56410SVille Syrjälä 
1943cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1944cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1945776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1946cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1947cfc33bf7SVille Syrjälä 				 port_name(port));
1948cfc33bf7SVille Syrjälä 	}
1949776ad806SJesse Barnes 
1950ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
195191d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
1952ce99c256SDaniel Vetter 
1953776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
195491d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
1955776ad806SJesse Barnes 
1956776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1957776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1958776ad806SJesse Barnes 
1959776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1960776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1961776ad806SJesse Barnes 
1962776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1963776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1964776ad806SJesse Barnes 
19659db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1966055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19679db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19689db4a9c7SJesse Barnes 					 pipe_name(pipe),
19699db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1970776ad806SJesse Barnes 
1971776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1972776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1973776ad806SJesse Barnes 
1974776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1975776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1976776ad806SJesse Barnes 
1977776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19781f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19798664281bSPaulo Zanoni 
19808664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19811f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19828664281bSPaulo Zanoni }
19838664281bSPaulo Zanoni 
198491d14251STvrtko Ursulin static void ivb_err_int_handler(struct drm_i915_private *dev_priv)
19858664281bSPaulo Zanoni {
19868664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19875a69b89fSDaniel Vetter 	enum pipe pipe;
19888664281bSPaulo Zanoni 
1989de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1990de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1991de032bf4SPaulo Zanoni 
1992055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19931f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19941f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19958664281bSPaulo Zanoni 
19965a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
199791d14251STvrtko Ursulin 			if (IS_IVYBRIDGE(dev_priv))
199891d14251STvrtko Ursulin 				ivb_pipe_crc_irq_handler(dev_priv, pipe);
19995a69b89fSDaniel Vetter 			else
200091d14251STvrtko Ursulin 				hsw_pipe_crc_irq_handler(dev_priv, pipe);
20015a69b89fSDaniel Vetter 		}
20025a69b89fSDaniel Vetter 	}
20038bf1e9f1SShuang He 
20048664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
20058664281bSPaulo Zanoni }
20068664281bSPaulo Zanoni 
200791d14251STvrtko Ursulin static void cpt_serr_int_handler(struct drm_i915_private *dev_priv)
20088664281bSPaulo Zanoni {
20098664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
20108664281bSPaulo Zanoni 
2011de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2012de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2013de032bf4SPaulo Zanoni 
20148664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
20151f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20168664281bSPaulo Zanoni 
20178664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
20181f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20198664281bSPaulo Zanoni 
20208664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
20211f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
20228664281bSPaulo Zanoni 
20238664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2024776ad806SJesse Barnes }
2025776ad806SJesse Barnes 
202691d14251STvrtko Ursulin static void cpt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
202723e81d69SAdam Jackson {
202823e81d69SAdam Jackson 	int pipe;
20296dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
2030aaf5ec2eSSonika Jindal 
203191d14251STvrtko Ursulin 	ibx_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_cpt);
203291d131d2SDaniel Vetter 
2033cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2034cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
203523e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2036cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2037cfc33bf7SVille Syrjälä 				 port_name(port));
2038cfc33bf7SVille Syrjälä 	}
203923e81d69SAdam Jackson 
204023e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
204191d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
204223e81d69SAdam Jackson 
204323e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
204491d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
204523e81d69SAdam Jackson 
204623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
204723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
204823e81d69SAdam Jackson 
204923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
205023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
205123e81d69SAdam Jackson 
205223e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2053055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
205423e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
205523e81d69SAdam Jackson 					 pipe_name(pipe),
205623e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20578664281bSPaulo Zanoni 
20588664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
205991d14251STvrtko Ursulin 		cpt_serr_int_handler(dev_priv);
206023e81d69SAdam Jackson }
206123e81d69SAdam Jackson 
206291d14251STvrtko Ursulin static void spt_irq_handler(struct drm_i915_private *dev_priv, u32 pch_iir)
20636dbf30ceSVille Syrjälä {
20646dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20656dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20666dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20676dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20686dbf30ceSVille Syrjälä 
20696dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20706dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20716dbf30ceSVille Syrjälä 
20726dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20736dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20746dbf30ceSVille Syrjälä 
20756dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
20766dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
207774c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20786dbf30ceSVille Syrjälä 	}
20796dbf30ceSVille Syrjälä 
20806dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20816dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20826dbf30ceSVille Syrjälä 
20836dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
20846dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20856dbf30ceSVille Syrjälä 
20866dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
20876dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
20886dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20896dbf30ceSVille Syrjälä 	}
20906dbf30ceSVille Syrjälä 
20916dbf30ceSVille Syrjälä 	if (pin_mask)
209291d14251STvrtko Ursulin 		intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
20936dbf30ceSVille Syrjälä 
20946dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
209591d14251STvrtko Ursulin 		gmbus_irq_handler(dev_priv);
20966dbf30ceSVille Syrjälä }
20976dbf30ceSVille Syrjälä 
209891d14251STvrtko Ursulin static void ilk_hpd_irq_handler(struct drm_i915_private *dev_priv,
209991d14251STvrtko Ursulin 				u32 hotplug_trigger,
210040e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2101c008bc6eSPaulo Zanoni {
2102e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2103e4ce95aaSVille Syrjälä 
2104e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2105e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2106e4ce95aaSVille Syrjälä 
2107e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
210840e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2109e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
211040e56410SVille Syrjälä 
211191d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2112e4ce95aaSVille Syrjälä }
2113c008bc6eSPaulo Zanoni 
211491d14251STvrtko Ursulin static void ilk_display_irq_handler(struct drm_i915_private *dev_priv,
211591d14251STvrtko Ursulin 				    u32 de_iir)
211640e56410SVille Syrjälä {
211740e56410SVille Syrjälä 	enum pipe pipe;
211840e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
211940e56410SVille Syrjälä 
212040e56410SVille Syrjälä 	if (hotplug_trigger)
212191d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ilk);
212240e56410SVille Syrjälä 
2123c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
212491d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
2125c008bc6eSPaulo Zanoni 
2126c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
212791d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
2128c008bc6eSPaulo Zanoni 
2129c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2130c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2131c008bc6eSPaulo Zanoni 
2132055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21335a21b665SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
21345a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
21355a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2136c008bc6eSPaulo Zanoni 
213740da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
21381f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2139c008bc6eSPaulo Zanoni 
214040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
214191d14251STvrtko Ursulin 			i9xx_pipe_crc_irq_handler(dev_priv, pipe);
21425b3a856bSDaniel Vetter 
214340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
21445251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE(pipe))
214551cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2146c008bc6eSPaulo Zanoni 	}
2147c008bc6eSPaulo Zanoni 
2148c008bc6eSPaulo Zanoni 	/* check event from PCH */
2149c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2150c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2151c008bc6eSPaulo Zanoni 
215291d14251STvrtko Ursulin 		if (HAS_PCH_CPT(dev_priv))
215391d14251STvrtko Ursulin 			cpt_irq_handler(dev_priv, pch_iir);
2154c008bc6eSPaulo Zanoni 		else
215591d14251STvrtko Ursulin 			ibx_irq_handler(dev_priv, pch_iir);
2156c008bc6eSPaulo Zanoni 
2157c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2158c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2159c008bc6eSPaulo Zanoni 	}
2160c008bc6eSPaulo Zanoni 
216191d14251STvrtko Ursulin 	if (IS_GEN5(dev_priv) && de_iir & DE_PCU_EVENT)
216291d14251STvrtko Ursulin 		ironlake_rps_change_irq_handler(dev_priv);
2163c008bc6eSPaulo Zanoni }
2164c008bc6eSPaulo Zanoni 
216591d14251STvrtko Ursulin static void ivb_display_irq_handler(struct drm_i915_private *dev_priv,
216691d14251STvrtko Ursulin 				    u32 de_iir)
21679719fb98SPaulo Zanoni {
216807d27e20SDamien Lespiau 	enum pipe pipe;
216923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
217023bb4cb5SVille Syrjälä 
217140e56410SVille Syrjälä 	if (hotplug_trigger)
217291d14251STvrtko Ursulin 		ilk_hpd_irq_handler(dev_priv, hotplug_trigger, hpd_ivb);
21739719fb98SPaulo Zanoni 
21749719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
217591d14251STvrtko Ursulin 		ivb_err_int_handler(dev_priv);
21769719fb98SPaulo Zanoni 
21779719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
217891d14251STvrtko Ursulin 		dp_aux_irq_handler(dev_priv);
21799719fb98SPaulo Zanoni 
21809719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
218191d14251STvrtko Ursulin 		intel_opregion_asle_intr(dev_priv);
21829719fb98SPaulo Zanoni 
2183055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
21845a21b665SDaniel Vetter 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
21855a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
21865a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
218740da17c2SDaniel Vetter 
218840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
21895251f04eSMaarten Lankhorst 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe))
219051cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
21919719fb98SPaulo Zanoni 	}
21929719fb98SPaulo Zanoni 
21939719fb98SPaulo Zanoni 	/* check event from PCH */
219491d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv) && (de_iir & DE_PCH_EVENT_IVB)) {
21959719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21969719fb98SPaulo Zanoni 
219791d14251STvrtko Ursulin 		cpt_irq_handler(dev_priv, pch_iir);
21989719fb98SPaulo Zanoni 
21999719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
22009719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
22019719fb98SPaulo Zanoni 	}
22029719fb98SPaulo Zanoni }
22039719fb98SPaulo Zanoni 
220472c90f62SOscar Mateo /*
220572c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
220672c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
220772c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
220872c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
220972c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
221072c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
221172c90f62SOscar Mateo  */
2212f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2213b1f14ad0SJesse Barnes {
221445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
2215fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2216f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
22170e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2218b1f14ad0SJesse Barnes 
22192dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
22202dd2a883SImre Deak 		return IRQ_NONE;
22212dd2a883SImre Deak 
22221f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22231f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
22241f814dacSImre Deak 
2225b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2226b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2227b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
222823a78516SPaulo Zanoni 	POSTING_READ(DEIER);
22290e43406bSChris Wilson 
223044498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
223144498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
223244498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
223344498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
223444498aeaSPaulo Zanoni 	 * due to its back queue). */
223591d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
223644498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
223744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
223844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2239ab5c608bSBen Widawsky 	}
224044498aeaSPaulo Zanoni 
224172c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
224272c90f62SOscar Mateo 
22430e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
22440e43406bSChris Wilson 	if (gt_iir) {
224572c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
224672c90f62SOscar Mateo 		ret = IRQ_HANDLED;
224791d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 6)
2248261e40b8SVille Syrjälä 			snb_gt_irq_handler(dev_priv, gt_iir);
2249d8fc8a47SPaulo Zanoni 		else
2250261e40b8SVille Syrjälä 			ilk_gt_irq_handler(dev_priv, gt_iir);
22510e43406bSChris Wilson 	}
2252b1f14ad0SJesse Barnes 
2253b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22540e43406bSChris Wilson 	if (de_iir) {
225572c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
225672c90f62SOscar Mateo 		ret = IRQ_HANDLED;
225791d14251STvrtko Ursulin 		if (INTEL_GEN(dev_priv) >= 7)
225891d14251STvrtko Ursulin 			ivb_display_irq_handler(dev_priv, de_iir);
2259f1af8fc1SPaulo Zanoni 		else
226091d14251STvrtko Ursulin 			ilk_display_irq_handler(dev_priv, de_iir);
22610e43406bSChris Wilson 	}
22620e43406bSChris Wilson 
226391d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 6) {
2264f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22650e43406bSChris Wilson 		if (pm_iir) {
2266b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22670e43406bSChris Wilson 			ret = IRQ_HANDLED;
226872c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22690e43406bSChris Wilson 		}
2270f1af8fc1SPaulo Zanoni 	}
2271b1f14ad0SJesse Barnes 
2272b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2273b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
227491d14251STvrtko Ursulin 	if (!HAS_PCH_NOP(dev_priv)) {
227544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
227644498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2277ab5c608bSBen Widawsky 	}
2278b1f14ad0SJesse Barnes 
22791f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22801f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22811f814dacSImre Deak 
2282b1f14ad0SJesse Barnes 	return ret;
2283b1f14ad0SJesse Barnes }
2284b1f14ad0SJesse Barnes 
228591d14251STvrtko Ursulin static void bxt_hpd_irq_handler(struct drm_i915_private *dev_priv,
228691d14251STvrtko Ursulin 				u32 hotplug_trigger,
228740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2288d04a492dSShashank Sharma {
2289cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2290d04a492dSShashank Sharma 
2291a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2292a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2293d04a492dSShashank Sharma 
2294cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
229540e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2296cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
229740e56410SVille Syrjälä 
229891d14251STvrtko Ursulin 	intel_hpd_irq_handler(dev_priv, pin_mask, long_mask);
2299d04a492dSShashank Sharma }
2300d04a492dSShashank Sharma 
2301f11a0f46STvrtko Ursulin static irqreturn_t
2302f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2303abd58f01SBen Widawsky {
2304abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2305f11a0f46STvrtko Ursulin 	u32 iir;
2306c42664ccSDaniel Vetter 	enum pipe pipe;
230788e04703SJesse Barnes 
2308abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2309e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2310e32192e1STvrtko Ursulin 		if (iir) {
2311e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2312abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2313e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
231491d14251STvrtko Ursulin 				intel_opregion_asle_intr(dev_priv);
231538cc46d7SOscar Mateo 			else
231638cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2317abd58f01SBen Widawsky 		}
231838cc46d7SOscar Mateo 		else
231938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2320abd58f01SBen Widawsky 	}
2321abd58f01SBen Widawsky 
23226d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2323e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2324e32192e1STvrtko Ursulin 		if (iir) {
2325e32192e1STvrtko Ursulin 			u32 tmp_mask;
2326d04a492dSShashank Sharma 			bool found = false;
2327cebd87a0SVille Syrjälä 
2328e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
23296d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
233088e04703SJesse Barnes 
2331e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2332e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2333e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2334e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2335e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2336e32192e1STvrtko Ursulin 
2337e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
233891d14251STvrtko Ursulin 				dp_aux_irq_handler(dev_priv);
2339d04a492dSShashank Sharma 				found = true;
2340d04a492dSShashank Sharma 			}
2341d04a492dSShashank Sharma 
2342e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2343e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2344e32192e1STvrtko Ursulin 				if (tmp_mask) {
234591d14251STvrtko Ursulin 					bxt_hpd_irq_handler(dev_priv, tmp_mask,
234691d14251STvrtko Ursulin 							    hpd_bxt);
2347d04a492dSShashank Sharma 					found = true;
2348d04a492dSShashank Sharma 				}
2349e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2350e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2351e32192e1STvrtko Ursulin 				if (tmp_mask) {
235291d14251STvrtko Ursulin 					ilk_hpd_irq_handler(dev_priv,
235391d14251STvrtko Ursulin 							    tmp_mask, hpd_bdw);
2354e32192e1STvrtko Ursulin 					found = true;
2355e32192e1STvrtko Ursulin 				}
2356e32192e1STvrtko Ursulin 			}
2357d04a492dSShashank Sharma 
235891d14251STvrtko Ursulin 			if (IS_BROXTON(dev_priv) && (iir & BXT_DE_PORT_GMBUS)) {
235991d14251STvrtko Ursulin 				gmbus_irq_handler(dev_priv);
23609e63743eSShashank Sharma 				found = true;
23619e63743eSShashank Sharma 			}
23629e63743eSShashank Sharma 
2363d04a492dSShashank Sharma 			if (!found)
236438cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
23656d766f02SDaniel Vetter 		}
236638cc46d7SOscar Mateo 		else
236738cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23686d766f02SDaniel Vetter 	}
23696d766f02SDaniel Vetter 
2370055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2371e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2372abd58f01SBen Widawsky 
2373c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2374c42664ccSDaniel Vetter 			continue;
2375c42664ccSDaniel Vetter 
2376e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2377e32192e1STvrtko Ursulin 		if (!iir) {
2378e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2379e32192e1STvrtko Ursulin 			continue;
2380e32192e1STvrtko Ursulin 		}
2381770de83dSDamien Lespiau 
2382e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2383e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2384e32192e1STvrtko Ursulin 
23855a21b665SDaniel Vetter 		if (iir & GEN8_PIPE_VBLANK &&
23865a21b665SDaniel Vetter 		    intel_pipe_handle_vblank(dev_priv, pipe))
23875a21b665SDaniel Vetter 			intel_check_page_flip(dev_priv, pipe);
2388abd58f01SBen Widawsky 
2389e32192e1STvrtko Ursulin 		flip_done = iir;
2390b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2391e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2392770de83dSDamien Lespiau 		else
2393e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2394770de83dSDamien Lespiau 
23955251f04eSMaarten Lankhorst 		if (flip_done)
239651cbaf01SMaarten Lankhorst 			intel_finish_page_flip_cs(dev_priv, pipe);
2397abd58f01SBen Widawsky 
2398e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
239991d14251STvrtko Ursulin 			hsw_pipe_crc_irq_handler(dev_priv, pipe);
24000fbe7870SDaniel Vetter 
2401e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2402e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
240338d83c96SDaniel Vetter 
2404e32192e1STvrtko Ursulin 		fault_errors = iir;
2405b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2406e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2407770de83dSDamien Lespiau 		else
2408e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2409770de83dSDamien Lespiau 
2410770de83dSDamien Lespiau 		if (fault_errors)
241130100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
241230100f2bSDaniel Vetter 				  pipe_name(pipe),
2413e32192e1STvrtko Ursulin 				  fault_errors);
2414abd58f01SBen Widawsky 	}
2415abd58f01SBen Widawsky 
241691d14251STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv) && !HAS_PCH_NOP(dev_priv) &&
2417266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
241892d03a80SDaniel Vetter 		/*
241992d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
242092d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
242192d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
242292d03a80SDaniel Vetter 		 */
2423e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2424e32192e1STvrtko Ursulin 		if (iir) {
2425e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
242692d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
24276dbf30ceSVille Syrjälä 
242822dea0beSRodrigo Vivi 			if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
242991d14251STvrtko Ursulin 				spt_irq_handler(dev_priv, iir);
24306dbf30ceSVille Syrjälä 			else
243191d14251STvrtko Ursulin 				cpt_irq_handler(dev_priv, iir);
24322dfb0b81SJani Nikula 		} else {
24332dfb0b81SJani Nikula 			/*
24342dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
24352dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
24362dfb0b81SJani Nikula 			 */
24372dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
24382dfb0b81SJani Nikula 		}
243992d03a80SDaniel Vetter 	}
244092d03a80SDaniel Vetter 
2441f11a0f46STvrtko Ursulin 	return ret;
2442f11a0f46STvrtko Ursulin }
2443f11a0f46STvrtko Ursulin 
2444f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2445f11a0f46STvrtko Ursulin {
2446f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2447fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2448f11a0f46STvrtko Ursulin 	u32 master_ctl;
2449e30e251aSVille Syrjälä 	u32 gt_iir[4] = {};
2450f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2451f11a0f46STvrtko Ursulin 
2452f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2453f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2454f11a0f46STvrtko Ursulin 
2455f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2456f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2457f11a0f46STvrtko Ursulin 	if (!master_ctl)
2458f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2459f11a0f46STvrtko Ursulin 
2460f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2461f11a0f46STvrtko Ursulin 
2462f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2463f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2464f11a0f46STvrtko Ursulin 
2465f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2466e30e251aSVille Syrjälä 	ret = gen8_gt_irq_ack(dev_priv, master_ctl, gt_iir);
2467e30e251aSVille Syrjälä 	gen8_gt_irq_handler(dev_priv, gt_iir);
2468f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2469f11a0f46STvrtko Ursulin 
2470cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2471cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2472abd58f01SBen Widawsky 
24731f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24741f814dacSImre Deak 
2475abd58f01SBen Widawsky 	return ret;
2476abd58f01SBen Widawsky }
2477abd58f01SBen Widawsky 
24781f15b76fSChris Wilson static void i915_error_wake_up(struct drm_i915_private *dev_priv)
247917e1df07SDaniel Vetter {
248017e1df07SDaniel Vetter 	/*
248117e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
248217e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
248317e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
248417e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
248517e1df07SDaniel Vetter 	 */
248617e1df07SDaniel Vetter 
248717e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
24881f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.wait_queue);
248917e1df07SDaniel Vetter 
249017e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
249117e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
249217e1df07SDaniel Vetter }
249317e1df07SDaniel Vetter 
24948a905236SJesse Barnes /**
2495b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
249614bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
24978a905236SJesse Barnes  *
24988a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24998a905236SJesse Barnes  * was detected.
25008a905236SJesse Barnes  */
2501c033666aSChris Wilson static void i915_reset_and_wakeup(struct drm_i915_private *dev_priv)
25028a905236SJesse Barnes {
250391c8a326SChris Wilson 	struct kobject *kobj = &dev_priv->drm.primary->kdev->kobj;
2504cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2505cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2506cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
25078a905236SJesse Barnes 
2508c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
25098a905236SJesse Barnes 
251044d98a61SZhao Yakui 	DRM_DEBUG_DRIVER("resetting chip\n");
2511c033666aSChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
25121f83fee0SDaniel Vetter 
251317e1df07SDaniel Vetter 	/*
2514f454c694SImre Deak 	 * In most cases it's guaranteed that we get here with an RPM
2515f454c694SImre Deak 	 * reference held, for example because there is a pending GPU
2516f454c694SImre Deak 	 * request that won't finish until the reset is done. This
2517f454c694SImre Deak 	 * isn't the case at least when we get here by doing a
2518f454c694SImre Deak 	 * simulated reset via debugs, so get an RPM reference.
2519f454c694SImre Deak 	 */
2520f454c694SImre Deak 	intel_runtime_pm_get(dev_priv);
2521c033666aSChris Wilson 	intel_prepare_reset(dev_priv);
25227514747dSVille Syrjälä 
2523780f262aSChris Wilson 	do {
2524f454c694SImre Deak 		/*
252517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
252617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
252717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
252817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
252917e1df07SDaniel Vetter 		 */
2530780f262aSChris Wilson 		if (mutex_trylock(&dev_priv->drm.struct_mutex)) {
2531780f262aSChris Wilson 			i915_reset(dev_priv);
2532221fe799SChris Wilson 			mutex_unlock(&dev_priv->drm.struct_mutex);
2533780f262aSChris Wilson 		}
2534780f262aSChris Wilson 
2535780f262aSChris Wilson 		/* We need to wait for anyone holding the lock to wakeup */
2536780f262aSChris Wilson 	} while (wait_on_bit_timeout(&dev_priv->gpu_error.flags,
2537780f262aSChris Wilson 				     I915_RESET_IN_PROGRESS,
2538780f262aSChris Wilson 				     TASK_UNINTERRUPTIBLE,
2539780f262aSChris Wilson 				     HZ));
2540f69061beSDaniel Vetter 
2541c033666aSChris Wilson 	intel_finish_reset(dev_priv);
2542f454c694SImre Deak 	intel_runtime_pm_put(dev_priv);
2543f454c694SImre Deak 
2544780f262aSChris Wilson 	if (!test_bit(I915_WEDGED, &dev_priv->gpu_error.flags))
2545c033666aSChris Wilson 		kobject_uevent_env(kobj,
2546f69061beSDaniel Vetter 				   KOBJ_CHANGE, reset_done_event);
25471f83fee0SDaniel Vetter 
254817e1df07SDaniel Vetter 	/*
254917e1df07SDaniel Vetter 	 * Note: The wake_up also serves as a memory barrier so that
25508af29b0cSChris Wilson 	 * waiters see the updated value of the dev_priv->gpu_error.
255117e1df07SDaniel Vetter 	 */
25521f15b76fSChris Wilson 	wake_up_all(&dev_priv->gpu_error.reset_queue);
2553f316a42cSBen Gamari }
25548a905236SJesse Barnes 
2555d636951eSBen Widawsky static inline void
2556d636951eSBen Widawsky i915_err_print_instdone(struct drm_i915_private *dev_priv,
2557d636951eSBen Widawsky 			struct intel_instdone *instdone)
2558d636951eSBen Widawsky {
2559f9e61372SBen Widawsky 	int slice;
2560f9e61372SBen Widawsky 	int subslice;
2561f9e61372SBen Widawsky 
2562d636951eSBen Widawsky 	pr_err("  INSTDONE: 0x%08x\n", instdone->instdone);
2563d636951eSBen Widawsky 
2564d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 3)
2565d636951eSBen Widawsky 		return;
2566d636951eSBen Widawsky 
2567d636951eSBen Widawsky 	pr_err("  SC_INSTDONE: 0x%08x\n", instdone->slice_common);
2568d636951eSBen Widawsky 
2569d636951eSBen Widawsky 	if (INTEL_GEN(dev_priv) <= 6)
2570d636951eSBen Widawsky 		return;
2571d636951eSBen Widawsky 
2572f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2573f9e61372SBen Widawsky 		pr_err("  SAMPLER_INSTDONE[%d][%d]: 0x%08x\n",
2574f9e61372SBen Widawsky 		       slice, subslice, instdone->sampler[slice][subslice]);
2575f9e61372SBen Widawsky 
2576f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice)
2577f9e61372SBen Widawsky 		pr_err("  ROW_INSTDONE[%d][%d]: 0x%08x\n",
2578f9e61372SBen Widawsky 		       slice, subslice, instdone->row[slice][subslice]);
2579d636951eSBen Widawsky }
2580d636951eSBen Widawsky 
2581c033666aSChris Wilson static void i915_report_and_clear_eir(struct drm_i915_private *dev_priv)
2582c0e09200SDave Airlie {
2583d636951eSBen Widawsky 	struct intel_instdone instdone;
258463eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2585d636951eSBen Widawsky 	int pipe;
258663eeaf38SJesse Barnes 
258735aed2e6SChris Wilson 	if (!eir)
258835aed2e6SChris Wilson 		return;
258963eeaf38SJesse Barnes 
2590a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25918a905236SJesse Barnes 
25923b3f1650SAkash Goel 	intel_engine_get_instdone(dev_priv->engine[RCS], &instdone);
2593bd9854f9SBen Widawsky 
2594c033666aSChris Wilson 	if (IS_G4X(dev_priv)) {
25958a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25968a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25978a905236SJesse Barnes 
2598a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2599a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2600d636951eSBen Widawsky 			i915_err_print_instdone(dev_priv, &instdone);
2601a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2602a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
26038a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26043143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
26058a905236SJesse Barnes 		}
26068a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
26078a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2608a70491ccSJoe Perches 			pr_err("page table error\n");
2609a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
26108a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
26113143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
26128a905236SJesse Barnes 		}
26138a905236SJesse Barnes 	}
26148a905236SJesse Barnes 
2615c033666aSChris Wilson 	if (!IS_GEN2(dev_priv)) {
261663eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
261763eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2618a70491ccSJoe Perches 			pr_err("page table error\n");
2619a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
262063eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
26213143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
262263eeaf38SJesse Barnes 		}
26238a905236SJesse Barnes 	}
26248a905236SJesse Barnes 
262563eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2626a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2627055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2628a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
26299db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
263063eeaf38SJesse Barnes 		/* pipestat has already been acked */
263163eeaf38SJesse Barnes 	}
263263eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2633a70491ccSJoe Perches 		pr_err("instruction error\n");
2634a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2635d636951eSBen Widawsky 		i915_err_print_instdone(dev_priv, &instdone);
2636c033666aSChris Wilson 		if (INTEL_GEN(dev_priv) < 4) {
263763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
263863eeaf38SJesse Barnes 
2639a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2640a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2641a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
264263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
26433143a2bfSChris Wilson 			POSTING_READ(IPEIR);
264463eeaf38SJesse Barnes 		} else {
264563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
264663eeaf38SJesse Barnes 
2647a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2648a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2649a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2650a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
265163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26523143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
265363eeaf38SJesse Barnes 		}
265463eeaf38SJesse Barnes 	}
265563eeaf38SJesse Barnes 
265663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
26573143a2bfSChris Wilson 	POSTING_READ(EIR);
265863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
265963eeaf38SJesse Barnes 	if (eir) {
266063eeaf38SJesse Barnes 		/*
266163eeaf38SJesse Barnes 		 * some errors might have become stuck,
266263eeaf38SJesse Barnes 		 * mask them.
266363eeaf38SJesse Barnes 		 */
266463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
266563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
266663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
266763eeaf38SJesse Barnes 	}
266835aed2e6SChris Wilson }
266935aed2e6SChris Wilson 
267035aed2e6SChris Wilson /**
2671b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
267214bb2c11STvrtko Ursulin  * @dev_priv: i915 device private
267314b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2674aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
267535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
267635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
267735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
267835aed2e6SChris Wilson  * of a ring dump etc.).
267914bb2c11STvrtko Ursulin  * @fmt: Error message format string
268035aed2e6SChris Wilson  */
2681c033666aSChris Wilson void i915_handle_error(struct drm_i915_private *dev_priv,
2682c033666aSChris Wilson 		       u32 engine_mask,
268358174462SMika Kuoppala 		       const char *fmt, ...)
268435aed2e6SChris Wilson {
268558174462SMika Kuoppala 	va_list args;
268658174462SMika Kuoppala 	char error_msg[80];
268735aed2e6SChris Wilson 
268858174462SMika Kuoppala 	va_start(args, fmt);
268958174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
269058174462SMika Kuoppala 	va_end(args);
269158174462SMika Kuoppala 
2692c033666aSChris Wilson 	i915_capture_error_state(dev_priv, engine_mask, error_msg);
2693c033666aSChris Wilson 	i915_report_and_clear_eir(dev_priv);
26948a905236SJesse Barnes 
26958af29b0cSChris Wilson 	if (!engine_mask)
26968af29b0cSChris Wilson 		return;
26978af29b0cSChris Wilson 
26988af29b0cSChris Wilson 	if (test_and_set_bit(I915_RESET_IN_PROGRESS,
26998af29b0cSChris Wilson 			     &dev_priv->gpu_error.flags))
27008af29b0cSChris Wilson 		return;
2701ba1234d1SBen Gamari 
270211ed50ecSBen Gamari 	/*
2703b8d24a06SMika Kuoppala 	 * Wakeup waiting processes so that the reset function
2704b8d24a06SMika Kuoppala 	 * i915_reset_and_wakeup doesn't deadlock trying to grab
2705b8d24a06SMika Kuoppala 	 * various locks. By bumping the reset counter first, the woken
270617e1df07SDaniel Vetter 	 * processes will see a reset in progress and back off,
270717e1df07SDaniel Vetter 	 * releasing their locks and then wait for the reset completion.
270817e1df07SDaniel Vetter 	 * We must do this for _all_ gpu waiters that might hold locks
270917e1df07SDaniel Vetter 	 * that the reset work needs to acquire.
271017e1df07SDaniel Vetter 	 *
27118af29b0cSChris Wilson 	 * Note: The wake_up also provides a memory barrier to ensure that the
27128af29b0cSChris Wilson 	 * waiters see the updated value of the reset flags.
271311ed50ecSBen Gamari 	 */
27141f15b76fSChris Wilson 	i915_error_wake_up(dev_priv);
271511ed50ecSBen Gamari 
2716c033666aSChris Wilson 	i915_reset_and_wakeup(dev_priv);
27178a905236SJesse Barnes }
27188a905236SJesse Barnes 
271942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
272042f52ef8SKeith Packard  * we use as a pipe index
272142f52ef8SKeith Packard  */
272286e83e35SChris Wilson static int i8xx_enable_vblank(struct drm_device *dev, unsigned int pipe)
27230a3e67a4SJesse Barnes {
2724fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2725e9d21d7fSKeith Packard 	unsigned long irqflags;
272671e0ffa5SJesse Barnes 
27271ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
272886e83e35SChris Wilson 	i915_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
272986e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
273086e83e35SChris Wilson 
273186e83e35SChris Wilson 	return 0;
273286e83e35SChris Wilson }
273386e83e35SChris Wilson 
273486e83e35SChris Wilson static int i965_enable_vblank(struct drm_device *dev, unsigned int pipe)
273586e83e35SChris Wilson {
273686e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
273786e83e35SChris Wilson 	unsigned long irqflags;
273886e83e35SChris Wilson 
273986e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27407c463586SKeith Packard 	i915_enable_pipestat(dev_priv, pipe,
2741755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27421ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27438692d00eSChris Wilson 
27440a3e67a4SJesse Barnes 	return 0;
27450a3e67a4SJesse Barnes }
27460a3e67a4SJesse Barnes 
274788e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2748f796cf8fSJesse Barnes {
2749fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2750f796cf8fSJesse Barnes 	unsigned long irqflags;
275186e83e35SChris Wilson 	uint32_t bit = INTEL_GEN(dev) >= 7 ?
275286e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2753f796cf8fSJesse Barnes 
2754f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2755fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2756b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2757b1f14ad0SJesse Barnes 
2758b1f14ad0SJesse Barnes 	return 0;
2759b1f14ad0SJesse Barnes }
2760b1f14ad0SJesse Barnes 
276188e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2762abd58f01SBen Widawsky {
2763fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2764abd58f01SBen Widawsky 	unsigned long irqflags;
2765abd58f01SBen Widawsky 
2766abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2767013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2768abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2769013d3752SVille Syrjälä 
2770abd58f01SBen Widawsky 	return 0;
2771abd58f01SBen Widawsky }
2772abd58f01SBen Widawsky 
277342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
277442f52ef8SKeith Packard  * we use as a pipe index
277542f52ef8SKeith Packard  */
277686e83e35SChris Wilson static void i8xx_disable_vblank(struct drm_device *dev, unsigned int pipe)
277786e83e35SChris Wilson {
277886e83e35SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
277986e83e35SChris Wilson 	unsigned long irqflags;
278086e83e35SChris Wilson 
278186e83e35SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
278286e83e35SChris Wilson 	i915_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_STATUS);
278386e83e35SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
278486e83e35SChris Wilson }
278586e83e35SChris Wilson 
278686e83e35SChris Wilson static void i965_disable_vblank(struct drm_device *dev, unsigned int pipe)
27870a3e67a4SJesse Barnes {
2788fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2789e9d21d7fSKeith Packard 	unsigned long irqflags;
27900a3e67a4SJesse Barnes 
27911ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27927c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2793755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27941ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27950a3e67a4SJesse Barnes }
27960a3e67a4SJesse Barnes 
279788e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2798f796cf8fSJesse Barnes {
2799fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2800f796cf8fSJesse Barnes 	unsigned long irqflags;
280186e83e35SChris Wilson 	uint32_t bit = INTEL_GEN(dev) >= 7 ?
280286e83e35SChris Wilson 		DE_PIPE_VBLANK_IVB(pipe) : DE_PIPE_VBLANK(pipe);
2803f796cf8fSJesse Barnes 
2804f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2805fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2806b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2807b1f14ad0SJesse Barnes }
2808b1f14ad0SJesse Barnes 
280988e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2810abd58f01SBen Widawsky {
2811fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
2812abd58f01SBen Widawsky 	unsigned long irqflags;
2813abd58f01SBen Widawsky 
2814abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2815013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2816abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2817abd58f01SBen Widawsky }
2818abd58f01SBen Widawsky 
28199107e9d2SChris Wilson static bool
282031bb59ccSChris Wilson ipehr_is_semaphore_wait(struct intel_engine_cs *engine, u32 ipehr)
2821a028c4b0SDaniel Vetter {
282231bb59ccSChris Wilson 	if (INTEL_GEN(engine->i915) >= 8) {
2823a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2824a028c4b0SDaniel Vetter 	} else {
2825a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2826a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2827a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2828a028c4b0SDaniel Vetter 	}
2829a028c4b0SDaniel Vetter }
2830a028c4b0SDaniel Vetter 
2831a4872ba6SOscar Mateo static struct intel_engine_cs *
28320bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28330bc40be8STvrtko Ursulin 				 u64 offset)
2834921d42eaSDaniel Vetter {
2835c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2836a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
28373b3f1650SAkash Goel 	enum intel_engine_id id;
2838921d42eaSDaniel Vetter 
2839c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
28403b3f1650SAkash Goel 		for_each_engine(signaller, dev_priv, id) {
28410bc40be8STvrtko Ursulin 			if (engine == signaller)
2842a6cdb93aSRodrigo Vivi 				continue;
2843a6cdb93aSRodrigo Vivi 
2844348b9b11SChris Wilson 			if (offset == signaller->semaphore.signal_ggtt[engine->hw_id])
2845a6cdb93aSRodrigo Vivi 				return signaller;
2846a6cdb93aSRodrigo Vivi 		}
2847921d42eaSDaniel Vetter 	} else {
2848921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2849921d42eaSDaniel Vetter 
28503b3f1650SAkash Goel 		for_each_engine(signaller, dev_priv, id) {
28510bc40be8STvrtko Ursulin 			if(engine == signaller)
2852921d42eaSDaniel Vetter 				continue;
2853921d42eaSDaniel Vetter 
2854348b9b11SChris Wilson 			if (sync_bits == signaller->semaphore.mbox.wait[engine->hw_id])
2855921d42eaSDaniel Vetter 				return signaller;
2856921d42eaSDaniel Vetter 		}
2857921d42eaSDaniel Vetter 	}
2858921d42eaSDaniel Vetter 
2859348b9b11SChris Wilson 	DRM_DEBUG_DRIVER("No signaller ring found for %s, ipehr 0x%08x, offset 0x%016llx\n",
2860348b9b11SChris Wilson 			 engine->name, ipehr, offset);
2861921d42eaSDaniel Vetter 
286280b5bdbdSChris Wilson 	return ERR_PTR(-ENODEV);
2863921d42eaSDaniel Vetter }
2864921d42eaSDaniel Vetter 
2865a4872ba6SOscar Mateo static struct intel_engine_cs *
28660bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2867a24a11e6SChris Wilson {
2868c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2869406ea8d2SChris Wilson 	void __iomem *vaddr;
287088fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2871a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2872a6cdb93aSRodrigo Vivi 	int i, backwards;
2873a24a11e6SChris Wilson 
2874381e8ae3STomas Elf 	/*
2875381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2876381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2877381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2878381e8ae3STomas Elf 	 * mode.
2879381e8ae3STomas Elf 	 *
2880381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2881381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2882381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2883381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2884381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2885381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2886381e8ae3STomas Elf 	 * the hang checker to deadlock.
2887381e8ae3STomas Elf 	 *
2888381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2889381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2890381e8ae3STomas Elf 	 */
28910bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2892381e8ae3STomas Elf 		return NULL;
2893381e8ae3STomas Elf 
28940bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
289531bb59ccSChris Wilson 	if (!ipehr_is_semaphore_wait(engine, ipehr))
28966274f212SChris Wilson 		return NULL;
2897a24a11e6SChris Wilson 
289888fe429dSDaniel Vetter 	/*
289988fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
290088fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2901a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2902a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
290388fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
290488fe429dSDaniel Vetter 	 * ringbuffer itself.
2905a24a11e6SChris Wilson 	 */
29060bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
2907c033666aSChris Wilson 	backwards = (INTEL_GEN(dev_priv) >= 8) ? 5 : 4;
2908f2f0ed71SChris Wilson 	vaddr = (void __iomem *)engine->buffer->vaddr;
290988fe429dSDaniel Vetter 
2910a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
291188fe429dSDaniel Vetter 		/*
291288fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
291388fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
291488fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
291588fe429dSDaniel Vetter 		 */
29160bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
291788fe429dSDaniel Vetter 
291888fe429dSDaniel Vetter 		/* This here seems to blow up */
2919406ea8d2SChris Wilson 		cmd = ioread32(vaddr + head);
2920a24a11e6SChris Wilson 		if (cmd == ipehr)
2921a24a11e6SChris Wilson 			break;
2922a24a11e6SChris Wilson 
292388fe429dSDaniel Vetter 		head -= 4;
292488fe429dSDaniel Vetter 	}
2925a24a11e6SChris Wilson 
292688fe429dSDaniel Vetter 	if (!i)
292788fe429dSDaniel Vetter 		return NULL;
292888fe429dSDaniel Vetter 
2929406ea8d2SChris Wilson 	*seqno = ioread32(vaddr + head + 4) + 1;
2930c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 8) {
2931406ea8d2SChris Wilson 		offset = ioread32(vaddr + head + 12);
2932a6cdb93aSRodrigo Vivi 		offset <<= 32;
2933406ea8d2SChris Wilson 		offset |= ioread32(vaddr + head + 8);
2934a6cdb93aSRodrigo Vivi 	}
29350bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2936a24a11e6SChris Wilson }
2937a24a11e6SChris Wilson 
29380bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29396274f212SChris Wilson {
2940c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
2941a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2942a0d036b0SChris Wilson 	u32 seqno;
29436274f212SChris Wilson 
29440bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29456274f212SChris Wilson 
29460bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29474be17381SChris Wilson 	if (signaller == NULL)
29484be17381SChris Wilson 		return -1;
29494be17381SChris Wilson 
295080b5bdbdSChris Wilson 	if (IS_ERR(signaller))
295180b5bdbdSChris Wilson 		return 0;
295280b5bdbdSChris Wilson 
29534be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2954666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29556274f212SChris Wilson 		return -1;
29566274f212SChris Wilson 
29571b7744e7SChris Wilson 	if (i915_seqno_passed(intel_engine_get_seqno(signaller), seqno))
29584be17381SChris Wilson 		return 1;
29594be17381SChris Wilson 
2960a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2961a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2962a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29634be17381SChris Wilson 		return -1;
29644be17381SChris Wilson 
29654be17381SChris Wilson 	return 0;
29666274f212SChris Wilson }
29676274f212SChris Wilson 
29686274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29696274f212SChris Wilson {
2970e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
29713b3f1650SAkash Goel 	enum intel_engine_id id;
29726274f212SChris Wilson 
29733b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id)
2974e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
29756274f212SChris Wilson }
29766274f212SChris Wilson 
2977d636951eSBen Widawsky static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
2978d636951eSBen Widawsky {
2979d636951eSBen Widawsky 	u32 tmp = current_instdone | *old_instdone;
2980d636951eSBen Widawsky 	bool unchanged;
2981d636951eSBen Widawsky 
2982d636951eSBen Widawsky 	unchanged = tmp == *old_instdone;
2983d636951eSBen Widawsky 	*old_instdone |= tmp;
2984d636951eSBen Widawsky 
2985d636951eSBen Widawsky 	return unchanged;
2986d636951eSBen Widawsky }
2987d636951eSBen Widawsky 
29880bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
29891ec14ad3SChris Wilson {
2990d636951eSBen Widawsky 	struct drm_i915_private *dev_priv = engine->i915;
2991d636951eSBen Widawsky 	struct intel_instdone instdone;
2992d636951eSBen Widawsky 	struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
299361642ff0SMika Kuoppala 	bool stuck;
2994f9e61372SBen Widawsky 	int slice;
2995f9e61372SBen Widawsky 	int subslice;
29969107e9d2SChris Wilson 
29970bc40be8STvrtko Ursulin 	if (engine->id != RCS)
299861642ff0SMika Kuoppala 		return true;
299961642ff0SMika Kuoppala 
30000e704476SChris Wilson 	intel_engine_get_instdone(engine, &instdone);
300161642ff0SMika Kuoppala 
300261642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
300361642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
300461642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
300561642ff0SMika Kuoppala 	 * consider those as progress.
300661642ff0SMika Kuoppala 	 */
3007d636951eSBen Widawsky 	stuck = instdone_unchanged(instdone.instdone,
3008d636951eSBen Widawsky 				   &accu_instdone->instdone);
3009d636951eSBen Widawsky 	stuck &= instdone_unchanged(instdone.slice_common,
3010d636951eSBen Widawsky 				    &accu_instdone->slice_common);
3011f9e61372SBen Widawsky 
3012f9e61372SBen Widawsky 	for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
3013f9e61372SBen Widawsky 		stuck &= instdone_unchanged(instdone.sampler[slice][subslice],
3014f9e61372SBen Widawsky 					    &accu_instdone->sampler[slice][subslice]);
3015f9e61372SBen Widawsky 		stuck &= instdone_unchanged(instdone.row[slice][subslice],
3016f9e61372SBen Widawsky 					    &accu_instdone->row[slice][subslice]);
3017f9e61372SBen Widawsky 	}
301861642ff0SMika Kuoppala 
301961642ff0SMika Kuoppala 	return stuck;
302061642ff0SMika Kuoppala }
302161642ff0SMika Kuoppala 
30227e37f889SChris Wilson static enum intel_engine_hangcheck_action
30230bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
302461642ff0SMika Kuoppala {
30250bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
302661642ff0SMika Kuoppala 
302761642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
3028d636951eSBen Widawsky 		memset(&engine->hangcheck.instdone, 0,
30290bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
303061642ff0SMika Kuoppala 
3031f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
3032f260fe7bSMika Kuoppala 	}
3033f260fe7bSMika Kuoppala 
30340bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
303561642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
303661642ff0SMika Kuoppala 
303761642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
303861642ff0SMika Kuoppala }
303961642ff0SMika Kuoppala 
30407e37f889SChris Wilson static enum intel_engine_hangcheck_action
30417e37f889SChris Wilson engine_stuck(struct intel_engine_cs *engine, u64 acthd)
304261642ff0SMika Kuoppala {
3043c033666aSChris Wilson 	struct drm_i915_private *dev_priv = engine->i915;
30447e37f889SChris Wilson 	enum intel_engine_hangcheck_action ha;
304561642ff0SMika Kuoppala 	u32 tmp;
304661642ff0SMika Kuoppala 
30470bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
304861642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
304961642ff0SMika Kuoppala 		return ha;
305061642ff0SMika Kuoppala 
3051c033666aSChris Wilson 	if (IS_GEN2(dev_priv))
3052f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30539107e9d2SChris Wilson 
30549107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30559107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30569107e9d2SChris Wilson 	 * and break the hang. This should work on
30579107e9d2SChris Wilson 	 * all but the second generation chipsets.
30589107e9d2SChris Wilson 	 */
30590bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30601ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
3061c033666aSChris Wilson 		i915_handle_error(dev_priv, 0,
306258174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30630bc40be8STvrtko Ursulin 				  engine->name);
30640bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3065f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30661ec14ad3SChris Wilson 	}
3067a24a11e6SChris Wilson 
3068c033666aSChris Wilson 	if (INTEL_GEN(dev_priv) >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30690bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
30706274f212SChris Wilson 		default:
3071f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
30726274f212SChris Wilson 		case 1:
3073c033666aSChris Wilson 			i915_handle_error(dev_priv, 0,
307458174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
30750bc40be8STvrtko Ursulin 					  engine->name);
30760bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3077f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
30786274f212SChris Wilson 		case 0:
3079f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
30806274f212SChris Wilson 		}
30819107e9d2SChris Wilson 	}
30829107e9d2SChris Wilson 
3083f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3084a24a11e6SChris Wilson }
3085d1e61e7fSChris Wilson 
3086737b1506SChris Wilson /*
3087f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
308805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
308905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
309005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
309105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
309205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3093f65d9421SBen Gamari  */
3094737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3095f65d9421SBen Gamari {
3096737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3097737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3098737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3099e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
31003b3f1650SAkash Goel 	enum intel_engine_id id;
31012b284288SChris Wilson 	unsigned int hung = 0, stuck = 0;
31022b284288SChris Wilson 	int busy_count = 0;
31039107e9d2SChris Wilson #define BUSY 1
31049107e9d2SChris Wilson #define KICK 5
31059107e9d2SChris Wilson #define HUNG 20
310624a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3107893eead0SChris Wilson 
3108d330a953SJani Nikula 	if (!i915.enable_hangcheck)
31093e0dc6b0SBen Widawsky 		return;
31103e0dc6b0SBen Widawsky 
3111b1379d49SChris Wilson 	if (!READ_ONCE(dev_priv->gt.awake))
311267d97da3SChris Wilson 		return;
31131f814dacSImre Deak 
311475714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
311575714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
311675714940SMika Kuoppala 	 * any invalid access.
311775714940SMika Kuoppala 	 */
311875714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
311975714940SMika Kuoppala 
31203b3f1650SAkash Goel 	for_each_engine(engine, dev_priv, id) {
3121688e6c72SChris Wilson 		bool busy = intel_engine_has_waiter(engine);
312250877445SChris Wilson 		u64 acthd;
312350877445SChris Wilson 		u32 seqno;
312434730fedSChris Wilson 		u32 submit;
3125b4519513SChris Wilson 
31266274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31276274f212SChris Wilson 
3128c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3129c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3130c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3131c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3132c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3133c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3134c04e0f3bSChris Wilson 		 */
3135c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3136c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3137c04e0f3bSChris Wilson 
31387e37f889SChris Wilson 		acthd = intel_engine_get_active_head(engine);
31391b7744e7SChris Wilson 		seqno = intel_engine_get_seqno(engine);
314034730fedSChris Wilson 		submit = READ_ONCE(engine->last_submitted_seqno);
314105407ff8SMika Kuoppala 
3142e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
314334730fedSChris Wilson 			if (i915_seqno_passed(seqno, submit)) {
3144e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
314505407ff8SMika Kuoppala 			} else {
31466274f212SChris Wilson 				/* We always increment the hangcheck score
31479930ca1aSChris Wilson 				 * if the engine is busy and still processing
31486274f212SChris Wilson 				 * the same request, so that no single request
31496274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31506274f212SChris Wilson 				 * batches). The only time we do not increment
31516274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31529930ca1aSChris Wilson 				 * engine is in a legitimate wait for another
31539930ca1aSChris Wilson 				 * engine. In that case the waiting engine is a
31546274f212SChris Wilson 				 * victim and we want to be sure we catch the
31556274f212SChris Wilson 				 * right culprit. Then every time we do kick
31566274f212SChris Wilson 				 * the ring, add a small increment to the
31576274f212SChris Wilson 				 * score so that we can catch a batch that is
31586274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31596274f212SChris Wilson 				 * for stalling the machine.
31609107e9d2SChris Wilson 				 */
31617e37f889SChris Wilson 				engine->hangcheck.action =
31627e37f889SChris Wilson 					engine_stuck(engine, acthd);
3163ad8beaeaSMika Kuoppala 
3164e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3165da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3166f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3167f260fe7bSMika Kuoppala 					break;
316824a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3169e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31706274f212SChris Wilson 					break;
3171f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3172e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
31736274f212SChris Wilson 					break;
3174f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3175e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
31766274f212SChris Wilson 					break;
31776274f212SChris Wilson 				}
317805407ff8SMika Kuoppala 			}
31792b284288SChris Wilson 
31802b284288SChris Wilson 			if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
31812b284288SChris Wilson 				hung |= intel_engine_flag(engine);
31822b284288SChris Wilson 				if (engine->hangcheck.action != HANGCHECK_HUNG)
31832b284288SChris Wilson 					stuck |= intel_engine_flag(engine);
31842b284288SChris Wilson 			}
31859107e9d2SChris Wilson 		} else {
3186e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3187da661464SMika Kuoppala 
31889107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
31899107e9d2SChris Wilson 			 * attempts across multiple batches.
31909107e9d2SChris Wilson 			 */
3191e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3192e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3193e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3194e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3195f260fe7bSMika Kuoppala 
319661642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
319712471ba8SChris Wilson 			acthd = 0;
319861642ff0SMika Kuoppala 
3199d636951eSBen Widawsky 			memset(&engine->hangcheck.instdone, 0,
3200e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3201cbb465e7SChris Wilson 		}
3202f65d9421SBen Gamari 
3203e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3204e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
32059107e9d2SChris Wilson 		busy_count += busy;
320605407ff8SMika Kuoppala 	}
320705407ff8SMika Kuoppala 
32082b284288SChris Wilson 	if (hung) {
32092b284288SChris Wilson 		char msg[80];
3210bafb0fceSChris Wilson 		unsigned int tmp;
32112b284288SChris Wilson 		int len;
321205407ff8SMika Kuoppala 
32132b284288SChris Wilson 		/* If some rings hung but others were still busy, only
32142b284288SChris Wilson 		 * blame the hanging rings in the synopsis.
32152b284288SChris Wilson 		 */
32162b284288SChris Wilson 		if (stuck != hung)
32172b284288SChris Wilson 			hung &= ~stuck;
32182b284288SChris Wilson 		len = scnprintf(msg, sizeof(msg),
32192b284288SChris Wilson 				"%s on ", stuck == hung ? "No progress" : "Hang");
3220bafb0fceSChris Wilson 		for_each_engine_masked(engine, dev_priv, hung, tmp)
32212b284288SChris Wilson 			len += scnprintf(msg + len, sizeof(msg) - len,
32222b284288SChris Wilson 					 "%s, ", engine->name);
32232b284288SChris Wilson 		msg[len-2] = '\0';
32242b284288SChris Wilson 
32252b284288SChris Wilson 		return i915_handle_error(dev_priv, hung, msg);
32262b284288SChris Wilson 	}
322705407ff8SMika Kuoppala 
322805535726SChris Wilson 	/* Reset timer in case GPU hangs without another request being added */
322905407ff8SMika Kuoppala 	if (busy_count)
3230c033666aSChris Wilson 		i915_queue_hangcheck(dev_priv);
323110cd45b6SMika Kuoppala }
323210cd45b6SMika Kuoppala 
32331c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
323491738a95SPaulo Zanoni {
3235fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
323691738a95SPaulo Zanoni 
3237*6e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
323891738a95SPaulo Zanoni 		return;
323991738a95SPaulo Zanoni 
3240f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3241105b122eSPaulo Zanoni 
3242*6e266956STvrtko Ursulin 	if (HAS_PCH_CPT(dev_priv) || HAS_PCH_LPT(dev_priv))
3243105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3244622364b6SPaulo Zanoni }
3245105b122eSPaulo Zanoni 
324691738a95SPaulo Zanoni /*
3247622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3248622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3249622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3250622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3251622364b6SPaulo Zanoni  *
3252622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
325391738a95SPaulo Zanoni  */
3254622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3255622364b6SPaulo Zanoni {
3256fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3257622364b6SPaulo Zanoni 
3258*6e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3259622364b6SPaulo Zanoni 		return;
3260622364b6SPaulo Zanoni 
3261622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
326291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
326391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
326491738a95SPaulo Zanoni }
326591738a95SPaulo Zanoni 
32667c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3267d18ea1b5SDaniel Vetter {
3268fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3269d18ea1b5SDaniel Vetter 
3270f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3271a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3272f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3273d18ea1b5SDaniel Vetter }
3274d18ea1b5SDaniel Vetter 
327570591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
327670591a41SVille Syrjälä {
327770591a41SVille Syrjälä 	enum pipe pipe;
327870591a41SVille Syrjälä 
327971b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
328071b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
328171b8b41dSVille Syrjälä 	else
328271b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
328371b8b41dSVille Syrjälä 
3284ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
328570591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
328670591a41SVille Syrjälä 
3287ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3288ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3289ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3290ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3291ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3292ad22d106SVille Syrjälä 	}
329370591a41SVille Syrjälä 
329470591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3295ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
329670591a41SVille Syrjälä }
329770591a41SVille Syrjälä 
32988bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32998bb61306SVille Syrjälä {
33008bb61306SVille Syrjälä 	u32 pipestat_mask;
33019ab981f2SVille Syrjälä 	u32 enable_mask;
33028bb61306SVille Syrjälä 	enum pipe pipe;
33038bb61306SVille Syrjälä 
33048bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
33058bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
33068bb61306SVille Syrjälä 
33078bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
33088bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
33098bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
33108bb61306SVille Syrjälä 
33119ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
33128bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33138bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
33148bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
33159ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
33166b7eafc1SVille Syrjälä 
33176b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
33186b7eafc1SVille Syrjälä 
33199ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33208bb61306SVille Syrjälä 
33219ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33228bb61306SVille Syrjälä }
33238bb61306SVille Syrjälä 
33248bb61306SVille Syrjälä /* drm_dma.h hooks
33258bb61306SVille Syrjälä */
33268bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33278bb61306SVille Syrjälä {
3328fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33298bb61306SVille Syrjälä 
33308bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
33318bb61306SVille Syrjälä 
33328bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
33338bb61306SVille Syrjälä 	if (IS_GEN7(dev))
33348bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33358bb61306SVille Syrjälä 
33368bb61306SVille Syrjälä 	gen5_gt_irq_reset(dev);
33378bb61306SVille Syrjälä 
33388bb61306SVille Syrjälä 	ibx_irq_reset(dev);
33398bb61306SVille Syrjälä }
33408bb61306SVille Syrjälä 
33417e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
33427e231dbeSJesse Barnes {
3343fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
33447e231dbeSJesse Barnes 
334534c7b8a7SVille Syrjälä 	I915_WRITE(VLV_MASTER_IER, 0);
334634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
334734c7b8a7SVille Syrjälä 
33487c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33497e231dbeSJesse Barnes 
3350ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33519918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
335270591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3353ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33547e231dbeSJesse Barnes }
33557e231dbeSJesse Barnes 
3356d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3357d6e3cca3SDaniel Vetter {
3358d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3359d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3360d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3361d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3362d6e3cca3SDaniel Vetter }
3363d6e3cca3SDaniel Vetter 
3364823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3365abd58f01SBen Widawsky {
3366fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3367abd58f01SBen Widawsky 	int pipe;
3368abd58f01SBen Widawsky 
3369abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3370abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3371abd58f01SBen Widawsky 
3372d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3373abd58f01SBen Widawsky 
3374055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3375f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3376813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3377f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3378abd58f01SBen Widawsky 
3379f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3380f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3381f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3382abd58f01SBen Widawsky 
3383*6e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
33841c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3385abd58f01SBen Widawsky }
3386abd58f01SBen Widawsky 
33874c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
33884c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3389d49bdb0eSPaulo Zanoni {
33901180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33916831f3e3SVille Syrjälä 	enum pipe pipe;
3392d49bdb0eSPaulo Zanoni 
339313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
33946831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33956831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
33966831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
33976831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
339813321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3399d49bdb0eSPaulo Zanoni }
3400d49bdb0eSPaulo Zanoni 
3401aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3402aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3403aae8ba84SVille Syrjälä {
34046831f3e3SVille Syrjälä 	enum pipe pipe;
34056831f3e3SVille Syrjälä 
3406aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34076831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34086831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3409aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3410aae8ba84SVille Syrjälä 
3411aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
341291c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
3413aae8ba84SVille Syrjälä }
3414aae8ba84SVille Syrjälä 
341543f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
341643f328d7SVille Syrjälä {
3417fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
341843f328d7SVille Syrjälä 
341943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
342043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
342143f328d7SVille Syrjälä 
3422d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
342343f328d7SVille Syrjälä 
342443f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
342543f328d7SVille Syrjälä 
3426ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34279918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
342870591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3429ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
343043f328d7SVille Syrjälä }
343143f328d7SVille Syrjälä 
343291d14251STvrtko Ursulin static u32 intel_hpd_enabled_irqs(struct drm_i915_private *dev_priv,
343387a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
343487a02106SVille Syrjälä {
343587a02106SVille Syrjälä 	struct intel_encoder *encoder;
343687a02106SVille Syrjälä 	u32 enabled_irqs = 0;
343787a02106SVille Syrjälä 
343891c8a326SChris Wilson 	for_each_intel_encoder(&dev_priv->drm, encoder)
343987a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
344087a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
344187a02106SVille Syrjälä 
344287a02106SVille Syrjälä 	return enabled_irqs;
344387a02106SVille Syrjälä }
344487a02106SVille Syrjälä 
344591d14251STvrtko Ursulin static void ibx_hpd_irq_setup(struct drm_i915_private *dev_priv)
344682a28bcfSDaniel Vetter {
344787a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
344882a28bcfSDaniel Vetter 
344991d14251STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv)) {
3450fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
345191d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ibx);
345282a28bcfSDaniel Vetter 	} else {
3453fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
345491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_cpt);
345582a28bcfSDaniel Vetter 	}
345682a28bcfSDaniel Vetter 
3457fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
345882a28bcfSDaniel Vetter 
34597fe0b973SKeith Packard 	/*
34607fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34616dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34626dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34637fe0b973SKeith Packard 	 */
34647fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34657fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34667fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34677fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34687fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34690b2eb33eSVille Syrjälä 	/*
34700b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
34710b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
34720b2eb33eSVille Syrjälä 	 */
347391d14251STvrtko Ursulin 	if (HAS_PCH_LPT_LP(dev_priv))
34740b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
34757fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34766dbf30ceSVille Syrjälä }
347726951cafSXiong Zhang 
347891d14251STvrtko Ursulin static void spt_hpd_irq_setup(struct drm_i915_private *dev_priv)
34796dbf30ceSVille Syrjälä {
34806dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
34816dbf30ceSVille Syrjälä 
34826dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
348391d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_spt);
34846dbf30ceSVille Syrjälä 
34856dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34866dbf30ceSVille Syrjälä 
34876dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
34886dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34896dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
349074c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
34916dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34926dbf30ceSVille Syrjälä 
349326951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
349426951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
349526951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
349626951cafSXiong Zhang }
34977fe0b973SKeith Packard 
349891d14251STvrtko Ursulin static void ilk_hpd_irq_setup(struct drm_i915_private *dev_priv)
3499e4ce95aaSVille Syrjälä {
3500e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3501e4ce95aaSVille Syrjälä 
350291d14251STvrtko Ursulin 	if (INTEL_GEN(dev_priv) >= 8) {
35033a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
350491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bdw);
35053a3b3c7dSVille Syrjälä 
35063a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
350791d14251STvrtko Ursulin 	} else if (INTEL_GEN(dev_priv) >= 7) {
350823bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
350991d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ivb);
35103a3b3c7dSVille Syrjälä 
35113a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
351223bb4cb5SVille Syrjälä 	} else {
3513e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
351491d14251STvrtko Ursulin 		enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_ilk);
3515e4ce95aaSVille Syrjälä 
3516e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35173a3b3c7dSVille Syrjälä 	}
3518e4ce95aaSVille Syrjälä 
3519e4ce95aaSVille Syrjälä 	/*
3520e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3521e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
352223bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3523e4ce95aaSVille Syrjälä 	 */
3524e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3525e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3526e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3527e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3528e4ce95aaSVille Syrjälä 
352991d14251STvrtko Ursulin 	ibx_hpd_irq_setup(dev_priv);
3530e4ce95aaSVille Syrjälä }
3531e4ce95aaSVille Syrjälä 
353291d14251STvrtko Ursulin static void bxt_hpd_irq_setup(struct drm_i915_private *dev_priv)
3533e0a20ad7SShashank Sharma {
3534a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3535e0a20ad7SShashank Sharma 
353691d14251STvrtko Ursulin 	enabled_irqs = intel_hpd_enabled_irqs(dev_priv, hpd_bxt);
3537a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3538e0a20ad7SShashank Sharma 
3539a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3540e0a20ad7SShashank Sharma 
3541a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3542a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3543a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3544d252bf68SShubhangi Shrivastava 
3545d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3546d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3547d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3548d252bf68SShubhangi Shrivastava 
3549d252bf68SShubhangi Shrivastava 	/*
3550d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3551d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3552d252bf68SShubhangi Shrivastava 	 */
3553d252bf68SShubhangi Shrivastava 
3554d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3555d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3556d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3557d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3558d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3559d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3560d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3561d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3562d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3563d252bf68SShubhangi Shrivastava 
3564a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3565e0a20ad7SShashank Sharma }
3566e0a20ad7SShashank Sharma 
3567d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3568d46da437SPaulo Zanoni {
3569fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
357082a28bcfSDaniel Vetter 	u32 mask;
3571d46da437SPaulo Zanoni 
3572*6e266956STvrtko Ursulin 	if (HAS_PCH_NOP(dev_priv))
3573692a04cfSDaniel Vetter 		return;
3574692a04cfSDaniel Vetter 
3575*6e266956STvrtko Ursulin 	if (HAS_PCH_IBX(dev_priv))
35765c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3577105b122eSPaulo Zanoni 	else
35785c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35798664281bSPaulo Zanoni 
3580b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3581d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3582d46da437SPaulo Zanoni }
3583d46da437SPaulo Zanoni 
35840a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35850a9a8c91SDaniel Vetter {
3586fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
35870a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
35880a9a8c91SDaniel Vetter 
35890a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
35900a9a8c91SDaniel Vetter 
35910a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3592040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
35930a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
359435a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
359535a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
35960a9a8c91SDaniel Vetter 	}
35970a9a8c91SDaniel Vetter 
35980a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
35990a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
3600f8973c21SChris Wilson 		gt_irqs |= ILK_BSD_USER_INTERRUPT;
36010a9a8c91SDaniel Vetter 	} else {
36020a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36030a9a8c91SDaniel Vetter 	}
36040a9a8c91SDaniel Vetter 
360535079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36060a9a8c91SDaniel Vetter 
36070a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
360878e68d36SImre Deak 		/*
360978e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
361078e68d36SImre Deak 		 * itself is enabled/disabled.
361178e68d36SImre Deak 		 */
36120a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
36130a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
36140a9a8c91SDaniel Vetter 
3615605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
361635079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
36170a9a8c91SDaniel Vetter 	}
36180a9a8c91SDaniel Vetter }
36190a9a8c91SDaniel Vetter 
3620f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3621036a4a7dSZhenyu Wang {
3622fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
36238e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36248e76f8dcSPaulo Zanoni 
36258e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36268e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36278e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36288e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36295c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36308e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
363123bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
363223bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36338e76f8dcSPaulo Zanoni 	} else {
36348e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3635ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36365b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36375b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36385b3a856bSDaniel Vetter 				DE_POISON);
3639e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3640e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3641e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36428e76f8dcSPaulo Zanoni 	}
3643036a4a7dSZhenyu Wang 
36441ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3645036a4a7dSZhenyu Wang 
36460c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36470c841212SPaulo Zanoni 
3648622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3649622364b6SPaulo Zanoni 
365035079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3651036a4a7dSZhenyu Wang 
36520a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3653036a4a7dSZhenyu Wang 
3654d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36557fe0b973SKeith Packard 
3656f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36576005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36586005ce42SDaniel Vetter 		 *
36596005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36604bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36614bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3662d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3663fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3664d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3665f97108d1SJesse Barnes 	}
3666f97108d1SJesse Barnes 
3667036a4a7dSZhenyu Wang 	return 0;
3668036a4a7dSZhenyu Wang }
3669036a4a7dSZhenyu Wang 
3670f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3671f8b79e58SImre Deak {
3672f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3673f8b79e58SImre Deak 
3674f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3675f8b79e58SImre Deak 		return;
3676f8b79e58SImre Deak 
3677f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3678f8b79e58SImre Deak 
3679d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3680d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3681ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3682f8b79e58SImre Deak 	}
3683d6c69803SVille Syrjälä }
3684f8b79e58SImre Deak 
3685f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3686f8b79e58SImre Deak {
3687f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3688f8b79e58SImre Deak 
3689f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3690f8b79e58SImre Deak 		return;
3691f8b79e58SImre Deak 
3692f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3693f8b79e58SImre Deak 
3694950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3695ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3696f8b79e58SImre Deak }
3697f8b79e58SImre Deak 
36980e6c9a9eSVille Syrjälä 
36990e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37000e6c9a9eSVille Syrjälä {
3701fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
37020e6c9a9eSVille Syrjälä 
37030a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37047e231dbeSJesse Barnes 
3705ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37069918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3707ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3708ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3709ad22d106SVille Syrjälä 
37107e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
371134c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
371220afbda2SDaniel Vetter 
371320afbda2SDaniel Vetter 	return 0;
371420afbda2SDaniel Vetter }
371520afbda2SDaniel Vetter 
3716abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3717abd58f01SBen Widawsky {
3718abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3719abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3720abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
372173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
372273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
372373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3724abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
372573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
372673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
372773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3728abd58f01SBen Widawsky 		0,
372973d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
373073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3731abd58f01SBen Widawsky 		};
3732abd58f01SBen Widawsky 
373398735739STvrtko Ursulin 	if (HAS_L3_DPF(dev_priv))
373498735739STvrtko Ursulin 		gt_interrupts[0] |= GT_RENDER_L3_PARITY_ERROR_INTERRUPT;
373598735739STvrtko Ursulin 
37360961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
37379a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37389a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
373978e68d36SImre Deak 	/*
374078e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
374178e68d36SImre Deak 	 * is enabled/disabled.
374278e68d36SImre Deak 	 */
374378e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
37449a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3745abd58f01SBen Widawsky }
3746abd58f01SBen Widawsky 
3747abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3748abd58f01SBen Widawsky {
3749770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3750770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37513a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37523a3b3c7dSVille Syrjälä 	u32 de_port_enables;
375311825b0dSVille Syrjälä 	u32 de_misc_masked = GEN8_DE_MISC_GSE;
37543a3b3c7dSVille Syrjälä 	enum pipe pipe;
3755770de83dSDamien Lespiau 
3756b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3757770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3758770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37593a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
376088e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37619e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37623a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37633a3b3c7dSVille Syrjälä 	} else {
3764770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3765770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37663a3b3c7dSVille Syrjälä 	}
3767770de83dSDamien Lespiau 
3768770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3769770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3770770de83dSDamien Lespiau 
37713a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3772a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3773a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3774a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
37753a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
37763a3b3c7dSVille Syrjälä 
377713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
377813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
377913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3780abd58f01SBen Widawsky 
3781055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3782f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3783813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3784813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3785813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
378635079899SPaulo Zanoni 					  de_pipe_enables);
3787abd58f01SBen Widawsky 
37883a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
378911825b0dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_MISC_, ~de_misc_masked, de_misc_masked);
3790abd58f01SBen Widawsky }
3791abd58f01SBen Widawsky 
3792abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3793abd58f01SBen Widawsky {
3794fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3795abd58f01SBen Widawsky 
3796*6e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3797622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3798622364b6SPaulo Zanoni 
3799abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3800abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3801abd58f01SBen Widawsky 
3802*6e266956STvrtko Ursulin 	if (HAS_PCH_SPLIT(dev_priv))
3803abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3804abd58f01SBen Widawsky 
3805e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
3806abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3807abd58f01SBen Widawsky 
3808abd58f01SBen Widawsky 	return 0;
3809abd58f01SBen Widawsky }
3810abd58f01SBen Widawsky 
381143f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
381243f328d7SVille Syrjälä {
3813fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
381443f328d7SVille Syrjälä 
381543f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
381643f328d7SVille Syrjälä 
3817ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38189918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3819ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3820ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3821ad22d106SVille Syrjälä 
3822e5328c43SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
382343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
382443f328d7SVille Syrjälä 
382543f328d7SVille Syrjälä 	return 0;
382643f328d7SVille Syrjälä }
382743f328d7SVille Syrjälä 
3828abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3829abd58f01SBen Widawsky {
3830fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3831abd58f01SBen Widawsky 
3832abd58f01SBen Widawsky 	if (!dev_priv)
3833abd58f01SBen Widawsky 		return;
3834abd58f01SBen Widawsky 
3835823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3836abd58f01SBen Widawsky }
3837abd58f01SBen Widawsky 
38387e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38397e231dbeSJesse Barnes {
3840fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38417e231dbeSJesse Barnes 
38427e231dbeSJesse Barnes 	if (!dev_priv)
38437e231dbeSJesse Barnes 		return;
38447e231dbeSJesse Barnes 
3845843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
384634c7b8a7SVille Syrjälä 	POSTING_READ(VLV_MASTER_IER);
3847843d0e7dSImre Deak 
3848893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3849893fce8eSVille Syrjälä 
38507e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3851f8b79e58SImre Deak 
3852ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38539918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3854ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3855ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38567e231dbeSJesse Barnes }
38577e231dbeSJesse Barnes 
385843f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
385943f328d7SVille Syrjälä {
3860fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
386143f328d7SVille Syrjälä 
386243f328d7SVille Syrjälä 	if (!dev_priv)
386343f328d7SVille Syrjälä 		return;
386443f328d7SVille Syrjälä 
386543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
386643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
386743f328d7SVille Syrjälä 
3868a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
386943f328d7SVille Syrjälä 
3870a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
387143f328d7SVille Syrjälä 
3872ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38739918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3874ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3875ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
387643f328d7SVille Syrjälä }
387743f328d7SVille Syrjälä 
3878f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3879036a4a7dSZhenyu Wang {
3880fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
38814697995bSJesse Barnes 
38824697995bSJesse Barnes 	if (!dev_priv)
38834697995bSJesse Barnes 		return;
38844697995bSJesse Barnes 
3885be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3886036a4a7dSZhenyu Wang }
3887036a4a7dSZhenyu Wang 
3888c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3889c2798b19SChris Wilson {
3890fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3891c2798b19SChris Wilson 	int pipe;
3892c2798b19SChris Wilson 
3893055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3894c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3895c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3896c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3897c2798b19SChris Wilson 	POSTING_READ16(IER);
3898c2798b19SChris Wilson }
3899c2798b19SChris Wilson 
3900c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3901c2798b19SChris Wilson {
3902fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3903c2798b19SChris Wilson 
3904c2798b19SChris Wilson 	I915_WRITE16(EMR,
3905c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3906c2798b19SChris Wilson 
3907c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3908c2798b19SChris Wilson 	dev_priv->irq_mask =
3909c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3910c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3911c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
391237ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3913c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3914c2798b19SChris Wilson 
3915c2798b19SChris Wilson 	I915_WRITE16(IER,
3916c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3917c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3918c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3919c2798b19SChris Wilson 	POSTING_READ16(IER);
3920c2798b19SChris Wilson 
3921379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3922379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3923d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3924755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3925755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3926d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3927379ef82dSDaniel Vetter 
3928c2798b19SChris Wilson 	return 0;
3929c2798b19SChris Wilson }
3930c2798b19SChris Wilson 
39315a21b665SDaniel Vetter /*
39325a21b665SDaniel Vetter  * Returns true when a page flip has completed.
39335a21b665SDaniel Vetter  */
39345a21b665SDaniel Vetter static bool i8xx_handle_vblank(struct drm_i915_private *dev_priv,
39355a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
39365a21b665SDaniel Vetter {
39375a21b665SDaniel Vetter 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
39385a21b665SDaniel Vetter 
39395a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
39405a21b665SDaniel Vetter 		return false;
39415a21b665SDaniel Vetter 
39425a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
39435a21b665SDaniel Vetter 		goto check_page_flip;
39445a21b665SDaniel Vetter 
39455a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
39465a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
39475a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
39485a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
39495a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
39505a21b665SDaniel Vetter 	 */
39515a21b665SDaniel Vetter 	if (I915_READ16(ISR) & flip_pending)
39525a21b665SDaniel Vetter 		goto check_page_flip;
39535a21b665SDaniel Vetter 
39545a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
39555a21b665SDaniel Vetter 	return true;
39565a21b665SDaniel Vetter 
39575a21b665SDaniel Vetter check_page_flip:
39585a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
39595a21b665SDaniel Vetter 	return false;
39605a21b665SDaniel Vetter }
39615a21b665SDaniel Vetter 
3962ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3963c2798b19SChris Wilson {
396445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
3965fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
3966c2798b19SChris Wilson 	u16 iir, new_iir;
3967c2798b19SChris Wilson 	u32 pipe_stats[2];
3968c2798b19SChris Wilson 	int pipe;
3969c2798b19SChris Wilson 	u16 flip_mask =
3970c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3971c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
39721f814dacSImre Deak 	irqreturn_t ret;
3973c2798b19SChris Wilson 
39742dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39752dd2a883SImre Deak 		return IRQ_NONE;
39762dd2a883SImre Deak 
39771f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39781f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39791f814dacSImre Deak 
39801f814dacSImre Deak 	ret = IRQ_NONE;
3981c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3982c2798b19SChris Wilson 	if (iir == 0)
39831f814dacSImre Deak 		goto out;
3984c2798b19SChris Wilson 
3985c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3986c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3987c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3988c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3989c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3990c2798b19SChris Wilson 		 */
3991222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3992c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3993aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3994c2798b19SChris Wilson 
3995055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3996f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3997c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3998c2798b19SChris Wilson 
3999c2798b19SChris Wilson 			/*
4000c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4001c2798b19SChris Wilson 			 */
40022d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4003c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4004c2798b19SChris Wilson 		}
4005222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4006c2798b19SChris Wilson 
4007c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4008c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4009c2798b19SChris Wilson 
4010c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40113b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4012c2798b19SChris Wilson 
4013055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40145a21b665SDaniel Vetter 			int plane = pipe;
40155a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
40165a21b665SDaniel Vetter 				plane = !plane;
40175a21b665SDaniel Vetter 
40185a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40195a21b665SDaniel Vetter 			    i8xx_handle_vblank(dev_priv, plane, pipe, iir))
40205a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4021c2798b19SChris Wilson 
40224356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
402391d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
40242d9d2b0bSVille Syrjälä 
40251f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40261f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40271f7247c0SDaniel Vetter 								    pipe);
40284356d586SDaniel Vetter 		}
4029c2798b19SChris Wilson 
4030c2798b19SChris Wilson 		iir = new_iir;
4031c2798b19SChris Wilson 	}
40321f814dacSImre Deak 	ret = IRQ_HANDLED;
4033c2798b19SChris Wilson 
40341f814dacSImre Deak out:
40351f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40361f814dacSImre Deak 
40371f814dacSImre Deak 	return ret;
4038c2798b19SChris Wilson }
4039c2798b19SChris Wilson 
4040c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4041c2798b19SChris Wilson {
4042fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4043c2798b19SChris Wilson 	int pipe;
4044c2798b19SChris Wilson 
4045055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4046c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4047c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4048c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4049c2798b19SChris Wilson 	}
4050c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4051c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4052c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4053c2798b19SChris Wilson }
4054c2798b19SChris Wilson 
4055a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4056a266c7d5SChris Wilson {
4057fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4058a266c7d5SChris Wilson 	int pipe;
4059a266c7d5SChris Wilson 
4060a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40610706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4062a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4063a266c7d5SChris Wilson 	}
4064a266c7d5SChris Wilson 
406500d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4066055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4067a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4068a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4069a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4070a266c7d5SChris Wilson 	POSTING_READ(IER);
4071a266c7d5SChris Wilson }
4072a266c7d5SChris Wilson 
4073a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4074a266c7d5SChris Wilson {
4075fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
407638bde180SChris Wilson 	u32 enable_mask;
4077a266c7d5SChris Wilson 
407838bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
407938bde180SChris Wilson 
408038bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
408138bde180SChris Wilson 	dev_priv->irq_mask =
408238bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
408338bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
408438bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
408538bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
408637ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
408738bde180SChris Wilson 
408838bde180SChris Wilson 	enable_mask =
408938bde180SChris Wilson 		I915_ASLE_INTERRUPT |
409038bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
409138bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
409238bde180SChris Wilson 		I915_USER_INTERRUPT;
409338bde180SChris Wilson 
4094a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40950706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
409620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
409720afbda2SDaniel Vetter 
4098a266c7d5SChris Wilson 		/* Enable in IER... */
4099a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4100a266c7d5SChris Wilson 		/* and unmask in IMR */
4101a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4102a266c7d5SChris Wilson 	}
4103a266c7d5SChris Wilson 
4104a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4105a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4106a266c7d5SChris Wilson 	POSTING_READ(IER);
4107a266c7d5SChris Wilson 
410891d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
410920afbda2SDaniel Vetter 
4110379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4111379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4112d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4113755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4114755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4115d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4116379ef82dSDaniel Vetter 
411720afbda2SDaniel Vetter 	return 0;
411820afbda2SDaniel Vetter }
411920afbda2SDaniel Vetter 
41205a21b665SDaniel Vetter /*
41215a21b665SDaniel Vetter  * Returns true when a page flip has completed.
41225a21b665SDaniel Vetter  */
41235a21b665SDaniel Vetter static bool i915_handle_vblank(struct drm_i915_private *dev_priv,
41245a21b665SDaniel Vetter 			       int plane, int pipe, u32 iir)
41255a21b665SDaniel Vetter {
41265a21b665SDaniel Vetter 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
41275a21b665SDaniel Vetter 
41285a21b665SDaniel Vetter 	if (!intel_pipe_handle_vblank(dev_priv, pipe))
41295a21b665SDaniel Vetter 		return false;
41305a21b665SDaniel Vetter 
41315a21b665SDaniel Vetter 	if ((iir & flip_pending) == 0)
41325a21b665SDaniel Vetter 		goto check_page_flip;
41335a21b665SDaniel Vetter 
41345a21b665SDaniel Vetter 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
41355a21b665SDaniel Vetter 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
41365a21b665SDaniel Vetter 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
41375a21b665SDaniel Vetter 	 * the flip is completed (no longer pending). Since this doesn't raise
41385a21b665SDaniel Vetter 	 * an interrupt per se, we watch for the change at vblank.
41395a21b665SDaniel Vetter 	 */
41405a21b665SDaniel Vetter 	if (I915_READ(ISR) & flip_pending)
41415a21b665SDaniel Vetter 		goto check_page_flip;
41425a21b665SDaniel Vetter 
41435a21b665SDaniel Vetter 	intel_finish_page_flip_cs(dev_priv, pipe);
41445a21b665SDaniel Vetter 	return true;
41455a21b665SDaniel Vetter 
41465a21b665SDaniel Vetter check_page_flip:
41475a21b665SDaniel Vetter 	intel_check_page_flip(dev_priv, pipe);
41485a21b665SDaniel Vetter 	return false;
41495a21b665SDaniel Vetter }
41505a21b665SDaniel Vetter 
4151ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4152a266c7d5SChris Wilson {
415345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4154fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
41558291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
415638bde180SChris Wilson 	u32 flip_mask =
415738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
415838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
415938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4160a266c7d5SChris Wilson 
41612dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41622dd2a883SImre Deak 		return IRQ_NONE;
41632dd2a883SImre Deak 
41641f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41651f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41661f814dacSImre Deak 
4167a266c7d5SChris Wilson 	iir = I915_READ(IIR);
416838bde180SChris Wilson 	do {
416938bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
41708291ee90SChris Wilson 		bool blc_event = false;
4171a266c7d5SChris Wilson 
4172a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4173a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4174a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4175a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4176a266c7d5SChris Wilson 		 */
4177222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4178a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4179aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4180a266c7d5SChris Wilson 
4181055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4182f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4183a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4184a266c7d5SChris Wilson 
418538bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4186a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4187a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
418838bde180SChris Wilson 				irq_received = true;
4189a266c7d5SChris Wilson 			}
4190a266c7d5SChris Wilson 		}
4191222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4192a266c7d5SChris Wilson 
4193a266c7d5SChris Wilson 		if (!irq_received)
4194a266c7d5SChris Wilson 			break;
4195a266c7d5SChris Wilson 
4196a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
419791d14251STvrtko Ursulin 		if (I915_HAS_HOTPLUG(dev_priv) &&
41981ae3c34cSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT) {
41991ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
42001ae3c34cSVille Syrjälä 			if (hotplug_status)
420191d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
42021ae3c34cSVille Syrjälä 		}
4203a266c7d5SChris Wilson 
420438bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4205a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4206a266c7d5SChris Wilson 
4207a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42083b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4209a266c7d5SChris Wilson 
4210055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42115a21b665SDaniel Vetter 			int plane = pipe;
42125a21b665SDaniel Vetter 			if (HAS_FBC(dev_priv))
42135a21b665SDaniel Vetter 				plane = !plane;
42145a21b665SDaniel Vetter 
42155a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
42165a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, plane, pipe, iir))
42175a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4218a266c7d5SChris Wilson 
4219a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4220a266c7d5SChris Wilson 				blc_event = true;
42214356d586SDaniel Vetter 
42224356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
422391d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
42242d9d2b0bSVille Syrjälä 
42251f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42261f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42271f7247c0SDaniel Vetter 								    pipe);
4228a266c7d5SChris Wilson 		}
4229a266c7d5SChris Wilson 
4230a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
423191d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4232a266c7d5SChris Wilson 
4233a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4234a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4235a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4236a266c7d5SChris Wilson 		 * we would never get another interrupt.
4237a266c7d5SChris Wilson 		 *
4238a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4239a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4240a266c7d5SChris Wilson 		 * another one.
4241a266c7d5SChris Wilson 		 *
4242a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4243a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4244a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4245a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4246a266c7d5SChris Wilson 		 * stray interrupts.
4247a266c7d5SChris Wilson 		 */
424838bde180SChris Wilson 		ret = IRQ_HANDLED;
4249a266c7d5SChris Wilson 		iir = new_iir;
425038bde180SChris Wilson 	} while (iir & ~flip_mask);
4251a266c7d5SChris Wilson 
42521f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42531f814dacSImre Deak 
4254a266c7d5SChris Wilson 	return ret;
4255a266c7d5SChris Wilson }
4256a266c7d5SChris Wilson 
4257a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4258a266c7d5SChris Wilson {
4259fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4260a266c7d5SChris Wilson 	int pipe;
4261a266c7d5SChris Wilson 
4262a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42630706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4264a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4265a266c7d5SChris Wilson 	}
4266a266c7d5SChris Wilson 
426700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4268055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
426955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4270a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
427155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
427255b39755SChris Wilson 	}
4273a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4274a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4275a266c7d5SChris Wilson 
4276a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4277a266c7d5SChris Wilson }
4278a266c7d5SChris Wilson 
4279a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4280a266c7d5SChris Wilson {
4281fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4282a266c7d5SChris Wilson 	int pipe;
4283a266c7d5SChris Wilson 
42840706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4285a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4286a266c7d5SChris Wilson 
4287a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4288055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4289a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4290a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4291a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4292a266c7d5SChris Wilson 	POSTING_READ(IER);
4293a266c7d5SChris Wilson }
4294a266c7d5SChris Wilson 
4295a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4296a266c7d5SChris Wilson {
4297fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4298bbba0a97SChris Wilson 	u32 enable_mask;
4299a266c7d5SChris Wilson 	u32 error_mask;
4300a266c7d5SChris Wilson 
4301a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4302bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4303adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4304bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4305bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4306bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4307bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4308bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4309bbba0a97SChris Wilson 
4310bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
431121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
431221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4313bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4314bbba0a97SChris Wilson 
431591d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4316bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4317a266c7d5SChris Wilson 
4318b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4319b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4320d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4321755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4322755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4323755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4324d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4325a266c7d5SChris Wilson 
4326a266c7d5SChris Wilson 	/*
4327a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4328a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4329a266c7d5SChris Wilson 	 */
433091d14251STvrtko Ursulin 	if (IS_G4X(dev_priv)) {
4331a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4332a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4333a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4334a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4335a266c7d5SChris Wilson 	} else {
4336a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4337a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4338a266c7d5SChris Wilson 	}
4339a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4340a266c7d5SChris Wilson 
4341a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4342a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4343a266c7d5SChris Wilson 	POSTING_READ(IER);
4344a266c7d5SChris Wilson 
43450706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
434620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
434720afbda2SDaniel Vetter 
434891d14251STvrtko Ursulin 	i915_enable_asle_pipestat(dev_priv);
434920afbda2SDaniel Vetter 
435020afbda2SDaniel Vetter 	return 0;
435120afbda2SDaniel Vetter }
435220afbda2SDaniel Vetter 
435391d14251STvrtko Ursulin static void i915_hpd_irq_setup(struct drm_i915_private *dev_priv)
435420afbda2SDaniel Vetter {
435520afbda2SDaniel Vetter 	u32 hotplug_en;
435620afbda2SDaniel Vetter 
4357b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4358b5ea2d56SDaniel Vetter 
4359adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4360e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
436191d14251STvrtko Ursulin 	hotplug_en = intel_hpd_enabled_irqs(dev_priv, hpd_mask_i915);
4362a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4363a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4364a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4365a266c7d5SChris Wilson 	*/
436691d14251STvrtko Ursulin 	if (IS_G4X(dev_priv))
4367a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4368a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4369a266c7d5SChris Wilson 
4370a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
43710706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4372f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4373f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4374f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
43750706f17cSEgbert Eich 					     hotplug_en);
4376a266c7d5SChris Wilson }
4377a266c7d5SChris Wilson 
4378ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4379a266c7d5SChris Wilson {
438045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
4381fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4382a266c7d5SChris Wilson 	u32 iir, new_iir;
4383a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4384a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
438521ad8330SVille Syrjälä 	u32 flip_mask =
438621ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
438721ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4388a266c7d5SChris Wilson 
43892dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43902dd2a883SImre Deak 		return IRQ_NONE;
43912dd2a883SImre Deak 
43921f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
43931f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
43941f814dacSImre Deak 
4395a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4396a266c7d5SChris Wilson 
4397a266c7d5SChris Wilson 	for (;;) {
4398501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
43992c8ba29fSChris Wilson 		bool blc_event = false;
44002c8ba29fSChris Wilson 
4401a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4402a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4403a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4404a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4405a266c7d5SChris Wilson 		 */
4406222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4407a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4408aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4409a266c7d5SChris Wilson 
4410055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4411f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4412a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4413a266c7d5SChris Wilson 
4414a266c7d5SChris Wilson 			/*
4415a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4416a266c7d5SChris Wilson 			 */
4417a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4418a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4419501e01d7SVille Syrjälä 				irq_received = true;
4420a266c7d5SChris Wilson 			}
4421a266c7d5SChris Wilson 		}
4422222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4423a266c7d5SChris Wilson 
4424a266c7d5SChris Wilson 		if (!irq_received)
4425a266c7d5SChris Wilson 			break;
4426a266c7d5SChris Wilson 
4427a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4428a266c7d5SChris Wilson 
4429a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
44301ae3c34cSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT) {
44311ae3c34cSVille Syrjälä 			u32 hotplug_status = i9xx_hpd_irq_ack(dev_priv);
44321ae3c34cSVille Syrjälä 			if (hotplug_status)
443391d14251STvrtko Ursulin 				i9xx_hpd_irq_handler(dev_priv, hotplug_status);
44341ae3c34cSVille Syrjälä 		}
4435a266c7d5SChris Wilson 
443621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4437a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4438a266c7d5SChris Wilson 
4439a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44403b3f1650SAkash Goel 			notify_ring(dev_priv->engine[RCS]);
4441a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44423b3f1650SAkash Goel 			notify_ring(dev_priv->engine[VCS]);
4443a266c7d5SChris Wilson 
4444055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44455a21b665SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
44465a21b665SDaniel Vetter 			    i915_handle_vblank(dev_priv, pipe, pipe, iir))
44475a21b665SDaniel Vetter 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4448a266c7d5SChris Wilson 
4449a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4450a266c7d5SChris Wilson 				blc_event = true;
44514356d586SDaniel Vetter 
44524356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
445391d14251STvrtko Ursulin 				i9xx_pipe_crc_irq_handler(dev_priv, pipe);
4454a266c7d5SChris Wilson 
44551f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44561f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44572d9d2b0bSVille Syrjälä 		}
4458a266c7d5SChris Wilson 
4459a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
446091d14251STvrtko Ursulin 			intel_opregion_asle_intr(dev_priv);
4461a266c7d5SChris Wilson 
4462515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
446391d14251STvrtko Ursulin 			gmbus_irq_handler(dev_priv);
4464515ac2bbSDaniel Vetter 
4465a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4466a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4467a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4468a266c7d5SChris Wilson 		 * we would never get another interrupt.
4469a266c7d5SChris Wilson 		 *
4470a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4471a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4472a266c7d5SChris Wilson 		 * another one.
4473a266c7d5SChris Wilson 		 *
4474a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4475a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4476a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4477a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4478a266c7d5SChris Wilson 		 * stray interrupts.
4479a266c7d5SChris Wilson 		 */
4480a266c7d5SChris Wilson 		iir = new_iir;
4481a266c7d5SChris Wilson 	}
4482a266c7d5SChris Wilson 
44831f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
44841f814dacSImre Deak 
4485a266c7d5SChris Wilson 	return ret;
4486a266c7d5SChris Wilson }
4487a266c7d5SChris Wilson 
4488a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4489a266c7d5SChris Wilson {
4490fac5e23eSChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
4491a266c7d5SChris Wilson 	int pipe;
4492a266c7d5SChris Wilson 
4493a266c7d5SChris Wilson 	if (!dev_priv)
4494a266c7d5SChris Wilson 		return;
4495a266c7d5SChris Wilson 
44960706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4497a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4498a266c7d5SChris Wilson 
4499a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4500055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4501a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4502a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4503a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4504a266c7d5SChris Wilson 
4505055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4506a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4507a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4508a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4509a266c7d5SChris Wilson }
4510a266c7d5SChris Wilson 
4511fca52a55SDaniel Vetter /**
4512fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4513fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4514fca52a55SDaniel Vetter  *
4515fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4516fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4517fca52a55SDaniel Vetter  */
4518b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4519f71d4af4SJesse Barnes {
452091c8a326SChris Wilson 	struct drm_device *dev = &dev_priv->drm;
45218b2e326dSChris Wilson 
452277913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
452377913b39SJani Nikula 
4524c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4525a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45268b2e326dSChris Wilson 
4527a6706b45SDeepak S 	/* Let's track the enabled rps events */
4528666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45296c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45306f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
453131685c25SDeepak S 	else
4532a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4533a6706b45SDeepak S 
45341800ad25SSagar Arun Kamble 	dev_priv->rps.pm_intr_keep = 0;
45351800ad25SSagar Arun Kamble 
45361800ad25SSagar Arun Kamble 	/*
45371800ad25SSagar Arun Kamble 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
45381800ad25SSagar Arun Kamble 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
45391800ad25SSagar Arun Kamble 	 *
45401800ad25SSagar Arun Kamble 	 * TODO: verify if this can be reproduced on VLV,CHV.
45411800ad25SSagar Arun Kamble 	 */
45421800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
45431800ad25SSagar Arun Kamble 		dev_priv->rps.pm_intr_keep |= GEN6_PM_RP_UP_EI_EXPIRED;
45441800ad25SSagar Arun Kamble 
45451800ad25SSagar Arun Kamble 	if (INTEL_INFO(dev_priv)->gen >= 8)
4546b20e3cfeSDave Gordon 		dev_priv->rps.pm_intr_keep |= GEN8_PMINTR_REDIRECT_TO_GUC;
45471800ad25SSagar Arun Kamble 
4548737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4549737b1506SChris Wilson 			  i915_hangcheck_elapsed);
455061bac78eSDaniel Vetter 
4551b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45524194c088SRodrigo Vivi 		/* Gen2 doesn't have a hardware frame counter */
45534cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45544194c088SRodrigo Vivi 		dev->driver->get_vblank_counter = drm_vblank_no_hw_counter;
4555b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4556f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4557fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4558391f75e2SVille Syrjälä 	} else {
4559391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4560391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4561f71d4af4SJesse Barnes 	}
4562f71d4af4SJesse Barnes 
456321da2700SVille Syrjälä 	/*
456421da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
456521da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
456621da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
456721da2700SVille Syrjälä 	 */
4568b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
456921da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
457021da2700SVille Syrjälä 
4571f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4572f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4573f71d4af4SJesse Barnes 
4574b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
457543f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
457643f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
457743f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
457843f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
457986e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
458086e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
458143f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4582b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
45837e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
45847e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
45857e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
45867e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
458786e83e35SChris Wilson 		dev->driver->enable_vblank = i965_enable_vblank;
458886e83e35SChris Wilson 		dev->driver->disable_vblank = i965_disable_vblank;
4589fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4590b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4591abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4592723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4593abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4594abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4595abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4596abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
45976dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4598e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4599*6e266956STvrtko Ursulin 		else if (HAS_PCH_SPT(dev_priv) || HAS_PCH_KBP(dev_priv))
46006dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46016dbf30ceSVille Syrjälä 		else
46023a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4603*6e266956STvrtko Ursulin 	} else if (HAS_PCH_SPLIT(dev_priv)) {
4604f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4605723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4606f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4607f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4608f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4609f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4610e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4611f71d4af4SJesse Barnes 	} else {
46127e22dbbbSTvrtko Ursulin 		if (IS_GEN2(dev_priv)) {
4613c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4614c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4615c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4616c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
461786e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
461886e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
46197e22dbbbSTvrtko Ursulin 		} else if (IS_GEN3(dev_priv)) {
4620a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4621a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4622a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4623a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
462486e83e35SChris Wilson 			dev->driver->enable_vblank = i8xx_enable_vblank;
462586e83e35SChris Wilson 			dev->driver->disable_vblank = i8xx_disable_vblank;
4626c2798b19SChris Wilson 		} else {
4627a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4628a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4629a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4630a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
463186e83e35SChris Wilson 			dev->driver->enable_vblank = i965_enable_vblank;
463286e83e35SChris Wilson 			dev->driver->disable_vblank = i965_disable_vblank;
4633c2798b19SChris Wilson 		}
4634778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4635778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4636f71d4af4SJesse Barnes 	}
4637f71d4af4SJesse Barnes }
463820afbda2SDaniel Vetter 
4639fca52a55SDaniel Vetter /**
4640fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4641fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4642fca52a55SDaniel Vetter  *
4643fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4644fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4645fca52a55SDaniel Vetter  *
4646fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4647fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4648fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4649fca52a55SDaniel Vetter  */
46502aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46512aeb7d3aSDaniel Vetter {
46522aeb7d3aSDaniel Vetter 	/*
46532aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46542aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46552aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46562aeb7d3aSDaniel Vetter 	 */
46572aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46582aeb7d3aSDaniel Vetter 
465991c8a326SChris Wilson 	return drm_irq_install(&dev_priv->drm, dev_priv->drm.pdev->irq);
46602aeb7d3aSDaniel Vetter }
46612aeb7d3aSDaniel Vetter 
4662fca52a55SDaniel Vetter /**
4663fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4664fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4665fca52a55SDaniel Vetter  *
4666fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4667fca52a55SDaniel Vetter  * resources acquired in the init functions.
4668fca52a55SDaniel Vetter  */
46692aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
46702aeb7d3aSDaniel Vetter {
467191c8a326SChris Wilson 	drm_irq_uninstall(&dev_priv->drm);
46722aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
46732aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46742aeb7d3aSDaniel Vetter }
46752aeb7d3aSDaniel Vetter 
4676fca52a55SDaniel Vetter /**
4677fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4678fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4679fca52a55SDaniel Vetter  *
4680fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4681fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4682fca52a55SDaniel Vetter  */
4683b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4684c67a470bSPaulo Zanoni {
468591c8a326SChris Wilson 	dev_priv->drm.driver->irq_uninstall(&dev_priv->drm);
46862aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
468791c8a326SChris Wilson 	synchronize_irq(dev_priv->drm.irq);
4688c67a470bSPaulo Zanoni }
4689c67a470bSPaulo Zanoni 
4690fca52a55SDaniel Vetter /**
4691fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4692fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4693fca52a55SDaniel Vetter  *
4694fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4695fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4696fca52a55SDaniel Vetter  */
4697b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4698c67a470bSPaulo Zanoni {
46992aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
470091c8a326SChris Wilson 	dev_priv->drm.driver->irq_preinstall(&dev_priv->drm);
470191c8a326SChris Wilson 	dev_priv->drm.driver->irq_postinstall(&dev_priv->drm);
4702c67a470bSPaulo Zanoni }
4703