xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 24a65e624bcdc726c7711ae90efeffaf0a8e9f32)
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 
99774cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
998549f7365SChris Wilson {
99993b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
1000475553deSChris Wilson 		return;
1001475553deSChris Wilson 
1002bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
10039862e600SChris Wilson 
1004549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
1005549f7365SChris Wilson }
1006549f7365SChris Wilson 
100743cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
100843cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
100931685c25SDeepak S {
101043cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
101143cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
101243cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
101331685c25SDeepak S }
101431685c25SDeepak S 
101543cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
101643cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
101743cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
101843cf3bf0SChris Wilson 			 int threshold)
101931685c25SDeepak S {
102043cf3bf0SChris Wilson 	u64 time, c0;
10217bad74d5SVille Syrjälä 	unsigned int mul = 100;
102231685c25SDeepak S 
102343cf3bf0SChris Wilson 	if (old->cz_clock == 0)
102443cf3bf0SChris Wilson 		return false;
102531685c25SDeepak S 
10267bad74d5SVille Syrjälä 	if (I915_READ(VLV_COUNTER_CONTROL) & VLV_COUNT_RANGE_HIGH)
10277bad74d5SVille Syrjälä 		mul <<= 8;
10287bad74d5SVille Syrjälä 
102943cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
10307bad74d5SVille Syrjälä 	time *= threshold * dev_priv->czclk_freq;
103131685c25SDeepak S 
103243cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
103343cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
103443cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
103543cf3bf0SChris Wilson 	 */
103643cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
103743cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
10387bad74d5SVille Syrjälä 	c0 *= mul * VLV_CZ_CLOCK_TO_MILLI_SEC;
103931685c25SDeepak S 
104043cf3bf0SChris Wilson 	return c0 >= time;
104131685c25SDeepak S }
104231685c25SDeepak S 
104343cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
104443cf3bf0SChris Wilson {
104543cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
104643cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
104743cf3bf0SChris Wilson }
104843cf3bf0SChris Wilson 
104943cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
105043cf3bf0SChris Wilson {
105143cf3bf0SChris Wilson 	struct intel_rps_ei now;
105243cf3bf0SChris Wilson 	u32 events = 0;
105343cf3bf0SChris Wilson 
10546f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
105543cf3bf0SChris Wilson 		return 0;
105643cf3bf0SChris Wilson 
105743cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
105843cf3bf0SChris Wilson 	if (now.cz_clock == 0)
105943cf3bf0SChris Wilson 		return 0;
106031685c25SDeepak S 
106143cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
106243cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
106343cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10648fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
106543cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
106643cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
106731685c25SDeepak S 	}
106831685c25SDeepak S 
106943cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
107043cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
107143cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10728fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
107343cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
107443cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
107543cf3bf0SChris Wilson 	}
107643cf3bf0SChris Wilson 
107743cf3bf0SChris Wilson 	return events;
107831685c25SDeepak S }
107931685c25SDeepak S 
1080f5a4c67dSChris Wilson static bool any_waiters(struct drm_i915_private *dev_priv)
1081f5a4c67dSChris Wilson {
1082f5a4c67dSChris Wilson 	struct intel_engine_cs *ring;
1083f5a4c67dSChris Wilson 	int i;
1084f5a4c67dSChris Wilson 
1085f5a4c67dSChris Wilson 	for_each_ring(ring, dev_priv, i)
1086f5a4c67dSChris Wilson 		if (ring->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--;
122235a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
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);
1261480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
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))
129474cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1295f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
129674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[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))
130674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1307cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
130874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1309cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
131074cdb337SChris Wilson 		notify_ring(&dev_priv->ring[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
1322e4ba99b9SDaniel Vetter gen8_cs_irq_handler(struct intel_engine_cs *ring, u32 iir, int test_shift)
1323fbcc1a0cSNick Hoath {
1324fbcc1a0cSNick Hoath 	if (iir & (GT_RENDER_USER_INTERRUPT << test_shift))
1325fbcc1a0cSNick Hoath 		notify_ring(ring);
1326fbcc1a0cSNick Hoath 	if (iir & (GT_CONTEXT_SWITCH_INTERRUPT << test_shift))
1327fbcc1a0cSNick Hoath 		intel_lrc_irq_handler(ring);
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 
1341fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[RCS],
1342fbcc1a0cSNick Hoath 					iir, GEN8_RCS_IRQ_SHIFT);
1343e981e7b1SThomas Daniel 
1344fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[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 
1356fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[VCS],
1357fbcc1a0cSNick Hoath 					iir, GEN8_VCS1_IRQ_SHIFT);
1358e981e7b1SThomas Daniel 
1359fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[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 
1371fbcc1a0cSNick Hoath 			gen8_cs_irq_handler(&dev_priv->ring[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 
16301403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
163112638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
163274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[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 
18328e5fd599SVille Syrjälä 	for (;;) {
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);
18608e5fd599SVille Syrjälä 	}
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 {
2452a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
245317e1df07SDaniel Vetter 	int i;
245417e1df07SDaniel Vetter 
245517e1df07SDaniel Vetter 	/*
245617e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
245717e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
245817e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
245917e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
246017e1df07SDaniel Vetter 	 */
246117e1df07SDaniel Vetter 
246217e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
246317e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
246417e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
246517e1df07SDaniel Vetter 
246617e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
246717e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
246817e1df07SDaniel Vetter 
246917e1df07SDaniel Vetter 	/*
247017e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
247117e1df07SDaniel Vetter 	 * reset state is cleared.
247217e1df07SDaniel Vetter 	 */
247317e1df07SDaniel Vetter 	if (reset_completed)
247417e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
247517e1df07SDaniel Vetter }
247617e1df07SDaniel Vetter 
24778a905236SJesse Barnes /**
2478b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
2479468f9d29SJavier Martinez Canillas  * @dev: drm device
24808a905236SJesse Barnes  *
24818a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
24828a905236SJesse Barnes  * was detected.
24838a905236SJesse Barnes  */
2484b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
24858a905236SJesse Barnes {
2486b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2487b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2488cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2489cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2490cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
249117e1df07SDaniel Vetter 	int ret;
24928a905236SJesse Barnes 
24935bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24948a905236SJesse Barnes 
24957db0ba24SDaniel Vetter 	/*
24967db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24977db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24987db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24997db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
25007db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
25017db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
25027db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
25037db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
25047db0ba24SDaniel Vetter 	 */
25057db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
250644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
25075bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
25087db0ba24SDaniel Vetter 				   reset_event);
25091f83fee0SDaniel Vetter 
251017e1df07SDaniel Vetter 		/*
2511f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2512f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2513f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2514f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2515f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2516f454c694SImre Deak 		 */
2517f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
25187514747dSVille Syrjälä 
25197514747dSVille Syrjälä 		intel_prepare_reset(dev);
25207514747dSVille Syrjälä 
2521f454c694SImre Deak 		/*
252217e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
252317e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
252417e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
252517e1df07SDaniel Vetter 		 * deadlocks with the reset work.
252617e1df07SDaniel Vetter 		 */
2527f69061beSDaniel Vetter 		ret = i915_reset(dev);
2528f69061beSDaniel Vetter 
25297514747dSVille Syrjälä 		intel_finish_reset(dev);
253017e1df07SDaniel Vetter 
2531f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2532f454c694SImre Deak 
2533f69061beSDaniel Vetter 		if (ret == 0) {
2534f69061beSDaniel Vetter 			/*
2535f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2536f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2537f69061beSDaniel Vetter 			 * complete.
2538f69061beSDaniel Vetter 			 *
2539f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2540f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2541f69061beSDaniel Vetter 			 * updates before
2542f69061beSDaniel Vetter 			 * the counter increment.
2543f69061beSDaniel Vetter 			 */
25444e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2545f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2546f69061beSDaniel Vetter 
25475bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2548f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
25491f83fee0SDaniel Vetter 		} else {
2550805de8f4SPeter Zijlstra 			atomic_or(I915_WEDGED, &error->reset_counter);
2551f316a42cSBen Gamari 		}
25521f83fee0SDaniel Vetter 
255317e1df07SDaniel Vetter 		/*
255417e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
255517e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
255617e1df07SDaniel Vetter 		 */
255717e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2558f316a42cSBen Gamari 	}
25598a905236SJesse Barnes }
25608a905236SJesse Barnes 
256135aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2562c0e09200SDave Airlie {
25638a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2564bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
256563eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2566050ee91fSBen Widawsky 	int pipe, i;
256763eeaf38SJesse Barnes 
256835aed2e6SChris Wilson 	if (!eir)
256935aed2e6SChris Wilson 		return;
257063eeaf38SJesse Barnes 
2571a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
25728a905236SJesse Barnes 
2573bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2574bd9854f9SBen Widawsky 
25758a905236SJesse Barnes 	if (IS_G4X(dev)) {
25768a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
25778a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
25788a905236SJesse Barnes 
2579a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2580a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2581050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2582050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2583a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2584a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
25858a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25863143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25878a905236SJesse Barnes 		}
25888a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25898a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2590a70491ccSJoe Perches 			pr_err("page table error\n");
2591a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25928a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25933143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25948a905236SJesse Barnes 		}
25958a905236SJesse Barnes 	}
25968a905236SJesse Barnes 
2597a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
259863eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
259963eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2600a70491ccSJoe Perches 			pr_err("page table error\n");
2601a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
260263eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
26033143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
260463eeaf38SJesse Barnes 		}
26058a905236SJesse Barnes 	}
26068a905236SJesse Barnes 
260763eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2608a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2609055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2610a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
26119db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
261263eeaf38SJesse Barnes 		/* pipestat has already been acked */
261363eeaf38SJesse Barnes 	}
261463eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2615a70491ccSJoe Perches 		pr_err("instruction error\n");
2616a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2617050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2618050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2619a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
262063eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
262163eeaf38SJesse Barnes 
2622a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2623a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2624a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
262563eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
26263143a2bfSChris Wilson 			POSTING_READ(IPEIR);
262763eeaf38SJesse Barnes 		} else {
262863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
262963eeaf38SJesse Barnes 
2630a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2631a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2632a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2633a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
263463eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
26353143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
263663eeaf38SJesse Barnes 		}
263763eeaf38SJesse Barnes 	}
263863eeaf38SJesse Barnes 
263963eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
26403143a2bfSChris Wilson 	POSTING_READ(EIR);
264163eeaf38SJesse Barnes 	eir = I915_READ(EIR);
264263eeaf38SJesse Barnes 	if (eir) {
264363eeaf38SJesse Barnes 		/*
264463eeaf38SJesse Barnes 		 * some errors might have become stuck,
264563eeaf38SJesse Barnes 		 * mask them.
264663eeaf38SJesse Barnes 		 */
264763eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
264863eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
264963eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
265063eeaf38SJesse Barnes 	}
265135aed2e6SChris Wilson }
265235aed2e6SChris Wilson 
265335aed2e6SChris Wilson /**
2654b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
265535aed2e6SChris Wilson  * @dev: drm device
265635aed2e6SChris Wilson  *
2657aafd8581SJavier Martinez Canillas  * Do some basic checking of register state at error time and
265835aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
265935aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
266035aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
266135aed2e6SChris Wilson  * of a ring dump etc.).
266235aed2e6SChris Wilson  */
266358174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
266458174462SMika Kuoppala 		       const char *fmt, ...)
266535aed2e6SChris Wilson {
266635aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
266758174462SMika Kuoppala 	va_list args;
266858174462SMika Kuoppala 	char error_msg[80];
266935aed2e6SChris Wilson 
267058174462SMika Kuoppala 	va_start(args, fmt);
267158174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
267258174462SMika Kuoppala 	va_end(args);
267358174462SMika Kuoppala 
267458174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
267535aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
26768a905236SJesse Barnes 
2677ba1234d1SBen Gamari 	if (wedged) {
2678805de8f4SPeter Zijlstra 		atomic_or(I915_RESET_IN_PROGRESS_FLAG,
2679f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2680ba1234d1SBen Gamari 
268111ed50ecSBen Gamari 		/*
2682b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2683b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2684b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
268517e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
268617e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
268717e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
268817e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
268917e1df07SDaniel Vetter 		 *
269017e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
269117e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
269217e1df07SDaniel Vetter 		 * counter atomic_t.
269311ed50ecSBen Gamari 		 */
269417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
269511ed50ecSBen Gamari 	}
269611ed50ecSBen Gamari 
2697b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
26988a905236SJesse Barnes }
26998a905236SJesse Barnes 
270042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
270142f52ef8SKeith Packard  * we use as a pipe index
270242f52ef8SKeith Packard  */
270388e72717SThierry Reding static int i915_enable_vblank(struct drm_device *dev, unsigned int pipe)
27040a3e67a4SJesse Barnes {
27052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2706e9d21d7fSKeith Packard 	unsigned long irqflags;
270771e0ffa5SJesse Barnes 
27081ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2709f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
27107c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2711755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
27120a3e67a4SJesse Barnes 	else
27137c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2714755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
27151ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27168692d00eSChris Wilson 
27170a3e67a4SJesse Barnes 	return 0;
27180a3e67a4SJesse Barnes }
27190a3e67a4SJesse Barnes 
272088e72717SThierry Reding static int ironlake_enable_vblank(struct drm_device *dev, unsigned int pipe)
2721f796cf8fSJesse Barnes {
27222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2723f796cf8fSJesse Barnes 	unsigned long irqflags;
2724b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
272540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2726f796cf8fSJesse Barnes 
2727f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2728fbdedaeaSVille Syrjälä 	ilk_enable_display_irq(dev_priv, bit);
2729b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2730b1f14ad0SJesse Barnes 
2731b1f14ad0SJesse Barnes 	return 0;
2732b1f14ad0SJesse Barnes }
2733b1f14ad0SJesse Barnes 
273488e72717SThierry Reding static int valleyview_enable_vblank(struct drm_device *dev, unsigned int pipe)
27357e231dbeSJesse Barnes {
27362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27377e231dbeSJesse Barnes 	unsigned long irqflags;
27387e231dbeSJesse Barnes 
27397e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
274031acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2741755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
27427e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27437e231dbeSJesse Barnes 
27447e231dbeSJesse Barnes 	return 0;
27457e231dbeSJesse Barnes }
27467e231dbeSJesse Barnes 
274788e72717SThierry Reding static int gen8_enable_vblank(struct drm_device *dev, unsigned int pipe)
2748abd58f01SBen Widawsky {
2749abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2750abd58f01SBen Widawsky 	unsigned long irqflags;
2751abd58f01SBen Widawsky 
2752abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2753013d3752SVille Syrjälä 	bdw_enable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2754abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2755013d3752SVille Syrjälä 
2756abd58f01SBen Widawsky 	return 0;
2757abd58f01SBen Widawsky }
2758abd58f01SBen Widawsky 
275942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
276042f52ef8SKeith Packard  * we use as a pipe index
276142f52ef8SKeith Packard  */
276288e72717SThierry Reding static void i915_disable_vblank(struct drm_device *dev, unsigned int pipe)
27630a3e67a4SJesse Barnes {
27642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2765e9d21d7fSKeith Packard 	unsigned long irqflags;
27660a3e67a4SJesse Barnes 
27671ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27687c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2769755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2770755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27711ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27720a3e67a4SJesse Barnes }
27730a3e67a4SJesse Barnes 
277488e72717SThierry Reding static void ironlake_disable_vblank(struct drm_device *dev, unsigned int pipe)
2775f796cf8fSJesse Barnes {
27762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2777f796cf8fSJesse Barnes 	unsigned long irqflags;
2778b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
277940da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2780f796cf8fSJesse Barnes 
2781f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2782fbdedaeaSVille Syrjälä 	ilk_disable_display_irq(dev_priv, bit);
2783b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2784b1f14ad0SJesse Barnes }
2785b1f14ad0SJesse Barnes 
278688e72717SThierry Reding static void valleyview_disable_vblank(struct drm_device *dev, unsigned int pipe)
27877e231dbeSJesse Barnes {
27882d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27897e231dbeSJesse Barnes 	unsigned long irqflags;
27907e231dbeSJesse Barnes 
27917e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
279231acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2793755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27947e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27957e231dbeSJesse Barnes }
27967e231dbeSJesse Barnes 
279788e72717SThierry Reding static void gen8_disable_vblank(struct drm_device *dev, unsigned int pipe)
2798abd58f01SBen Widawsky {
2799abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2800abd58f01SBen Widawsky 	unsigned long irqflags;
2801abd58f01SBen Widawsky 
2802abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2803013d3752SVille Syrjälä 	bdw_disable_pipe_irq(dev_priv, pipe, GEN8_PIPE_VBLANK);
2804abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2805abd58f01SBen Widawsky }
2806abd58f01SBen Widawsky 
28079107e9d2SChris Wilson static bool
280894f7bbe1STomas Elf ring_idle(struct intel_engine_cs *ring, u32 seqno)
2809893eead0SChris Wilson {
28109107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
281194f7bbe1STomas Elf 		i915_seqno_passed(seqno, ring->last_submitted_seqno));
2812f65d9421SBen Gamari }
2813f65d9421SBen Gamari 
2814a028c4b0SDaniel Vetter static bool
2815a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2816a028c4b0SDaniel Vetter {
2817a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2818a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2819a028c4b0SDaniel Vetter 	} else {
2820a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2821a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2822a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2823a028c4b0SDaniel Vetter 	}
2824a028c4b0SDaniel Vetter }
2825a028c4b0SDaniel Vetter 
2826a4872ba6SOscar Mateo static struct intel_engine_cs *
2827a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2828921d42eaSDaniel Vetter {
2829921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2830a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2831921d42eaSDaniel Vetter 	int i;
2832921d42eaSDaniel Vetter 
2833921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2834a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2835a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2836a6cdb93aSRodrigo Vivi 				continue;
2837a6cdb93aSRodrigo Vivi 
2838a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2839a6cdb93aSRodrigo Vivi 				return signaller;
2840a6cdb93aSRodrigo Vivi 		}
2841921d42eaSDaniel Vetter 	} else {
2842921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2843921d42eaSDaniel Vetter 
2844921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2845921d42eaSDaniel Vetter 			if(ring == signaller)
2846921d42eaSDaniel Vetter 				continue;
2847921d42eaSDaniel Vetter 
2848ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2849921d42eaSDaniel Vetter 				return signaller;
2850921d42eaSDaniel Vetter 		}
2851921d42eaSDaniel Vetter 	}
2852921d42eaSDaniel Vetter 
2853a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2854a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2855921d42eaSDaniel Vetter 
2856921d42eaSDaniel Vetter 	return NULL;
2857921d42eaSDaniel Vetter }
2858921d42eaSDaniel Vetter 
2859a4872ba6SOscar Mateo static struct intel_engine_cs *
2860a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2861a24a11e6SChris Wilson {
2862a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
286388fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2864a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2865a6cdb93aSRodrigo Vivi 	int i, backwards;
2866a24a11e6SChris Wilson 
2867381e8ae3STomas Elf 	/*
2868381e8ae3STomas Elf 	 * This function does not support execlist mode - any attempt to
2869381e8ae3STomas Elf 	 * proceed further into this function will result in a kernel panic
2870381e8ae3STomas Elf 	 * when dereferencing ring->buffer, which is not set up in execlist
2871381e8ae3STomas Elf 	 * mode.
2872381e8ae3STomas Elf 	 *
2873381e8ae3STomas Elf 	 * The correct way of doing it would be to derive the currently
2874381e8ae3STomas Elf 	 * executing ring buffer from the current context, which is derived
2875381e8ae3STomas Elf 	 * from the currently running request. Unfortunately, to get the
2876381e8ae3STomas Elf 	 * current request we would have to grab the struct_mutex before doing
2877381e8ae3STomas Elf 	 * anything else, which would be ill-advised since some other thread
2878381e8ae3STomas Elf 	 * might have grabbed it already and managed to hang itself, causing
2879381e8ae3STomas Elf 	 * the hang checker to deadlock.
2880381e8ae3STomas Elf 	 *
2881381e8ae3STomas Elf 	 * Therefore, this function does not support execlist mode in its
2882381e8ae3STomas Elf 	 * current form. Just return NULL and move on.
2883381e8ae3STomas Elf 	 */
2884381e8ae3STomas Elf 	if (ring->buffer == NULL)
2885381e8ae3STomas Elf 		return NULL;
2886381e8ae3STomas Elf 
2887a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2888a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
28896274f212SChris Wilson 		return NULL;
2890a24a11e6SChris Wilson 
289188fe429dSDaniel Vetter 	/*
289288fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
289388fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2894a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2895a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
289688fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
289788fe429dSDaniel Vetter 	 * ringbuffer itself.
2898a24a11e6SChris Wilson 	 */
289988fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2900a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
290188fe429dSDaniel Vetter 
2902a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
290388fe429dSDaniel Vetter 		/*
290488fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
290588fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
290688fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
290788fe429dSDaniel Vetter 		 */
2908ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
290988fe429dSDaniel Vetter 
291088fe429dSDaniel Vetter 		/* This here seems to blow up */
2911ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2912a24a11e6SChris Wilson 		if (cmd == ipehr)
2913a24a11e6SChris Wilson 			break;
2914a24a11e6SChris Wilson 
291588fe429dSDaniel Vetter 		head -= 4;
291688fe429dSDaniel Vetter 	}
2917a24a11e6SChris Wilson 
291888fe429dSDaniel Vetter 	if (!i)
291988fe429dSDaniel Vetter 		return NULL;
292088fe429dSDaniel Vetter 
2921ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2922a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2923a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2924a6cdb93aSRodrigo Vivi 		offset <<= 32;
2925a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2926a6cdb93aSRodrigo Vivi 	}
2927a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2928a24a11e6SChris Wilson }
2929a24a11e6SChris Wilson 
2930a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
29316274f212SChris Wilson {
29326274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2933a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2934a0d036b0SChris Wilson 	u32 seqno;
29356274f212SChris Wilson 
29364be17381SChris Wilson 	ring->hangcheck.deadlock++;
29376274f212SChris Wilson 
29386274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
29394be17381SChris Wilson 	if (signaller == NULL)
29404be17381SChris Wilson 		return -1;
29414be17381SChris Wilson 
29424be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
29434be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
29446274f212SChris Wilson 		return -1;
29456274f212SChris Wilson 
29464be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
29474be17381SChris Wilson 		return 1;
29484be17381SChris Wilson 
2949a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2950a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2951a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
29524be17381SChris Wilson 		return -1;
29534be17381SChris Wilson 
29544be17381SChris Wilson 	return 0;
29556274f212SChris Wilson }
29566274f212SChris Wilson 
29576274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
29586274f212SChris Wilson {
2959a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
29606274f212SChris Wilson 	int i;
29616274f212SChris Wilson 
29626274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
29634be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
29646274f212SChris Wilson }
29656274f212SChris Wilson 
296661642ff0SMika Kuoppala static bool subunits_stuck(struct intel_engine_cs *ring)
29671ec14ad3SChris Wilson {
296861642ff0SMika Kuoppala 	u32 instdone[I915_NUM_INSTDONE_REG];
296961642ff0SMika Kuoppala 	bool stuck;
297061642ff0SMika Kuoppala 	int i;
29719107e9d2SChris Wilson 
297261642ff0SMika Kuoppala 	if (ring->id != RCS)
297361642ff0SMika Kuoppala 		return true;
297461642ff0SMika Kuoppala 
297561642ff0SMika Kuoppala 	i915_get_extra_instdone(ring->dev, instdone);
297661642ff0SMika Kuoppala 
297761642ff0SMika Kuoppala 	/* There might be unstable subunit states even when
297861642ff0SMika Kuoppala 	 * actual head is not moving. Filter out the unstable ones by
297961642ff0SMika Kuoppala 	 * accumulating the undone -> done transitions and only
298061642ff0SMika Kuoppala 	 * consider those as progress.
298161642ff0SMika Kuoppala 	 */
298261642ff0SMika Kuoppala 	stuck = true;
298361642ff0SMika Kuoppala 	for (i = 0; i < I915_NUM_INSTDONE_REG; i++) {
298461642ff0SMika Kuoppala 		const u32 tmp = instdone[i] | ring->hangcheck.instdone[i];
298561642ff0SMika Kuoppala 
298661642ff0SMika Kuoppala 		if (tmp != ring->hangcheck.instdone[i])
298761642ff0SMika Kuoppala 			stuck = false;
298861642ff0SMika Kuoppala 
298961642ff0SMika Kuoppala 		ring->hangcheck.instdone[i] |= tmp;
299061642ff0SMika Kuoppala 	}
299161642ff0SMika Kuoppala 
299261642ff0SMika Kuoppala 	return stuck;
299361642ff0SMika Kuoppala }
299461642ff0SMika Kuoppala 
299561642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
299661642ff0SMika Kuoppala head_stuck(struct intel_engine_cs *ring, u64 acthd)
299761642ff0SMika Kuoppala {
2998f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
299961642ff0SMika Kuoppala 
300061642ff0SMika Kuoppala 		/* Clear subunit states on head movement */
300161642ff0SMika Kuoppala 		memset(ring->hangcheck.instdone, 0,
300261642ff0SMika Kuoppala 		       sizeof(ring->hangcheck.instdone));
300361642ff0SMika Kuoppala 
3004f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
3005f260fe7bSMika Kuoppala 	}
3006f260fe7bSMika Kuoppala 
300761642ff0SMika Kuoppala 	if (!subunits_stuck(ring))
300861642ff0SMika Kuoppala 		return HANGCHECK_ACTIVE;
300961642ff0SMika Kuoppala 
301061642ff0SMika Kuoppala 	return HANGCHECK_HUNG;
301161642ff0SMika Kuoppala }
301261642ff0SMika Kuoppala 
301361642ff0SMika Kuoppala static enum intel_ring_hangcheck_action
301461642ff0SMika Kuoppala ring_stuck(struct intel_engine_cs *ring, u64 acthd)
301561642ff0SMika Kuoppala {
301661642ff0SMika Kuoppala 	struct drm_device *dev = ring->dev;
301761642ff0SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
301861642ff0SMika Kuoppala 	enum intel_ring_hangcheck_action ha;
301961642ff0SMika Kuoppala 	u32 tmp;
302061642ff0SMika Kuoppala 
302161642ff0SMika Kuoppala 	ha = head_stuck(ring, acthd);
302261642ff0SMika Kuoppala 	if (ha != HANGCHECK_HUNG)
302361642ff0SMika Kuoppala 		return ha;
302461642ff0SMika Kuoppala 
30259107e9d2SChris Wilson 	if (IS_GEN2(dev))
3026f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
30279107e9d2SChris Wilson 
30289107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
30299107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
30309107e9d2SChris Wilson 	 * and break the hang. This should work on
30319107e9d2SChris Wilson 	 * all but the second generation chipsets.
30329107e9d2SChris Wilson 	 */
30339107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
30341ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
303558174462SMika Kuoppala 		i915_handle_error(dev, false,
303658174462SMika Kuoppala 				  "Kicking stuck wait on %s",
30371ec14ad3SChris Wilson 				  ring->name);
30381ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
3039f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
30401ec14ad3SChris Wilson 	}
3041a24a11e6SChris Wilson 
30426274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
30436274f212SChris Wilson 		switch (semaphore_passed(ring)) {
30446274f212SChris Wilson 		default:
3045f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
30466274f212SChris Wilson 		case 1:
304758174462SMika Kuoppala 			i915_handle_error(dev, false,
304858174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
3049a24a11e6SChris Wilson 					  ring->name);
3050a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
3051f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
30526274f212SChris Wilson 		case 0:
3053f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
30546274f212SChris Wilson 		}
30559107e9d2SChris Wilson 	}
30569107e9d2SChris Wilson 
3057f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
3058a24a11e6SChris Wilson }
3059d1e61e7fSChris Wilson 
3060737b1506SChris Wilson /*
3061f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
306205407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
306305407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
306405407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
306505407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
306605407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
3067f65d9421SBen Gamari  */
3068737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
3069f65d9421SBen Gamari {
3070737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
3071737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
3072737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
3073737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
3074a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
3075b4519513SChris Wilson 	int i;
307605407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
30779107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
30789107e9d2SChris Wilson #define BUSY 1
30799107e9d2SChris Wilson #define KICK 5
30809107e9d2SChris Wilson #define HUNG 20
3081*24a65e62SMika Kuoppala #define ACTIVE_DECAY 15
3082893eead0SChris Wilson 
3083d330a953SJani Nikula 	if (!i915.enable_hangcheck)
30843e0dc6b0SBen Widawsky 		return;
30853e0dc6b0SBen Widawsky 
30861f814dacSImre Deak 	/*
30871f814dacSImre Deak 	 * The hangcheck work is synced during runtime suspend, we don't
30881f814dacSImre Deak 	 * require a wakeref. TODO: instead of disabling the asserts make
30891f814dacSImre Deak 	 * sure that we hold a reference when this work is running.
30901f814dacSImre Deak 	 */
30911f814dacSImre Deak 	DISABLE_RPM_WAKEREF_ASSERTS(dev_priv);
30921f814dacSImre Deak 
309375714940SMika Kuoppala 	/* As enabling the GPU requires fairly extensive mmio access,
309475714940SMika Kuoppala 	 * periodically arm the mmio checker to see if we are triggering
309575714940SMika Kuoppala 	 * any invalid access.
309675714940SMika Kuoppala 	 */
309775714940SMika Kuoppala 	intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
309875714940SMika Kuoppala 
3099b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
310050877445SChris Wilson 		u64 acthd;
310150877445SChris Wilson 		u32 seqno;
31029107e9d2SChris Wilson 		bool busy = true;
3103b4519513SChris Wilson 
31046274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
31056274f212SChris Wilson 
310605407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
310705407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
310805407ff8SMika Kuoppala 
310905407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
311094f7bbe1STomas Elf 			if (ring_idle(ring, seqno)) {
3111da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
3112da661464SMika Kuoppala 
31139107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
31149107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
3115094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
3116f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
31179107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
31189107e9d2SChris Wilson 								  ring->name);
3119f4adcd24SDaniel Vetter 						else
3120f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
3121f4adcd24SDaniel Vetter 								 ring->name);
31229107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
3123094f9a54SChris Wilson 					}
3124094f9a54SChris Wilson 					/* Safeguard against driver failure */
3125094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
31269107e9d2SChris Wilson 				} else
31279107e9d2SChris Wilson 					busy = false;
312805407ff8SMika Kuoppala 			} else {
31296274f212SChris Wilson 				/* We always increment the hangcheck score
31306274f212SChris Wilson 				 * if the ring is busy and still processing
31316274f212SChris Wilson 				 * the same request, so that no single request
31326274f212SChris Wilson 				 * can run indefinitely (such as a chain of
31336274f212SChris Wilson 				 * batches). The only time we do not increment
31346274f212SChris Wilson 				 * the hangcheck score on this ring, if this
31356274f212SChris Wilson 				 * ring is in a legitimate wait for another
31366274f212SChris Wilson 				 * ring. In that case the waiting ring is a
31376274f212SChris Wilson 				 * victim and we want to be sure we catch the
31386274f212SChris Wilson 				 * right culprit. Then every time we do kick
31396274f212SChris Wilson 				 * the ring, add a small increment to the
31406274f212SChris Wilson 				 * score so that we can catch a batch that is
31416274f212SChris Wilson 				 * being repeatedly kicked and so responsible
31426274f212SChris Wilson 				 * for stalling the machine.
31439107e9d2SChris Wilson 				 */
3144ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3145ad8beaeaSMika Kuoppala 								    acthd);
3146ad8beaeaSMika Kuoppala 
3147ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3148da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3149f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3150f260fe7bSMika Kuoppala 					break;
3151*24a65e62SMika Kuoppala 				case HANGCHECK_ACTIVE:
3152ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
31536274f212SChris Wilson 					break;
3154f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3155ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
31566274f212SChris Wilson 					break;
3157f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3158ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
31596274f212SChris Wilson 					stuck[i] = true;
31606274f212SChris Wilson 					break;
31616274f212SChris Wilson 				}
316205407ff8SMika Kuoppala 			}
31639107e9d2SChris Wilson 		} else {
3164da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3165da661464SMika Kuoppala 
31669107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
31679107e9d2SChris Wilson 			 * attempts across multiple batches.
31689107e9d2SChris Wilson 			 */
31699107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
3170*24a65e62SMika Kuoppala 				ring->hangcheck.score -= ACTIVE_DECAY;
3171*24a65e62SMika Kuoppala 			if (ring->hangcheck.score < 0)
3172*24a65e62SMika Kuoppala 				ring->hangcheck.score = 0;
3173f260fe7bSMika Kuoppala 
317461642ff0SMika Kuoppala 			/* Clear head and subunit states on seqno movement */
3175*24a65e62SMika Kuoppala 			ring->hangcheck.acthd = 0;
317661642ff0SMika Kuoppala 
317761642ff0SMika Kuoppala 			memset(ring->hangcheck.instdone, 0,
317861642ff0SMika Kuoppala 			       sizeof(ring->hangcheck.instdone));
3179cbb465e7SChris Wilson 		}
3180f65d9421SBen Gamari 
318105407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
318205407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
31839107e9d2SChris Wilson 		busy_count += busy;
318405407ff8SMika Kuoppala 	}
318505407ff8SMika Kuoppala 
318605407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3187b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3188b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
318905407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3190a43adf07SChris Wilson 				 ring->name);
3191a43adf07SChris Wilson 			rings_hung++;
319205407ff8SMika Kuoppala 		}
319305407ff8SMika Kuoppala 	}
319405407ff8SMika Kuoppala 
31951f814dacSImre Deak 	if (rings_hung) {
31961f814dacSImre Deak 		i915_handle_error(dev, true, "Ring hung");
31971f814dacSImre Deak 		goto out;
31981f814dacSImre Deak 	}
319905407ff8SMika Kuoppala 
320005407ff8SMika Kuoppala 	if (busy_count)
320105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
320205407ff8SMika Kuoppala 		 * being added */
320310cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
32041f814dacSImre Deak 
32051f814dacSImre Deak out:
32061f814dacSImre Deak 	ENABLE_RPM_WAKEREF_ASSERTS(dev_priv);
320710cd45b6SMika Kuoppala }
320810cd45b6SMika Kuoppala 
320910cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
321010cd45b6SMika Kuoppala {
3211737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3212672e7b7cSChris Wilson 
3213d330a953SJani Nikula 	if (!i915.enable_hangcheck)
321410cd45b6SMika Kuoppala 		return;
321510cd45b6SMika Kuoppala 
3216737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3217737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3218737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3219737b1506SChris Wilson 	 */
3220737b1506SChris Wilson 
3221737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3222737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3223f65d9421SBen Gamari }
3224f65d9421SBen Gamari 
32251c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
322691738a95SPaulo Zanoni {
322791738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
322891738a95SPaulo Zanoni 
322991738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
323091738a95SPaulo Zanoni 		return;
323191738a95SPaulo Zanoni 
3232f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3233105b122eSPaulo Zanoni 
3234105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3235105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3236622364b6SPaulo Zanoni }
3237105b122eSPaulo Zanoni 
323891738a95SPaulo Zanoni /*
3239622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3240622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3241622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3242622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3243622364b6SPaulo Zanoni  *
3244622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
324591738a95SPaulo Zanoni  */
3246622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3247622364b6SPaulo Zanoni {
3248622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3249622364b6SPaulo Zanoni 
3250622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3251622364b6SPaulo Zanoni 		return;
3252622364b6SPaulo Zanoni 
3253622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
325491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
325591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
325691738a95SPaulo Zanoni }
325791738a95SPaulo Zanoni 
32587c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3259d18ea1b5SDaniel Vetter {
3260d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3261d18ea1b5SDaniel Vetter 
3262f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3263a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3264f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3265d18ea1b5SDaniel Vetter }
3266d18ea1b5SDaniel Vetter 
3267c0e09200SDave Airlie /* drm_dma.h hooks
3268c0e09200SDave Airlie */
3269be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3270036a4a7dSZhenyu Wang {
32712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3272036a4a7dSZhenyu Wang 
32730c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3274bdfcdb63SDaniel Vetter 
3275f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3276c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3277c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3278036a4a7dSZhenyu Wang 
32797c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3280c650156aSZhenyu Wang 
32811c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
32827d99163dSBen Widawsky }
32837d99163dSBen Widawsky 
328470591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
328570591a41SVille Syrjälä {
328670591a41SVille Syrjälä 	enum pipe pipe;
328770591a41SVille Syrjälä 
32880706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xFFFFFFFF, 0);
328970591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
329070591a41SVille Syrjälä 
329170591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
329270591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
329370591a41SVille Syrjälä 
329470591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
329570591a41SVille Syrjälä }
329670591a41SVille Syrjälä 
32977e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
32987e231dbeSJesse Barnes {
32992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33007e231dbeSJesse Barnes 
33017e231dbeSJesse Barnes 	/* VLV magic */
33027e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
33037e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
33047e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
33057e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
33067e231dbeSJesse Barnes 
33077c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
33087e231dbeSJesse Barnes 
33097c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
33107e231dbeSJesse Barnes 
331170591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
33127e231dbeSJesse Barnes }
33137e231dbeSJesse Barnes 
3314d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3315d6e3cca3SDaniel Vetter {
3316d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3317d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3318d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3319d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3320d6e3cca3SDaniel Vetter }
3321d6e3cca3SDaniel Vetter 
3322823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3323abd58f01SBen Widawsky {
3324abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3325abd58f01SBen Widawsky 	int pipe;
3326abd58f01SBen Widawsky 
3327abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3328abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3329abd58f01SBen Widawsky 
3330d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3331abd58f01SBen Widawsky 
3332055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3333f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3334813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3335f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3336abd58f01SBen Widawsky 
3337f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3338f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3339f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3340abd58f01SBen Widawsky 
3341266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
33421c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3343abd58f01SBen Widawsky }
3344abd58f01SBen Widawsky 
33454c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
33464c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3347d49bdb0eSPaulo Zanoni {
33481180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
33496831f3e3SVille Syrjälä 	enum pipe pipe;
3350d49bdb0eSPaulo Zanoni 
335113321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
33526831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33536831f3e3SVille Syrjälä 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
33546831f3e3SVille Syrjälä 				  dev_priv->de_irq_mask[pipe],
33556831f3e3SVille Syrjälä 				  ~dev_priv->de_irq_mask[pipe] | extra_ier);
335613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3357d49bdb0eSPaulo Zanoni }
3358d49bdb0eSPaulo Zanoni 
3359aae8ba84SVille Syrjälä void gen8_irq_power_well_pre_disable(struct drm_i915_private *dev_priv,
3360aae8ba84SVille Syrjälä 				     unsigned int pipe_mask)
3361aae8ba84SVille Syrjälä {
33626831f3e3SVille Syrjälä 	enum pipe pipe;
33636831f3e3SVille Syrjälä 
3364aae8ba84SVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
33656831f3e3SVille Syrjälä 	for_each_pipe_masked(dev_priv, pipe, pipe_mask)
33666831f3e3SVille Syrjälä 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3367aae8ba84SVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
3368aae8ba84SVille Syrjälä 
3369aae8ba84SVille Syrjälä 	/* make sure we're done processing display irqs */
3370aae8ba84SVille Syrjälä 	synchronize_irq(dev_priv->dev->irq);
3371aae8ba84SVille Syrjälä }
3372aae8ba84SVille Syrjälä 
337343f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
337443f328d7SVille Syrjälä {
337543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
337643f328d7SVille Syrjälä 
337743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
337843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
337943f328d7SVille Syrjälä 
3380d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
338143f328d7SVille Syrjälä 
338243f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
338343f328d7SVille Syrjälä 
338443f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
338543f328d7SVille Syrjälä 
338670591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
338743f328d7SVille Syrjälä }
338843f328d7SVille Syrjälä 
338987a02106SVille Syrjälä static u32 intel_hpd_enabled_irqs(struct drm_device *dev,
339087a02106SVille Syrjälä 				  const u32 hpd[HPD_NUM_PINS])
339187a02106SVille Syrjälä {
339287a02106SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(dev);
339387a02106SVille Syrjälä 	struct intel_encoder *encoder;
339487a02106SVille Syrjälä 	u32 enabled_irqs = 0;
339587a02106SVille Syrjälä 
339687a02106SVille Syrjälä 	for_each_intel_encoder(dev, encoder)
339787a02106SVille Syrjälä 		if (dev_priv->hotplug.stats[encoder->hpd_pin].state == HPD_ENABLED)
339887a02106SVille Syrjälä 			enabled_irqs |= hpd[encoder->hpd_pin];
339987a02106SVille Syrjälä 
340087a02106SVille Syrjälä 	return enabled_irqs;
340187a02106SVille Syrjälä }
340287a02106SVille Syrjälä 
340382a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
340482a28bcfSDaniel Vetter {
34052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
340687a02106SVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
340782a28bcfSDaniel Vetter 
340882a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3409fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
341087a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ibx);
341182a28bcfSDaniel Vetter 	} else {
3412fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
341387a02106SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_cpt);
341482a28bcfSDaniel Vetter 	}
341582a28bcfSDaniel Vetter 
3416fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
341782a28bcfSDaniel Vetter 
34187fe0b973SKeith Packard 	/*
34197fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
34206dbf30ceSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec).
34216dbf30ceSVille Syrjälä 	 * The pulse duration bits are reserved on LPT+.
34227fe0b973SKeith Packard 	 */
34237fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34247fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
34257fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
34267fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
34277fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
34280b2eb33eSVille Syrjälä 	/*
34290b2eb33eSVille Syrjälä 	 * When CPU and PCH are on the same package, port A
34300b2eb33eSVille Syrjälä 	 * HPD must be enabled in both north and south.
34310b2eb33eSVille Syrjälä 	 */
34320b2eb33eSVille Syrjälä 	if (HAS_PCH_LPT_LP(dev))
34330b2eb33eSVille Syrjälä 		hotplug |= PORTA_HOTPLUG_ENABLE;
34347fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34356dbf30ceSVille Syrjälä }
343626951cafSXiong Zhang 
34376dbf30ceSVille Syrjälä static void spt_hpd_irq_setup(struct drm_device *dev)
34386dbf30ceSVille Syrjälä {
34396dbf30ceSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34406dbf30ceSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
34416dbf30ceSVille Syrjälä 
34426dbf30ceSVille Syrjälä 	hotplug_irqs = SDE_HOTPLUG_MASK_SPT;
34436dbf30ceSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_spt);
34446dbf30ceSVille Syrjälä 
34456dbf30ceSVille Syrjälä 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
34466dbf30ceSVille Syrjälä 
34476dbf30ceSVille Syrjälä 	/* Enable digital hotplug on the PCH */
34486dbf30ceSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
34496dbf30ceSVille Syrjälä 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTC_HOTPLUG_ENABLE |
345074c0b395SVille Syrjälä 		PORTB_HOTPLUG_ENABLE | PORTA_HOTPLUG_ENABLE;
34516dbf30ceSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
34526dbf30ceSVille Syrjälä 
345326951cafSXiong Zhang 	hotplug = I915_READ(PCH_PORT_HOTPLUG2);
345426951cafSXiong Zhang 	hotplug |= PORTE_HOTPLUG_ENABLE;
345526951cafSXiong Zhang 	I915_WRITE(PCH_PORT_HOTPLUG2, hotplug);
345626951cafSXiong Zhang }
34577fe0b973SKeith Packard 
3458e4ce95aaSVille Syrjälä static void ilk_hpd_irq_setup(struct drm_device *dev)
3459e4ce95aaSVille Syrjälä {
3460e4ce95aaSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3461e4ce95aaSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3462e4ce95aaSVille Syrjälä 
34633a3b3c7dSVille Syrjälä 	if (INTEL_INFO(dev)->gen >= 8) {
34643a3b3c7dSVille Syrjälä 		hotplug_irqs = GEN8_PORT_DP_A_HOTPLUG;
34653a3b3c7dSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bdw);
34663a3b3c7dSVille Syrjälä 
34673a3b3c7dSVille Syrjälä 		bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
34683a3b3c7dSVille Syrjälä 	} else if (INTEL_INFO(dev)->gen >= 7) {
346923bb4cb5SVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG_IVB;
347023bb4cb5SVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ivb);
34713a3b3c7dSVille Syrjälä 
34723a3b3c7dSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
347323bb4cb5SVille Syrjälä 	} else {
3474e4ce95aaSVille Syrjälä 		hotplug_irqs = DE_DP_A_HOTPLUG;
3475e4ce95aaSVille Syrjälä 		enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_ilk);
3476e4ce95aaSVille Syrjälä 
3477e4ce95aaSVille Syrjälä 		ilk_update_display_irq(dev_priv, hotplug_irqs, enabled_irqs);
34783a3b3c7dSVille Syrjälä 	}
3479e4ce95aaSVille Syrjälä 
3480e4ce95aaSVille Syrjälä 	/*
3481e4ce95aaSVille Syrjälä 	 * Enable digital hotplug on the CPU, and configure the DP short pulse
3482e4ce95aaSVille Syrjälä 	 * duration to 2ms (which is the minimum in the Display Port spec)
348323bb4cb5SVille Syrjälä 	 * The pulse duration bits are reserved on HSW+.
3484e4ce95aaSVille Syrjälä 	 */
3485e4ce95aaSVille Syrjälä 	hotplug = I915_READ(DIGITAL_PORT_HOTPLUG_CNTRL);
3486e4ce95aaSVille Syrjälä 	hotplug &= ~DIGITAL_PORTA_PULSE_DURATION_MASK;
3487e4ce95aaSVille Syrjälä 	hotplug |= DIGITAL_PORTA_HOTPLUG_ENABLE | DIGITAL_PORTA_PULSE_DURATION_2ms;
3488e4ce95aaSVille Syrjälä 	I915_WRITE(DIGITAL_PORT_HOTPLUG_CNTRL, hotplug);
3489e4ce95aaSVille Syrjälä 
3490e4ce95aaSVille Syrjälä 	ibx_hpd_irq_setup(dev);
3491e4ce95aaSVille Syrjälä }
3492e4ce95aaSVille Syrjälä 
3493e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3494e0a20ad7SShashank Sharma {
3495e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3496a52bb15bSVille Syrjälä 	u32 hotplug_irqs, hotplug, enabled_irqs;
3497e0a20ad7SShashank Sharma 
3498a52bb15bSVille Syrjälä 	enabled_irqs = intel_hpd_enabled_irqs(dev, hpd_bxt);
3499a52bb15bSVille Syrjälä 	hotplug_irqs = BXT_DE_PORT_HOTPLUG_MASK;
3500e0a20ad7SShashank Sharma 
3501a52bb15bSVille Syrjälä 	bdw_update_port_irq(dev_priv, hotplug_irqs, enabled_irqs);
3502e0a20ad7SShashank Sharma 
3503a52bb15bSVille Syrjälä 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
3504a52bb15bSVille Syrjälä 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTB_HOTPLUG_ENABLE |
3505a52bb15bSVille Syrjälä 		PORTA_HOTPLUG_ENABLE;
3506a52bb15bSVille Syrjälä 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
3507e0a20ad7SShashank Sharma }
3508e0a20ad7SShashank Sharma 
3509d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3510d46da437SPaulo Zanoni {
35112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
351282a28bcfSDaniel Vetter 	u32 mask;
3513d46da437SPaulo Zanoni 
3514692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3515692a04cfSDaniel Vetter 		return;
3516692a04cfSDaniel Vetter 
3517105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
35185c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3519105b122eSPaulo Zanoni 	else
35205c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
35218664281bSPaulo Zanoni 
3522b51a2842SVille Syrjälä 	gen5_assert_iir_is_zero(dev_priv, SDEIIR);
3523d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3524d46da437SPaulo Zanoni }
3525d46da437SPaulo Zanoni 
35260a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
35270a9a8c91SDaniel Vetter {
35280a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
35290a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
35300a9a8c91SDaniel Vetter 
35310a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
35320a9a8c91SDaniel Vetter 
35330a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3534040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
35350a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
353635a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
353735a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
35380a9a8c91SDaniel Vetter 	}
35390a9a8c91SDaniel Vetter 
35400a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
35410a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
35420a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
35430a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
35440a9a8c91SDaniel Vetter 	} else {
35450a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
35460a9a8c91SDaniel Vetter 	}
35470a9a8c91SDaniel Vetter 
354835079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
35490a9a8c91SDaniel Vetter 
35500a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
355178e68d36SImre Deak 		/*
355278e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
355378e68d36SImre Deak 		 * itself is enabled/disabled.
355478e68d36SImre Deak 		 */
35550a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
35560a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
35570a9a8c91SDaniel Vetter 
3558605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
355935079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
35600a9a8c91SDaniel Vetter 	}
35610a9a8c91SDaniel Vetter }
35620a9a8c91SDaniel Vetter 
3563f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3564036a4a7dSZhenyu Wang {
35652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35668e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
35678e76f8dcSPaulo Zanoni 
35688e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
35698e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
35708e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
35718e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
35725c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
35738e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
357423bb4cb5SVille Syrjälä 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB |
357523bb4cb5SVille Syrjälä 			      DE_DP_A_HOTPLUG_IVB);
35768e76f8dcSPaulo Zanoni 	} else {
35778e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3578ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
35795b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
35805b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
35815b3a856bSDaniel Vetter 				DE_POISON);
3582e4ce95aaSVille Syrjälä 		extra_mask = (DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
3583e4ce95aaSVille Syrjälä 			      DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN |
3584e4ce95aaSVille Syrjälä 			      DE_DP_A_HOTPLUG);
35858e76f8dcSPaulo Zanoni 	}
3586036a4a7dSZhenyu Wang 
35871ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3588036a4a7dSZhenyu Wang 
35890c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
35900c841212SPaulo Zanoni 
3591622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3592622364b6SPaulo Zanoni 
359335079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3594036a4a7dSZhenyu Wang 
35950a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3596036a4a7dSZhenyu Wang 
3597d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
35987fe0b973SKeith Packard 
3599f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
36006005ce42SDaniel Vetter 		/* Enable PCU event interrupts
36016005ce42SDaniel Vetter 		 *
36026005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
36034bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
36044bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3605d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3606fbdedaeaSVille Syrjälä 		ilk_enable_display_irq(dev_priv, DE_PCU_EVENT);
3607d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3608f97108d1SJesse Barnes 	}
3609f97108d1SJesse Barnes 
3610036a4a7dSZhenyu Wang 	return 0;
3611036a4a7dSZhenyu Wang }
3612036a4a7dSZhenyu Wang 
3613f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3614f8b79e58SImre Deak {
3615f8b79e58SImre Deak 	u32 pipestat_mask;
3616f8b79e58SImre Deak 	u32 iir_mask;
3617120dda4fSVille Syrjälä 	enum pipe pipe;
3618f8b79e58SImre Deak 
3619f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3620f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3621f8b79e58SImre Deak 
3622120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3623120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3624f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3625f8b79e58SImre Deak 
3626f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3627f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3628f8b79e58SImre Deak 
3629120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3630120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3631120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3632f8b79e58SImre Deak 
3633f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3634f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3635f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3636120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3637120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3638f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3639f8b79e58SImre Deak 
3640f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3641f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3642f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
364376e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
364476e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3645f8b79e58SImre Deak }
3646f8b79e58SImre Deak 
3647f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3648f8b79e58SImre Deak {
3649f8b79e58SImre Deak 	u32 pipestat_mask;
3650f8b79e58SImre Deak 	u32 iir_mask;
3651120dda4fSVille Syrjälä 	enum pipe pipe;
3652f8b79e58SImre Deak 
3653f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3654f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
36556c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3656120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3657120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3658f8b79e58SImre Deak 
3659f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3660f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
366176e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3662f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3663f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3664f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3665f8b79e58SImre Deak 
3666f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3667f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3668f8b79e58SImre Deak 
3669120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3670120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3671120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3672f8b79e58SImre Deak 
3673f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3674f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3675120dda4fSVille Syrjälä 
3676120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3677120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3678f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3679f8b79e58SImre Deak }
3680f8b79e58SImre Deak 
3681f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3682f8b79e58SImre Deak {
3683f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3684f8b79e58SImre Deak 
3685f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3686f8b79e58SImre Deak 		return;
3687f8b79e58SImre Deak 
3688f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3689f8b79e58SImre Deak 
3690950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3691f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3692f8b79e58SImre Deak }
3693f8b79e58SImre Deak 
3694f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3695f8b79e58SImre Deak {
3696f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3697f8b79e58SImre Deak 
3698f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3699f8b79e58SImre Deak 		return;
3700f8b79e58SImre Deak 
3701f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3702f8b79e58SImre Deak 
3703950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3704f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3705f8b79e58SImre Deak }
3706f8b79e58SImre Deak 
37070e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
37087e231dbeSJesse Barnes {
3709f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
37107e231dbeSJesse Barnes 
37110706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
371220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
371320afbda2SDaniel Vetter 
37147e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
371576e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
371676e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
371776e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
371876e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
37197e231dbeSJesse Barnes 
3720b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3721b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3722d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3723f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3724f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3725d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
37260e6c9a9eSVille Syrjälä }
37270e6c9a9eSVille Syrjälä 
37280e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
37290e6c9a9eSVille Syrjälä {
37300e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
37310e6c9a9eSVille Syrjälä 
37320e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
37337e231dbeSJesse Barnes 
37340a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
37357e231dbeSJesse Barnes 
37367e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
37377e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
37387e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
37397e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
37407e231dbeSJesse Barnes #endif
37417e231dbeSJesse Barnes 
37427e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
374320afbda2SDaniel Vetter 
374420afbda2SDaniel Vetter 	return 0;
374520afbda2SDaniel Vetter }
374620afbda2SDaniel Vetter 
3747abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3748abd58f01SBen Widawsky {
3749abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3750abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3751abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
375273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3753abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
375473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
375573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3756abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
375873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
375973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3760abd58f01SBen Widawsky 		0,
376173d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
376273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3763abd58f01SBen Widawsky 		};
3764abd58f01SBen Widawsky 
37650961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
37669a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
37679a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
376878e68d36SImre Deak 	/*
376978e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
377078e68d36SImre Deak 	 * is enabled/disabled.
377178e68d36SImre Deak 	 */
377278e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
37739a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3774abd58f01SBen Widawsky }
3775abd58f01SBen Widawsky 
3776abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3777abd58f01SBen Widawsky {
3778770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3779770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
37803a3b3c7dSVille Syrjälä 	u32 de_port_masked = GEN8_AUX_CHANNEL_A;
37813a3b3c7dSVille Syrjälä 	u32 de_port_enables;
37823a3b3c7dSVille Syrjälä 	enum pipe pipe;
3783770de83dSDamien Lespiau 
3784b4834a50SRodrigo Vivi 	if (INTEL_INFO(dev_priv)->gen >= 9) {
3785770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3786770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
37873a3b3c7dSVille Syrjälä 		de_port_masked |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
378888e04703SJesse Barnes 				  GEN9_AUX_CHANNEL_D;
37899e63743eSShashank Sharma 		if (IS_BROXTON(dev_priv))
37903a3b3c7dSVille Syrjälä 			de_port_masked |= BXT_DE_PORT_GMBUS;
37913a3b3c7dSVille Syrjälä 	} else {
3792770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3793770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
37943a3b3c7dSVille Syrjälä 	}
3795770de83dSDamien Lespiau 
3796770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3797770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3798770de83dSDamien Lespiau 
37993a3b3c7dSVille Syrjälä 	de_port_enables = de_port_masked;
3800a52bb15bSVille Syrjälä 	if (IS_BROXTON(dev_priv))
3801a52bb15bSVille Syrjälä 		de_port_enables |= BXT_DE_PORT_HOTPLUG_MASK;
3802a52bb15bSVille Syrjälä 	else if (IS_BROADWELL(dev_priv))
38033a3b3c7dSVille Syrjälä 		de_port_enables |= GEN8_PORT_DP_A_HOTPLUG;
38043a3b3c7dSVille Syrjälä 
380513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
380613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
380713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3808abd58f01SBen Widawsky 
3809055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3810f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3811813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3812813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3813813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
381435079899SPaulo Zanoni 					  de_pipe_enables);
3815abd58f01SBen Widawsky 
38163a3b3c7dSVille Syrjälä 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~de_port_masked, de_port_enables);
3817abd58f01SBen Widawsky }
3818abd58f01SBen Widawsky 
3819abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3820abd58f01SBen Widawsky {
3821abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3822abd58f01SBen Widawsky 
3823266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3824622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3825622364b6SPaulo Zanoni 
3826abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3827abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3828abd58f01SBen Widawsky 
3829266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3830abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3831abd58f01SBen Widawsky 
3832abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3833abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3834abd58f01SBen Widawsky 
3835abd58f01SBen Widawsky 	return 0;
3836abd58f01SBen Widawsky }
3837abd58f01SBen Widawsky 
383843f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
383943f328d7SVille Syrjälä {
384043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
384143f328d7SVille Syrjälä 
3842c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
384343f328d7SVille Syrjälä 
384443f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
384543f328d7SVille Syrjälä 
384643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
384743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
384843f328d7SVille Syrjälä 
384943f328d7SVille Syrjälä 	return 0;
385043f328d7SVille Syrjälä }
385143f328d7SVille Syrjälä 
3852abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3853abd58f01SBen Widawsky {
3854abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3855abd58f01SBen Widawsky 
3856abd58f01SBen Widawsky 	if (!dev_priv)
3857abd58f01SBen Widawsky 		return;
3858abd58f01SBen Widawsky 
3859823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3860abd58f01SBen Widawsky }
3861abd58f01SBen Widawsky 
38628ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
38638ea0be4fSVille Syrjälä {
38648ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
38658ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
38668ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
38678ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
38688ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
38698ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
38708ea0be4fSVille Syrjälä 
38718ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
38728ea0be4fSVille Syrjälä 
3873c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
38748ea0be4fSVille Syrjälä }
38758ea0be4fSVille Syrjälä 
38767e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
38777e231dbeSJesse Barnes {
38782d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38797e231dbeSJesse Barnes 
38807e231dbeSJesse Barnes 	if (!dev_priv)
38817e231dbeSJesse Barnes 		return;
38827e231dbeSJesse Barnes 
3883843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3884843d0e7dSImre Deak 
3885893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3886893fce8eSVille Syrjälä 
38877e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3888f8b79e58SImre Deak 
38898ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
38907e231dbeSJesse Barnes }
38917e231dbeSJesse Barnes 
389243f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
389343f328d7SVille Syrjälä {
389443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
389543f328d7SVille Syrjälä 
389643f328d7SVille Syrjälä 	if (!dev_priv)
389743f328d7SVille Syrjälä 		return;
389843f328d7SVille Syrjälä 
389943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
390043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
390143f328d7SVille Syrjälä 
3902a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
390343f328d7SVille Syrjälä 
3904a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
390543f328d7SVille Syrjälä 
3906c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
390743f328d7SVille Syrjälä }
390843f328d7SVille Syrjälä 
3909f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3910036a4a7dSZhenyu Wang {
39112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39124697995bSJesse Barnes 
39134697995bSJesse Barnes 	if (!dev_priv)
39144697995bSJesse Barnes 		return;
39154697995bSJesse Barnes 
3916be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3917036a4a7dSZhenyu Wang }
3918036a4a7dSZhenyu Wang 
3919c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3920c2798b19SChris Wilson {
39212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3922c2798b19SChris Wilson 	int pipe;
3923c2798b19SChris Wilson 
3924055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3925c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3926c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3927c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3928c2798b19SChris Wilson 	POSTING_READ16(IER);
3929c2798b19SChris Wilson }
3930c2798b19SChris Wilson 
3931c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3932c2798b19SChris Wilson {
39332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3934c2798b19SChris Wilson 
3935c2798b19SChris Wilson 	I915_WRITE16(EMR,
3936c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3937c2798b19SChris Wilson 
3938c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3939c2798b19SChris Wilson 	dev_priv->irq_mask =
3940c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3941c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3942c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
394337ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3944c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3945c2798b19SChris Wilson 
3946c2798b19SChris Wilson 	I915_WRITE16(IER,
3947c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3948c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3949c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3950c2798b19SChris Wilson 	POSTING_READ16(IER);
3951c2798b19SChris Wilson 
3952379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3953379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3954d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3955755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3956755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3957d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3958379ef82dSDaniel Vetter 
3959c2798b19SChris Wilson 	return 0;
3960c2798b19SChris Wilson }
3961c2798b19SChris Wilson 
396290a72f87SVille Syrjälä /*
396390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
396490a72f87SVille Syrjälä  */
396590a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
39661f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
396790a72f87SVille Syrjälä {
39682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39691f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
397090a72f87SVille Syrjälä 
39718d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
397290a72f87SVille Syrjälä 		return false;
397390a72f87SVille Syrjälä 
397490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3975d6bbafa1SChris Wilson 		goto check_page_flip;
397690a72f87SVille Syrjälä 
397790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
397890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
397990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
398090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
398190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
398290a72f87SVille Syrjälä 	 */
398390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3984d6bbafa1SChris Wilson 		goto check_page_flip;
398590a72f87SVille Syrjälä 
39867d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
398790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
398890a72f87SVille Syrjälä 	return true;
3989d6bbafa1SChris Wilson 
3990d6bbafa1SChris Wilson check_page_flip:
3991d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3992d6bbafa1SChris Wilson 	return false;
399390a72f87SVille Syrjälä }
399490a72f87SVille Syrjälä 
3995ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3996c2798b19SChris Wilson {
399745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39982d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3999c2798b19SChris Wilson 	u16 iir, new_iir;
4000c2798b19SChris Wilson 	u32 pipe_stats[2];
4001c2798b19SChris Wilson 	int pipe;
4002c2798b19SChris Wilson 	u16 flip_mask =
4003c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4004c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
40051f814dacSImre Deak 	irqreturn_t ret;
4006c2798b19SChris Wilson 
40072dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40082dd2a883SImre Deak 		return IRQ_NONE;
40092dd2a883SImre Deak 
40101f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
40111f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
40121f814dacSImre Deak 
40131f814dacSImre Deak 	ret = IRQ_NONE;
4014c2798b19SChris Wilson 	iir = I915_READ16(IIR);
4015c2798b19SChris Wilson 	if (iir == 0)
40161f814dacSImre Deak 		goto out;
4017c2798b19SChris Wilson 
4018c2798b19SChris Wilson 	while (iir & ~flip_mask) {
4019c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4020c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4021c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4022c2798b19SChris Wilson 		 * interrupts (for non-MSI).
4023c2798b19SChris Wilson 		 */
4024222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4025c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4026aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4027c2798b19SChris Wilson 
4028055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4029f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4030c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4031c2798b19SChris Wilson 
4032c2798b19SChris Wilson 			/*
4033c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4034c2798b19SChris Wilson 			 */
40352d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
4036c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4037c2798b19SChris Wilson 		}
4038222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4039c2798b19SChris Wilson 
4040c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
4041c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
4042c2798b19SChris Wilson 
4043c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
404474cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4045c2798b19SChris Wilson 
4046055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
40471f1c2e24SVille Syrjälä 			int plane = pipe;
40483a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
40491f1c2e24SVille Syrjälä 				plane = !plane;
40501f1c2e24SVille Syrjälä 
40514356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
40521f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
40531f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4054c2798b19SChris Wilson 
40554356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4056277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40572d9d2b0bSVille Syrjälä 
40581f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
40591f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
40601f7247c0SDaniel Vetter 								    pipe);
40614356d586SDaniel Vetter 		}
4062c2798b19SChris Wilson 
4063c2798b19SChris Wilson 		iir = new_iir;
4064c2798b19SChris Wilson 	}
40651f814dacSImre Deak 	ret = IRQ_HANDLED;
4066c2798b19SChris Wilson 
40671f814dacSImre Deak out:
40681f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
40691f814dacSImre Deak 
40701f814dacSImre Deak 	return ret;
4071c2798b19SChris Wilson }
4072c2798b19SChris Wilson 
4073c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
4074c2798b19SChris Wilson {
40752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4076c2798b19SChris Wilson 	int pipe;
4077c2798b19SChris Wilson 
4078055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
4079c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
4080c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4081c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
4082c2798b19SChris Wilson 	}
4083c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
4084c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
4085c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
4086c2798b19SChris Wilson }
4087c2798b19SChris Wilson 
4088a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
4089a266c7d5SChris Wilson {
40902d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4091a266c7d5SChris Wilson 	int pipe;
4092a266c7d5SChris Wilson 
4093a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
40940706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4095a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4096a266c7d5SChris Wilson 	}
4097a266c7d5SChris Wilson 
409800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
4099055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4100a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4101a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4102a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4103a266c7d5SChris Wilson 	POSTING_READ(IER);
4104a266c7d5SChris Wilson }
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
4107a266c7d5SChris Wilson {
41082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
410938bde180SChris Wilson 	u32 enable_mask;
4110a266c7d5SChris Wilson 
411138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
411238bde180SChris Wilson 
411338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
411438bde180SChris Wilson 	dev_priv->irq_mask =
411538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
411638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
411738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
411838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
411937ef01abSDaniel Vetter 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
412038bde180SChris Wilson 
412138bde180SChris Wilson 	enable_mask =
412238bde180SChris Wilson 		I915_ASLE_INTERRUPT |
412338bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
412438bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
412538bde180SChris Wilson 		I915_USER_INTERRUPT;
412638bde180SChris Wilson 
4127a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
41280706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
412920afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
413020afbda2SDaniel Vetter 
4131a266c7d5SChris Wilson 		/* Enable in IER... */
4132a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
4133a266c7d5SChris Wilson 		/* and unmask in IMR */
4134a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
4135a266c7d5SChris Wilson 	}
4136a266c7d5SChris Wilson 
4137a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4138a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4139a266c7d5SChris Wilson 	POSTING_READ(IER);
4140a266c7d5SChris Wilson 
4141f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
414220afbda2SDaniel Vetter 
4143379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4144379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4145d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4146755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4147755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4148d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4149379ef82dSDaniel Vetter 
415020afbda2SDaniel Vetter 	return 0;
415120afbda2SDaniel Vetter }
415220afbda2SDaniel Vetter 
415390a72f87SVille Syrjälä /*
415490a72f87SVille Syrjälä  * Returns true when a page flip has completed.
415590a72f87SVille Syrjälä  */
415690a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
415790a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
415890a72f87SVille Syrjälä {
41592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
416090a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
416190a72f87SVille Syrjälä 
41628d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
416390a72f87SVille Syrjälä 		return false;
416490a72f87SVille Syrjälä 
416590a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
4166d6bbafa1SChris Wilson 		goto check_page_flip;
416790a72f87SVille Syrjälä 
416890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
416990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
417090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
417190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
417290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
417390a72f87SVille Syrjälä 	 */
417490a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
4175d6bbafa1SChris Wilson 		goto check_page_flip;
417690a72f87SVille Syrjälä 
41777d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
417890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
417990a72f87SVille Syrjälä 	return true;
4180d6bbafa1SChris Wilson 
4181d6bbafa1SChris Wilson check_page_flip:
4182d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
4183d6bbafa1SChris Wilson 	return false;
418490a72f87SVille Syrjälä }
418590a72f87SVille Syrjälä 
4186ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
4187a266c7d5SChris Wilson {
418845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
41908291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
419138bde180SChris Wilson 	u32 flip_mask =
419238bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
419338bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
419438bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
4195a266c7d5SChris Wilson 
41962dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41972dd2a883SImre Deak 		return IRQ_NONE;
41982dd2a883SImre Deak 
41991f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
42001f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
42011f814dacSImre Deak 
4202a266c7d5SChris Wilson 	iir = I915_READ(IIR);
420338bde180SChris Wilson 	do {
420438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
42058291ee90SChris Wilson 		bool blc_event = false;
4206a266c7d5SChris Wilson 
4207a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4208a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4209a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4210a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4211a266c7d5SChris Wilson 		 */
4212222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4213a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4214aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4215a266c7d5SChris Wilson 
4216055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4217f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4218a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4219a266c7d5SChris Wilson 
422038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4221a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4222a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
422338bde180SChris Wilson 				irq_received = true;
4224a266c7d5SChris Wilson 			}
4225a266c7d5SChris Wilson 		}
4226222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4227a266c7d5SChris Wilson 
4228a266c7d5SChris Wilson 		if (!irq_received)
4229a266c7d5SChris Wilson 			break;
4230a266c7d5SChris Wilson 
4231a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
423216c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
423316c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
423416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4235a266c7d5SChris Wilson 
423638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4237a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4238a266c7d5SChris Wilson 
4239a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
424074cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4241a266c7d5SChris Wilson 
4242055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
424338bde180SChris Wilson 			int plane = pipe;
42443a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
424538bde180SChris Wilson 				plane = !plane;
42465e2032d4SVille Syrjälä 
424790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
424890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
424990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4250a266c7d5SChris Wilson 
4251a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4252a266c7d5SChris Wilson 				blc_event = true;
42534356d586SDaniel Vetter 
42544356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4255277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
42562d9d2b0bSVille Syrjälä 
42571f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42581f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
42591f7247c0SDaniel Vetter 								    pipe);
4260a266c7d5SChris Wilson 		}
4261a266c7d5SChris Wilson 
4262a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4263a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4264a266c7d5SChris Wilson 
4265a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4266a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4267a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4268a266c7d5SChris Wilson 		 * we would never get another interrupt.
4269a266c7d5SChris Wilson 		 *
4270a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4271a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4272a266c7d5SChris Wilson 		 * another one.
4273a266c7d5SChris Wilson 		 *
4274a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4275a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4276a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4277a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4278a266c7d5SChris Wilson 		 * stray interrupts.
4279a266c7d5SChris Wilson 		 */
428038bde180SChris Wilson 		ret = IRQ_HANDLED;
4281a266c7d5SChris Wilson 		iir = new_iir;
428238bde180SChris Wilson 	} while (iir & ~flip_mask);
4283a266c7d5SChris Wilson 
42841f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
42851f814dacSImre Deak 
4286a266c7d5SChris Wilson 	return ret;
4287a266c7d5SChris Wilson }
4288a266c7d5SChris Wilson 
4289a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4290a266c7d5SChris Wilson {
42912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4292a266c7d5SChris Wilson 	int pipe;
4293a266c7d5SChris Wilson 
4294a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
42950706f17cSEgbert Eich 		i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4296a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4297a266c7d5SChris Wilson 	}
4298a266c7d5SChris Wilson 
429900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4300055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
430155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4302a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
430355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
430455b39755SChris Wilson 	}
4305a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4306a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4307a266c7d5SChris Wilson 
4308a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4309a266c7d5SChris Wilson }
4310a266c7d5SChris Wilson 
4311a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4312a266c7d5SChris Wilson {
43132d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4314a266c7d5SChris Wilson 	int pipe;
4315a266c7d5SChris Wilson 
43160706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4317a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4318a266c7d5SChris Wilson 
4319a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4320055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4321a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4322a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4323a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4324a266c7d5SChris Wilson 	POSTING_READ(IER);
4325a266c7d5SChris Wilson }
4326a266c7d5SChris Wilson 
4327a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4328a266c7d5SChris Wilson {
43292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4330bbba0a97SChris Wilson 	u32 enable_mask;
4331a266c7d5SChris Wilson 	u32 error_mask;
4332a266c7d5SChris Wilson 
4333a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4334bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4335adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4336bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4337bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4338bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4339bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4340bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4341bbba0a97SChris Wilson 
4342bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
434321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
434421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4345bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4346bbba0a97SChris Wilson 
4347bbba0a97SChris Wilson 	if (IS_G4X(dev))
4348bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4349a266c7d5SChris Wilson 
4350b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4351b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4352d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4353755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4354755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4355755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4356d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4357a266c7d5SChris Wilson 
4358a266c7d5SChris Wilson 	/*
4359a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4360a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4361a266c7d5SChris Wilson 	 */
4362a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4363a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4364a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4365a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4366a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4367a266c7d5SChris Wilson 	} else {
4368a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4369a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4370a266c7d5SChris Wilson 	}
4371a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4372a266c7d5SChris Wilson 
4373a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4374a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4375a266c7d5SChris Wilson 	POSTING_READ(IER);
4376a266c7d5SChris Wilson 
43770706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
437820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
437920afbda2SDaniel Vetter 
4380f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
438120afbda2SDaniel Vetter 
438220afbda2SDaniel Vetter 	return 0;
438320afbda2SDaniel Vetter }
438420afbda2SDaniel Vetter 
4385bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
438620afbda2SDaniel Vetter {
43872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
438820afbda2SDaniel Vetter 	u32 hotplug_en;
438920afbda2SDaniel Vetter 
4390b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4391b5ea2d56SDaniel Vetter 
4392adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4393e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
43940706f17cSEgbert Eich 	hotplug_en = intel_hpd_enabled_irqs(dev, hpd_mask_i915);
4395a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4396a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4397a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4398a266c7d5SChris Wilson 	*/
4399a266c7d5SChris Wilson 	if (IS_G4X(dev))
4400a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
4401a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4402a266c7d5SChris Wilson 
4403a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
44040706f17cSEgbert Eich 	i915_hotplug_interrupt_update_locked(dev_priv,
4405f9e3dc78SJani Nikula 					     HOTPLUG_INT_EN_MASK |
4406f9e3dc78SJani Nikula 					     CRT_HOTPLUG_VOLTAGE_COMPARE_MASK |
4407f9e3dc78SJani Nikula 					     CRT_HOTPLUG_ACTIVATION_PERIOD_64,
44080706f17cSEgbert Eich 					     hotplug_en);
4409a266c7d5SChris Wilson }
4410a266c7d5SChris Wilson 
4411ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4412a266c7d5SChris Wilson {
441345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
44142d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4415a266c7d5SChris Wilson 	u32 iir, new_iir;
4416a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4417a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
441821ad8330SVille Syrjälä 	u32 flip_mask =
441921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
442021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4421a266c7d5SChris Wilson 
44222dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
44232dd2a883SImre Deak 		return IRQ_NONE;
44242dd2a883SImre Deak 
44251f814dacSImre Deak 	/* IRQs are synced during runtime_suspend, we don't require a wakeref */
44261f814dacSImre Deak 	disable_rpm_wakeref_asserts(dev_priv);
44271f814dacSImre Deak 
4428a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4429a266c7d5SChris Wilson 
4430a266c7d5SChris Wilson 	for (;;) {
4431501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
44322c8ba29fSChris Wilson 		bool blc_event = false;
44332c8ba29fSChris Wilson 
4434a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4435a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4436a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4437a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4438a266c7d5SChris Wilson 		 */
4439222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4440a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4441aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4442a266c7d5SChris Wilson 
4443055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4444f0f59a00SVille Syrjälä 			i915_reg_t reg = PIPESTAT(pipe);
4445a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4446a266c7d5SChris Wilson 
4447a266c7d5SChris Wilson 			/*
4448a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4449a266c7d5SChris Wilson 			 */
4450a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4451a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4452501e01d7SVille Syrjälä 				irq_received = true;
4453a266c7d5SChris Wilson 			}
4454a266c7d5SChris Wilson 		}
4455222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4456a266c7d5SChris Wilson 
4457a266c7d5SChris Wilson 		if (!irq_received)
4458a266c7d5SChris Wilson 			break;
4459a266c7d5SChris Wilson 
4460a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4461a266c7d5SChris Wilson 
4462a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
446316c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
446416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4465a266c7d5SChris Wilson 
446621ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4467a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4468a266c7d5SChris Wilson 
4469a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
447074cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4471a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
447274cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4473a266c7d5SChris Wilson 
4474055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
44752c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
447690a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
447790a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4478a266c7d5SChris Wilson 
4479a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4480a266c7d5SChris Wilson 				blc_event = true;
44814356d586SDaniel Vetter 
44824356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4483277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4484a266c7d5SChris Wilson 
44851f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
44861f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
44872d9d2b0bSVille Syrjälä 		}
4488a266c7d5SChris Wilson 
4489a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4490a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4491a266c7d5SChris Wilson 
4492515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4493515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4494515ac2bbSDaniel Vetter 
4495a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4496a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4497a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4498a266c7d5SChris Wilson 		 * we would never get another interrupt.
4499a266c7d5SChris Wilson 		 *
4500a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4501a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4502a266c7d5SChris Wilson 		 * another one.
4503a266c7d5SChris Wilson 		 *
4504a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4505a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4506a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4507a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4508a266c7d5SChris Wilson 		 * stray interrupts.
4509a266c7d5SChris Wilson 		 */
4510a266c7d5SChris Wilson 		iir = new_iir;
4511a266c7d5SChris Wilson 	}
4512a266c7d5SChris Wilson 
45131f814dacSImre Deak 	enable_rpm_wakeref_asserts(dev_priv);
45141f814dacSImre Deak 
4515a266c7d5SChris Wilson 	return ret;
4516a266c7d5SChris Wilson }
4517a266c7d5SChris Wilson 
4518a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4519a266c7d5SChris Wilson {
45202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4521a266c7d5SChris Wilson 	int pipe;
4522a266c7d5SChris Wilson 
4523a266c7d5SChris Wilson 	if (!dev_priv)
4524a266c7d5SChris Wilson 		return;
4525a266c7d5SChris Wilson 
45260706f17cSEgbert Eich 	i915_hotplug_interrupt_update(dev_priv, 0xffffffff, 0);
4527a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4528a266c7d5SChris Wilson 
4529a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4530055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4531a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4532a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4533a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4534a266c7d5SChris Wilson 
4535055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4536a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4537a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4538a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4539a266c7d5SChris Wilson }
4540a266c7d5SChris Wilson 
4541fca52a55SDaniel Vetter /**
4542fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4543fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4544fca52a55SDaniel Vetter  *
4545fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4546fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4547fca52a55SDaniel Vetter  */
4548b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4549f71d4af4SJesse Barnes {
4550b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
45518b2e326dSChris Wilson 
455277913b39SJani Nikula 	intel_hpd_init_work(dev_priv);
455377913b39SJani Nikula 
4554c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4555a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
45568b2e326dSChris Wilson 
4557a6706b45SDeepak S 	/* Let's track the enabled rps events */
4558666a4537SWayne Boyer 	if (IS_VALLEYVIEW(dev_priv))
45596c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
45606f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
456131685c25SDeepak S 	else
4562a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4563a6706b45SDeepak S 
4564737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4565737b1506SChris Wilson 			  i915_hangcheck_elapsed);
456661bac78eSDaniel Vetter 
456797a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
45689ee32feaSDaniel Vetter 
4569b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
45704cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
45714cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4572b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4573f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4574fd8f507cSVille Syrjälä 		dev->driver->get_vblank_counter = g4x_get_vblank_counter;
4575391f75e2SVille Syrjälä 	} else {
4576391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4577391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4578f71d4af4SJesse Barnes 	}
4579f71d4af4SJesse Barnes 
458021da2700SVille Syrjälä 	/*
458121da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
458221da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
458321da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
458421da2700SVille Syrjälä 	 */
4585b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
458621da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
458721da2700SVille Syrjälä 
4588f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4589f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4590f71d4af4SJesse Barnes 
4591b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
459243f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
459343f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
459443f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
459543f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
459643f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
459743f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
459843f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4599b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
46007e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
46017e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
46027e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
46037e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
46047e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
46057e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4606fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4607b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4608abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4609723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4610abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4611abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4612abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4613abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
46146dbf30ceSVille Syrjälä 		if (IS_BROXTON(dev))
4615e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
46166dbf30ceSVille Syrjälä 		else if (HAS_PCH_SPT(dev))
46176dbf30ceSVille Syrjälä 			dev_priv->display.hpd_irq_setup = spt_hpd_irq_setup;
46186dbf30ceSVille Syrjälä 		else
46193a3b3c7dSVille Syrjälä 			dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4620f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4621f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4622723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4623f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4624f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4625f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4626f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
4627e4ce95aaSVille Syrjälä 		dev_priv->display.hpd_irq_setup = ilk_hpd_irq_setup;
4628f71d4af4SJesse Barnes 	} else {
4629b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4630c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4631c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4632c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4633c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4634b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4635a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4636a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4637a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4638a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4639c2798b19SChris Wilson 		} else {
4640a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4641a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4642a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4643a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4644c2798b19SChris Wilson 		}
4645778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4646778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4647f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4648f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4649f71d4af4SJesse Barnes 	}
4650f71d4af4SJesse Barnes }
465120afbda2SDaniel Vetter 
4652fca52a55SDaniel Vetter /**
4653fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4654fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4655fca52a55SDaniel Vetter  *
4656fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4657fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4658fca52a55SDaniel Vetter  *
4659fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4660fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4661fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4662fca52a55SDaniel Vetter  */
46632aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
46642aeb7d3aSDaniel Vetter {
46652aeb7d3aSDaniel Vetter 	/*
46662aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
46672aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
46682aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
46692aeb7d3aSDaniel Vetter 	 */
46702aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
46712aeb7d3aSDaniel Vetter 
46722aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
46732aeb7d3aSDaniel Vetter }
46742aeb7d3aSDaniel Vetter 
4675fca52a55SDaniel Vetter /**
4676fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4677fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4678fca52a55SDaniel Vetter  *
4679fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4680fca52a55SDaniel Vetter  * resources acquired in the init functions.
4681fca52a55SDaniel Vetter  */
46822aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
46832aeb7d3aSDaniel Vetter {
46842aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
46852aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
46862aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
46872aeb7d3aSDaniel Vetter }
46882aeb7d3aSDaniel Vetter 
4689fca52a55SDaniel Vetter /**
4690fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4691fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4692fca52a55SDaniel Vetter  *
4693fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4694fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4695fca52a55SDaniel Vetter  */
4696b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4697c67a470bSPaulo Zanoni {
4698b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
46992aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
47002dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4701c67a470bSPaulo Zanoni }
4702c67a470bSPaulo Zanoni 
4703fca52a55SDaniel Vetter /**
4704fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4705fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4706fca52a55SDaniel Vetter  *
4707fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4708fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4709fca52a55SDaniel Vetter  */
4710b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4711c67a470bSPaulo Zanoni {
47122aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4713b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4714b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4715c67a470bSPaulo Zanoni }
4716