xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision d98c52cf4fa2bb7116a89f1132fc773b1cfa6436)
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 	POSTING_READ(GTIMR);
26343eaea13SPaulo Zanoni }
26443eaea13SPaulo Zanoni 
265480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
26643eaea13SPaulo Zanoni {
26743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
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 
3393cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
3403cc134e3SImre Deak {
3413cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
342f0f59a00SVille Syrjälä 	i915_reg_t reg = gen6_pm_iir(dev_priv);
3433cc134e3SImre Deak 
3443cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3453cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3463cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
3473cc134e3SImre Deak 	POSTING_READ(reg);
348096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
3493cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
3503cc134e3SImre Deak }
3513cc134e3SImre Deak 
352b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
353b900b949SImre Deak {
354b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
355b900b949SImre Deak 
356b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
35778e68d36SImre Deak 
358b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
3593cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
360d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
36178e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
36278e68d36SImre Deak 				dev_priv->pm_rps_events);
363b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
36478e68d36SImre Deak 
365b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
366b900b949SImre Deak }
367b900b949SImre Deak 
36859d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
36959d02a1fSImre Deak {
37059d02a1fSImre Deak 	/*
371f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
37259d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
373f24eeb19SImre Deak 	 *
374f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
37559d02a1fSImre Deak 	 */
37659d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
37759d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
37859d02a1fSImre Deak 
37959d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
38059d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
38159d02a1fSImre Deak 
38259d02a1fSImre Deak 	return mask;
38359d02a1fSImre Deak }
38459d02a1fSImre Deak 
385b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
386b900b949SImre Deak {
387b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
388b900b949SImre Deak 
389d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
390d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
391d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
392d4d70aa5SImre Deak 
393d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
394d4d70aa5SImre Deak 
3959939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3969939fba2SImre Deak 
39759d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3989939fba2SImre Deak 
3999939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
400b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
401b900b949SImre Deak 				~dev_priv->pm_rps_events);
40258072ccbSImre Deak 
40358072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
40458072ccbSImre Deak 
40558072ccbSImre Deak 	synchronize_irq(dev->irq);
406b900b949SImre Deak }
407b900b949SImre Deak 
4080961021aSBen Widawsky /**
4093a3b3c7dSVille Syrjälä  * bdw_update_port_irq - update DE port interrupt
4103a3b3c7dSVille Syrjälä  * @dev_priv: driver private
4113a3b3c7dSVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
4123a3b3c7dSVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
4133a3b3c7dSVille Syrjälä  */
4143a3b3c7dSVille Syrjälä static void bdw_update_port_irq(struct drm_i915_private *dev_priv,
4153a3b3c7dSVille Syrjälä 				uint32_t interrupt_mask,
4163a3b3c7dSVille Syrjälä 				uint32_t enabled_irq_mask)
4173a3b3c7dSVille Syrjälä {
4183a3b3c7dSVille Syrjälä 	uint32_t new_val;
4193a3b3c7dSVille Syrjälä 	uint32_t old_val;
4203a3b3c7dSVille Syrjälä 
4213a3b3c7dSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
4223a3b3c7dSVille Syrjälä 
4233a3b3c7dSVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
4243a3b3c7dSVille Syrjälä 
4253a3b3c7dSVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
4263a3b3c7dSVille Syrjälä 		return;
4273a3b3c7dSVille Syrjälä 
4283a3b3c7dSVille Syrjälä 	old_val = I915_READ(GEN8_DE_PORT_IMR);
4293a3b3c7dSVille Syrjälä 
4303a3b3c7dSVille Syrjälä 	new_val = old_val;
4313a3b3c7dSVille Syrjälä 	new_val &= ~interrupt_mask;
4323a3b3c7dSVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
4333a3b3c7dSVille Syrjälä 
4343a3b3c7dSVille Syrjälä 	if (new_val != old_val) {
4353a3b3c7dSVille Syrjälä 		I915_WRITE(GEN8_DE_PORT_IMR, new_val);
4363a3b3c7dSVille Syrjälä 		POSTING_READ(GEN8_DE_PORT_IMR);
4373a3b3c7dSVille Syrjälä 	}
4383a3b3c7dSVille Syrjälä }
4393a3b3c7dSVille Syrjälä 
4403a3b3c7dSVille Syrjälä /**
441013d3752SVille Syrjälä  * bdw_update_pipe_irq - update DE pipe interrupt
442013d3752SVille Syrjälä  * @dev_priv: driver private
443013d3752SVille Syrjälä  * @pipe: pipe whose interrupt to update
444013d3752SVille Syrjälä  * @interrupt_mask: mask of interrupt bits to update
445013d3752SVille Syrjälä  * @enabled_irq_mask: mask of interrupt bits to enable
446013d3752SVille Syrjälä  */
447013d3752SVille Syrjälä void bdw_update_pipe_irq(struct drm_i915_private *dev_priv,
448013d3752SVille Syrjälä 			 enum pipe pipe,
449013d3752SVille Syrjälä 			 uint32_t interrupt_mask,
450013d3752SVille Syrjälä 			 uint32_t enabled_irq_mask)
451013d3752SVille Syrjälä {
452013d3752SVille Syrjälä 	uint32_t new_val;
453013d3752SVille Syrjälä 
454013d3752SVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
455013d3752SVille Syrjälä 
456013d3752SVille Syrjälä 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
457013d3752SVille Syrjälä 
458013d3752SVille Syrjälä 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
459013d3752SVille Syrjälä 		return;
460013d3752SVille Syrjälä 
461013d3752SVille Syrjälä 	new_val = dev_priv->de_irq_mask[pipe];
462013d3752SVille Syrjälä 	new_val &= ~interrupt_mask;
463013d3752SVille Syrjälä 	new_val |= (~enabled_irq_mask & interrupt_mask);
464013d3752SVille Syrjälä 
465013d3752SVille Syrjälä 	if (new_val != dev_priv->de_irq_mask[pipe]) {
466013d3752SVille Syrjälä 		dev_priv->de_irq_mask[pipe] = new_val;
467013d3752SVille Syrjälä 		I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
468013d3752SVille Syrjälä 		POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
469013d3752SVille Syrjälä 	}
470013d3752SVille Syrjälä }
471013d3752SVille Syrjälä 
472013d3752SVille Syrjälä /**
473fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
474fee884edSDaniel Vetter  * @dev_priv: driver private
475fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
476fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
477fee884edSDaniel Vetter  */
47847339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
479fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
480fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
481fee884edSDaniel Vetter {
482fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
483fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
484fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
485fee884edSDaniel Vetter 
48615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
48715a17aaeSDaniel Vetter 
488fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
489fee884edSDaniel Vetter 
4909df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
491c67a470bSPaulo Zanoni 		return;
492c67a470bSPaulo Zanoni 
493fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
494fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
495fee884edSDaniel Vetter }
4968664281bSPaulo Zanoni 
497b5ea642aSDaniel Vetter static void
498755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
499755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5007c463586SKeith Packard {
501f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
502755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5037c463586SKeith Packard 
504b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
505d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
506b79480baSDaniel Vetter 
50704feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
50804feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
50904feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
51004feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
511755e9019SImre Deak 		return;
512755e9019SImre Deak 
513755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
51446c06a30SVille Syrjälä 		return;
51546c06a30SVille Syrjälä 
51691d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
51791d181ddSImre Deak 
5187c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
519755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
52046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5213143a2bfSChris Wilson 	POSTING_READ(reg);
5227c463586SKeith Packard }
5237c463586SKeith Packard 
524b5ea642aSDaniel Vetter static void
525755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
526755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
5277c463586SKeith Packard {
528f0f59a00SVille Syrjälä 	i915_reg_t reg = PIPESTAT(pipe);
529755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5307c463586SKeith Packard 
531b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
532d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
533b79480baSDaniel Vetter 
53404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
53504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
53604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
53704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
53846c06a30SVille Syrjälä 		return;
53946c06a30SVille Syrjälä 
540755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
541755e9019SImre Deak 		return;
542755e9019SImre Deak 
54391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
54491d181ddSImre Deak 
545755e9019SImre Deak 	pipestat &= ~enable_mask;
54646c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
5473143a2bfSChris Wilson 	POSTING_READ(reg);
5487c463586SKeith Packard }
5497c463586SKeith Packard 
55010c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
55110c59c51SImre Deak {
55210c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
55310c59c51SImre Deak 
55410c59c51SImre Deak 	/*
555724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
556724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
55710c59c51SImre Deak 	 */
55810c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
55910c59c51SImre Deak 		return 0;
560724a6905SVille Syrjälä 	/*
561724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
562724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
563724a6905SVille Syrjälä 	 */
564724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
565724a6905SVille Syrjälä 		return 0;
56610c59c51SImre Deak 
56710c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
56810c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
56910c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
57010c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
57110c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
57210c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
57310c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
57410c59c51SImre Deak 
57510c59c51SImre Deak 	return enable_mask;
57610c59c51SImre Deak }
57710c59c51SImre Deak 
578755e9019SImre Deak void
579755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
580755e9019SImre Deak 		     u32 status_mask)
581755e9019SImre Deak {
582755e9019SImre Deak 	u32 enable_mask;
583755e9019SImre Deak 
584666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
58510c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
58610c59c51SImre Deak 							   status_mask);
58710c59c51SImre Deak 	else
588755e9019SImre Deak 		enable_mask = status_mask << 16;
589755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
590755e9019SImre Deak }
591755e9019SImre Deak 
592755e9019SImre Deak void
593755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
594755e9019SImre Deak 		      u32 status_mask)
595755e9019SImre Deak {
596755e9019SImre Deak 	u32 enable_mask;
597755e9019SImre Deak 
598666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
59910c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
60010c59c51SImre Deak 							   status_mask);
60110c59c51SImre Deak 	else
602755e9019SImre Deak 		enable_mask = status_mask << 16;
603755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
604755e9019SImre Deak }
605755e9019SImre Deak 
606c0e09200SDave Airlie /**
607f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
608468f9d29SJavier Martinez Canillas  * @dev: drm device
60901c66889SZhao Yakui  */
610f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
61101c66889SZhao Yakui {
6122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6131ec14ad3SChris Wilson 
614f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
615f49e38ddSJani Nikula 		return;
616f49e38ddSJani Nikula 
61713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
61801c66889SZhao Yakui 
619755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
620a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
6213b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
622755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
6231ec14ad3SChris Wilson 
62413321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
62501c66889SZhao Yakui }
62601c66889SZhao Yakui 
627f75f3746SVille Syrjälä /*
628f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
629f75f3746SVille Syrjälä  * around the vertical blanking period.
630f75f3746SVille Syrjälä  *
631f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
632f75f3746SVille Syrjälä  *  vblank_start >= 3
633f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
634f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
635f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
636f75f3746SVille Syrjälä  *
637f75f3746SVille Syrjälä  *           start of vblank:
638f75f3746SVille Syrjälä  *           latch double buffered registers
639f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
640f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
641f75f3746SVille Syrjälä  *           |
642f75f3746SVille Syrjälä  *           |          frame start:
643f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
644f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
645f75f3746SVille Syrjälä  *           |          |
646f75f3746SVille Syrjälä  *           |          |  start of vsync:
647f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
648f75f3746SVille Syrjälä  *           |          |  |
649f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
650f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
651f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
652f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
653f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
654f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
655f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
656f75f3746SVille Syrjälä  *       |          |                                         |
657f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
658f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
659f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
660f75f3746SVille Syrjälä  *
661f75f3746SVille Syrjälä  * x  = horizontal active
662f75f3746SVille Syrjälä  * _  = horizontal blanking
663f75f3746SVille Syrjälä  * hs = horizontal sync
664f75f3746SVille Syrjälä  * va = vertical active
665f75f3746SVille Syrjälä  * vb = vertical blanking
666f75f3746SVille Syrjälä  * vs = vertical sync
667f75f3746SVille Syrjälä  * vbs = vblank_start (number)
668f75f3746SVille Syrjälä  *
669f75f3746SVille Syrjälä  * Summary:
670f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
671f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
672f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
673f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
674f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
675f75f3746SVille Syrjälä  */
676f75f3746SVille Syrjälä 
67788e72717SThierry Reding static u32 i8xx_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6784cdb83ecSVille Syrjälä {
6794cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
6804cdb83ecSVille Syrjälä 	return 0;
6814cdb83ecSVille Syrjälä }
6824cdb83ecSVille Syrjälä 
68342f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
68442f52ef8SKeith Packard  * we use as a pipe index
68542f52ef8SKeith Packard  */
68688e72717SThierry Reding static u32 i915_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
6870a3e67a4SJesse Barnes {
6882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
689f0f59a00SVille Syrjälä 	i915_reg_t high_frame, low_frame;
6900b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
691391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
692391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
693fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &intel_crtc->base.hwmode;
694391f75e2SVille Syrjälä 
6950b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
6960b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
6970b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
6980b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
6990b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
700391f75e2SVille Syrjälä 
7010b2a8e09SVille Syrjälä 	/* Convert to pixel count */
7020b2a8e09SVille Syrjälä 	vbl_start *= htotal;
7030b2a8e09SVille Syrjälä 
7040b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
7050b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
7060b2a8e09SVille Syrjälä 
7079db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
7089db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
7095eddb70bSChris Wilson 
7100a3e67a4SJesse Barnes 	/*
7110a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
7120a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
7130a3e67a4SJesse Barnes 	 * register.
7140a3e67a4SJesse Barnes 	 */
7150a3e67a4SJesse Barnes 	do {
7165eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
717391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
7185eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
7190a3e67a4SJesse Barnes 	} while (high1 != high2);
7200a3e67a4SJesse Barnes 
7215eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
722391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
7235eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
724391f75e2SVille Syrjälä 
725391f75e2SVille Syrjälä 	/*
726391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
727391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
728391f75e2SVille Syrjälä 	 * counter against vblank start.
729391f75e2SVille Syrjälä 	 */
730edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
7310a3e67a4SJesse Barnes }
7320a3e67a4SJesse Barnes 
733974e59baSDave Airlie static u32 g4x_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7349880b7a5SJesse Barnes {
7352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7369880b7a5SJesse Barnes 
737649636efSVille Syrjälä 	return I915_READ(PIPE_FRMCOUNT_G4X(pipe));
7389880b7a5SJesse Barnes }
7399880b7a5SJesse Barnes 
74075aa3f63SVille Syrjälä /* I915_READ_FW, only for fast reads of display block, no need for forcewake etc. */
741a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
742a225f079SVille Syrjälä {
743a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
744a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
745fc467a22SMaarten Lankhorst 	const struct drm_display_mode *mode = &crtc->base.hwmode;
746a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
74780715b2fSVille Syrjälä 	int position, vtotal;
748a225f079SVille Syrjälä 
74980715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
750a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
751a225f079SVille Syrjälä 		vtotal /= 2;
752a225f079SVille Syrjälä 
753a225f079SVille Syrjälä 	if (IS_GEN2(dev))
75475aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
755a225f079SVille Syrjälä 	else
75675aa3f63SVille Syrjälä 		position = I915_READ_FW(PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
757a225f079SVille Syrjälä 
758a225f079SVille Syrjälä 	/*
75941b578fbSJesse Barnes 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
76041b578fbSJesse Barnes 	 * read it just before the start of vblank.  So try it again
76141b578fbSJesse Barnes 	 * so we don't accidentally end up spanning a vblank frame
76241b578fbSJesse Barnes 	 * increment, causing the pipe_update_end() code to squak at us.
76341b578fbSJesse Barnes 	 *
76441b578fbSJesse Barnes 	 * The nature of this problem means we can't simply check the ISR
76541b578fbSJesse Barnes 	 * bit and return the vblank start value; nor can we use the scanline
76641b578fbSJesse Barnes 	 * debug register in the transcoder as it appears to have the same
76741b578fbSJesse Barnes 	 * problem.  We may need to extend this to include other platforms,
76841b578fbSJesse Barnes 	 * but so far testing only shows the problem on HSW.
76941b578fbSJesse Barnes 	 */
770b2916819SMaarten Lankhorst 	if (HAS_DDI(dev) && !position) {
77141b578fbSJesse Barnes 		int i, temp;
77241b578fbSJesse Barnes 
77341b578fbSJesse Barnes 		for (i = 0; i < 100; i++) {
77441b578fbSJesse Barnes 			udelay(1);
77541b578fbSJesse Barnes 			temp = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) &
77641b578fbSJesse Barnes 				DSL_LINEMASK_GEN3;
77741b578fbSJesse Barnes 			if (temp != position) {
77841b578fbSJesse Barnes 				position = temp;
77941b578fbSJesse Barnes 				break;
78041b578fbSJesse Barnes 			}
78141b578fbSJesse Barnes 		}
78241b578fbSJesse Barnes 	}
78341b578fbSJesse Barnes 
78441b578fbSJesse Barnes 	/*
78580715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
78680715b2fSVille Syrjälä 	 * scanline_offset adjustment.
787a225f079SVille Syrjälä 	 */
78880715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
789a225f079SVille Syrjälä }
790a225f079SVille Syrjälä 
79188e72717SThierry Reding static int i915_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
792abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
7933bb403bfSVille Syrjälä 				    ktime_t *stime, ktime_t *etime,
7943bb403bfSVille Syrjälä 				    const struct drm_display_mode *mode)
7950af7e4dfSMario Kleiner {
796c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
797c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
798c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7993aa18df8SVille Syrjälä 	int position;
80078e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
8010af7e4dfSMario Kleiner 	bool in_vbl = true;
8020af7e4dfSMario Kleiner 	int ret = 0;
803ad3543edSMario Kleiner 	unsigned long irqflags;
8040af7e4dfSMario Kleiner 
805fc467a22SMaarten Lankhorst 	if (WARN_ON(!mode->crtc_clock)) {
8060af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
8079db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8080af7e4dfSMario Kleiner 		return 0;
8090af7e4dfSMario Kleiner 	}
8100af7e4dfSMario Kleiner 
811c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
81278e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
813c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
814c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
815c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
8160af7e4dfSMario Kleiner 
817d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
818d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
819d31faf65SVille Syrjälä 		vbl_end /= 2;
820d31faf65SVille Syrjälä 		vtotal /= 2;
821d31faf65SVille Syrjälä 	}
822d31faf65SVille Syrjälä 
823c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
824c2baf4b7SVille Syrjälä 
825ad3543edSMario Kleiner 	/*
826ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
827ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
828ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
829ad3543edSMario Kleiner 	 */
830ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
831ad3543edSMario Kleiner 
832ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
833ad3543edSMario Kleiner 
834ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
835ad3543edSMario Kleiner 	if (stime)
836ad3543edSMario Kleiner 		*stime = ktime_get();
837ad3543edSMario Kleiner 
8387c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
8390af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
8400af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
8410af7e4dfSMario Kleiner 		 */
842a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
8430af7e4dfSMario Kleiner 	} else {
8440af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
8450af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
8460af7e4dfSMario Kleiner 		 * scanout position.
8470af7e4dfSMario Kleiner 		 */
84875aa3f63SVille Syrjälä 		position = (I915_READ_FW(PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
8490af7e4dfSMario Kleiner 
8503aa18df8SVille Syrjälä 		/* convert to pixel counts */
8513aa18df8SVille Syrjälä 		vbl_start *= htotal;
8523aa18df8SVille Syrjälä 		vbl_end *= htotal;
8533aa18df8SVille Syrjälä 		vtotal *= htotal;
85478e8fc6bSVille Syrjälä 
85578e8fc6bSVille Syrjälä 		/*
8567e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
8577e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
8587e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
8597e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
8607e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
8617e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
8627e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
8637e78f1cbSVille Syrjälä 		 */
8647e78f1cbSVille Syrjälä 		if (position >= vtotal)
8657e78f1cbSVille Syrjälä 			position = vtotal - 1;
8667e78f1cbSVille Syrjälä 
8677e78f1cbSVille Syrjälä 		/*
86878e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
86978e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
87078e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
87178e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
87278e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
87378e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
87478e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
87578e8fc6bSVille Syrjälä 		 */
87678e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
8773aa18df8SVille Syrjälä 	}
8783aa18df8SVille Syrjälä 
879ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
880ad3543edSMario Kleiner 	if (etime)
881ad3543edSMario Kleiner 		*etime = ktime_get();
882ad3543edSMario Kleiner 
883ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
884ad3543edSMario Kleiner 
885ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
886ad3543edSMario Kleiner 
8873aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
8883aa18df8SVille Syrjälä 
8893aa18df8SVille Syrjälä 	/*
8903aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
8913aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
8923aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
8933aa18df8SVille Syrjälä 	 * up since vbl_end.
8943aa18df8SVille Syrjälä 	 */
8953aa18df8SVille Syrjälä 	if (position >= vbl_start)
8963aa18df8SVille Syrjälä 		position -= vbl_end;
8973aa18df8SVille Syrjälä 	else
8983aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
8993aa18df8SVille Syrjälä 
9007c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
9013aa18df8SVille Syrjälä 		*vpos = position;
9023aa18df8SVille Syrjälä 		*hpos = 0;
9033aa18df8SVille Syrjälä 	} else {
9040af7e4dfSMario Kleiner 		*vpos = position / htotal;
9050af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
9060af7e4dfSMario Kleiner 	}
9070af7e4dfSMario Kleiner 
9080af7e4dfSMario Kleiner 	/* In vblank? */
9090af7e4dfSMario Kleiner 	if (in_vbl)
9103d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
9110af7e4dfSMario Kleiner 
9120af7e4dfSMario Kleiner 	return ret;
9130af7e4dfSMario Kleiner }
9140af7e4dfSMario Kleiner 
915a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
916a225f079SVille Syrjälä {
917a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
918a225f079SVille Syrjälä 	unsigned long irqflags;
919a225f079SVille Syrjälä 	int position;
920a225f079SVille Syrjälä 
921a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
922a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
923a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
924a225f079SVille Syrjälä 
925a225f079SVille Syrjälä 	return position;
926a225f079SVille Syrjälä }
927a225f079SVille Syrjälä 
92888e72717SThierry Reding static int i915_get_vblank_timestamp(struct drm_device *dev, unsigned int pipe,
9290af7e4dfSMario Kleiner 			      int *max_error,
9300af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
9310af7e4dfSMario Kleiner 			      unsigned flags)
9320af7e4dfSMario Kleiner {
9334041b853SChris Wilson 	struct drm_crtc *crtc;
9340af7e4dfSMario Kleiner 
93588e72717SThierry Reding 	if (pipe >= INTEL_INFO(dev)->num_pipes) {
93688e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9370af7e4dfSMario Kleiner 		return -EINVAL;
9380af7e4dfSMario Kleiner 	}
9390af7e4dfSMario Kleiner 
9400af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
9414041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
9424041b853SChris Wilson 	if (crtc == NULL) {
94388e72717SThierry Reding 		DRM_ERROR("Invalid crtc %u\n", pipe);
9444041b853SChris Wilson 		return -EINVAL;
9454041b853SChris Wilson 	}
9464041b853SChris Wilson 
947fc467a22SMaarten Lankhorst 	if (!crtc->hwmode.crtc_clock) {
94888e72717SThierry Reding 		DRM_DEBUG_KMS("crtc %u is disabled\n", pipe);
9494041b853SChris Wilson 		return -EBUSY;
9504041b853SChris Wilson 	}
9510af7e4dfSMario Kleiner 
9520af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
9534041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
9544041b853SChris Wilson 						     vblank_time, flags,
955fc467a22SMaarten Lankhorst 						     &crtc->hwmode);
9560af7e4dfSMario Kleiner }
9570af7e4dfSMario Kleiner 
958d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
959f97108d1SJesse Barnes {
9602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
961b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9629270388eSDaniel Vetter 	u8 new_delay;
9639270388eSDaniel Vetter 
964d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
965f97108d1SJesse Barnes 
96673edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
96773edd18fSDaniel Vetter 
96820e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9699270388eSDaniel Vetter 
9707648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
971b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
972b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
973f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
974f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
975f97108d1SJesse Barnes 
976f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
977b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
97820e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
97920e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
98020e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
98120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
982b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
98320e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
98420e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
98520e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
98620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
987f97108d1SJesse Barnes 	}
988f97108d1SJesse Barnes 
9897648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
99020e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
991f97108d1SJesse Barnes 
992d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9939270388eSDaniel Vetter 
994f97108d1SJesse Barnes 	return;
995f97108d1SJesse Barnes }
996f97108d1SJesse Barnes 
9970bc40be8STvrtko Ursulin static void notify_ring(struct intel_engine_cs *engine)
998549f7365SChris Wilson {
999117897f4STvrtko Ursulin 	if (!intel_engine_initialized(engine))
1000475553deSChris Wilson 		return;
1001475553deSChris Wilson 
10020bc40be8STvrtko Ursulin 	trace_i915_gem_request_notify(engine);
100312471ba8SChris Wilson 	engine->user_interrupts++;
10049862e600SChris Wilson 
10050bc40be8STvrtko Ursulin 	wake_up_all(&engine->irq_queue);
1006549f7365SChris Wilson }
1007549f7365SChris Wilson 
100843cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
100943cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
101031685c25SDeepak S {
101143cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
101243cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
101343cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
101431685c25SDeepak S }
101531685c25SDeepak S 
101643cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
101743cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
101843cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
101943cf3bf0SChris Wilson 			 int threshold)
102031685c25SDeepak S {
102143cf3bf0SChris Wilson 	u64 time, c0;
10227bad74d5SVille Syrjälä 	unsigned int mul = 100;
102331685c25SDeepak S 
102443cf3bf0SChris Wilson 	if (old->cz_clock == 0)
102543cf3bf0SChris Wilson 		return false;
102631685c25SDeepak S 
10277bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10287bad74d5SVille Syrjälä 		mul <<= 8;
10297bad74d5SVille Syrjälä 
103043cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10317bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
103231685c25SDeepak S 
103343cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
103443cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
103543cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
103643cf3bf0SChris Wilson 	 */
103743cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
103843cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10397bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
104031685c25SDeepak S 
104143cf3bf0SChris Wilson 	return c0 >= time;
104231685c25SDeepak S }
104331685c25SDeepak S 
104443cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
104543cf3bf0SChris Wilson {
104643cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
104743cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
104843cf3bf0SChris Wilson }
104943cf3bf0SChris Wilson 
105043cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
105143cf3bf0SChris Wilson {
105243cf3bf0SChris Wilson 	struct intel_rps_ei now;
105343cf3bf0SChris Wilson 	u32 events = 0;
105443cf3bf0SChris Wilson 
10556f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
105643cf3bf0SChris Wilson 		return 0;
105743cf3bf0SChris Wilson 
105843cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
105943cf3bf0SChris Wilson 	if (now.cz_clock == 0)
106043cf3bf0SChris Wilson 		return 0;
106131685c25SDeepak S 
106243cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
106343cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
106443cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10658fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
106643cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
106743cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
106831685c25SDeepak S 	}
106931685c25SDeepak S 
107043cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
107143cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
107243cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10738fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
107443cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
107543cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
107643cf3bf0SChris Wilson 	}
107743cf3bf0SChris Wilson 
107843cf3bf0SChris Wilson 	return events;
107931685c25SDeepak S }
108031685c25SDeepak S 
1081f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1082f5a4c67dSChris Wilson {
1083e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
1084f5a4c67dSChris Wilson 
1085b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
1086e2f80391STvrtko Ursulin 		if (engine->irq_refcount)
1087f5a4c67dSChris Wilson 			return true;
1088f5a4c67dSChris Wilson 
1089f5a4c67dSChris Wilson 	return false;
1090f5a4c67dSChris Wilson }
1091f5a4c67dSChris Wilson 
10924912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10933b8d8d91SJesse Barnes {
10942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10952d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
10968d3afd7dSChris Wilson 	bool client_boost;
10978d3afd7dSChris Wilson 	int new_delay, adj, min, max;
1098edbfdb45SPaulo Zanoni 	u32 pm_iir;
10993b8d8d91SJesse Barnes 
110059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1101d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1102d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1103d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1104d4d70aa5SImre Deak 		return;
1105d4d70aa5SImre Deak 	}
11061f814dacSImre Deak 
11071f814dacSImre Deak 	/*
11081f814dacSImre Deak 	 * The RPS work is synced during runtime suspend, we don't require a
11091f814dacSImre Deak 	 * wakeref. TODO: instead of disabling the asserts make sure that we
11101f814dacSImre Deak 	 * always hold an RPM reference while the work is running.
11111f814dacSImre Deak 	 */
11121f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
11131f814dacSImre Deak 
1114c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1115c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1116a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1117480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
11188d3afd7dSChris Wilson 	client_boost = dev_priv->rps.client_boost;
11198d3afd7dSChris Wilson 	dev_priv->rps.client_boost = false;
112059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11214912d041SBen Widawsky 
112260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1123a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
112460611c13SPaulo Zanoni 
11258d3afd7dSChris Wilson 	if ((pm_iir & dev_priv->pm_rps_events) == 0 && !client_boost)
11261f814dacSImre Deak 		goto out;
11273b8d8d91SJesse Barnes 
11284fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11297b9e0ae6SChris Wilson 
113043cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
113143cf3bf0SChris Wilson 
1132dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1133edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11348d3afd7dSChris Wilson 	min = dev_priv->rps.min_freq_softlimit;
11358d3afd7dSChris Wilson 	max = dev_priv->rps.max_freq_softlimit;
11368d3afd7dSChris Wilson 
11378d3afd7dSChris Wilson 	if (client_boost) {
11388d3afd7dSChris Wilson 		new_delay = dev_priv->rps.max_freq_softlimit;
11398d3afd7dSChris Wilson 		adj = 0;
11408d3afd7dSChris Wilson 	} else if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1141dd75fdc8SChris Wilson 		if (adj > 0)
1142dd75fdc8SChris Wilson 			adj *= 2;
1143edcf284bSChris Wilson 		else /* CHV needs even encode values */
1144edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11457425034aSVille Syrjälä 		/*
11467425034aSVille Syrjälä 		 * For better performance, jump directly
11477425034aSVille Syrjälä 		 * to RPe if we're below it.
11487425034aSVille Syrjälä 		 */
1149edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1150b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1151edcf284bSChris Wilson 			adj = 0;
1152edcf284bSChris Wilson 		}
1153f5a4c67dSChris Wilson 	} else if (any_waiters(dev_priv)) {
1154f5a4c67dSChris Wilson 		adj = 0;
1155dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1156b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1157b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1158dd75fdc8SChris Wilson 		else
1159b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1160dd75fdc8SChris Wilson 		adj = 0;
1161dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1162dd75fdc8SChris Wilson 		if (adj < 0)
1163dd75fdc8SChris Wilson 			adj *= 2;
1164edcf284bSChris Wilson 		else /* CHV needs even encode values */
1165edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1166dd75fdc8SChris Wilson 	} else { /* unknown event */
1167edcf284bSChris Wilson 		adj = 0;
1168dd75fdc8SChris Wilson 	}
11693b8d8d91SJesse Barnes 
1170edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1171edcf284bSChris Wilson 
117279249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
117379249636SBen Widawsky 	 * interrupt
117479249636SBen Widawsky 	 */
1175edcf284bSChris Wilson 	new_delay += adj;
11768d3afd7dSChris Wilson 	new_delay = clamp_t(int, new_delay, min, max);
117727544369SDeepak S 
1178ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11793b8d8d91SJesse Barnes 
11804fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11811f814dacSImre Deak out:
11821f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
11833b8d8d91SJesse Barnes }
11843b8d8d91SJesse Barnes 
1185e3689190SBen Widawsky 
1186e3689190SBen Widawsky /**
1187e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1188e3689190SBen Widawsky  * occurred.
1189e3689190SBen Widawsky  * @work: workqueue struct
1190e3689190SBen Widawsky  *
1191e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1192e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1193e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1194e3689190SBen Widawsky  */
1195e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1196e3689190SBen Widawsky {
11972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11982d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1199e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
120035a85ac6SBen Widawsky 	char *parity_event[6];
1201e3689190SBen Widawsky 	uint32_t misccpctl;
120235a85ac6SBen Widawsky 	uint8_t slice = 0;
1203e3689190SBen Widawsky 
1204e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1205e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1206e3689190SBen Widawsky 	 * any time we access those registers.
1207e3689190SBen Widawsky 	 */
1208e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1209e3689190SBen Widawsky 
121035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
121135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
121235a85ac6SBen Widawsky 		goto out;
121335a85ac6SBen Widawsky 
1214e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1215e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1216e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1217e3689190SBen Widawsky 
121835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
1219f0f59a00SVille Syrjälä 		i915_reg_t reg;
122035a85ac6SBen Widawsky 
122135a85ac6SBen Widawsky 		slice--;
12222d1fe073SJoonas Lahtinen 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv)))
122335a85ac6SBen Widawsky 			break;
122435a85ac6SBen Widawsky 
122535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
122635a85ac6SBen Widawsky 
12276fa1c5f1SVille Syrjälä 		reg = GEN7_L3CDERRST1(slice);
122835a85ac6SBen Widawsky 
122935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1230e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1231e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1232e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1233e3689190SBen Widawsky 
123435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
123535a85ac6SBen Widawsky 		POSTING_READ(reg);
1236e3689190SBen Widawsky 
1237cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1238e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1239e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1240e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
124135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
124235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1243e3689190SBen Widawsky 
12445bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1245e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1246e3689190SBen Widawsky 
124735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
124835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1249e3689190SBen Widawsky 
125035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1251e3689190SBen Widawsky 		kfree(parity_event[3]);
1252e3689190SBen Widawsky 		kfree(parity_event[2]);
1253e3689190SBen Widawsky 		kfree(parity_event[1]);
1254e3689190SBen Widawsky 	}
1255e3689190SBen Widawsky 
125635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
125735a85ac6SBen Widawsky 
125835a85ac6SBen Widawsky out:
125935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12604cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
12612d1fe073SJoonas Lahtinen 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv));
12624cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
126335a85ac6SBen Widawsky 
126435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
126535a85ac6SBen Widawsky }
126635a85ac6SBen Widawsky 
126735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1268e3689190SBen Widawsky {
12692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1270e3689190SBen Widawsky 
1271040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1272e3689190SBen Widawsky 		return;
1273e3689190SBen Widawsky 
1274d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1275480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1276d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1277e3689190SBen Widawsky 
127835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
127935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
128035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
128135a85ac6SBen Widawsky 
128235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
128335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
128435a85ac6SBen Widawsky 
1285a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1286e3689190SBen Widawsky }
1287e3689190SBen Widawsky 
1288f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1289f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1290f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1291f1af8fc1SPaulo Zanoni {
1292f1af8fc1SPaulo Zanoni 	if (gt_iir &
1293f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
12944a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1295f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
12964a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1297f1af8fc1SPaulo Zanoni }
1298f1af8fc1SPaulo Zanoni 
1299e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1300e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1301e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1302e7b4c6b1SDaniel Vetter {
1303e7b4c6b1SDaniel Vetter 
1304cc609d5dSBen Widawsky 	if (gt_iir &
1305cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
13064a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[RCS]);
1307cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
13084a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[VCS]);
1309cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
13104a570db5STvrtko Ursulin 		notify_ring(&dev_priv->engine[BCS]);
1311e7b4c6b1SDaniel Vetter 
1312cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1313cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1314aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1315aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1316e3689190SBen Widawsky 
131735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
131835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1319e7b4c6b1SDaniel Vetter }
1320e7b4c6b1SDaniel Vetter 
1321fbcc1a0cSNick Hoath static __always_inline void
13220bc40be8STvrtko Ursulin gen8_cs_irq_handler(struct intel_engine_cs *engine, u32 iir, int test_shift)
1323fbcc1a0cSNick Hoath {
1324fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
13250bc40be8STvrtko Ursulin 		notify_ring(engine);
1326fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
132727af5eeaSTvrtko Ursulin 		tasklet_schedule(&engine->irq_tasklet);
1328fbcc1a0cSNick Hoath }
1329fbcc1a0cSNick Hoath 
133074cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1331abd58f01SBen Widawsky 				       u32 master_ctl)
1332abd58f01SBen Widawsky {
1333abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1334abd58f01SBen Widawsky 
1335abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
13365dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(0));
13375dd280b0SNick Hoath 		if (iir) {
13385dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(0), iir);
1339abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1340e981e7b1SThomas Daniel 
13414a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[RCS],
1342fbcc1a0cSNick Hoath 					    iir, GEN8_RCS_IRQ_SHIFT);
1343e981e7b1SThomas Daniel 
13444a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[BCS],
1345fbcc1a0cSNick Hoath 					    iir, GEN8_BCS_IRQ_SHIFT);
1346abd58f01SBen Widawsky 		} else
1347abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1348abd58f01SBen Widawsky 	}
1349abd58f01SBen Widawsky 
135085f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
13515dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(1));
13525dd280b0SNick Hoath 		if (iir) {
13535dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(1), iir);
1354abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1355e981e7b1SThomas Daniel 
13564a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[VCS],
1357fbcc1a0cSNick Hoath 					    iir, GEN8_VCS1_IRQ_SHIFT);
1358e981e7b1SThomas Daniel 
13594a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[VCS2],
1360fbcc1a0cSNick Hoath 					    iir, GEN8_VCS2_IRQ_SHIFT);
1361abd58f01SBen Widawsky 		} else
1362abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1363abd58f01SBen Widawsky 	}
1364abd58f01SBen Widawsky 
136574cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
13665dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(3));
13675dd280b0SNick Hoath 		if (iir) {
13685dd280b0SNick Hoath 			I915_WRITE_FW(GEN8_GT_IIR(3), iir);
136974cdb337SChris Wilson 			ret = IRQ_HANDLED;
137074cdb337SChris Wilson 
13714a570db5STvrtko Ursulin 			gen8_cs_irq_handler(&dev_priv->engine[VECS],
1372fbcc1a0cSNick Hoath 					    iir, GEN8_VECS_IRQ_SHIFT);
137374cdb337SChris Wilson 		} else
137474cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
137574cdb337SChris Wilson 	}
137674cdb337SChris Wilson 
13770961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
13785dd280b0SNick Hoath 		u32 iir = I915_READ_FW(GEN8_GT_IIR(2));
13795dd280b0SNick Hoath 		if (iir & dev_priv->pm_rps_events) {
1380cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13815dd280b0SNick Hoath 				      iir & dev_priv->pm_rps_events);
138238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
13835dd280b0SNick Hoath 			gen6_rps_irq_handler(dev_priv, iir);
13840961021aSBen Widawsky 		} else
13850961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13860961021aSBen Widawsky 	}
13870961021aSBen Widawsky 
1388abd58f01SBen Widawsky 	return ret;
1389abd58f01SBen Widawsky }
1390abd58f01SBen Widawsky 
139163c88d22SImre Deak static bool bxt_port_hotplug_long_detect(enum port port, u32 val)
139263c88d22SImre Deak {
139363c88d22SImre Deak 	switch (port) {
139463c88d22SImre Deak 	case PORT_A:
1395195baa06SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
139663c88d22SImre Deak 	case PORT_B:
139763c88d22SImre Deak 		return val & PORTB_HOTPLUG_LONG_DETECT;
139863c88d22SImre Deak 	case PORT_C:
139963c88d22SImre Deak 		return val & PORTC_HOTPLUG_LONG_DETECT;
140063c88d22SImre Deak 	default:
140163c88d22SImre Deak 		return false;
140263c88d22SImre Deak 	}
140363c88d22SImre Deak }
140463c88d22SImre Deak 
14056dbf30ceSVille Syrjälä static bool spt_port_hotplug2_long_detect(enum port port, u32 val)
14066dbf30ceSVille Syrjälä {
14076dbf30ceSVille Syrjälä 	switch (port) {
14086dbf30ceSVille Syrjälä 	case PORT_E:
14096dbf30ceSVille Syrjälä 		return val & PORTE_HOTPLUG_LONG_DETECT;
14106dbf30ceSVille Syrjälä 	default:
14116dbf30ceSVille Syrjälä 		return false;
14126dbf30ceSVille Syrjälä 	}
14136dbf30ceSVille Syrjälä }
14146dbf30ceSVille Syrjälä 
141574c0b395SVille Syrjälä static bool spt_port_hotplug_long_detect(enum port port, u32 val)
141674c0b395SVille Syrjälä {
141774c0b395SVille Syrjälä 	switch (port) {
141874c0b395SVille Syrjälä 	case PORT_A:
141974c0b395SVille Syrjälä 		return val & PORTA_HOTPLUG_LONG_DETECT;
142074c0b395SVille Syrjälä 	case PORT_B:
142174c0b395SVille Syrjälä 		return val & PORTB_HOTPLUG_LONG_DETECT;
142274c0b395SVille Syrjälä 	case PORT_C:
142374c0b395SVille Syrjälä 		return val & PORTC_HOTPLUG_LONG_DETECT;
142474c0b395SVille Syrjälä 	case PORT_D:
142574c0b395SVille Syrjälä 		return val & PORTD_HOTPLUG_LONG_DETECT;
142674c0b395SVille Syrjälä 	default:
142774c0b395SVille Syrjälä 		return false;
142874c0b395SVille Syrjälä 	}
142974c0b395SVille Syrjälä }
143074c0b395SVille Syrjälä 
1431e4ce95aaSVille Syrjälä static bool ilk_port_hotplug_long_detect(enum port port, u32 val)
1432e4ce95aaSVille Syrjälä {
1433e4ce95aaSVille Syrjälä 	switch (port) {
1434e4ce95aaSVille Syrjälä 	case PORT_A:
1435e4ce95aaSVille Syrjälä 		return val & DIGITAL_PORTA_HOTPLUG_LONG_DETECT;
1436e4ce95aaSVille Syrjälä 	default:
1437e4ce95aaSVille Syrjälä 		return false;
1438e4ce95aaSVille Syrjälä 	}
1439e4ce95aaSVille Syrjälä }
1440e4ce95aaSVille Syrjälä 
1441676574dfSJani Nikula static bool pch_port_hotplug_long_detect(enum port port, u32 val)
144213cf5504SDave Airlie {
144313cf5504SDave Airlie 	switch (port) {
144413cf5504SDave Airlie 	case PORT_B:
1445676574dfSJani Nikula 		return val & PORTB_HOTPLUG_LONG_DETECT;
144613cf5504SDave Airlie 	case PORT_C:
1447676574dfSJani Nikula 		return val & PORTC_HOTPLUG_LONG_DETECT;
144813cf5504SDave Airlie 	case PORT_D:
1449676574dfSJani Nikula 		return val & PORTD_HOTPLUG_LONG_DETECT;
1450676574dfSJani Nikula 	default:
1451676574dfSJani Nikula 		return false;
145213cf5504SDave Airlie 	}
145313cf5504SDave Airlie }
145413cf5504SDave Airlie 
1455676574dfSJani Nikula static bool i9xx_port_hotplug_long_detect(enum port port, u32 val)
145613cf5504SDave Airlie {
145713cf5504SDave Airlie 	switch (port) {
145813cf5504SDave Airlie 	case PORT_B:
1459676574dfSJani Nikula 		return val & PORTB_HOTPLUG_INT_LONG_PULSE;
146013cf5504SDave Airlie 	case PORT_C:
1461676574dfSJani Nikula 		return val & PORTC_HOTPLUG_INT_LONG_PULSE;
146213cf5504SDave Airlie 	case PORT_D:
1463676574dfSJani Nikula 		return val & PORTD_HOTPLUG_INT_LONG_PULSE;
1464676574dfSJani Nikula 	default:
1465676574dfSJani Nikula 		return false;
146613cf5504SDave Airlie 	}
146713cf5504SDave Airlie }
146813cf5504SDave Airlie 
146942db67d6SVille Syrjälä /*
147042db67d6SVille Syrjälä  * Get a bit mask of pins that have triggered, and which ones may be long.
147142db67d6SVille Syrjälä  * This can be called multiple times with the same masks to accumulate
147242db67d6SVille Syrjälä  * hotplug detection results from several registers.
147342db67d6SVille Syrjälä  *
147442db67d6SVille Syrjälä  * Note that the caller is expected to zero out the masks initially.
147542db67d6SVille Syrjälä  */
1476fd63e2a9SImre Deak static void intel_get_hpd_pins(u32 *pin_mask, u32 *long_mask,
14778c841e57SJani Nikula 			     u32 hotplug_trigger, u32 dig_hotplug_reg,
1478fd63e2a9SImre Deak 			     const u32 hpd[HPD_NUM_PINS],
1479fd63e2a9SImre Deak 			     bool long_pulse_detect(enum port port, u32 val))
1480676574dfSJani Nikula {
14818c841e57SJani Nikula 	enum port port;
1482676574dfSJani Nikula 	int i;
1483676574dfSJani Nikula 
1484676574dfSJani Nikula 	for_each_hpd_pin(i) {
14858c841e57SJani Nikula 		if ((hpd[i] & hotplug_trigger) == 0)
14868c841e57SJani Nikula 			continue;
14878c841e57SJani Nikula 
1488676574dfSJani Nikula 		*pin_mask |= BIT(i);
1489676574dfSJani Nikula 
1490cc24fcdcSImre Deak 		if (!intel_hpd_pin_to_port(i, &port))
1491cc24fcdcSImre Deak 			continue;
1492cc24fcdcSImre Deak 
1493fd63e2a9SImre Deak 		if (long_pulse_detect(port, dig_hotplug_reg))
1494676574dfSJani Nikula 			*long_mask |= BIT(i);
1495676574dfSJani Nikula 	}
1496676574dfSJani Nikula 
1497676574dfSJani Nikula 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x, pins 0x%08x\n",
1498676574dfSJani Nikula 			 hotplug_trigger, dig_hotplug_reg, *pin_mask);
1499676574dfSJani Nikula 
1500676574dfSJani Nikula }
1501676574dfSJani Nikula 
1502515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1503515ac2bbSDaniel Vetter {
15042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
150528c70f16SDaniel Vetter 
150628c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1507515ac2bbSDaniel Vetter }
1508515ac2bbSDaniel Vetter 
1509ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1510ce99c256SDaniel Vetter {
15112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15129ee32feaSDaniel Vetter 
15139ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1514ce99c256SDaniel Vetter }
1515ce99c256SDaniel Vetter 
15168bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1517277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1518eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1519eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15208bc5e955SDaniel Vetter 					 uint32_t crc4)
15218bf1e9f1SShuang He {
15228bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15238bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15248bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1525ac2300d4SDamien Lespiau 	int head, tail;
1526b2c88f5bSDamien Lespiau 
1527d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1528d538bbdfSDamien Lespiau 
15290c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1530d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
153134273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15320c912c79SDamien Lespiau 		return;
15330c912c79SDamien Lespiau 	}
15340c912c79SDamien Lespiau 
1535d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1536d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1537b2c88f5bSDamien Lespiau 
1538b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1539d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1540b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1541b2c88f5bSDamien Lespiau 		return;
1542b2c88f5bSDamien Lespiau 	}
1543b2c88f5bSDamien Lespiau 
1544b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15458bf1e9f1SShuang He 
15468bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1547eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1548eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1549eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1550eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1551eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1552b2c88f5bSDamien Lespiau 
1553b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1554d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1555d538bbdfSDamien Lespiau 
1556d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
155707144428SDamien Lespiau 
155807144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15598bf1e9f1SShuang He }
1560277de95eSDaniel Vetter #else
1561277de95eSDaniel Vetter static inline void
1562277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1563277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1564277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1565277de95eSDaniel Vetter 			     uint32_t crc4) {}
1566277de95eSDaniel Vetter #endif
1567eba94eb9SDaniel Vetter 
1568277de95eSDaniel Vetter 
1569277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15705a69b89fSDaniel Vetter {
15715a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15725a69b89fSDaniel Vetter 
1573277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15745a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15755a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15765a69b89fSDaniel Vetter }
15775a69b89fSDaniel Vetter 
1578277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1579eba94eb9SDaniel Vetter {
1580eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1581eba94eb9SDaniel Vetter 
1582277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1583eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1584eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1585eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1586eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15878bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1588eba94eb9SDaniel Vetter }
15895b3a856bSDaniel Vetter 
1590277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15915b3a856bSDaniel Vetter {
15925b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15930b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
15940b5c5ed0SDaniel Vetter 
15950b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
15960b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
15970b5c5ed0SDaniel Vetter 	else
15980b5c5ed0SDaniel Vetter 		res1 = 0;
15990b5c5ed0SDaniel Vetter 
16000b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16010b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16020b5c5ed0SDaniel Vetter 	else
16030b5c5ed0SDaniel Vetter 		res2 = 0;
16045b3a856bSDaniel Vetter 
1605277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16060b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16070b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16080b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16090b5c5ed0SDaniel Vetter 				     res1, res2);
16105b3a856bSDaniel Vetter }
16118bf1e9f1SShuang He 
16121403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16131403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16141403c0d4SPaulo Zanoni  * the work queue. */
16151403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1616baf02a1fSBen Widawsky {
1617a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
161859cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1619480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1620d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1621d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16222adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
162341a05a3aSDaniel Vetter 		}
1624d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1625d4d70aa5SImre Deak 	}
1626baf02a1fSBen Widawsky 
1627c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1628c9a9a268SImre Deak 		return;
1629c9a9a268SImre Deak 
16302d1fe073SJoonas Lahtinen 	if (HAS_VEBOX(dev_priv)) {
163112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
16324a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VECS]);
163312638c57SBen Widawsky 
1634aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1635aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
163612638c57SBen Widawsky 	}
16371403c0d4SPaulo Zanoni }
1638baf02a1fSBen Widawsky 
16398d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16408d7849dbSVille Syrjälä {
16418d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16428d7849dbSVille Syrjälä 		return false;
16438d7849dbSVille Syrjälä 
16448d7849dbSVille Syrjälä 	return true;
16458d7849dbSVille Syrjälä }
16468d7849dbSVille Syrjälä 
1647c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16487e231dbeSJesse Barnes {
1649c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
165091d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16517e231dbeSJesse Barnes 	int pipe;
16527e231dbeSJesse Barnes 
165358ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
16541ca993d2SVille Syrjälä 
16551ca993d2SVille Syrjälä 	if (!dev_priv->display_irqs_enabled) {
16561ca993d2SVille Syrjälä 		spin_unlock(&dev_priv->irq_lock);
16571ca993d2SVille Syrjälä 		return;
16581ca993d2SVille Syrjälä 	}
16591ca993d2SVille Syrjälä 
1660055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1661f0f59a00SVille Syrjälä 		i915_reg_t reg;
1662bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
166391d181ddSImre Deak 
1664bbb5eebfSDaniel Vetter 		/*
1665bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1666bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1667bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1668bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1669bbb5eebfSDaniel Vetter 		 * handle.
1670bbb5eebfSDaniel Vetter 		 */
16710f239f4cSDaniel Vetter 
16720f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16730f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1674bbb5eebfSDaniel Vetter 
1675bbb5eebfSDaniel Vetter 		switch (pipe) {
1676bbb5eebfSDaniel Vetter 		case PIPE_A:
1677bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1678bbb5eebfSDaniel Vetter 			break;
1679bbb5eebfSDaniel Vetter 		case PIPE_B:
1680bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1681bbb5eebfSDaniel Vetter 			break;
16823278f67fSVille Syrjälä 		case PIPE_C:
16833278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16843278f67fSVille Syrjälä 			break;
1685bbb5eebfSDaniel Vetter 		}
1686bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1687bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1688bbb5eebfSDaniel Vetter 
1689bbb5eebfSDaniel Vetter 		if (!mask)
169091d181ddSImre Deak 			continue;
169191d181ddSImre Deak 
169291d181ddSImre Deak 		reg = PIPESTAT(pipe);
1693bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1694bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16957e231dbeSJesse Barnes 
16967e231dbeSJesse Barnes 		/*
16977e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16987e231dbeSJesse Barnes 		 */
169991d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
170091d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17017e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17027e231dbeSJesse Barnes 	}
170358ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17047e231dbeSJesse Barnes 
1705055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1706d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1707d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1708d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
170931acc7f5SJesse Barnes 
1710579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
171131acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
171231acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
171331acc7f5SJesse Barnes 		}
17144356d586SDaniel Vetter 
17154356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1716277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17172d9d2b0bSVille Syrjälä 
17181f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17191f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
172031acc7f5SJesse Barnes 	}
172131acc7f5SJesse Barnes 
1722c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1723c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1724c1874ed7SImre Deak }
1725c1874ed7SImre Deak 
172616c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
172716c6c56bSVille Syrjälä {
172816c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
172916c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
173042db67d6SVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
173116c6c56bSVille Syrjälä 
17320d2e4297SJani Nikula 	if (!hotplug_status)
17330d2e4297SJani Nikula 		return;
17340d2e4297SJani Nikula 
17353ff60f89SOscar Mateo 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17363ff60f89SOscar Mateo 	/*
17373ff60f89SOscar Mateo 	 * Make sure hotplug status is cleared before we clear IIR, or else we
17383ff60f89SOscar Mateo 	 * may miss hotplug events.
17393ff60f89SOscar Mateo 	 */
17403ff60f89SOscar Mateo 	POSTING_READ(PORT_HOTPLUG_STAT);
17413ff60f89SOscar Mateo 
1742666a4537SWayne Boyer 	if (IS_G4X(dev) || IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
174316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
174416c6c56bSVille Syrjälä 
174558f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1746fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
1747fd63e2a9SImre Deak 					   hotplug_trigger, hpd_status_g4x,
1748fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
174958f2cf24SVille Syrjälä 
1750676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
175158f2cf24SVille Syrjälä 		}
1752369712e8SJani Nikula 
1753369712e8SJani Nikula 		if (hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
1754369712e8SJani Nikula 			dp_aux_irq_handler(dev);
175516c6c56bSVille Syrjälä 	} else {
175616c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
175716c6c56bSVille Syrjälä 
175858f2cf24SVille Syrjälä 		if (hotplug_trigger) {
1759fd63e2a9SImre Deak 			intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
17604e3d1e26SVille Syrjälä 					   hotplug_trigger, hpd_status_i915,
1761fd63e2a9SImre Deak 					   i9xx_port_hotplug_long_detect);
1762676574dfSJani Nikula 			intel_hpd_irq_handler(dev, pin_mask, long_mask);
176316c6c56bSVille Syrjälä 		}
17643ff60f89SOscar Mateo 	}
176558f2cf24SVille Syrjälä }
176616c6c56bSVille Syrjälä 
1767c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1768c1874ed7SImre Deak {
176945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1771c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1772c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1773c1874ed7SImre Deak 
17742dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17752dd2a883SImre Deak 		return IRQ_NONE;
17762dd2a883SImre Deak 
17771f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
17781f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
17791f814dacSImre Deak 
1780c1874ed7SImre Deak 	while (true) {
17813ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17823ff60f89SOscar Mateo 
1783c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17843ff60f89SOscar Mateo 		if (gt_iir)
17853ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17863ff60f89SOscar Mateo 
1787c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17883ff60f89SOscar Mateo 		if (pm_iir)
17893ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17903ff60f89SOscar Mateo 
17913ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17923ff60f89SOscar Mateo 		if (iir) {
17933ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17943ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17953ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17963ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17973ff60f89SOscar Mateo 		}
1798c1874ed7SImre Deak 
1799c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1800c1874ed7SImre Deak 			goto out;
1801c1874ed7SImre Deak 
1802c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1803c1874ed7SImre Deak 
18043ff60f89SOscar Mateo 		if (gt_iir)
1805c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
180660611c13SPaulo Zanoni 		if (pm_iir)
1807d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18083ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18093ff60f89SOscar Mateo 		 * signalled in iir */
18103ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18117e231dbeSJesse Barnes 	}
18127e231dbeSJesse Barnes 
18137e231dbeSJesse Barnes out:
18141f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18151f814dacSImre Deak 
18167e231dbeSJesse Barnes 	return ret;
18177e231dbeSJesse Barnes }
18187e231dbeSJesse Barnes 
181943f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
182043f328d7SVille Syrjälä {
182145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
182243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
182343f328d7SVille Syrjälä 	u32 master_ctl, iir;
182443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
182543f328d7SVille Syrjälä 
18262dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18272dd2a883SImre Deak 		return IRQ_NONE;
18282dd2a883SImre Deak 
18291f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
18301f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
18311f814dacSImre Deak 
1832579de73bSChris Wilson 	do {
18338e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18343278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18353278f67fSVille Syrjälä 
18363278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18378e5fd599SVille Syrjälä 			break;
183843f328d7SVille Syrjälä 
183927b6c122SOscar Mateo 		ret = IRQ_HANDLED;
184027b6c122SOscar Mateo 
184143f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
184243f328d7SVille Syrjälä 
184327b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
184427b6c122SOscar Mateo 
184527b6c122SOscar Mateo 		if (iir) {
184627b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
184727b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
184827b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
184927b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
185027b6c122SOscar Mateo 		}
185127b6c122SOscar Mateo 
185274cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
185343f328d7SVille Syrjälä 
185427b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
185527b6c122SOscar Mateo 		 * signalled in iir */
18563278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
185743f328d7SVille Syrjälä 
185843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
185943f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
1860579de73bSChris Wilson 	} while (0);
18613278f67fSVille Syrjälä 
18621f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
18631f814dacSImre Deak 
186443f328d7SVille Syrjälä 	return ret;
186543f328d7SVille Syrjälä }
186643f328d7SVille Syrjälä 
186740e56410SVille Syrjälä static void ibx_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
186840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
1869776ad806SJesse Barnes {
187040e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
187142db67d6SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
1872776ad806SJesse Barnes 
18736a39d7c9SJani Nikula 	/*
18746a39d7c9SJani Nikula 	 * Somehow the PCH doesn't seem to really ack the interrupt to the CPU
18756a39d7c9SJani Nikula 	 * unless we touch the hotplug register, even if hotplug_trigger is
18766a39d7c9SJani Nikula 	 * zero. Not acking leads to "The master control interrupt lied (SDE)!"
18776a39d7c9SJani Nikula 	 * errors.
18786a39d7c9SJani Nikula 	 */
187913cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
18806a39d7c9SJani Nikula 	if (!hotplug_trigger) {
18816a39d7c9SJani Nikula 		u32 mask = PORTA_HOTPLUG_STATUS_MASK |
18826a39d7c9SJani Nikula 			PORTD_HOTPLUG_STATUS_MASK |
18836a39d7c9SJani Nikula 			PORTC_HOTPLUG_STATUS_MASK |
18846a39d7c9SJani Nikula 			PORTB_HOTPLUG_STATUS_MASK;
18856a39d7c9SJani Nikula 		dig_hotplug_reg &= ~mask;
18866a39d7c9SJani Nikula 	}
18876a39d7c9SJani Nikula 
188813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
18896a39d7c9SJani Nikula 	if (!hotplug_trigger)
18906a39d7c9SJani Nikula 		return;
189113cf5504SDave Airlie 
1892fd63e2a9SImre Deak 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
189340e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
1894fd63e2a9SImre Deak 			   pch_port_hotplug_long_detect);
189540e56410SVille Syrjälä 
1896676574dfSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
1897aaf5ec2eSSonika Jindal }
189891d131d2SDaniel Vetter 
189940e56410SVille Syrjälä static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
190040e56410SVille Syrjälä {
190140e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
190240e56410SVille Syrjälä 	int pipe;
190340e56410SVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
190440e56410SVille Syrjälä 
190540e56410SVille Syrjälä 	ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
190640e56410SVille Syrjälä 
1907cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1908cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1909776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1910cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1911cfc33bf7SVille Syrjälä 				 port_name(port));
1912cfc33bf7SVille Syrjälä 	}
1913776ad806SJesse Barnes 
1914ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1915ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1916ce99c256SDaniel Vetter 
1917776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1918515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1919776ad806SJesse Barnes 
1920776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1921776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1922776ad806SJesse Barnes 
1923776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1924776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1925776ad806SJesse Barnes 
1926776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1927776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1928776ad806SJesse Barnes 
19299db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1930055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19319db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19329db4a9c7SJesse Barnes 					 pipe_name(pipe),
19339db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1934776ad806SJesse Barnes 
1935776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1936776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1937776ad806SJesse Barnes 
1938776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1939776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1940776ad806SJesse Barnes 
1941776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19421f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19438664281bSPaulo Zanoni 
19448664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19451f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19468664281bSPaulo Zanoni }
19478664281bSPaulo Zanoni 
19488664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19498664281bSPaulo Zanoni {
19508664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19518664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19525a69b89fSDaniel Vetter 	enum pipe pipe;
19538664281bSPaulo Zanoni 
1954de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1955de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1956de032bf4SPaulo Zanoni 
1957055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19581f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19591f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19608664281bSPaulo Zanoni 
19615a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19625a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1963277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19645a69b89fSDaniel Vetter 			else
1965277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19665a69b89fSDaniel Vetter 		}
19675a69b89fSDaniel Vetter 	}
19688bf1e9f1SShuang He 
19698664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19708664281bSPaulo Zanoni }
19718664281bSPaulo Zanoni 
19728664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19738664281bSPaulo Zanoni {
19748664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19758664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19768664281bSPaulo Zanoni 
1977de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1978de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1979de032bf4SPaulo Zanoni 
19808664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19811f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19828664281bSPaulo Zanoni 
19838664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19841f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19858664281bSPaulo Zanoni 
19868664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19871f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19888664281bSPaulo Zanoni 
19898664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1990776ad806SJesse Barnes }
1991776ad806SJesse Barnes 
199223e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
199323e81d69SAdam Jackson {
19942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
199523e81d69SAdam Jackson 	int pipe;
19966dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
1997aaf5ec2eSSonika Jindal 
199840e56410SVille Syrjälä 	ibx_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
199991d131d2SDaniel Vetter 
2000cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2001cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
200223e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2003cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2004cfc33bf7SVille Syrjälä 				 port_name(port));
2005cfc33bf7SVille Syrjälä 	}
200623e81d69SAdam Jackson 
200723e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2008ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
200923e81d69SAdam Jackson 
201023e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2011515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
201223e81d69SAdam Jackson 
201323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
201423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
201523e81d69SAdam Jackson 
201623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
201723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
201823e81d69SAdam Jackson 
201923e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2020055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
202123e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
202223e81d69SAdam Jackson 					 pipe_name(pipe),
202323e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20248664281bSPaulo Zanoni 
20258664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20268664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
202723e81d69SAdam Jackson }
202823e81d69SAdam Jackson 
20296dbf30ceSVille Syrjälä static void spt_irq_handler(struct drm_device *dev, u32 pch_iir)
20306dbf30ceSVille Syrjälä {
20316dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
20326dbf30ceSVille Syrjälä 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_SPT &
20336dbf30ceSVille Syrjälä 		~SDE_PORTE_HOTPLUG_SPT;
20346dbf30ceSVille Syrjälä 	u32 hotplug2_trigger = pch_iir & SDE_PORTE_HOTPLUG_SPT;
20356dbf30ceSVille Syrjälä 	u32 pin_mask = 0, long_mask = 0;
20366dbf30ceSVille Syrjälä 
20376dbf30ceSVille Syrjälä 	if (hotplug_trigger) {
20386dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20396dbf30ceSVille Syrjälä 
20406dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
20416dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
20426dbf30ceSVille Syrjälä 
20436dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
20446dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
204574c0b395SVille Syrjälä 				   spt_port_hotplug_long_detect);
20466dbf30ceSVille Syrjälä 	}
20476dbf30ceSVille Syrjälä 
20486dbf30ceSVille Syrjälä 	if (hotplug2_trigger) {
20496dbf30ceSVille Syrjälä 		u32 dig_hotplug_reg;
20506dbf30ceSVille Syrjälä 
20516dbf30ceSVille Syrjälä 		dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG2);
20526dbf30ceSVille Syrjälä 		I915_WRITE(PCH_PORT_HOTPLUG2, dig_hotplug_reg);
20536dbf30ceSVille Syrjälä 
20546dbf30ceSVille Syrjälä 		intel_get_hpd_pins(&pin_mask, &long_mask, hotplug2_trigger,
20556dbf30ceSVille Syrjälä 				   dig_hotplug_reg, hpd_spt,
20566dbf30ceSVille Syrjälä 				   spt_port_hotplug2_long_detect);
20576dbf30ceSVille Syrjälä 	}
20586dbf30ceSVille Syrjälä 
20596dbf30ceSVille Syrjälä 	if (pin_mask)
20606dbf30ceSVille Syrjälä 		intel_hpd_irq_handler(dev, pin_mask, long_mask);
20616dbf30ceSVille Syrjälä 
20626dbf30ceSVille Syrjälä 	if (pch_iir & SDE_GMBUS_CPT)
20636dbf30ceSVille Syrjälä 		gmbus_irq_handler(dev);
20646dbf30ceSVille Syrjälä }
20656dbf30ceSVille Syrjälä 
206640e56410SVille Syrjälä static void ilk_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
206740e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2068c008bc6eSPaulo Zanoni {
206940e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2070e4ce95aaSVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2071e4ce95aaSVille Syrjälä 
2072e4ce95aaSVille Syrjälä 	dig_hotplug_reg = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
2073e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, dig_hotplug_reg);
2074e4ce95aaSVille Syrjälä 
2075e4ce95aaSVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
207640e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2077e4ce95aaSVille Syrjälä 			   ilk_port_hotplug_long_detect);
207840e56410SVille Syrjälä 
2079e4ce95aaSVille Syrjälä 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2080e4ce95aaSVille Syrjälä }
2081c008bc6eSPaulo Zanoni 
208240e56410SVille Syrjälä static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
208340e56410SVille Syrjälä {
208440e56410SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
208540e56410SVille Syrjälä 	enum pipe pipe;
208640e56410SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG;
208740e56410SVille Syrjälä 
208840e56410SVille Syrjälä 	if (hotplug_trigger)
208940e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ilk);
209040e56410SVille Syrjälä 
2091c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2092c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2093c008bc6eSPaulo Zanoni 
2094c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2095c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2096c008bc6eSPaulo Zanoni 
2097c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2098c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2099c008bc6eSPaulo Zanoni 
2100055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2101d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2102d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2103d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2104c008bc6eSPaulo Zanoni 
210540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
21061f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2107c008bc6eSPaulo Zanoni 
210840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
210940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
21105b3a856bSDaniel Vetter 
211140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
211240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
211340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
211440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2115c008bc6eSPaulo Zanoni 		}
2116c008bc6eSPaulo Zanoni 	}
2117c008bc6eSPaulo Zanoni 
2118c008bc6eSPaulo Zanoni 	/* check event from PCH */
2119c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2120c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2121c008bc6eSPaulo Zanoni 
2122c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2123c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2124c008bc6eSPaulo Zanoni 		else
2125c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2126c008bc6eSPaulo Zanoni 
2127c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2128c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2129c008bc6eSPaulo Zanoni 	}
2130c008bc6eSPaulo Zanoni 
2131c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2132c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2133c008bc6eSPaulo Zanoni }
2134c008bc6eSPaulo Zanoni 
21359719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21369719fb98SPaulo Zanoni {
21379719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
213807d27e20SDamien Lespiau 	enum pipe pipe;
213923bb4cb5SVille Syrjälä 	u32 hotplug_trigger = de_iir & DE_DP_A_HOTPLUG_IVB;
214023bb4cb5SVille Syrjälä 
214140e56410SVille Syrjälä 	if (hotplug_trigger)
214240e56410SVille Syrjälä 		ilk_hpd_irq_handler(dev, hotplug_trigger, hpd_ivb);
21439719fb98SPaulo Zanoni 
21449719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21459719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21469719fb98SPaulo Zanoni 
21479719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21489719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21499719fb98SPaulo Zanoni 
21509719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21519719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21529719fb98SPaulo Zanoni 
2153055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2154d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2155d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2156d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
215740da17c2SDaniel Vetter 
215840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
215907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
216007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
216107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21629719fb98SPaulo Zanoni 		}
21639719fb98SPaulo Zanoni 	}
21649719fb98SPaulo Zanoni 
21659719fb98SPaulo Zanoni 	/* check event from PCH */
21669719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21679719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21689719fb98SPaulo Zanoni 
21699719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21709719fb98SPaulo Zanoni 
21719719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21729719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21739719fb98SPaulo Zanoni 	}
21749719fb98SPaulo Zanoni }
21759719fb98SPaulo Zanoni 
217672c90f62SOscar Mateo /*
217772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
217872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
217972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
218072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
218172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
218272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
218372c90f62SOscar Mateo  */
2184f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2185b1f14ad0SJesse Barnes {
218645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2188f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21890e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2190b1f14ad0SJesse Barnes 
21912dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21922dd2a883SImre Deak 		return IRQ_NONE;
21932dd2a883SImre Deak 
21941f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
21951f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
21961f814dacSImre Deak 
2197b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2198b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2199b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
220023a78516SPaulo Zanoni 	POSTING_READ(DEIER);
22010e43406bSChris Wilson 
220244498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
220344498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
220444498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
220544498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
220644498aeaSPaulo Zanoni 	 * due to its back queue). */
2207ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
220844498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
220944498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
221044498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2211ab5c608bSBen Widawsky 	}
221244498aeaSPaulo Zanoni 
221372c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
221472c90f62SOscar Mateo 
22150e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
22160e43406bSChris Wilson 	if (gt_iir) {
221772c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
221872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2219d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
22200e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2221d8fc8a47SPaulo Zanoni 		else
2222d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
22230e43406bSChris Wilson 	}
2224b1f14ad0SJesse Barnes 
2225b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22260e43406bSChris Wilson 	if (de_iir) {
222772c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
222872c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2229f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
22309719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2231f1af8fc1SPaulo Zanoni 		else
2232f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
22330e43406bSChris Wilson 	}
22340e43406bSChris Wilson 
2235f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2236f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22370e43406bSChris Wilson 		if (pm_iir) {
2238b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22390e43406bSChris Wilson 			ret = IRQ_HANDLED;
224072c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22410e43406bSChris Wilson 		}
2242f1af8fc1SPaulo Zanoni 	}
2243b1f14ad0SJesse Barnes 
2244b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2245b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2246ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
224744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
224844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2249ab5c608bSBen Widawsky 	}
2250b1f14ad0SJesse Barnes 
22511f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
22521f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
22531f814dacSImre Deak 
2254b1f14ad0SJesse Barnes 	return ret;
2255b1f14ad0SJesse Barnes }
2256b1f14ad0SJesse Barnes 
225740e56410SVille Syrjälä static void bxt_hpd_irq_handler(struct drm_device *dev, u32 hotplug_trigger,
225840e56410SVille Syrjälä 				const u32 hpd[HPD_NUM_PINS])
2259d04a492dSShashank Sharma {
2260cebd87a0SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
2261cebd87a0SVille Syrjälä 	u32 dig_hotplug_reg, pin_mask = 0, long_mask = 0;
2262d04a492dSShashank Sharma 
2263a52bb15bSVille Syrjälä 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
2264a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
2265d04a492dSShashank Sharma 
2266cebd87a0SVille Syrjälä 	intel_get_hpd_pins(&pin_mask, &long_mask, hotplug_trigger,
226740e56410SVille Syrjälä 			   dig_hotplug_reg, hpd,
2268cebd87a0SVille Syrjälä 			   bxt_port_hotplug_long_detect);
226940e56410SVille Syrjälä 
2270475c2e3bSJani Nikula 	intel_hpd_irq_handler(dev, pin_mask, long_mask);
2271d04a492dSShashank Sharma }
2272d04a492dSShashank Sharma 
2273f11a0f46STvrtko Ursulin static irqreturn_t
2274f11a0f46STvrtko Ursulin gen8_de_irq_handler(struct drm_i915_private *dev_priv, u32 master_ctl)
2275abd58f01SBen Widawsky {
2276f11a0f46STvrtko Ursulin 	struct drm_device *dev = dev_priv->dev;
2277abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2278f11a0f46STvrtko Ursulin 	u32 iir;
2279c42664ccSDaniel Vetter 	enum pipe pipe;
228088e04703SJesse Barnes 
2281abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2282e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_MISC_IIR);
2283e32192e1STvrtko Ursulin 		if (iir) {
2284e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_MISC_IIR, iir);
2285abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2286e32192e1STvrtko Ursulin 			if (iir & GEN8_DE_MISC_GSE)
228738cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
228838cc46d7SOscar Mateo 			else
228938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2290abd58f01SBen Widawsky 		}
229138cc46d7SOscar Mateo 		else
229238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2293abd58f01SBen Widawsky 	}
2294abd58f01SBen Widawsky 
22956d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
2296e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PORT_IIR);
2297e32192e1STvrtko Ursulin 		if (iir) {
2298e32192e1STvrtko Ursulin 			u32 tmp_mask;
2299d04a492dSShashank Sharma 			bool found = false;
2300cebd87a0SVille Syrjälä 
2301e32192e1STvrtko Ursulin 			I915_WRITE(GEN8_DE_PORT_IIR, iir);
23026d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
230388e04703SJesse Barnes 
2304e32192e1STvrtko Ursulin 			tmp_mask = GEN8_AUX_CHANNEL_A;
2305e32192e1STvrtko Ursulin 			if (INTEL_INFO(dev_priv)->gen >= 9)
2306e32192e1STvrtko Ursulin 				tmp_mask |= GEN9_AUX_CHANNEL_B |
2307e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_C |
2308e32192e1STvrtko Ursulin 					    GEN9_AUX_CHANNEL_D;
2309e32192e1STvrtko Ursulin 
2310e32192e1STvrtko Ursulin 			if (iir & tmp_mask) {
231138cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2312d04a492dSShashank Sharma 				found = true;
2313d04a492dSShashank Sharma 			}
2314d04a492dSShashank Sharma 
2315e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev_priv)) {
2316e32192e1STvrtko Ursulin 				tmp_mask = iir & BXT_DE_PORT_HOTPLUG_MASK;
2317e32192e1STvrtko Ursulin 				if (tmp_mask) {
2318e32192e1STvrtko Ursulin 					bxt_hpd_irq_handler(dev, tmp_mask, hpd_bxt);
2319d04a492dSShashank Sharma 					found = true;
2320d04a492dSShashank Sharma 				}
2321e32192e1STvrtko Ursulin 			} else if (IS_BROADWELL(dev_priv)) {
2322e32192e1STvrtko Ursulin 				tmp_mask = iir & GEN8_PORT_DP_A_HOTPLUG;
2323e32192e1STvrtko Ursulin 				if (tmp_mask) {
2324e32192e1STvrtko Ursulin 					ilk_hpd_irq_handler(dev, tmp_mask, hpd_bdw);
2325e32192e1STvrtko Ursulin 					found = true;
2326e32192e1STvrtko Ursulin 				}
2327e32192e1STvrtko Ursulin 			}
2328d04a492dSShashank Sharma 
2329e32192e1STvrtko Ursulin 			if (IS_BROXTON(dev) && (iir & BXT_DE_PORT_GMBUS)) {
23309e63743eSShashank Sharma 				gmbus_irq_handler(dev);
23319e63743eSShashank Sharma 				found = true;
23329e63743eSShashank Sharma 			}
23339e63743eSShashank Sharma 
2334d04a492dSShashank Sharma 			if (!found)
233538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
23366d766f02SDaniel Vetter 		}
233738cc46d7SOscar Mateo 		else
233838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
23396d766f02SDaniel Vetter 	}
23406d766f02SDaniel Vetter 
2341055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2342e32192e1STvrtko Ursulin 		u32 flip_done, fault_errors;
2343abd58f01SBen Widawsky 
2344c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2345c42664ccSDaniel Vetter 			continue;
2346c42664ccSDaniel Vetter 
2347e32192e1STvrtko Ursulin 		iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2348e32192e1STvrtko Ursulin 		if (!iir) {
2349e32192e1STvrtko Ursulin 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2350e32192e1STvrtko Ursulin 			continue;
2351e32192e1STvrtko Ursulin 		}
2352770de83dSDamien Lespiau 
2353e32192e1STvrtko Ursulin 		ret = IRQ_HANDLED;
2354e32192e1STvrtko Ursulin 		I915_WRITE(GEN8_DE_PIPE_IIR(pipe), iir);
2355e32192e1STvrtko Ursulin 
2356e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_VBLANK &&
2357d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2358d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2359abd58f01SBen Widawsky 
2360e32192e1STvrtko Ursulin 		flip_done = iir;
2361b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2362e32192e1STvrtko Ursulin 			flip_done &= GEN9_PIPE_PLANE1_FLIP_DONE;
2363770de83dSDamien Lespiau 		else
2364e32192e1STvrtko Ursulin 			flip_done &= GEN8_PIPE_PRIMARY_FLIP_DONE;
2365770de83dSDamien Lespiau 
2366770de83dSDamien Lespiau 		if (flip_done) {
2367abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2368abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2369abd58f01SBen Widawsky 		}
2370abd58f01SBen Widawsky 
2371e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_CDCLK_CRC_DONE)
23720fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
23730fbe7870SDaniel Vetter 
2374e32192e1STvrtko Ursulin 		if (iir & GEN8_PIPE_FIFO_UNDERRUN)
2375e32192e1STvrtko Ursulin 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
237638d83c96SDaniel Vetter 
2377e32192e1STvrtko Ursulin 		fault_errors = iir;
2378b4834a50SRodrigo Vivi 		if (INTEL_INFO(dev_priv)->gen >= 9)
2379e32192e1STvrtko Ursulin 			fault_errors &= GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2380770de83dSDamien Lespiau 		else
2381e32192e1STvrtko Ursulin 			fault_errors &= GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2382770de83dSDamien Lespiau 
2383770de83dSDamien Lespiau 		if (fault_errors)
238430100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
238530100f2bSDaniel Vetter 				  pipe_name(pipe),
2386e32192e1STvrtko Ursulin 				  fault_errors);
2387abd58f01SBen Widawsky 	}
2388abd58f01SBen Widawsky 
2389266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2390266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
239192d03a80SDaniel Vetter 		/*
239292d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
239392d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
239492d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
239592d03a80SDaniel Vetter 		 */
2396e32192e1STvrtko Ursulin 		iir = I915_READ(SDEIIR);
2397e32192e1STvrtko Ursulin 		if (iir) {
2398e32192e1STvrtko Ursulin 			I915_WRITE(SDEIIR, iir);
239992d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
24006dbf30ceSVille Syrjälä 
24016dbf30ceSVille Syrjälä 			if (HAS_PCH_SPT(dev_priv))
2402e32192e1STvrtko Ursulin 				spt_irq_handler(dev, iir);
24036dbf30ceSVille Syrjälä 			else
2404e32192e1STvrtko Ursulin 				cpt_irq_handler(dev, iir);
24052dfb0b81SJani Nikula 		} else {
24062dfb0b81SJani Nikula 			/*
24072dfb0b81SJani Nikula 			 * Like on previous PCH there seems to be something
24082dfb0b81SJani Nikula 			 * fishy going on with forwarding PCH interrupts.
24092dfb0b81SJani Nikula 			 */
24102dfb0b81SJani Nikula 			DRM_DEBUG_DRIVER("The master control interrupt lied (SDE)!\n");
24112dfb0b81SJani Nikula 		}
241292d03a80SDaniel Vetter 	}
241392d03a80SDaniel Vetter 
2414f11a0f46STvrtko Ursulin 	return ret;
2415f11a0f46STvrtko Ursulin }
2416f11a0f46STvrtko Ursulin 
2417f11a0f46STvrtko Ursulin static irqreturn_t gen8_irq_handler(int irq, void *arg)
2418f11a0f46STvrtko Ursulin {
2419f11a0f46STvrtko Ursulin 	struct drm_device *dev = arg;
2420f11a0f46STvrtko Ursulin 	struct drm_i915_private *dev_priv = dev->dev_private;
2421f11a0f46STvrtko Ursulin 	u32 master_ctl;
2422f11a0f46STvrtko Ursulin 	irqreturn_t ret;
2423f11a0f46STvrtko Ursulin 
2424f11a0f46STvrtko Ursulin 	if (!intel_irqs_enabled(dev_priv))
2425f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2426f11a0f46STvrtko Ursulin 
2427f11a0f46STvrtko Ursulin 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2428f11a0f46STvrtko Ursulin 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2429f11a0f46STvrtko Ursulin 	if (!master_ctl)
2430f11a0f46STvrtko Ursulin 		return IRQ_NONE;
2431f11a0f46STvrtko Ursulin 
2432f11a0f46STvrtko Ursulin 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2433f11a0f46STvrtko Ursulin 
2434f11a0f46STvrtko Ursulin 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
2435f11a0f46STvrtko Ursulin 	disable_rpm_wakeref_asserts(dev_priv);
2436f11a0f46STvrtko Ursulin 
2437f11a0f46STvrtko Ursulin 	/* Find, clear, then process each source of interrupt */
2438f11a0f46STvrtko Ursulin 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2439f11a0f46STvrtko Ursulin 	ret |= gen8_de_irq_handler(dev_priv, master_ctl);
2440f11a0f46STvrtko Ursulin 
2441cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2442cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2443abd58f01SBen Widawsky 
24441f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
24451f814dacSImre Deak 
2446abd58f01SBen Widawsky 	return ret;
2447abd58f01SBen Widawsky }
2448abd58f01SBen Widawsky 
244917e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
245017e1df07SDaniel Vetter 			       bool reset_completed)
245117e1df07SDaniel Vetter {
2452e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
245317e1df07SDaniel Vetter 
245417e1df07SDaniel Vetter 	/*
245517e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
245617e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
245717e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
245817e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
245917e1df07SDaniel Vetter 	 */
246017e1df07SDaniel Vetter 
246117e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
2462b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
2463e2f80391STvrtko Ursulin 		wake_up_all(&engine->irq_queue);
246417e1df07SDaniel Vetter 
246517e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
246617e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
246717e1df07SDaniel Vetter 
246817e1df07SDaniel Vetter 	/*
246917e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
247017e1df07SDaniel Vetter 	 * reset state is cleared.
247117e1df07SDaniel Vetter 	 */
247217e1df07SDaniel Vetter 	if (reset_completed)
247317e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
247417e1df07SDaniel Vetter }
247517e1df07SDaniel Vetter 
24768a905236SJesse Barnes /**
2477b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
2478468f9d29SJavier Martinez Canillas  * @dev: drm device
24798a905236SJesse Barnes  *
24808a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24818a905236SJesse Barnes  * was detected.
24828a905236SJesse Barnes  */
2483b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24848a905236SJesse Barnes {
2485b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2486cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2487cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2488cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
248917e1df07SDaniel Vetter 	int ret;
24908a905236SJesse Barnes 
24915bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24928a905236SJesse Barnes 
24937db0ba24SDaniel Vetter 	/*
24947db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24957db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24967db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24977db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24987db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24997db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
25007db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
25017db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
25027db0ba24SDaniel Vetter 	 */
2503*d98c52cfSChris Wilson 	if (i915_reset_in_progress(&dev_priv->gpu_error)) {
250444d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
25055bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
25067db0ba24SDaniel Vetter 				   reset_event);
25071f83fee0SDaniel Vetter 
250817e1df07SDaniel Vetter 		/*
2509f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2510f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2511f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2512f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2513f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2514f454c694SImre Deak 		 */
2515f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
25167514747dSVille Syrjälä 
25177514747dSVille Syrjälä 		intel_prepare_reset(dev);
25187514747dSVille Syrjälä 
2519f454c694SImre Deak 		/*
252017e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
252117e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
252217e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
252317e1df07SDaniel Vetter 		 * deadlocks with the reset work.
252417e1df07SDaniel Vetter 		 */
2525f69061beSDaniel Vetter 		ret = i915_reset(dev);
2526f69061beSDaniel Vetter 
25277514747dSVille Syrjälä 		intel_finish_reset(dev);
252817e1df07SDaniel Vetter 
2529f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2530f454c694SImre Deak 
2531*d98c52cfSChris Wilson 		if (ret == 0)
25325bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2533f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
25341f83fee0SDaniel Vetter 
253517e1df07SDaniel Vetter 		/*
253617e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
253717e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
253817e1df07SDaniel Vetter 		 */
253917e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2540f316a42cSBen Gamari 	}
25418a905236SJesse Barnes }
25428a905236SJesse Barnes 
254335aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2544c0e09200SDave Airlie {
25458a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2546bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
254763eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2548050ee91fSBen Widawsky 	int pipe, i;
254963eeaf38SJesse Barnes 
255035aed2e6SChris Wilson 	if (!eir)
255135aed2e6SChris Wilson 		return;
255263eeaf38SJesse Barnes 
2553a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25548a905236SJesse Barnes 
2555bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2556bd9854f9SBen Widawsky 
25578a905236SJesse Barnes 	if (IS_G4X(dev)) {
25588a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25598a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25608a905236SJesse Barnes 
2561a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2562a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2563050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2564050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2565a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2566a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25678a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25683143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25698a905236SJesse Barnes 		}
25708a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25718a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2572a70491ccSJoe Perches 			pr_err("page table error\n");
2573a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25748a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25753143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25768a905236SJesse Barnes 		}
25778a905236SJesse Barnes 	}
25788a905236SJesse Barnes 
2579a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
258063eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
258163eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2582a70491ccSJoe Perches 			pr_err("page table error\n");
2583a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
258463eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25853143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
258663eeaf38SJesse Barnes 		}
25878a905236SJesse Barnes 	}
25888a905236SJesse Barnes 
258963eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2590a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2591055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2592a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25939db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
259463eeaf38SJesse Barnes 		/* pipestat has already been acked */
259563eeaf38SJesse Barnes 	}
259663eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2597a70491ccSJoe Perches 		pr_err("instruction error\n");
2598a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2599050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2600050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2601a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
260263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
260363eeaf38SJesse Barnes 
2604a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2605a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2606a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
260763eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
26083143a2bfSChris Wilson 			POSTING_READ(IPEIR);
260963eeaf38SJesse Barnes 		} else {
261063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
261163eeaf38SJesse Barnes 
2612a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2613a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2614a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2615a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
261663eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26173143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
261863eeaf38SJesse Barnes 		}
261963eeaf38SJesse Barnes 	}
262063eeaf38SJesse Barnes 
262163eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
26223143a2bfSChris Wilson 	POSTING_READ(EIR);
262363eeaf38SJesse Barnes 	eir = I915_READ(EIR);
262463eeaf38SJesse Barnes 	if (eir) {
262563eeaf38SJesse Barnes 		/*
262663eeaf38SJesse Barnes 		 * some errors might have become stuck,
262763eeaf38SJesse Barnes 		 * mask them.
262863eeaf38SJesse Barnes 		 */
262963eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
263063eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
263163eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
263263eeaf38SJesse Barnes 	}
263335aed2e6SChris Wilson }
263435aed2e6SChris Wilson 
263535aed2e6SChris Wilson /**
2636b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
263735aed2e6SChris Wilson  * @dev: drm device
263814b730fcSarun.siluvery@linux.intel.com  * @engine_mask: mask representing engines that are hung
2639aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
264035aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
264135aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
264235aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
264335aed2e6SChris Wilson  * of a ring dump etc.).
264435aed2e6SChris Wilson  */
264514b730fcSarun.siluvery@linux.intel.com void i915_handle_error(struct drm_device *dev, u32 engine_mask,
264658174462SMika Kuoppala 		       const char *fmt, ...)
264735aed2e6SChris Wilson {
264835aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
264958174462SMika Kuoppala 	va_list args;
265058174462SMika Kuoppala 	char error_msg[80];
265135aed2e6SChris Wilson 
265258174462SMika Kuoppala 	va_start(args, fmt);
265358174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
265458174462SMika Kuoppala 	va_end(args);
265558174462SMika Kuoppala 
265614b730fcSarun.siluvery@linux.intel.com 	i915_capture_error_state(dev, engine_mask, error_msg);
265735aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
26588a905236SJesse Barnes 
265914b730fcSarun.siluvery@linux.intel.com 	if (engine_mask) {
2660805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2661f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2662ba1234d1SBen Gamari 
266311ed50ecSBen Gamari 		/*
2664b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2665b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2666b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
266717e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
266817e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
266917e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
267017e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
267117e1df07SDaniel Vetter 		 *
267217e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
267317e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
267417e1df07SDaniel Vetter 		 * counter atomic_t.
267511ed50ecSBen Gamari 		 */
267617e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
267711ed50ecSBen Gamari 	}
267811ed50ecSBen Gamari 
2679b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26808a905236SJesse Barnes }
26818a905236SJesse Barnes 
268242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
268342f52ef8SKeith Packard  * we use as a pipe index
268442f52ef8SKeith Packard  */
268588e72717SThierry Reding static int i915_enable_vblank(struct drm_device *dev, unsigned int pipe)
26860a3e67a4SJesse Barnes {
26872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2688e9d21d7fSKeith Packard 	unsigned long irqflags;
268971e0ffa5SJesse Barnes 
26901ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2691f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26927c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2693755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26940a3e67a4SJesse Barnes 	else
26957c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2696755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26971ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26988692d00eSChris Wilson 
26990a3e67a4SJesse Barnes 	return 0;
27000a3e67a4SJesse Barnes }
27010a3e67a4SJesse Barnes 
270288e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2703f796cf8fSJesse Barnes {
27042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2705f796cf8fSJesse Barnes 	unsigned long irqflags;
2706b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
270740da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2708f796cf8fSJesse Barnes 
2709f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2710fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2711b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2712b1f14ad0SJesse Barnes 
2713b1f14ad0SJesse Barnes 	return 0;
2714b1f14ad0SJesse Barnes }
2715b1f14ad0SJesse Barnes 
271688e72717SThierry Reding static int valleyview_enable_vblank(struct drm_device *dev, unsigned int pipe)
27177e231dbeSJesse Barnes {
27182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27197e231dbeSJesse Barnes 	unsigned long irqflags;
27207e231dbeSJesse Barnes 
27217e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
272231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2723755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27247e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27257e231dbeSJesse Barnes 
27267e231dbeSJesse Barnes 	return 0;
27277e231dbeSJesse Barnes }
27287e231dbeSJesse Barnes 
272988e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2730abd58f01SBen Widawsky {
2731abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2732abd58f01SBen Widawsky 	unsigned long irqflags;
2733abd58f01SBen Widawsky 
2734abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2735013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2736abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2737013d3752SVille Syrjälä 
2738abd58f01SBen Widawsky 	return 0;
2739abd58f01SBen Widawsky }
2740abd58f01SBen Widawsky 
274142f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
274242f52ef8SKeith Packard  * we use as a pipe index
274342f52ef8SKeith Packard  */
274488e72717SThierry Reding static void i915_disable_vblank(struct drm_device *dev, unsigned int pipe)
27450a3e67a4SJesse Barnes {
27462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2747e9d21d7fSKeith Packard 	unsigned long irqflags;
27480a3e67a4SJesse Barnes 
27491ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27507c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2751755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2752755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27531ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27540a3e67a4SJesse Barnes }
27550a3e67a4SJesse Barnes 
275688e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2757f796cf8fSJesse Barnes {
27582d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2759f796cf8fSJesse Barnes 	unsigned long irqflags;
2760b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
276140da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2762f796cf8fSJesse Barnes 
2763f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2764fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2765b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2766b1f14ad0SJesse Barnes }
2767b1f14ad0SJesse Barnes 
276888e72717SThierry Reding static void valleyview_disable_vblank(struct drm_device *dev, unsigned int pipe)
27697e231dbeSJesse Barnes {
27702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27717e231dbeSJesse Barnes 	unsigned long irqflags;
27727e231dbeSJesse Barnes 
27737e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
277431acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2775755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27767e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27777e231dbeSJesse Barnes }
27787e231dbeSJesse Barnes 
277988e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2780abd58f01SBen Widawsky {
2781abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2782abd58f01SBen Widawsky 	unsigned long irqflags;
2783abd58f01SBen Widawsky 
2784abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2785013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2786abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2787abd58f01SBen Widawsky }
2788abd58f01SBen Widawsky 
27899107e9d2SChris Wilson static bool
27900bc40be8STvrtko Ursulin ring_idle(struct intel_engine_cs *engine, u32 seqno)
2791893eead0SChris Wilson {
2792cffa781eSChris Wilson 	return i915_seqno_passed(seqno,
2793cffa781eSChris Wilson 				 READ_ONCE(engine->last_submitted_seqno));
2794f65d9421SBen Gamari }
2795f65d9421SBen Gamari 
2796a028c4b0SDaniel Vetter static bool
2797a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2798a028c4b0SDaniel Vetter {
2799a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2800a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2801a028c4b0SDaniel Vetter 	} else {
2802a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2803a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2804a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2805a028c4b0SDaniel Vetter 	}
2806a028c4b0SDaniel Vetter }
2807a028c4b0SDaniel Vetter 
2808a4872ba6SOscar Mateo static struct intel_engine_cs *
28090bc40be8STvrtko Ursulin semaphore_wait_to_signaller_ring(struct intel_engine_cs *engine, u32 ipehr,
28100bc40be8STvrtko Ursulin 				 u64 offset)
2811921d42eaSDaniel Vetter {
28120bc40be8STvrtko Ursulin 	struct drm_i915_private *dev_priv = engine->dev->dev_private;
2813a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2814921d42eaSDaniel Vetter 
28152d1fe073SJoonas Lahtinen 	if (INTEL_INFO(dev_priv)->gen >= 8) {
2816b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28170bc40be8STvrtko Ursulin 			if (engine == signaller)
2818a6cdb93aSRodrigo Vivi 				continue;
2819a6cdb93aSRodrigo Vivi 
28200bc40be8STvrtko Ursulin 			if (offset == signaller->semaphore.signal_ggtt[engine->id])
2821a6cdb93aSRodrigo Vivi 				return signaller;
2822a6cdb93aSRodrigo Vivi 		}
2823921d42eaSDaniel Vetter 	} else {
2824921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2825921d42eaSDaniel Vetter 
2826b4ac5afcSDave Gordon 		for_each_engine(signaller, dev_priv) {
28270bc40be8STvrtko Ursulin 			if(engine == signaller)
2828921d42eaSDaniel Vetter 				continue;
2829921d42eaSDaniel Vetter 
28300bc40be8STvrtko Ursulin 			if (sync_bits == signaller->semaphore.mbox.wait[engine->id])
2831921d42eaSDaniel Vetter 				return signaller;
2832921d42eaSDaniel Vetter 		}
2833921d42eaSDaniel Vetter 	}
2834921d42eaSDaniel Vetter 
2835a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
28360bc40be8STvrtko Ursulin 		  engine->id, ipehr, offset);
2837921d42eaSDaniel Vetter 
2838921d42eaSDaniel Vetter 	return NULL;
2839921d42eaSDaniel Vetter }
2840921d42eaSDaniel Vetter 
2841a4872ba6SOscar Mateo static struct intel_engine_cs *
28420bc40be8STvrtko Ursulin semaphore_waits_for(struct intel_engine_cs *engine, u32 *seqno)
2843a24a11e6SChris Wilson {
28440bc40be8STvrtko Ursulin 	struct drm_i915_private *dev_priv = engine->dev->dev_private;
284588fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2846a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2847a6cdb93aSRodrigo Vivi 	int i, backwards;
2848a24a11e6SChris Wilson 
2849381e8ae3STomas Elf 	/*
2850381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2851381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2852381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2853381e8ae3STomas Elf 	 * mode.
2854381e8ae3STomas Elf 	 *
2855381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2856381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2857381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2858381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2859381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2860381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2861381e8ae3STomas Elf 	 * the hang checker to deadlock.
2862381e8ae3STomas Elf 	 *
2863381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2864381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2865381e8ae3STomas Elf 	 */
28660bc40be8STvrtko Ursulin 	if (engine->buffer == NULL)
2867381e8ae3STomas Elf 		return NULL;
2868381e8ae3STomas Elf 
28690bc40be8STvrtko Ursulin 	ipehr = I915_READ(RING_IPEHR(engine->mmio_base));
28700bc40be8STvrtko Ursulin 	if (!ipehr_is_semaphore_wait(engine->dev, ipehr))
28716274f212SChris Wilson 		return NULL;
2872a24a11e6SChris Wilson 
287388fe429dSDaniel Vetter 	/*
287488fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
287588fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2876a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2877a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
287888fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
287988fe429dSDaniel Vetter 	 * ringbuffer itself.
2880a24a11e6SChris Wilson 	 */
28810bc40be8STvrtko Ursulin 	head = I915_READ_HEAD(engine) & HEAD_ADDR;
28820bc40be8STvrtko Ursulin 	backwards = (INTEL_INFO(engine->dev)->gen >= 8) ? 5 : 4;
288388fe429dSDaniel Vetter 
2884a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
288588fe429dSDaniel Vetter 		/*
288688fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
288788fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
288888fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
288988fe429dSDaniel Vetter 		 */
28900bc40be8STvrtko Ursulin 		head &= engine->buffer->size - 1;
289188fe429dSDaniel Vetter 
289288fe429dSDaniel Vetter 		/* This here seems to blow up */
28930bc40be8STvrtko Ursulin 		cmd = ioread32(engine->buffer->virtual_start + head);
2894a24a11e6SChris Wilson 		if (cmd == ipehr)
2895a24a11e6SChris Wilson 			break;
2896a24a11e6SChris Wilson 
289788fe429dSDaniel Vetter 		head -= 4;
289888fe429dSDaniel Vetter 	}
2899a24a11e6SChris Wilson 
290088fe429dSDaniel Vetter 	if (!i)
290188fe429dSDaniel Vetter 		return NULL;
290288fe429dSDaniel Vetter 
29030bc40be8STvrtko Ursulin 	*seqno = ioread32(engine->buffer->virtual_start + head + 4) + 1;
29040bc40be8STvrtko Ursulin 	if (INTEL_INFO(engine->dev)->gen >= 8) {
29050bc40be8STvrtko Ursulin 		offset = ioread32(engine->buffer->virtual_start + head + 12);
2906a6cdb93aSRodrigo Vivi 		offset <<= 32;
29070bc40be8STvrtko Ursulin 		offset = ioread32(engine->buffer->virtual_start + head + 8);
2908a6cdb93aSRodrigo Vivi 	}
29090bc40be8STvrtko Ursulin 	return semaphore_wait_to_signaller_ring(engine, ipehr, offset);
2910a24a11e6SChris Wilson }
2911a24a11e6SChris Wilson 
29120bc40be8STvrtko Ursulin static int semaphore_passed(struct intel_engine_cs *engine)
29136274f212SChris Wilson {
29140bc40be8STvrtko Ursulin 	struct drm_i915_private *dev_priv = engine->dev->dev_private;
2915a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2916a0d036b0SChris Wilson 	u32 seqno;
29176274f212SChris Wilson 
29180bc40be8STvrtko Ursulin 	engine->hangcheck.deadlock++;
29196274f212SChris Wilson 
29200bc40be8STvrtko Ursulin 	signaller = semaphore_waits_for(engine, &seqno);
29214be17381SChris Wilson 	if (signaller == NULL)
29224be17381SChris Wilson 		return -1;
29234be17381SChris Wilson 
29244be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
2925666796daSTvrtko Ursulin 	if (signaller->hangcheck.deadlock >= I915_NUM_ENGINES)
29266274f212SChris Wilson 		return -1;
29276274f212SChris Wilson 
2928c04e0f3bSChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller), seqno))
29294be17381SChris Wilson 		return 1;
29304be17381SChris Wilson 
2931a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2932a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2933a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29344be17381SChris Wilson 		return -1;
29354be17381SChris Wilson 
29364be17381SChris Wilson 	return 0;
29376274f212SChris Wilson }
29386274f212SChris Wilson 
29396274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29406274f212SChris Wilson {
2941e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
29426274f212SChris Wilson 
2943b4ac5afcSDave Gordon 	for_each_engine(engine, dev_priv)
2944e2f80391STvrtko Ursulin 		engine->hangcheck.deadlock = 0;
29456274f212SChris Wilson }
29466274f212SChris Wilson 
29470bc40be8STvrtko Ursulin static bool subunits_stuck(struct intel_engine_cs *engine)
29481ec14ad3SChris Wilson {
294961642ff0SMika Kuoppala 	u32 instdone[I915_NUM_INSTDONE_REG];
295061642ff0SMika Kuoppala 	bool stuck;
295161642ff0SMika Kuoppala 	int i;
29529107e9d2SChris Wilson 
29530bc40be8STvrtko Ursulin 	if (engine->id != RCS)
295461642ff0SMika Kuoppala 		return true;
295561642ff0SMika Kuoppala 
29560bc40be8STvrtko Ursulin 	i915_get_extra_instdone(engine->dev, instdone);
295761642ff0SMika Kuoppala 
295861642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
295961642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
296061642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
296161642ff0SMika Kuoppala 	 * consider those as progress.
296261642ff0SMika Kuoppala 	 */
296361642ff0SMika Kuoppala 	stuck = true;
296461642ff0SMika Kuoppala 	for (i = 0; i < I915_NUM_INSTDONE_REG; i++) {
29650bc40be8STvrtko Ursulin 		const u32 tmp = instdone[i] | engine->hangcheck.instdone[i];
296661642ff0SMika Kuoppala 
29670bc40be8STvrtko Ursulin 		if (tmp != engine->hangcheck.instdone[i])
296861642ff0SMika Kuoppala 			stuck = false;
296961642ff0SMika Kuoppala 
29700bc40be8STvrtko Ursulin 		engine->hangcheck.instdone[i] |= tmp;
297161642ff0SMika Kuoppala 	}
297261642ff0SMika Kuoppala 
297361642ff0SMika Kuoppala 	return stuck;
297461642ff0SMika Kuoppala }
297561642ff0SMika Kuoppala 
297661642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
29770bc40be8STvrtko Ursulin head_stuck(struct intel_engine_cs *engine, u64 acthd)
297861642ff0SMika Kuoppala {
29790bc40be8STvrtko Ursulin 	if (acthd != engine->hangcheck.acthd) {
298061642ff0SMika Kuoppala 
298161642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
29820bc40be8STvrtko Ursulin 		memset(engine->hangcheck.instdone, 0,
29830bc40be8STvrtko Ursulin 		       sizeof(engine->hangcheck.instdone));
298461642ff0SMika Kuoppala 
2985f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
2986f260fe7bSMika Kuoppala 	}
2987f260fe7bSMika Kuoppala 
29880bc40be8STvrtko Ursulin 	if (!subunits_stuck(engine))
298961642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
299061642ff0SMika Kuoppala 
299161642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
299261642ff0SMika Kuoppala }
299361642ff0SMika Kuoppala 
299461642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
29950bc40be8STvrtko Ursulin ring_stuck(struct intel_engine_cs *engine, u64 acthd)
299661642ff0SMika Kuoppala {
29970bc40be8STvrtko Ursulin 	struct drm_device *dev = engine->dev;
299861642ff0SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
299961642ff0SMika Kuoppala 	enum intel_ring_hangcheck_action ha;
300061642ff0SMika Kuoppala 	u32 tmp;
300161642ff0SMika Kuoppala 
30020bc40be8STvrtko Ursulin 	ha = head_stuck(engine, acthd);
300361642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
300461642ff0SMika Kuoppala 		return ha;
300561642ff0SMika Kuoppala 
30069107e9d2SChris Wilson 	if (IS_GEN2(dev))
3007f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30089107e9d2SChris Wilson 
30099107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30109107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30119107e9d2SChris Wilson 	 * and break the hang. This should work on
30129107e9d2SChris Wilson 	 * all but the second generation chipsets.
30139107e9d2SChris Wilson 	 */
30140bc40be8STvrtko Ursulin 	tmp = I915_READ_CTL(engine);
30151ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
301614b730fcSarun.siluvery@linux.intel.com 		i915_handle_error(dev, 0,
301758174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30180bc40be8STvrtko Ursulin 				  engine->name);
30190bc40be8STvrtko Ursulin 		I915_WRITE_CTL(engine, tmp);
3020f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30211ec14ad3SChris Wilson 	}
3022a24a11e6SChris Wilson 
30236274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30240bc40be8STvrtko Ursulin 		switch (semaphore_passed(engine)) {
30256274f212SChris Wilson 		default:
3026f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
30276274f212SChris Wilson 		case 1:
302814b730fcSarun.siluvery@linux.intel.com 			i915_handle_error(dev, 0,
302958174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
30300bc40be8STvrtko Ursulin 					  engine->name);
30310bc40be8STvrtko Ursulin 			I915_WRITE_CTL(engine, tmp);
3032f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
30336274f212SChris Wilson 		case 0:
3034f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
30356274f212SChris Wilson 		}
30369107e9d2SChris Wilson 	}
30379107e9d2SChris Wilson 
3038f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3039a24a11e6SChris Wilson }
3040d1e61e7fSChris Wilson 
304112471ba8SChris Wilson static unsigned kick_waiters(struct intel_engine_cs *engine)
304212471ba8SChris Wilson {
304312471ba8SChris Wilson 	struct drm_i915_private *i915 = to_i915(engine->dev);
304412471ba8SChris Wilson 	unsigned user_interrupts = READ_ONCE(engine->user_interrupts);
304512471ba8SChris Wilson 
304612471ba8SChris Wilson 	if (engine->hangcheck.user_interrupts == user_interrupts &&
304712471ba8SChris Wilson 	    !test_and_set_bit(engine->id, &i915->gpu_error.missed_irq_rings)) {
304812471ba8SChris Wilson 		if (!(i915->gpu_error.test_irq_rings & intel_engine_flag(engine)))
304912471ba8SChris Wilson 			DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
305012471ba8SChris Wilson 				  engine->name);
305112471ba8SChris Wilson 		else
305212471ba8SChris Wilson 			DRM_INFO("Fake missed irq on %s\n",
305312471ba8SChris Wilson 				 engine->name);
305412471ba8SChris Wilson 		wake_up_all(&engine->irq_queue);
305512471ba8SChris Wilson 	}
305612471ba8SChris Wilson 
305712471ba8SChris Wilson 	return user_interrupts;
305812471ba8SChris Wilson }
3059737b1506SChris Wilson /*
3060f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
306105407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
306205407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
306305407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
306405407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
306505407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3066f65d9421SBen Gamari  */
3067737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3068f65d9421SBen Gamari {
3069737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3070737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3071737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3072737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
3073e2f80391STvrtko Ursulin 	struct intel_engine_cs *engine;
3074c3232b18SDave Gordon 	enum intel_engine_id id;
307505407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
3076666796daSTvrtko Ursulin 	bool stuck[I915_NUM_ENGINES] = { 0 };
30779107e9d2SChris Wilson #define BUSY 1
30789107e9d2SChris Wilson #define KICK 5
30799107e9d2SChris Wilson #define HUNG 20
308024a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3081893eead0SChris Wilson 
3082d330a953SJani Nikula 	if (!i915.enable_hangcheck)
30833e0dc6b0SBen Widawsky 		return;
30843e0dc6b0SBen Widawsky 
30851f814dacSImre Deak 	/*
30861f814dacSImre Deak 	 * The hangcheck work is synced during runtime suspend, we don't
30871f814dacSImre Deak 	 * require a wakeref. TODO: instead of disabling the asserts make
30881f814dacSImre Deak 	 * sure that we hold a reference when this work is running.
30891f814dacSImre Deak 	 */
30901f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
30911f814dacSImre Deak 
309275714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
309375714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
309475714940SMika Kuoppala 	 * any invalid access.
309575714940SMika Kuoppala 	 */
309675714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
309775714940SMika Kuoppala 
3098c3232b18SDave Gordon 	for_each_engine_id(engine, dev_priv, id) {
309950877445SChris Wilson 		u64 acthd;
310050877445SChris Wilson 		u32 seqno;
310112471ba8SChris Wilson 		unsigned user_interrupts;
31029107e9d2SChris Wilson 		bool busy = true;
3103b4519513SChris Wilson 
31046274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31056274f212SChris Wilson 
3106c04e0f3bSChris Wilson 		/* We don't strictly need an irq-barrier here, as we are not
3107c04e0f3bSChris Wilson 		 * serving an interrupt request, be paranoid in case the
3108c04e0f3bSChris Wilson 		 * barrier has side-effects (such as preventing a broken
3109c04e0f3bSChris Wilson 		 * cacheline snoop) and so be sure that we can see the seqno
3110c04e0f3bSChris Wilson 		 * advance. If the seqno should stick, due to a stale
3111c04e0f3bSChris Wilson 		 * cacheline, we would erroneously declare the GPU hung.
3112c04e0f3bSChris Wilson 		 */
3113c04e0f3bSChris Wilson 		if (engine->irq_seqno_barrier)
3114c04e0f3bSChris Wilson 			engine->irq_seqno_barrier(engine);
3115c04e0f3bSChris Wilson 
3116e2f80391STvrtko Ursulin 		acthd = intel_ring_get_active_head(engine);
3117c04e0f3bSChris Wilson 		seqno = engine->get_seqno(engine);
311805407ff8SMika Kuoppala 
311912471ba8SChris Wilson 		/* Reset stuck interrupts between batch advances */
312012471ba8SChris Wilson 		user_interrupts = 0;
312112471ba8SChris Wilson 
3122e2f80391STvrtko Ursulin 		if (engine->hangcheck.seqno == seqno) {
3123e2f80391STvrtko Ursulin 			if (ring_idle(engine, seqno)) {
3124e2f80391STvrtko Ursulin 				engine->hangcheck.action = HANGCHECK_IDLE;
3125e2f80391STvrtko Ursulin 				if (waitqueue_active(&engine->irq_queue)) {
3126094f9a54SChris Wilson 					/* Safeguard against driver failure */
312712471ba8SChris Wilson 					user_interrupts = kick_waiters(engine);
3128e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31299107e9d2SChris Wilson 				} else
31309107e9d2SChris Wilson 					busy = false;
313105407ff8SMika Kuoppala 			} else {
31326274f212SChris Wilson 				/* We always increment the hangcheck score
31336274f212SChris Wilson 				 * if the ring is busy and still processing
31346274f212SChris Wilson 				 * the same request, so that no single request
31356274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31366274f212SChris Wilson 				 * batches). The only time we do not increment
31376274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31386274f212SChris Wilson 				 * ring is in a legitimate wait for another
31396274f212SChris Wilson 				 * ring. In that case the waiting ring is a
31406274f212SChris Wilson 				 * victim and we want to be sure we catch the
31416274f212SChris Wilson 				 * right culprit. Then every time we do kick
31426274f212SChris Wilson 				 * the ring, add a small increment to the
31436274f212SChris Wilson 				 * score so that we can catch a batch that is
31446274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31456274f212SChris Wilson 				 * for stalling the machine.
31469107e9d2SChris Wilson 				 */
3147e2f80391STvrtko Ursulin 				engine->hangcheck.action = ring_stuck(engine,
3148ad8beaeaSMika Kuoppala 								      acthd);
3149ad8beaeaSMika Kuoppala 
3150e2f80391STvrtko Ursulin 				switch (engine->hangcheck.action) {
3151da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3152f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3153f260fe7bSMika Kuoppala 					break;
315424a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3155e2f80391STvrtko Ursulin 					engine->hangcheck.score += BUSY;
31566274f212SChris Wilson 					break;
3157f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3158e2f80391STvrtko Ursulin 					engine->hangcheck.score += KICK;
31596274f212SChris Wilson 					break;
3160f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3161e2f80391STvrtko Ursulin 					engine->hangcheck.score += HUNG;
3162c3232b18SDave Gordon 					stuck[id] = true;
31636274f212SChris Wilson 					break;
31646274f212SChris Wilson 				}
316505407ff8SMika Kuoppala 			}
31669107e9d2SChris Wilson 		} else {
3167e2f80391STvrtko Ursulin 			engine->hangcheck.action = HANGCHECK_ACTIVE;
3168da661464SMika Kuoppala 
31699107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
31709107e9d2SChris Wilson 			 * attempts across multiple batches.
31719107e9d2SChris Wilson 			 */
3172e2f80391STvrtko Ursulin 			if (engine->hangcheck.score > 0)
3173e2f80391STvrtko Ursulin 				engine->hangcheck.score -= ACTIVE_DECAY;
3174e2f80391STvrtko Ursulin 			if (engine->hangcheck.score < 0)
3175e2f80391STvrtko Ursulin 				engine->hangcheck.score = 0;
3176f260fe7bSMika Kuoppala 
317761642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
317812471ba8SChris Wilson 			acthd = 0;
317961642ff0SMika Kuoppala 
3180e2f80391STvrtko Ursulin 			memset(engine->hangcheck.instdone, 0,
3181e2f80391STvrtko Ursulin 			       sizeof(engine->hangcheck.instdone));
3182cbb465e7SChris Wilson 		}
3183f65d9421SBen Gamari 
3184e2f80391STvrtko Ursulin 		engine->hangcheck.seqno = seqno;
3185e2f80391STvrtko Ursulin 		engine->hangcheck.acthd = acthd;
318612471ba8SChris Wilson 		engine->hangcheck.user_interrupts = user_interrupts;
31879107e9d2SChris Wilson 		busy_count += busy;
318805407ff8SMika Kuoppala 	}
318905407ff8SMika Kuoppala 
3190c3232b18SDave Gordon 	for_each_engine_id(engine, dev_priv, id) {
3191e2f80391STvrtko Ursulin 		if (engine->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3192b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
3193c3232b18SDave Gordon 				 stuck[id] ? "stuck" : "no progress",
3194e2f80391STvrtko Ursulin 				 engine->name);
319514b730fcSarun.siluvery@linux.intel.com 			rings_hung |= intel_engine_flag(engine);
319605407ff8SMika Kuoppala 		}
319705407ff8SMika Kuoppala 	}
319805407ff8SMika Kuoppala 
31991f814dacSImre Deak 	if (rings_hung) {
320014b730fcSarun.siluvery@linux.intel.com 		i915_handle_error(dev, rings_hung, "Engine(s) hung");
32011f814dacSImre Deak 		goto out;
32021f814dacSImre Deak 	}
320305407ff8SMika Kuoppala 
320405407ff8SMika Kuoppala 	if (busy_count)
320505407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
320605407ff8SMika Kuoppala 		 * being added */
320710cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
32081f814dacSImre Deak 
32091f814dacSImre Deak out:
32101f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
321110cd45b6SMika Kuoppala }
321210cd45b6SMika Kuoppala 
321310cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
321410cd45b6SMika Kuoppala {
3215737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3216672e7b7cSChris Wilson 
3217d330a953SJani Nikula 	if (!i915.enable_hangcheck)
321810cd45b6SMika Kuoppala 		return;
321910cd45b6SMika Kuoppala 
3220737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3221737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3222737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3223737b1506SChris Wilson 	 */
3224737b1506SChris Wilson 
3225737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3226737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3227f65d9421SBen Gamari }
3228f65d9421SBen Gamari 
32291c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
323091738a95SPaulo Zanoni {
323191738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
323291738a95SPaulo Zanoni 
323391738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
323491738a95SPaulo Zanoni 		return;
323591738a95SPaulo Zanoni 
3236f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3237105b122eSPaulo Zanoni 
3238105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3239105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3240622364b6SPaulo Zanoni }
3241105b122eSPaulo Zanoni 
324291738a95SPaulo Zanoni /*
3243622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3244622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3245622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3246622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3247622364b6SPaulo Zanoni  *
3248622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
324991738a95SPaulo Zanoni  */
3250622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3251622364b6SPaulo Zanoni {
3252622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3253622364b6SPaulo Zanoni 
3254622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3255622364b6SPaulo Zanoni 		return;
3256622364b6SPaulo Zanoni 
3257622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
325891738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
325991738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
326091738a95SPaulo Zanoni }
326191738a95SPaulo Zanoni 
32627c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3263d18ea1b5SDaniel Vetter {
3264d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3265d18ea1b5SDaniel Vetter 
3266f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3267a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3268f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3269d18ea1b5SDaniel Vetter }
3270d18ea1b5SDaniel Vetter 
327170591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
327270591a41SVille Syrjälä {
327370591a41SVille Syrjälä 	enum pipe pipe;
327470591a41SVille Syrjälä 
327571b8b41dSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
327671b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
327771b8b41dSVille Syrjälä 	else
327871b8b41dSVille Syrjälä 		I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
327971b8b41dSVille Syrjälä 
3280ad22d106SVille Syrjälä 	i915_hotplug_interrupt_update_locked(dev_priv, 0xffffffff, 0);
328170591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
328270591a41SVille Syrjälä 
3283ad22d106SVille Syrjälä 	for_each_pipe(dev_priv, pipe) {
3284ad22d106SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe),
3285ad22d106SVille Syrjälä 			   PIPE_FIFO_UNDERRUN_STATUS |
3286ad22d106SVille Syrjälä 			   PIPESTAT_INT_STATUS_MASK);
3287ad22d106SVille Syrjälä 		dev_priv->pipestat_irq_mask[pipe] = 0;
3288ad22d106SVille Syrjälä 	}
328970591a41SVille Syrjälä 
329070591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
3291ad22d106SVille Syrjälä 	dev_priv->irq_mask = ~0;
329270591a41SVille Syrjälä }
329370591a41SVille Syrjälä 
32948bb61306SVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
32958bb61306SVille Syrjälä {
32968bb61306SVille Syrjälä 	u32 pipestat_mask;
32979ab981f2SVille Syrjälä 	u32 enable_mask;
32988bb61306SVille Syrjälä 	enum pipe pipe;
32998bb61306SVille Syrjälä 
33008bb61306SVille Syrjälä 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
33018bb61306SVille Syrjälä 			PIPE_CRC_DONE_INTERRUPT_STATUS;
33028bb61306SVille Syrjälä 
33038bb61306SVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
33048bb61306SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
33058bb61306SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
33068bb61306SVille Syrjälä 
33079ab981f2SVille Syrjälä 	enable_mask = I915_DISPLAY_PORT_INTERRUPT |
33088bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33098bb61306SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
33108bb61306SVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
33119ab981f2SVille Syrjälä 		enable_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
33126b7eafc1SVille Syrjälä 
33136b7eafc1SVille Syrjälä 	WARN_ON(dev_priv->irq_mask != ~0);
33146b7eafc1SVille Syrjälä 
33159ab981f2SVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
33168bb61306SVille Syrjälä 
33179ab981f2SVille Syrjälä 	GEN5_IRQ_INIT(VLV_, dev_priv->irq_mask, enable_mask);
33188bb61306SVille Syrjälä }
33198bb61306SVille Syrjälä 
33208bb61306SVille Syrjälä /* drm_dma.h hooks
33218bb61306SVille Syrjälä */
33228bb61306SVille Syrjälä static void ironlake_irq_reset(struct drm_device *dev)
33238bb61306SVille Syrjälä {
33248bb61306SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
33258bb61306SVille Syrjälä 
33268bb61306SVille Syrjälä 	I915_WRITE(HWSTAM, 0xffffffff);
33278bb61306SVille Syrjälä 
33288bb61306SVille Syrjälä 	GEN5_IRQ_RESET(DE);
33298bb61306SVille Syrjälä 	if (IS_GEN7(dev))
33308bb61306SVille Syrjälä 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
33318bb61306SVille Syrjälä 
33328bb61306SVille Syrjälä 	gen5_gt_irq_reset(dev);
33338bb61306SVille Syrjälä 
33348bb61306SVille Syrjälä 	ibx_irq_reset(dev);
33358bb61306SVille Syrjälä }
33368bb61306SVille Syrjälä 
33377e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
33387e231dbeSJesse Barnes {
33392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33407e231dbeSJesse Barnes 
33417c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33427e231dbeSJesse Barnes 
3343ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33449918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
334570591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3346ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
33477e231dbeSJesse Barnes }
33487e231dbeSJesse Barnes 
3349d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3350d6e3cca3SDaniel Vetter {
3351d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3352d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3353d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3354d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3355d6e3cca3SDaniel Vetter }
3356d6e3cca3SDaniel Vetter 
3357823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3358abd58f01SBen Widawsky {
3359abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3360abd58f01SBen Widawsky 	int pipe;
3361abd58f01SBen Widawsky 
3362abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3363abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3364abd58f01SBen Widawsky 
3365d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3366abd58f01SBen Widawsky 
3367055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3368f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3369813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3370f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3371abd58f01SBen Widawsky 
3372f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3373f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3374f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3375abd58f01SBen Widawsky 
3376266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
33771c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3378abd58f01SBen Widawsky }
3379abd58f01SBen Widawsky 
33804c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
33814c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3382d49bdb0eSPaulo Zanoni {
33831180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33846831f3e3SVille Syrjälä 	enum pipe pipe;
3385d49bdb0eSPaulo Zanoni 
338613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
33876831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33886831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
33896831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
33906831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
339113321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3392d49bdb0eSPaulo Zanoni }
3393d49bdb0eSPaulo Zanoni 
3394aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3395aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3396aae8ba84SVille Syrjälä {
33976831f3e3SVille Syrjälä 	enum pipe pipe;
33986831f3e3SVille Syrjälä 
3399aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34006831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
34016831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3402aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3403aae8ba84SVille Syrjälä 
3404aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3405aae8ba84SVille Syrjälä 	synchronize_irq(dev_priv->dev->irq);
3406aae8ba84SVille Syrjälä }
3407aae8ba84SVille Syrjälä 
340843f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
340943f328d7SVille Syrjälä {
341043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
341143f328d7SVille Syrjälä 
341243f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
341343f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
341443f328d7SVille Syrjälä 
3415d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
341643f328d7SVille Syrjälä 
341743f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
341843f328d7SVille Syrjälä 
3419ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
34209918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
342170591a41SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3422ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
342343f328d7SVille Syrjälä }
342443f328d7SVille Syrjälä 
342587a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
342687a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
342787a02106SVille Syrjälä {
342887a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
342987a02106SVille Syrjälä 	struct intel_encoder *encoder;
343087a02106SVille Syrjälä 	u32 enabled_irqs = 0;
343187a02106SVille Syrjälä 
343287a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
343387a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
343487a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
343587a02106SVille Syrjälä 
343687a02106SVille Syrjälä 	return enabled_irqs;
343787a02106SVille Syrjälä }
343887a02106SVille Syrjälä 
343982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
344082a28bcfSDaniel Vetter {
34412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
344287a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
344382a28bcfSDaniel Vetter 
344482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3445fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
344687a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
344782a28bcfSDaniel Vetter 	} else {
3448fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
344987a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
345082a28bcfSDaniel Vetter 	}
345182a28bcfSDaniel Vetter 
3452fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
345382a28bcfSDaniel Vetter 
34547fe0b973SKeith Packard 	/*
34557fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34566dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34576dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34587fe0b973SKeith Packard 	 */
34597fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34607fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34617fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34627fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34637fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34640b2eb33eSVille Syrjälä 	/*
34650b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
34660b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
34670b2eb33eSVille Syrjälä 	 */
34680b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
34690b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
34707fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34716dbf30ceSVille Syrjälä }
347226951cafSXiong Zhang 
34736dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
34746dbf30ceSVille Syrjälä {
34756dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34766dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
34776dbf30ceSVille Syrjälä 
34786dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
34796dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
34806dbf30ceSVille Syrjälä 
34816dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34826dbf30ceSVille Syrjälä 
34836dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
34846dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34856dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
348674c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
34876dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34886dbf30ceSVille Syrjälä 
348926951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
349026951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
349126951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
349226951cafSXiong Zhang }
34937fe0b973SKeith Packard 
3494e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3495e4ce95aaSVille Syrjälä {
3496e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3497e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3498e4ce95aaSVille Syrjälä 
34993a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
35003a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
35013a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
35023a3b3c7dSVille Syrjälä 
35033a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
35043a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
350523bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
350623bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
35073a3b3c7dSVille Syrjälä 
35083a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
350923bb4cb5SVille Syrjälä 	} else {
3510e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3511e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3512e4ce95aaSVille Syrjälä 
3513e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
35143a3b3c7dSVille Syrjälä 	}
3515e4ce95aaSVille Syrjälä 
3516e4ce95aaSVille Syrjälä 	/*
3517e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3518e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
351923bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3520e4ce95aaSVille Syrjälä 	 */
3521e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3522e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3523e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3524e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3525e4ce95aaSVille Syrjälä 
3526e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3527e4ce95aaSVille Syrjälä }
3528e4ce95aaSVille Syrjälä 
3529e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3530e0a20ad7SShashank Sharma {
3531e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3532a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3533e0a20ad7SShashank Sharma 
3534a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3535a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3536e0a20ad7SShashank Sharma 
3537a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3538e0a20ad7SShashank Sharma 
3539a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3540a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3541a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3542d252bf68SShubhangi Shrivastava 
3543d252bf68SShubhangi Shrivastava 	DRM_DEBUG_KMS("Invert bit setting: hp_ctl:%x hp_port:%x\n",
3544d252bf68SShubhangi Shrivastava 		      hotplug, enabled_irqs);
3545d252bf68SShubhangi Shrivastava 	hotplug &= ~BXT_DDI_HPD_INVERT_MASK;
3546d252bf68SShubhangi Shrivastava 
3547d252bf68SShubhangi Shrivastava 	/*
3548d252bf68SShubhangi Shrivastava 	 * For BXT invert bit has to be set based on AOB design
3549d252bf68SShubhangi Shrivastava 	 * for HPD detection logic, update it based on VBT fields.
3550d252bf68SShubhangi Shrivastava 	 */
3551d252bf68SShubhangi Shrivastava 
3552d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIA) &&
3553d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_A))
3554d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIA_HPD_INVERT;
3555d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIB) &&
3556d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_B))
3557d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIB_HPD_INVERT;
3558d252bf68SShubhangi Shrivastava 	if ((enabled_irqs & BXT_DE_PORT_HP_DDIC) &&
3559d252bf68SShubhangi Shrivastava 	    intel_bios_is_port_hpd_inverted(dev_priv, PORT_C))
3560d252bf68SShubhangi Shrivastava 		hotplug |= BXT_DDIC_HPD_INVERT;
3561d252bf68SShubhangi Shrivastava 
3562a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3563e0a20ad7SShashank Sharma }
3564e0a20ad7SShashank Sharma 
3565d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3566d46da437SPaulo Zanoni {
35672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
356882a28bcfSDaniel Vetter 	u32 mask;
3569d46da437SPaulo Zanoni 
3570692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3571692a04cfSDaniel Vetter 		return;
3572692a04cfSDaniel Vetter 
3573105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
35745c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3575105b122eSPaulo Zanoni 	else
35765c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35778664281bSPaulo Zanoni 
3578b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3579d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3580d46da437SPaulo Zanoni }
3581d46da437SPaulo Zanoni 
35820a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35830a9a8c91SDaniel Vetter {
35840a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
35850a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
35860a9a8c91SDaniel Vetter 
35870a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
35880a9a8c91SDaniel Vetter 
35890a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3590040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
35910a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
359235a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
359335a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
35940a9a8c91SDaniel Vetter 	}
35950a9a8c91SDaniel Vetter 
35960a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
35970a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
35980a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
35990a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
36000a9a8c91SDaniel Vetter 	} else {
36010a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
36020a9a8c91SDaniel Vetter 	}
36030a9a8c91SDaniel Vetter 
360435079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
36050a9a8c91SDaniel Vetter 
36060a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
360778e68d36SImre Deak 		/*
360878e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
360978e68d36SImre Deak 		 * itself is enabled/disabled.
361078e68d36SImre Deak 		 */
36110a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
36120a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
36130a9a8c91SDaniel Vetter 
3614605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
361535079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
36160a9a8c91SDaniel Vetter 	}
36170a9a8c91SDaniel Vetter }
36180a9a8c91SDaniel Vetter 
3619f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3620036a4a7dSZhenyu Wang {
36212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36228e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
36238e76f8dcSPaulo Zanoni 
36248e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
36258e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
36268e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
36278e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
36285c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
36298e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
363023bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
363123bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
36328e76f8dcSPaulo Zanoni 	} else {
36338e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3634ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
36355b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
36365b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
36375b3a856bSDaniel Vetter 				DE_POISON);
3638e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3639e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3640e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
36418e76f8dcSPaulo Zanoni 	}
3642036a4a7dSZhenyu Wang 
36431ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3644036a4a7dSZhenyu Wang 
36450c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
36460c841212SPaulo Zanoni 
3647622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3648622364b6SPaulo Zanoni 
364935079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3650036a4a7dSZhenyu Wang 
36510a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3652036a4a7dSZhenyu Wang 
3653d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
36547fe0b973SKeith Packard 
3655f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36566005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36576005ce42SDaniel Vetter 		 *
36586005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36594bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36604bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3661d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3662fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3663d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3664f97108d1SJesse Barnes 	}
3665f97108d1SJesse Barnes 
3666036a4a7dSZhenyu Wang 	return 0;
3667036a4a7dSZhenyu Wang }
3668036a4a7dSZhenyu Wang 
3669f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3670f8b79e58SImre Deak {
3671f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3672f8b79e58SImre Deak 
3673f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3674f8b79e58SImre Deak 		return;
3675f8b79e58SImre Deak 
3676f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3677f8b79e58SImre Deak 
3678d6c69803SVille Syrjälä 	if (intel_irqs_enabled(dev_priv)) {
3679d6c69803SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3680ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3681f8b79e58SImre Deak 	}
3682d6c69803SVille Syrjälä }
3683f8b79e58SImre Deak 
3684f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3685f8b79e58SImre Deak {
3686f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3687f8b79e58SImre Deak 
3688f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3689f8b79e58SImre Deak 		return;
3690f8b79e58SImre Deak 
3691f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3692f8b79e58SImre Deak 
3693950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3694ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3695f8b79e58SImre Deak }
3696f8b79e58SImre Deak 
36970e6c9a9eSVille Syrjälä 
36980e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
36990e6c9a9eSVille Syrjälä {
37000e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
37010e6c9a9eSVille Syrjälä 
37020a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37037e231dbeSJesse Barnes 
3704ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
37059918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3706ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3707ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3708ad22d106SVille Syrjälä 
37097e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
371020afbda2SDaniel Vetter 
371120afbda2SDaniel Vetter 	return 0;
371220afbda2SDaniel Vetter }
371320afbda2SDaniel Vetter 
3714abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3715abd58f01SBen Widawsky {
3716abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3717abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3718abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
371973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3720abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
372173d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
372273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3723abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
372473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
372573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
372673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3727abd58f01SBen Widawsky 		0,
372873d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
372973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3730abd58f01SBen Widawsky 		};
3731abd58f01SBen Widawsky 
37320961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
37339a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37349a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
373578e68d36SImre Deak 	/*
373678e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
373778e68d36SImre Deak 	 * is enabled/disabled.
373878e68d36SImre Deak 	 */
373978e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
37409a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3741abd58f01SBen Widawsky }
3742abd58f01SBen Widawsky 
3743abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3744abd58f01SBen Widawsky {
3745770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3746770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37473a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37483a3b3c7dSVille Syrjälä 	u32 de_port_enables;
37493a3b3c7dSVille Syrjälä 	enum pipe pipe;
3750770de83dSDamien Lespiau 
3751b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3752770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3753770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37543a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
375588e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37569e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37573a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37583a3b3c7dSVille Syrjälä 	} else {
3759770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3760770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37613a3b3c7dSVille Syrjälä 	}
3762770de83dSDamien Lespiau 
3763770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3764770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3765770de83dSDamien Lespiau 
37663a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3767a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3768a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3769a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
37703a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
37713a3b3c7dSVille Syrjälä 
377213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
377313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
377413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3775abd58f01SBen Widawsky 
3776055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3777f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3778813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3779813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3780813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
378135079899SPaulo Zanoni 					  de_pipe_enables);
3782abd58f01SBen Widawsky 
37833a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3784abd58f01SBen Widawsky }
3785abd58f01SBen Widawsky 
3786abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3787abd58f01SBen Widawsky {
3788abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3789abd58f01SBen Widawsky 
3790266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3791622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3792622364b6SPaulo Zanoni 
3793abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3794abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3795abd58f01SBen Widawsky 
3796266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3797abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3798abd58f01SBen Widawsky 
3799abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3800abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3801abd58f01SBen Widawsky 
3802abd58f01SBen Widawsky 	return 0;
3803abd58f01SBen Widawsky }
3804abd58f01SBen Widawsky 
380543f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
380643f328d7SVille Syrjälä {
380743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
380843f328d7SVille Syrjälä 
380943f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
381043f328d7SVille Syrjälä 
3811ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38129918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3813ad22d106SVille Syrjälä 		vlv_display_irq_postinstall(dev_priv);
3814ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3815ad22d106SVille Syrjälä 
381643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
381743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
381843f328d7SVille Syrjälä 
381943f328d7SVille Syrjälä 	return 0;
382043f328d7SVille Syrjälä }
382143f328d7SVille Syrjälä 
3822abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3823abd58f01SBen Widawsky {
3824abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3825abd58f01SBen Widawsky 
3826abd58f01SBen Widawsky 	if (!dev_priv)
3827abd58f01SBen Widawsky 		return;
3828abd58f01SBen Widawsky 
3829823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3830abd58f01SBen Widawsky }
3831abd58f01SBen Widawsky 
38327e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38337e231dbeSJesse Barnes {
38342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38357e231dbeSJesse Barnes 
38367e231dbeSJesse Barnes 	if (!dev_priv)
38377e231dbeSJesse Barnes 		return;
38387e231dbeSJesse Barnes 
3839843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3840843d0e7dSImre Deak 
3841893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3842893fce8eSVille Syrjälä 
38437e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3844f8b79e58SImre Deak 
3845ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38469918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3847ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3848ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38497e231dbeSJesse Barnes }
38507e231dbeSJesse Barnes 
385143f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
385243f328d7SVille Syrjälä {
385343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
385443f328d7SVille Syrjälä 
385543f328d7SVille Syrjälä 	if (!dev_priv)
385643f328d7SVille Syrjälä 		return;
385743f328d7SVille Syrjälä 
385843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
385943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
386043f328d7SVille Syrjälä 
3861a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
386243f328d7SVille Syrjälä 
3863a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
386443f328d7SVille Syrjälä 
3865ad22d106SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38669918271eSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
3867ad22d106SVille Syrjälä 		vlv_display_irq_reset(dev_priv);
3868ad22d106SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
386943f328d7SVille Syrjälä }
387043f328d7SVille Syrjälä 
3871f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3872036a4a7dSZhenyu Wang {
38732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38744697995bSJesse Barnes 
38754697995bSJesse Barnes 	if (!dev_priv)
38764697995bSJesse Barnes 		return;
38774697995bSJesse Barnes 
3878be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3879036a4a7dSZhenyu Wang }
3880036a4a7dSZhenyu Wang 
3881c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3882c2798b19SChris Wilson {
38832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3884c2798b19SChris Wilson 	int pipe;
3885c2798b19SChris Wilson 
3886055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3887c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3888c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3889c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3890c2798b19SChris Wilson 	POSTING_READ16(IER);
3891c2798b19SChris Wilson }
3892c2798b19SChris Wilson 
3893c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3894c2798b19SChris Wilson {
38952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3896c2798b19SChris Wilson 
3897c2798b19SChris Wilson 	I915_WRITE16(EMR,
3898c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3899c2798b19SChris Wilson 
3900c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3901c2798b19SChris Wilson 	dev_priv->irq_mask =
3902c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3903c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3904c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
390537ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3906c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3907c2798b19SChris Wilson 
3908c2798b19SChris Wilson 	I915_WRITE16(IER,
3909c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3910c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3911c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3912c2798b19SChris Wilson 	POSTING_READ16(IER);
3913c2798b19SChris Wilson 
3914379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3915379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3916d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3917755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3918755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3919d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3920379ef82dSDaniel Vetter 
3921c2798b19SChris Wilson 	return 0;
3922c2798b19SChris Wilson }
3923c2798b19SChris Wilson 
392490a72f87SVille Syrjälä /*
392590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
392690a72f87SVille Syrjälä  */
392790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
39281f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
392990a72f87SVille Syrjälä {
39302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39311f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
393290a72f87SVille Syrjälä 
39338d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
393490a72f87SVille Syrjälä 		return false;
393590a72f87SVille Syrjälä 
393690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3937d6bbafa1SChris Wilson 		goto check_page_flip;
393890a72f87SVille Syrjälä 
393990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
394090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
394190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
394290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
394390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
394490a72f87SVille Syrjälä 	 */
394590a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3946d6bbafa1SChris Wilson 		goto check_page_flip;
394790a72f87SVille Syrjälä 
39487d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
394990a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
395090a72f87SVille Syrjälä 	return true;
3951d6bbafa1SChris Wilson 
3952d6bbafa1SChris Wilson check_page_flip:
3953d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3954d6bbafa1SChris Wilson 	return false;
395590a72f87SVille Syrjälä }
395690a72f87SVille Syrjälä 
3957ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3958c2798b19SChris Wilson {
395945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3961c2798b19SChris Wilson 	u16 iir, new_iir;
3962c2798b19SChris Wilson 	u32 pipe_stats[2];
3963c2798b19SChris Wilson 	int pipe;
3964c2798b19SChris Wilson 	u16 flip_mask =
3965c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3966c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
39671f814dacSImre Deak 	irqreturn_t ret;
3968c2798b19SChris Wilson 
39692dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39702dd2a883SImre Deak 		return IRQ_NONE;
39712dd2a883SImre Deak 
39721f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
39731f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
39741f814dacSImre Deak 
39751f814dacSImre Deak 	ret = IRQ_NONE;
3976c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3977c2798b19SChris Wilson 	if (iir == 0)
39781f814dacSImre Deak 		goto out;
3979c2798b19SChris Wilson 
3980c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3981c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3982c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3983c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3984c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3985c2798b19SChris Wilson 		 */
3986222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3987c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3988aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3989c2798b19SChris Wilson 
3990055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3991f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
3992c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3993c2798b19SChris Wilson 
3994c2798b19SChris Wilson 			/*
3995c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3996c2798b19SChris Wilson 			 */
39972d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3998c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3999c2798b19SChris Wilson 		}
4000222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4001c2798b19SChris Wilson 
4002c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4003c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4004c2798b19SChris Wilson 
4005c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
40064a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4007c2798b19SChris Wilson 
4008055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40091f1c2e24SVille Syrjälä 			int plane = pipe;
40103a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
40111f1c2e24SVille Syrjälä 				plane = !plane;
40121f1c2e24SVille Syrjälä 
40134356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40141f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
40151f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4016c2798b19SChris Wilson 
40174356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4018277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40192d9d2b0bSVille Syrjälä 
40201f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40211f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40221f7247c0SDaniel Vetter 								    pipe);
40234356d586SDaniel Vetter 		}
4024c2798b19SChris Wilson 
4025c2798b19SChris Wilson 		iir = new_iir;
4026c2798b19SChris Wilson 	}
40271f814dacSImre Deak 	ret = IRQ_HANDLED;
4028c2798b19SChris Wilson 
40291f814dacSImre Deak out:
40301f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40311f814dacSImre Deak 
40321f814dacSImre Deak 	return ret;
4033c2798b19SChris Wilson }
4034c2798b19SChris Wilson 
4035c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4036c2798b19SChris Wilson {
40372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4038c2798b19SChris Wilson 	int pipe;
4039c2798b19SChris Wilson 
4040055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4041c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4042c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4043c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4044c2798b19SChris Wilson 	}
4045c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4046c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4047c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4048c2798b19SChris Wilson }
4049c2798b19SChris Wilson 
4050a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4051a266c7d5SChris Wilson {
40522d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4053a266c7d5SChris Wilson 	int pipe;
4054a266c7d5SChris Wilson 
4055a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40560706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4057a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4058a266c7d5SChris Wilson 	}
4059a266c7d5SChris Wilson 
406000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4061055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4062a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4063a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4064a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4065a266c7d5SChris Wilson 	POSTING_READ(IER);
4066a266c7d5SChris Wilson }
4067a266c7d5SChris Wilson 
4068a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4069a266c7d5SChris Wilson {
40702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
407138bde180SChris Wilson 	u32 enable_mask;
4072a266c7d5SChris Wilson 
407338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
407438bde180SChris Wilson 
407538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
407638bde180SChris Wilson 	dev_priv->irq_mask =
407738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
407838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
407938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
408038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
408137ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
408238bde180SChris Wilson 
408338bde180SChris Wilson 	enable_mask =
408438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
408538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
408638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
408738bde180SChris Wilson 		I915_USER_INTERRUPT;
408838bde180SChris Wilson 
4089a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40900706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
409120afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
409220afbda2SDaniel Vetter 
4093a266c7d5SChris Wilson 		/* Enable in IER... */
4094a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4095a266c7d5SChris Wilson 		/* and unmask in IMR */
4096a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4097a266c7d5SChris Wilson 	}
4098a266c7d5SChris Wilson 
4099a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4100a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4101a266c7d5SChris Wilson 	POSTING_READ(IER);
4102a266c7d5SChris Wilson 
4103f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
410420afbda2SDaniel Vetter 
4105379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4106379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4107d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4108755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4109755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4110d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4111379ef82dSDaniel Vetter 
411220afbda2SDaniel Vetter 	return 0;
411320afbda2SDaniel Vetter }
411420afbda2SDaniel Vetter 
411590a72f87SVille Syrjälä /*
411690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
411790a72f87SVille Syrjälä  */
411890a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
411990a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
412090a72f87SVille Syrjälä {
41212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
412290a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
412390a72f87SVille Syrjälä 
41248d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
412590a72f87SVille Syrjälä 		return false;
412690a72f87SVille Syrjälä 
412790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
4128d6bbafa1SChris Wilson 		goto check_page_flip;
412990a72f87SVille Syrjälä 
413090a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
413190a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
413290a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
413390a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
413490a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
413590a72f87SVille Syrjälä 	 */
413690a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
4137d6bbafa1SChris Wilson 		goto check_page_flip;
413890a72f87SVille Syrjälä 
41397d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
414090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
414190a72f87SVille Syrjälä 	return true;
4142d6bbafa1SChris Wilson 
4143d6bbafa1SChris Wilson check_page_flip:
4144d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
4145d6bbafa1SChris Wilson 	return false;
414690a72f87SVille Syrjälä }
414790a72f87SVille Syrjälä 
4148ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4149a266c7d5SChris Wilson {
415045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41512d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
41528291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
415338bde180SChris Wilson 	u32 flip_mask =
415438bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
415538bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
415638bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4157a266c7d5SChris Wilson 
41582dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41592dd2a883SImre Deak 		return IRQ_NONE;
41602dd2a883SImre Deak 
41611f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
41621f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
41631f814dacSImre Deak 
4164a266c7d5SChris Wilson 	iir = I915_READ(IIR);
416538bde180SChris Wilson 	do {
416638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
41678291ee90SChris Wilson 		bool blc_event = false;
4168a266c7d5SChris Wilson 
4169a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4170a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4171a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4172a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4173a266c7d5SChris Wilson 		 */
4174222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4175a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4176aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4177a266c7d5SChris Wilson 
4178055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4179f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4180a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4181a266c7d5SChris Wilson 
418238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4183a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4184a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
418538bde180SChris Wilson 				irq_received = true;
4186a266c7d5SChris Wilson 			}
4187a266c7d5SChris Wilson 		}
4188222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4189a266c7d5SChris Wilson 
4190a266c7d5SChris Wilson 		if (!irq_received)
4191a266c7d5SChris Wilson 			break;
4192a266c7d5SChris Wilson 
4193a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
419416c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
419516c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
419616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4197a266c7d5SChris Wilson 
419838bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4199a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4200a266c7d5SChris Wilson 
4201a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
42024a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4203a266c7d5SChris Wilson 
4204055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
420538bde180SChris Wilson 			int plane = pipe;
42063a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
420738bde180SChris Wilson 				plane = !plane;
42085e2032d4SVille Syrjälä 
420990a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
421090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
421190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4212a266c7d5SChris Wilson 
4213a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4214a266c7d5SChris Wilson 				blc_event = true;
42154356d586SDaniel Vetter 
42164356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4217277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
42182d9d2b0bSVille Syrjälä 
42191f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42201f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42211f7247c0SDaniel Vetter 								    pipe);
4222a266c7d5SChris Wilson 		}
4223a266c7d5SChris Wilson 
4224a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4225a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4226a266c7d5SChris Wilson 
4227a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4228a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4229a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4230a266c7d5SChris Wilson 		 * we would never get another interrupt.
4231a266c7d5SChris Wilson 		 *
4232a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4233a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4234a266c7d5SChris Wilson 		 * another one.
4235a266c7d5SChris Wilson 		 *
4236a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4237a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4238a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4239a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4240a266c7d5SChris Wilson 		 * stray interrupts.
4241a266c7d5SChris Wilson 		 */
424238bde180SChris Wilson 		ret = IRQ_HANDLED;
4243a266c7d5SChris Wilson 		iir = new_iir;
424438bde180SChris Wilson 	} while (iir & ~flip_mask);
4245a266c7d5SChris Wilson 
42461f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42471f814dacSImre Deak 
4248a266c7d5SChris Wilson 	return ret;
4249a266c7d5SChris Wilson }
4250a266c7d5SChris Wilson 
4251a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4252a266c7d5SChris Wilson {
42532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4254a266c7d5SChris Wilson 	int pipe;
4255a266c7d5SChris Wilson 
4256a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42570706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4258a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4259a266c7d5SChris Wilson 	}
4260a266c7d5SChris Wilson 
426100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4262055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
426355b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4264a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
426555b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
426655b39755SChris Wilson 	}
4267a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4268a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4269a266c7d5SChris Wilson 
4270a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4271a266c7d5SChris Wilson }
4272a266c7d5SChris Wilson 
4273a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4274a266c7d5SChris Wilson {
42752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4276a266c7d5SChris Wilson 	int pipe;
4277a266c7d5SChris Wilson 
42780706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4279a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4280a266c7d5SChris Wilson 
4281a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4282055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4283a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4284a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4285a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4286a266c7d5SChris Wilson 	POSTING_READ(IER);
4287a266c7d5SChris Wilson }
4288a266c7d5SChris Wilson 
4289a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4290a266c7d5SChris Wilson {
42912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4292bbba0a97SChris Wilson 	u32 enable_mask;
4293a266c7d5SChris Wilson 	u32 error_mask;
4294a266c7d5SChris Wilson 
4295a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4296bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4297adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4298bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4299bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4300bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4301bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4302bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4303bbba0a97SChris Wilson 
4304bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
430521ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
430621ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4307bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4308bbba0a97SChris Wilson 
4309bbba0a97SChris Wilson 	if (IS_G4X(dev))
4310bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4311a266c7d5SChris Wilson 
4312b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4313b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4314d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4315755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4316755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4317755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4318d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4319a266c7d5SChris Wilson 
4320a266c7d5SChris Wilson 	/*
4321a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4322a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4323a266c7d5SChris Wilson 	 */
4324a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4325a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4326a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4327a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4328a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4329a266c7d5SChris Wilson 	} else {
4330a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4331a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4332a266c7d5SChris Wilson 	}
4333a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4334a266c7d5SChris Wilson 
4335a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4336a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4337a266c7d5SChris Wilson 	POSTING_READ(IER);
4338a266c7d5SChris Wilson 
43390706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
434020afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
434120afbda2SDaniel Vetter 
4342f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
434320afbda2SDaniel Vetter 
434420afbda2SDaniel Vetter 	return 0;
434520afbda2SDaniel Vetter }
434620afbda2SDaniel Vetter 
4347bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
434820afbda2SDaniel Vetter {
43492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
435020afbda2SDaniel Vetter 	u32 hotplug_en;
435120afbda2SDaniel Vetter 
4352b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4353b5ea2d56SDaniel Vetter 
4354adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4355e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
43560706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4357a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4358a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4359a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4360a266c7d5SChris Wilson 	*/
4361a266c7d5SChris Wilson 	if (IS_G4X(dev))
4362a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4363a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4364a266c7d5SChris Wilson 
4365a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
43660706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4367f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4368f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4369f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
43700706f17cSEgbert Eich 					     hotplug_en);
4371a266c7d5SChris Wilson }
4372a266c7d5SChris Wilson 
4373ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4374a266c7d5SChris Wilson {
437545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
43762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4377a266c7d5SChris Wilson 	u32 iir, new_iir;
4378a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4379a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
438021ad8330SVille Syrjälä 	u32 flip_mask =
438121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
438221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4383a266c7d5SChris Wilson 
43842dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
43852dd2a883SImre Deak 		return IRQ_NONE;
43862dd2a883SImre Deak 
43871f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
43881f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
43891f814dacSImre Deak 
4390a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4391a266c7d5SChris Wilson 
4392a266c7d5SChris Wilson 	for (;;) {
4393501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
43942c8ba29fSChris Wilson 		bool blc_event = false;
43952c8ba29fSChris Wilson 
4396a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4397a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4398a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4399a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4400a266c7d5SChris Wilson 		 */
4401222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4402a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4403aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4404a266c7d5SChris Wilson 
4405055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4406f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4407a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4408a266c7d5SChris Wilson 
4409a266c7d5SChris Wilson 			/*
4410a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4411a266c7d5SChris Wilson 			 */
4412a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4413a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4414501e01d7SVille Syrjälä 				irq_received = true;
4415a266c7d5SChris Wilson 			}
4416a266c7d5SChris Wilson 		}
4417222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4418a266c7d5SChris Wilson 
4419a266c7d5SChris Wilson 		if (!irq_received)
4420a266c7d5SChris Wilson 			break;
4421a266c7d5SChris Wilson 
4422a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4423a266c7d5SChris Wilson 
4424a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
442516c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
442616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4427a266c7d5SChris Wilson 
442821ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4429a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4430a266c7d5SChris Wilson 
4431a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
44324a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[RCS]);
4433a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
44344a570db5STvrtko Ursulin 			notify_ring(&dev_priv->engine[VCS]);
4435a266c7d5SChris Wilson 
4436055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44372c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
443890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
443990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4440a266c7d5SChris Wilson 
4441a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4442a266c7d5SChris Wilson 				blc_event = true;
44434356d586SDaniel Vetter 
44444356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4445277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4446a266c7d5SChris Wilson 
44471f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44481f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44492d9d2b0bSVille Syrjälä 		}
4450a266c7d5SChris Wilson 
4451a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4452a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4453a266c7d5SChris Wilson 
4454515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4455515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4456515ac2bbSDaniel Vetter 
4457a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4458a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4459a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4460a266c7d5SChris Wilson 		 * we would never get another interrupt.
4461a266c7d5SChris Wilson 		 *
4462a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4463a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4464a266c7d5SChris Wilson 		 * another one.
4465a266c7d5SChris Wilson 		 *
4466a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4467a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4468a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4469a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4470a266c7d5SChris Wilson 		 * stray interrupts.
4471a266c7d5SChris Wilson 		 */
4472a266c7d5SChris Wilson 		iir = new_iir;
4473a266c7d5SChris Wilson 	}
4474a266c7d5SChris Wilson 
44751f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
44761f814dacSImre Deak 
4477a266c7d5SChris Wilson 	return ret;
4478a266c7d5SChris Wilson }
4479a266c7d5SChris Wilson 
4480a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4481a266c7d5SChris Wilson {
44822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4483a266c7d5SChris Wilson 	int pipe;
4484a266c7d5SChris Wilson 
4485a266c7d5SChris Wilson 	if (!dev_priv)
4486a266c7d5SChris Wilson 		return;
4487a266c7d5SChris Wilson 
44880706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4489a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4490a266c7d5SChris Wilson 
4491a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4492055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4493a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4494a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4495a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4496a266c7d5SChris Wilson 
4497055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4498a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4499a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4500a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4501a266c7d5SChris Wilson }
4502a266c7d5SChris Wilson 
4503fca52a55SDaniel Vetter /**
4504fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4505fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4506fca52a55SDaniel Vetter  *
4507fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4508fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4509fca52a55SDaniel Vetter  */
4510b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4511f71d4af4SJesse Barnes {
4512b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
45138b2e326dSChris Wilson 
451477913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
451577913b39SJani Nikula 
4516c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4517a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45188b2e326dSChris Wilson 
4519a6706b45SDeepak S 	/* Let's track the enabled rps events */
4520666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45216c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45226f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
452331685c25SDeepak S 	else
4524a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4525a6706b45SDeepak S 
4526737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4527737b1506SChris Wilson 			  i915_hangcheck_elapsed);
452861bac78eSDaniel Vetter 
4529b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45304cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45314cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4532b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4533f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4534fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4535391f75e2SVille Syrjälä 	} else {
4536391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4537391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4538f71d4af4SJesse Barnes 	}
4539f71d4af4SJesse Barnes 
454021da2700SVille Syrjälä 	/*
454121da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
454221da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
454321da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
454421da2700SVille Syrjälä 	 */
4545b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
454621da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
454721da2700SVille Syrjälä 
4548f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4549f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4550f71d4af4SJesse Barnes 
4551b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
455243f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
455343f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
455443f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
455543f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
455643f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
455743f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
455843f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4559b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
45607e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
45617e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
45627e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
45637e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
45647e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
45657e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4566fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4567b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4568abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4569723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4570abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4571abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4572abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4573abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
45746dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4575e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
45766dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
45776dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
45786dbf30ceSVille Syrjälä 		else
45793a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4580f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4581f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4582723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4583f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4584f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4585f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4586f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4587e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4588f71d4af4SJesse Barnes 	} else {
4589b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4590c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4591c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4592c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4593c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4594b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4595a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4596a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4597a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4598a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4599c2798b19SChris Wilson 		} else {
4600a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4601a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4602a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4603a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4604c2798b19SChris Wilson 		}
4605778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4606778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4607f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4608f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4609f71d4af4SJesse Barnes 	}
4610f71d4af4SJesse Barnes }
461120afbda2SDaniel Vetter 
4612fca52a55SDaniel Vetter /**
4613fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4614fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4615fca52a55SDaniel Vetter  *
4616fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4617fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4618fca52a55SDaniel Vetter  *
4619fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4620fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4621fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4622fca52a55SDaniel Vetter  */
46232aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46242aeb7d3aSDaniel Vetter {
46252aeb7d3aSDaniel Vetter 	/*
46262aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46272aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46282aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46292aeb7d3aSDaniel Vetter 	 */
46302aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46312aeb7d3aSDaniel Vetter 
46322aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
46332aeb7d3aSDaniel Vetter }
46342aeb7d3aSDaniel Vetter 
4635fca52a55SDaniel Vetter /**
4636fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4637fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4638fca52a55SDaniel Vetter  *
4639fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4640fca52a55SDaniel Vetter  * resources acquired in the init functions.
4641fca52a55SDaniel Vetter  */
46422aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
46432aeb7d3aSDaniel Vetter {
46442aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
46452aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
46462aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46472aeb7d3aSDaniel Vetter }
46482aeb7d3aSDaniel Vetter 
4649fca52a55SDaniel Vetter /**
4650fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4651fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4652fca52a55SDaniel Vetter  *
4653fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4654fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4655fca52a55SDaniel Vetter  */
4656b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4657c67a470bSPaulo Zanoni {
4658b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
46592aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46602dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4661c67a470bSPaulo Zanoni }
4662c67a470bSPaulo Zanoni 
4663fca52a55SDaniel Vetter /**
4664fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4665fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4666fca52a55SDaniel Vetter  *
4667fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4668fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4669fca52a55SDaniel Vetter  */
4670b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4671c67a470bSPaulo Zanoni {
46722aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4673b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4674b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4675c67a470bSPaulo Zanoni }
4676