xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 266ea3d9c4f69a4d97533128aba494f7d8b52459)
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 
487c7e10dbSVille Syrjälä static const u32 hpd_ibx[HPD_NUM_PINS] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
50e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
567c7e10dbSVille Syrjälä static const u32 hpd_cpt[HPD_NUM_PINS] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5873c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
59e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
60e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
61e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
62e5868a31SEgbert Eich };
63e5868a31SEgbert Eich 
647c7e10dbSVille Syrjälä static const u32 hpd_mask_i915[HPD_NUM_PINS] = {
65e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
66e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
67e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
68e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
69e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
70e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
71e5868a31SEgbert Eich };
72e5868a31SEgbert Eich 
737c7e10dbSVille Syrjälä static const u32 hpd_status_g4x[HPD_NUM_PINS] = {
74e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
75e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
76e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
77e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
78e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
80e5868a31SEgbert Eich };
81e5868a31SEgbert Eich 
827c7e10dbSVille Syrjälä static const u32 hpd_status_i915[HPD_NUM_PINS] = { /* i915 and valleyview are the same */
83e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
84e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
85e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
86e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
87e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
88e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
89e5868a31SEgbert Eich };
90e5868a31SEgbert Eich 
91e0a20ad7SShashank Sharma /* BXT hpd list */
92e0a20ad7SShashank Sharma static const u32 hpd_bxt[HPD_NUM_PINS] = {
93e0a20ad7SShashank Sharma 	[HPD_PORT_B] = BXT_DE_PORT_HP_DDIB,
94e0a20ad7SShashank Sharma 	[HPD_PORT_C] = BXT_DE_PORT_HP_DDIC
95e0a20ad7SShashank Sharma };
96e0a20ad7SShashank Sharma 
975c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
98f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
995c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
1005c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
1015c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
1025c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1035c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1045c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
1055c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1065c502442SPaulo Zanoni } while (0)
1075c502442SPaulo Zanoni 
108f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
109a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1105c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
111a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1125c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1135c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1145c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1155c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
116a9d356a6SPaulo Zanoni } while (0)
117a9d356a6SPaulo Zanoni 
118337ba017SPaulo Zanoni /*
119337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
120337ba017SPaulo Zanoni  */
121337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
122337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
123337ba017SPaulo Zanoni 	if (val) { \
124337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
125337ba017SPaulo Zanoni 		     (reg), val); \
126337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
127337ba017SPaulo Zanoni 		POSTING_READ(reg); \
128337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
129337ba017SPaulo Zanoni 		POSTING_READ(reg); \
130337ba017SPaulo Zanoni 	} \
131337ba017SPaulo Zanoni } while (0)
132337ba017SPaulo Zanoni 
13335079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
134337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
13535079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1367d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1377d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
13835079899SPaulo Zanoni } while (0)
13935079899SPaulo Zanoni 
14035079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
141337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
14235079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1437d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1447d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
14535079899SPaulo Zanoni } while (0)
14635079899SPaulo Zanoni 
147c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
148c9a9a268SImre Deak 
149036a4a7dSZhenyu Wang /* For display hotplug interrupt */
15047339cd9SDaniel Vetter void
1512d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
152036a4a7dSZhenyu Wang {
1534bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1544bc9d430SDaniel Vetter 
1559df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
156c67a470bSPaulo Zanoni 		return;
157c67a470bSPaulo Zanoni 
1581ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1591ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1601ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1613143a2bfSChris Wilson 		POSTING_READ(DEIMR);
162036a4a7dSZhenyu Wang 	}
163036a4a7dSZhenyu Wang }
164036a4a7dSZhenyu Wang 
16547339cd9SDaniel Vetter void
1662d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
167036a4a7dSZhenyu Wang {
1684bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1694bc9d430SDaniel Vetter 
17006ffc778SPaulo Zanoni 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
171c67a470bSPaulo Zanoni 		return;
172c67a470bSPaulo Zanoni 
1731ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1741ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1751ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1763143a2bfSChris Wilson 		POSTING_READ(DEIMR);
177036a4a7dSZhenyu Wang 	}
178036a4a7dSZhenyu Wang }
179036a4a7dSZhenyu Wang 
18043eaea13SPaulo Zanoni /**
18143eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
18243eaea13SPaulo Zanoni  * @dev_priv: driver private
18343eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
18443eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
18543eaea13SPaulo Zanoni  */
18643eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
18743eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
18843eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
18943eaea13SPaulo Zanoni {
19043eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
19143eaea13SPaulo Zanoni 
19215a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
19315a17aaeSDaniel Vetter 
1949df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
195c67a470bSPaulo Zanoni 		return;
196c67a470bSPaulo Zanoni 
19743eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
19843eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
19943eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
20043eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
20143eaea13SPaulo Zanoni }
20243eaea13SPaulo Zanoni 
203480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
20443eaea13SPaulo Zanoni {
20543eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
20643eaea13SPaulo Zanoni }
20743eaea13SPaulo Zanoni 
208480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
20943eaea13SPaulo Zanoni {
21043eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
21143eaea13SPaulo Zanoni }
21243eaea13SPaulo Zanoni 
213b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
214b900b949SImre Deak {
215b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
216b900b949SImre Deak }
217b900b949SImre Deak 
218a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
219a72fbc3aSImre Deak {
220a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
221a72fbc3aSImre Deak }
222a72fbc3aSImre Deak 
223b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
224b900b949SImre Deak {
225b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
226b900b949SImre Deak }
227b900b949SImre Deak 
228edbfdb45SPaulo Zanoni /**
229edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
230edbfdb45SPaulo Zanoni   * @dev_priv: driver private
231edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
232edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
233edbfdb45SPaulo Zanoni   */
234edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
235edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
236edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
237edbfdb45SPaulo Zanoni {
238605cd25bSPaulo Zanoni 	uint32_t new_val;
239edbfdb45SPaulo Zanoni 
24015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
24115a17aaeSDaniel Vetter 
242edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
243edbfdb45SPaulo Zanoni 
244605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
245f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
246f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
247f52ecbcfSPaulo Zanoni 
248605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
249605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
250a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
251a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
252edbfdb45SPaulo Zanoni 	}
253f52ecbcfSPaulo Zanoni }
254edbfdb45SPaulo Zanoni 
255480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
256edbfdb45SPaulo Zanoni {
2579939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2589939fba2SImre Deak 		return;
2599939fba2SImre Deak 
260edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
261edbfdb45SPaulo Zanoni }
262edbfdb45SPaulo Zanoni 
2639939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
2649939fba2SImre Deak 				  uint32_t mask)
2659939fba2SImre Deak {
2669939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
2679939fba2SImre Deak }
2689939fba2SImre Deak 
269480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
270edbfdb45SPaulo Zanoni {
2719939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2729939fba2SImre Deak 		return;
2739939fba2SImre Deak 
2749939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
275edbfdb45SPaulo Zanoni }
276edbfdb45SPaulo Zanoni 
2773cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
2783cc134e3SImre Deak {
2793cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
2803cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
2813cc134e3SImre Deak 
2823cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
2833cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2843cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2853cc134e3SImre Deak 	POSTING_READ(reg);
286096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
2873cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
2883cc134e3SImre Deak }
2893cc134e3SImre Deak 
290b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
291b900b949SImre Deak {
292b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
293b900b949SImre Deak 
294b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
29578e68d36SImre Deak 
296b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
2973cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
298d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
29978e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
30078e68d36SImre Deak 				dev_priv->pm_rps_events);
301b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
30278e68d36SImre Deak 
303b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
304b900b949SImre Deak }
305b900b949SImre Deak 
30659d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
30759d02a1fSImre Deak {
30859d02a1fSImre Deak 	/*
309f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
31059d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
311f24eeb19SImre Deak 	 *
312f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
31359d02a1fSImre Deak 	 */
31459d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
31559d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
31659d02a1fSImre Deak 
31759d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
31859d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
31959d02a1fSImre Deak 
32059d02a1fSImre Deak 	return mask;
32159d02a1fSImre Deak }
32259d02a1fSImre Deak 
323b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
324b900b949SImre Deak {
325b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
326b900b949SImre Deak 
327d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
328d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
329d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
330d4d70aa5SImre Deak 
331d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
332d4d70aa5SImre Deak 
3339939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3349939fba2SImre Deak 
33559d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3369939fba2SImre Deak 
3379939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
338b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
339b900b949SImre Deak 				~dev_priv->pm_rps_events);
34058072ccbSImre Deak 
34158072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
34258072ccbSImre Deak 
34358072ccbSImre Deak 	synchronize_irq(dev->irq);
344b900b949SImre Deak }
345b900b949SImre Deak 
3460961021aSBen Widawsky /**
347fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
348fee884edSDaniel Vetter  * @dev_priv: driver private
349fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
350fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
351fee884edSDaniel Vetter  */
35247339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
353fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
354fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
355fee884edSDaniel Vetter {
356fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
357fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
358fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
359fee884edSDaniel Vetter 
36015a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
36115a17aaeSDaniel Vetter 
362fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
363fee884edSDaniel Vetter 
3649df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
365c67a470bSPaulo Zanoni 		return;
366c67a470bSPaulo Zanoni 
367fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
368fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
369fee884edSDaniel Vetter }
3708664281bSPaulo Zanoni 
371b5ea642aSDaniel Vetter static void
372755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
373755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
3747c463586SKeith Packard {
3759db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
376755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3777c463586SKeith Packard 
378b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
379d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
380b79480baSDaniel Vetter 
38104feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
38204feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
38304feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
38404feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
385755e9019SImre Deak 		return;
386755e9019SImre Deak 
387755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
38846c06a30SVille Syrjälä 		return;
38946c06a30SVille Syrjälä 
39091d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
39191d181ddSImre Deak 
3927c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
393755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
39446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3953143a2bfSChris Wilson 	POSTING_READ(reg);
3967c463586SKeith Packard }
3977c463586SKeith Packard 
398b5ea642aSDaniel Vetter static void
399755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
400755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
4017c463586SKeith Packard {
4029db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
403755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
4047c463586SKeith Packard 
405b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
406d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
407b79480baSDaniel Vetter 
40804feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
40904feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
41004feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
41104feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
41246c06a30SVille Syrjälä 		return;
41346c06a30SVille Syrjälä 
414755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
415755e9019SImre Deak 		return;
416755e9019SImre Deak 
41791d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
41891d181ddSImre Deak 
419755e9019SImre Deak 	pipestat &= ~enable_mask;
42046c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4213143a2bfSChris Wilson 	POSTING_READ(reg);
4227c463586SKeith Packard }
4237c463586SKeith Packard 
42410c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
42510c59c51SImre Deak {
42610c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
42710c59c51SImre Deak 
42810c59c51SImre Deak 	/*
429724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
430724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
43110c59c51SImre Deak 	 */
43210c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
43310c59c51SImre Deak 		return 0;
434724a6905SVille Syrjälä 	/*
435724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
436724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
437724a6905SVille Syrjälä 	 */
438724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
439724a6905SVille Syrjälä 		return 0;
44010c59c51SImre Deak 
44110c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
44210c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
44310c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
44410c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
44510c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
44610c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
44710c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
44810c59c51SImre Deak 
44910c59c51SImre Deak 	return enable_mask;
45010c59c51SImre Deak }
45110c59c51SImre Deak 
452755e9019SImre Deak void
453755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
454755e9019SImre Deak 		     u32 status_mask)
455755e9019SImre Deak {
456755e9019SImre Deak 	u32 enable_mask;
457755e9019SImre Deak 
45810c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
45910c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
46010c59c51SImre Deak 							   status_mask);
46110c59c51SImre Deak 	else
462755e9019SImre Deak 		enable_mask = status_mask << 16;
463755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
464755e9019SImre Deak }
465755e9019SImre Deak 
466755e9019SImre Deak void
467755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
468755e9019SImre Deak 		      u32 status_mask)
469755e9019SImre Deak {
470755e9019SImre Deak 	u32 enable_mask;
471755e9019SImre Deak 
47210c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
47310c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
47410c59c51SImre Deak 							   status_mask);
47510c59c51SImre Deak 	else
476755e9019SImre Deak 		enable_mask = status_mask << 16;
477755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
478755e9019SImre Deak }
479755e9019SImre Deak 
480c0e09200SDave Airlie /**
481f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
48201c66889SZhao Yakui  */
483f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
48401c66889SZhao Yakui {
4852d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4861ec14ad3SChris Wilson 
487f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
488f49e38ddSJani Nikula 		return;
489f49e38ddSJani Nikula 
49013321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
49101c66889SZhao Yakui 
492755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
493a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4943b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
495755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
4961ec14ad3SChris Wilson 
49713321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
49801c66889SZhao Yakui }
49901c66889SZhao Yakui 
500f75f3746SVille Syrjälä /*
501f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
502f75f3746SVille Syrjälä  * around the vertical blanking period.
503f75f3746SVille Syrjälä  *
504f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
505f75f3746SVille Syrjälä  *  vblank_start >= 3
506f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
507f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
508f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
509f75f3746SVille Syrjälä  *
510f75f3746SVille Syrjälä  *           start of vblank:
511f75f3746SVille Syrjälä  *           latch double buffered registers
512f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
513f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
514f75f3746SVille Syrjälä  *           |
515f75f3746SVille Syrjälä  *           |          frame start:
516f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
517f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
518f75f3746SVille Syrjälä  *           |          |
519f75f3746SVille Syrjälä  *           |          |  start of vsync:
520f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
521f75f3746SVille Syrjälä  *           |          |  |
522f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
523f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
524f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
525f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
526f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
527f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
528f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
529f75f3746SVille Syrjälä  *       |          |                                         |
530f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
531f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
532f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
533f75f3746SVille Syrjälä  *
534f75f3746SVille Syrjälä  * x  = horizontal active
535f75f3746SVille Syrjälä  * _  = horizontal blanking
536f75f3746SVille Syrjälä  * hs = horizontal sync
537f75f3746SVille Syrjälä  * va = vertical active
538f75f3746SVille Syrjälä  * vb = vertical blanking
539f75f3746SVille Syrjälä  * vs = vertical sync
540f75f3746SVille Syrjälä  * vbs = vblank_start (number)
541f75f3746SVille Syrjälä  *
542f75f3746SVille Syrjälä  * Summary:
543f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
544f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
545f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
546f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
547f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
548f75f3746SVille Syrjälä  */
549f75f3746SVille Syrjälä 
5504cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5514cdb83ecSVille Syrjälä {
5524cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5534cdb83ecSVille Syrjälä 	return 0;
5544cdb83ecSVille Syrjälä }
5554cdb83ecSVille Syrjälä 
55642f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
55742f52ef8SKeith Packard  * we use as a pipe index
55842f52ef8SKeith Packard  */
559f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5600a3e67a4SJesse Barnes {
5612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5620a3e67a4SJesse Barnes 	unsigned long high_frame;
5630a3e67a4SJesse Barnes 	unsigned long low_frame;
5640b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
565391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
566391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
567391f75e2SVille Syrjälä 	const struct drm_display_mode *mode =
5686e3c9717SAnder Conselvan de Oliveira 		&intel_crtc->config->base.adjusted_mode;
569391f75e2SVille Syrjälä 
5700b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
5710b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
5720b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
5730b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5740b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
575391f75e2SVille Syrjälä 
5760b2a8e09SVille Syrjälä 	/* Convert to pixel count */
5770b2a8e09SVille Syrjälä 	vbl_start *= htotal;
5780b2a8e09SVille Syrjälä 
5790b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
5800b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
5810b2a8e09SVille Syrjälä 
5829db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5839db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5845eddb70bSChris Wilson 
5850a3e67a4SJesse Barnes 	/*
5860a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5870a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5880a3e67a4SJesse Barnes 	 * register.
5890a3e67a4SJesse Barnes 	 */
5900a3e67a4SJesse Barnes 	do {
5915eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
592391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5935eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5940a3e67a4SJesse Barnes 	} while (high1 != high2);
5950a3e67a4SJesse Barnes 
5965eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
597391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5985eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
599391f75e2SVille Syrjälä 
600391f75e2SVille Syrjälä 	/*
601391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
602391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
603391f75e2SVille Syrjälä 	 * counter against vblank start.
604391f75e2SVille Syrjälä 	 */
605edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6060a3e67a4SJesse Barnes }
6070a3e67a4SJesse Barnes 
608f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6099880b7a5SJesse Barnes {
6102d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6119db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6129880b7a5SJesse Barnes 
6139880b7a5SJesse Barnes 	return I915_READ(reg);
6149880b7a5SJesse Barnes }
6159880b7a5SJesse Barnes 
616ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
617ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
618ad3543edSMario Kleiner 
619a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
620a225f079SVille Syrjälä {
621a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
622a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
6236e3c9717SAnder Conselvan de Oliveira 	const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
624a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
62580715b2fSVille Syrjälä 	int position, vtotal;
626a225f079SVille Syrjälä 
62780715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
628a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
629a225f079SVille Syrjälä 		vtotal /= 2;
630a225f079SVille Syrjälä 
631a225f079SVille Syrjälä 	if (IS_GEN2(dev))
632a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
633a225f079SVille Syrjälä 	else
634a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
635a225f079SVille Syrjälä 
636a225f079SVille Syrjälä 	/*
63780715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
63880715b2fSVille Syrjälä 	 * scanline_offset adjustment.
639a225f079SVille Syrjälä 	 */
64080715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
641a225f079SVille Syrjälä }
642a225f079SVille Syrjälä 
643f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
644abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
645abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6460af7e4dfSMario Kleiner {
647c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
648c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
649c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6506e3c9717SAnder Conselvan de Oliveira 	const struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
6513aa18df8SVille Syrjälä 	int position;
65278e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
6530af7e4dfSMario Kleiner 	bool in_vbl = true;
6540af7e4dfSMario Kleiner 	int ret = 0;
655ad3543edSMario Kleiner 	unsigned long irqflags;
6560af7e4dfSMario Kleiner 
657c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6580af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6599db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6600af7e4dfSMario Kleiner 		return 0;
6610af7e4dfSMario Kleiner 	}
6620af7e4dfSMario Kleiner 
663c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
66478e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
665c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
666c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
667c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6680af7e4dfSMario Kleiner 
669d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
670d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
671d31faf65SVille Syrjälä 		vbl_end /= 2;
672d31faf65SVille Syrjälä 		vtotal /= 2;
673d31faf65SVille Syrjälä 	}
674d31faf65SVille Syrjälä 
675c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
676c2baf4b7SVille Syrjälä 
677ad3543edSMario Kleiner 	/*
678ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
679ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
680ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
681ad3543edSMario Kleiner 	 */
682ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
683ad3543edSMario Kleiner 
684ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
685ad3543edSMario Kleiner 
686ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
687ad3543edSMario Kleiner 	if (stime)
688ad3543edSMario Kleiner 		*stime = ktime_get();
689ad3543edSMario Kleiner 
6907c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6910af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6920af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6930af7e4dfSMario Kleiner 		 */
694a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
6950af7e4dfSMario Kleiner 	} else {
6960af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6970af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6980af7e4dfSMario Kleiner 		 * scanout position.
6990af7e4dfSMario Kleiner 		 */
700ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
7010af7e4dfSMario Kleiner 
7023aa18df8SVille Syrjälä 		/* convert to pixel counts */
7033aa18df8SVille Syrjälä 		vbl_start *= htotal;
7043aa18df8SVille Syrjälä 		vbl_end *= htotal;
7053aa18df8SVille Syrjälä 		vtotal *= htotal;
70678e8fc6bSVille Syrjälä 
70778e8fc6bSVille Syrjälä 		/*
7087e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7097e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7107e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7117e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7127e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7137e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7147e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7157e78f1cbSVille Syrjälä 		 */
7167e78f1cbSVille Syrjälä 		if (position >= vtotal)
7177e78f1cbSVille Syrjälä 			position = vtotal - 1;
7187e78f1cbSVille Syrjälä 
7197e78f1cbSVille Syrjälä 		/*
72078e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
72178e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
72278e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
72378e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
72478e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
72578e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
72678e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
72778e8fc6bSVille Syrjälä 		 */
72878e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7293aa18df8SVille Syrjälä 	}
7303aa18df8SVille Syrjälä 
731ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
732ad3543edSMario Kleiner 	if (etime)
733ad3543edSMario Kleiner 		*etime = ktime_get();
734ad3543edSMario Kleiner 
735ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
736ad3543edSMario Kleiner 
737ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
738ad3543edSMario Kleiner 
7393aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7403aa18df8SVille Syrjälä 
7413aa18df8SVille Syrjälä 	/*
7423aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7433aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7443aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7453aa18df8SVille Syrjälä 	 * up since vbl_end.
7463aa18df8SVille Syrjälä 	 */
7473aa18df8SVille Syrjälä 	if (position >= vbl_start)
7483aa18df8SVille Syrjälä 		position -= vbl_end;
7493aa18df8SVille Syrjälä 	else
7503aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7513aa18df8SVille Syrjälä 
7527c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7533aa18df8SVille Syrjälä 		*vpos = position;
7543aa18df8SVille Syrjälä 		*hpos = 0;
7553aa18df8SVille Syrjälä 	} else {
7560af7e4dfSMario Kleiner 		*vpos = position / htotal;
7570af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7580af7e4dfSMario Kleiner 	}
7590af7e4dfSMario Kleiner 
7600af7e4dfSMario Kleiner 	/* In vblank? */
7610af7e4dfSMario Kleiner 	if (in_vbl)
7623d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
7630af7e4dfSMario Kleiner 
7640af7e4dfSMario Kleiner 	return ret;
7650af7e4dfSMario Kleiner }
7660af7e4dfSMario Kleiner 
767a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
768a225f079SVille Syrjälä {
769a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
770a225f079SVille Syrjälä 	unsigned long irqflags;
771a225f079SVille Syrjälä 	int position;
772a225f079SVille Syrjälä 
773a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
774a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
775a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
776a225f079SVille Syrjälä 
777a225f079SVille Syrjälä 	return position;
778a225f079SVille Syrjälä }
779a225f079SVille Syrjälä 
780f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7810af7e4dfSMario Kleiner 			      int *max_error,
7820af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7830af7e4dfSMario Kleiner 			      unsigned flags)
7840af7e4dfSMario Kleiner {
7854041b853SChris Wilson 	struct drm_crtc *crtc;
7860af7e4dfSMario Kleiner 
7877eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7884041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7890af7e4dfSMario Kleiner 		return -EINVAL;
7900af7e4dfSMario Kleiner 	}
7910af7e4dfSMario Kleiner 
7920af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7934041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7944041b853SChris Wilson 	if (crtc == NULL) {
7954041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7964041b853SChris Wilson 		return -EINVAL;
7974041b853SChris Wilson 	}
7984041b853SChris Wilson 
79983d65738SMatt Roper 	if (!crtc->state->enable) {
8004041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
8014041b853SChris Wilson 		return -EBUSY;
8024041b853SChris Wilson 	}
8030af7e4dfSMario Kleiner 
8040af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8054041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8064041b853SChris Wilson 						     vblank_time, flags,
8077da903efSVille Syrjälä 						     crtc,
8086e3c9717SAnder Conselvan de Oliveira 						     &to_intel_crtc(crtc)->config->base.adjusted_mode);
8090af7e4dfSMario Kleiner }
8100af7e4dfSMario Kleiner 
81167c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
81267c347ffSJani Nikula 				struct drm_connector *connector)
813321a1b30SEgbert Eich {
814321a1b30SEgbert Eich 	enum drm_connector_status old_status;
815321a1b30SEgbert Eich 
816321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
817321a1b30SEgbert Eich 	old_status = connector->status;
818321a1b30SEgbert Eich 
819321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
82067c347ffSJani Nikula 	if (old_status == connector->status)
82167c347ffSJani Nikula 		return false;
82267c347ffSJani Nikula 
82367c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
824321a1b30SEgbert Eich 		      connector->base.id,
825c23cc417SJani Nikula 		      connector->name,
82667c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
82767c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
82867c347ffSJani Nikula 
82967c347ffSJani Nikula 	return true;
830321a1b30SEgbert Eich }
831321a1b30SEgbert Eich 
83213cf5504SDave Airlie static void i915_digport_work_func(struct work_struct *work)
83313cf5504SDave Airlie {
83413cf5504SDave Airlie 	struct drm_i915_private *dev_priv =
83513cf5504SDave Airlie 		container_of(work, struct drm_i915_private, dig_port_work);
83613cf5504SDave Airlie 	u32 long_port_mask, short_port_mask;
83713cf5504SDave Airlie 	struct intel_digital_port *intel_dig_port;
838b2c5c181SDaniel Vetter 	int i;
83913cf5504SDave Airlie 	u32 old_bits = 0;
84013cf5504SDave Airlie 
8414cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
84213cf5504SDave Airlie 	long_port_mask = dev_priv->long_hpd_port_mask;
84313cf5504SDave Airlie 	dev_priv->long_hpd_port_mask = 0;
84413cf5504SDave Airlie 	short_port_mask = dev_priv->short_hpd_port_mask;
84513cf5504SDave Airlie 	dev_priv->short_hpd_port_mask = 0;
8464cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
84713cf5504SDave Airlie 
84813cf5504SDave Airlie 	for (i = 0; i < I915_MAX_PORTS; i++) {
84913cf5504SDave Airlie 		bool valid = false;
85013cf5504SDave Airlie 		bool long_hpd = false;
85113cf5504SDave Airlie 		intel_dig_port = dev_priv->hpd_irq_port[i];
85213cf5504SDave Airlie 		if (!intel_dig_port || !intel_dig_port->hpd_pulse)
85313cf5504SDave Airlie 			continue;
85413cf5504SDave Airlie 
85513cf5504SDave Airlie 		if (long_port_mask & (1 << i))  {
85613cf5504SDave Airlie 			valid = true;
85713cf5504SDave Airlie 			long_hpd = true;
85813cf5504SDave Airlie 		} else if (short_port_mask & (1 << i))
85913cf5504SDave Airlie 			valid = true;
86013cf5504SDave Airlie 
86113cf5504SDave Airlie 		if (valid) {
862b2c5c181SDaniel Vetter 			enum irqreturn ret;
863b2c5c181SDaniel Vetter 
86413cf5504SDave Airlie 			ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
865b2c5c181SDaniel Vetter 			if (ret == IRQ_NONE) {
866b2c5c181SDaniel Vetter 				/* fall back to old school hpd */
86713cf5504SDave Airlie 				old_bits |= (1 << intel_dig_port->base.hpd_pin);
86813cf5504SDave Airlie 			}
86913cf5504SDave Airlie 		}
87013cf5504SDave Airlie 	}
87113cf5504SDave Airlie 
87213cf5504SDave Airlie 	if (old_bits) {
8734cb21832SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
87413cf5504SDave Airlie 		dev_priv->hpd_event_bits |= old_bits;
8754cb21832SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
87613cf5504SDave Airlie 		schedule_work(&dev_priv->hotplug_work);
87713cf5504SDave Airlie 	}
87813cf5504SDave Airlie }
87913cf5504SDave Airlie 
8805ca58282SJesse Barnes /*
8815ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8825ca58282SJesse Barnes  */
883ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
884ac4c16c5SEgbert Eich 
8855ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8865ca58282SJesse Barnes {
8872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
8882d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
8895ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
890c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
891cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
892cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
893cd569aedSEgbert Eich 	struct drm_connector *connector;
894cd569aedSEgbert Eich 	bool hpd_disabled = false;
895321a1b30SEgbert Eich 	bool changed = false;
896142e2398SEgbert Eich 	u32 hpd_event_bits;
8975ca58282SJesse Barnes 
898a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
899e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
900e67189abSJesse Barnes 
9014cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
902142e2398SEgbert Eich 
903142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
904142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
905cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
906cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
90736cd7444SDave Airlie 		if (!intel_connector->encoder)
90836cd7444SDave Airlie 			continue;
909cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
910cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
911cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
912cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
913cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
914cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
915c23cc417SJani Nikula 				connector->name);
916cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
917cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
918cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
919cd569aedSEgbert Eich 			hpd_disabled = true;
920cd569aedSEgbert Eich 		}
921142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
922142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
923c23cc417SJani Nikula 				      connector->name, intel_encoder->hpd_pin);
924142e2398SEgbert Eich 		}
925cd569aedSEgbert Eich 	}
926cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
927cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
928cd569aedSEgbert Eich 	  * some connectors */
929ac4c16c5SEgbert Eich 	if (hpd_disabled) {
930cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
9316323751dSImre Deak 		mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
9326323751dSImre Deak 				 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
933ac4c16c5SEgbert Eich 	}
934cd569aedSEgbert Eich 
9354cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
936cd569aedSEgbert Eich 
937321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
938321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
93936cd7444SDave Airlie 		if (!intel_connector->encoder)
94036cd7444SDave Airlie 			continue;
941321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
942321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
943cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
944cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
945321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
946321a1b30SEgbert Eich 				changed = true;
947321a1b30SEgbert Eich 		}
948321a1b30SEgbert Eich 	}
94940ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
95040ee3381SKeith Packard 
951321a1b30SEgbert Eich 	if (changed)
952321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9535ca58282SJesse Barnes }
9545ca58282SJesse Barnes 
955d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
956f97108d1SJesse Barnes {
9572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
958b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9599270388eSDaniel Vetter 	u8 new_delay;
9609270388eSDaniel Vetter 
961d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
962f97108d1SJesse Barnes 
96373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
96473edd18fSDaniel Vetter 
96520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9669270388eSDaniel Vetter 
9677648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
968b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
969b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
970f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
971f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
972f97108d1SJesse Barnes 
973f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
974b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
97520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
97620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
97720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
97820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
979b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
98020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
98120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
98220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
98320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
984f97108d1SJesse Barnes 	}
985f97108d1SJesse Barnes 
9867648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
98720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
988f97108d1SJesse Barnes 
989d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9909270388eSDaniel Vetter 
991f97108d1SJesse Barnes 	return;
992f97108d1SJesse Barnes }
993f97108d1SJesse Barnes 
99474cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
995549f7365SChris Wilson {
99693b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
997475553deSChris Wilson 		return;
998475553deSChris Wilson 
999bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
10009862e600SChris Wilson 
1001549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
1002549f7365SChris Wilson }
1003549f7365SChris Wilson 
100443cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
100543cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
100631685c25SDeepak S {
100743cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
100843cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
100943cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
101031685c25SDeepak S }
101131685c25SDeepak S 
101243cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
101343cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
101443cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
101543cf3bf0SChris Wilson 			 int threshold)
101631685c25SDeepak S {
101743cf3bf0SChris Wilson 	u64 time, c0;
101831685c25SDeepak S 
101943cf3bf0SChris Wilson 	if (old->cz_clock == 0)
102043cf3bf0SChris Wilson 		return false;
102131685c25SDeepak S 
102243cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
102343cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
102431685c25SDeepak S 
102543cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
102643cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
102743cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
102843cf3bf0SChris Wilson 	 */
102943cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
103043cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
103143cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
103231685c25SDeepak S 
103343cf3bf0SChris Wilson 	return c0 >= time;
103431685c25SDeepak S }
103531685c25SDeepak S 
103643cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
103743cf3bf0SChris Wilson {
103843cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
103943cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
104043cf3bf0SChris Wilson }
104143cf3bf0SChris Wilson 
104243cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
104343cf3bf0SChris Wilson {
104443cf3bf0SChris Wilson 	struct intel_rps_ei now;
104543cf3bf0SChris Wilson 	u32 events = 0;
104643cf3bf0SChris Wilson 
10476f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
104843cf3bf0SChris Wilson 		return 0;
104943cf3bf0SChris Wilson 
105043cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
105143cf3bf0SChris Wilson 	if (now.cz_clock == 0)
105243cf3bf0SChris Wilson 		return 0;
105331685c25SDeepak S 
105443cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
105543cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
105643cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10578fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
105843cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
105943cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
106031685c25SDeepak S 	}
106131685c25SDeepak S 
106243cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
106343cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
106443cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10658fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
106643cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
106743cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
106843cf3bf0SChris Wilson 	}
106943cf3bf0SChris Wilson 
107043cf3bf0SChris Wilson 	return events;
107131685c25SDeepak S }
107231685c25SDeepak S 
10734912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10743b8d8d91SJesse Barnes {
10752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10762d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1077edbfdb45SPaulo Zanoni 	u32 pm_iir;
1078dd75fdc8SChris Wilson 	int new_delay, adj;
10793b8d8d91SJesse Barnes 
108059cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1081d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1082d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1083d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1084d4d70aa5SImre Deak 		return;
1085d4d70aa5SImre Deak 	}
1086c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1087c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1088a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1089480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
109059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10914912d041SBen Widawsky 
109260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1093a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
109460611c13SPaulo Zanoni 
1095a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
10963b8d8d91SJesse Barnes 		return;
10973b8d8d91SJesse Barnes 
10984fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10997b9e0ae6SChris Wilson 
110043cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
110143cf3bf0SChris Wilson 
1102dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1103edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
11047425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1105dd75fdc8SChris Wilson 		if (adj > 0)
1106dd75fdc8SChris Wilson 			adj *= 2;
1107edcf284bSChris Wilson 		else /* CHV needs even encode values */
1108edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11097425034aSVille Syrjälä 		/*
11107425034aSVille Syrjälä 		 * For better performance, jump directly
11117425034aSVille Syrjälä 		 * to RPe if we're below it.
11127425034aSVille Syrjälä 		 */
1113edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1114b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1115edcf284bSChris Wilson 			adj = 0;
1116edcf284bSChris Wilson 		}
1117dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1118b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1119b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1120dd75fdc8SChris Wilson 		else
1121b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1122dd75fdc8SChris Wilson 		adj = 0;
1123dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1124dd75fdc8SChris Wilson 		if (adj < 0)
1125dd75fdc8SChris Wilson 			adj *= 2;
1126edcf284bSChris Wilson 		else /* CHV needs even encode values */
1127edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1128dd75fdc8SChris Wilson 	} else { /* unknown event */
1129edcf284bSChris Wilson 		adj = 0;
1130dd75fdc8SChris Wilson 	}
11313b8d8d91SJesse Barnes 
1132edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1133edcf284bSChris Wilson 
113479249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
113579249636SBen Widawsky 	 * interrupt
113679249636SBen Widawsky 	 */
1137edcf284bSChris Wilson 	new_delay += adj;
11381272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1139b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1140b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
114127544369SDeepak S 
1142ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11433b8d8d91SJesse Barnes 
11444fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11453b8d8d91SJesse Barnes }
11463b8d8d91SJesse Barnes 
1147e3689190SBen Widawsky 
1148e3689190SBen Widawsky /**
1149e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1150e3689190SBen Widawsky  * occurred.
1151e3689190SBen Widawsky  * @work: workqueue struct
1152e3689190SBen Widawsky  *
1153e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1154e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1155e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1156e3689190SBen Widawsky  */
1157e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1158e3689190SBen Widawsky {
11592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11602d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1161e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
116235a85ac6SBen Widawsky 	char *parity_event[6];
1163e3689190SBen Widawsky 	uint32_t misccpctl;
116435a85ac6SBen Widawsky 	uint8_t slice = 0;
1165e3689190SBen Widawsky 
1166e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1167e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1168e3689190SBen Widawsky 	 * any time we access those registers.
1169e3689190SBen Widawsky 	 */
1170e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1171e3689190SBen Widawsky 
117235a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
117335a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
117435a85ac6SBen Widawsky 		goto out;
117535a85ac6SBen Widawsky 
1176e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1177e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1178e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1179e3689190SBen Widawsky 
118035a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
118135a85ac6SBen Widawsky 		u32 reg;
118235a85ac6SBen Widawsky 
118335a85ac6SBen Widawsky 		slice--;
118435a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
118535a85ac6SBen Widawsky 			break;
118635a85ac6SBen Widawsky 
118735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
118835a85ac6SBen Widawsky 
118935a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
119035a85ac6SBen Widawsky 
119135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1192e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1193e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1194e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1195e3689190SBen Widawsky 
119635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
119735a85ac6SBen Widawsky 		POSTING_READ(reg);
1198e3689190SBen Widawsky 
1199cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1200e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1201e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1202e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
120335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
120435a85ac6SBen Widawsky 		parity_event[5] = NULL;
1205e3689190SBen Widawsky 
12065bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1207e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1208e3689190SBen Widawsky 
120935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
121035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1211e3689190SBen Widawsky 
121235a85ac6SBen Widawsky 		kfree(parity_event[4]);
1213e3689190SBen Widawsky 		kfree(parity_event[3]);
1214e3689190SBen Widawsky 		kfree(parity_event[2]);
1215e3689190SBen Widawsky 		kfree(parity_event[1]);
1216e3689190SBen Widawsky 	}
1217e3689190SBen Widawsky 
121835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
121935a85ac6SBen Widawsky 
122035a85ac6SBen Widawsky out:
122135a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12224cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1223480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12244cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
122535a85ac6SBen Widawsky 
122635a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
122735a85ac6SBen Widawsky }
122835a85ac6SBen Widawsky 
122935a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1230e3689190SBen Widawsky {
12312d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1232e3689190SBen Widawsky 
1233040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1234e3689190SBen Widawsky 		return;
1235e3689190SBen Widawsky 
1236d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1237480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1238d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1239e3689190SBen Widawsky 
124035a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
124135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
124235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
124335a85ac6SBen Widawsky 
124435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
124535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
124635a85ac6SBen Widawsky 
1247a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1248e3689190SBen Widawsky }
1249e3689190SBen Widawsky 
1250f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1251f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1252f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1253f1af8fc1SPaulo Zanoni {
1254f1af8fc1SPaulo Zanoni 	if (gt_iir &
1255f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
125674cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1257f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
125874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1259f1af8fc1SPaulo Zanoni }
1260f1af8fc1SPaulo Zanoni 
1261e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1262e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1263e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1264e7b4c6b1SDaniel Vetter {
1265e7b4c6b1SDaniel Vetter 
1266cc609d5dSBen Widawsky 	if (gt_iir &
1267cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
126874cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1269cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
127074cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1271cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
127274cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1273e7b4c6b1SDaniel Vetter 
1274cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1275cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1276aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1277aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1278e3689190SBen Widawsky 
127935a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
128035a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1281e7b4c6b1SDaniel Vetter }
1282e7b4c6b1SDaniel Vetter 
128374cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1284abd58f01SBen Widawsky 				       u32 master_ctl)
1285abd58f01SBen Widawsky {
1286abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1287abd58f01SBen Widawsky 
1288abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
128974cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1290abd58f01SBen Widawsky 		if (tmp) {
1291cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1292abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1293e981e7b1SThomas Daniel 
129474cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
129574cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
129674cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
129774cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1298e981e7b1SThomas Daniel 
129974cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
130074cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
130174cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
130274cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1303abd58f01SBen Widawsky 		} else
1304abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1305abd58f01SBen Widawsky 	}
1306abd58f01SBen Widawsky 
130785f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
130874cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1309abd58f01SBen Widawsky 		if (tmp) {
1310cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1311abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1312e981e7b1SThomas Daniel 
131374cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
131474cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
131574cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
131674cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1317e981e7b1SThomas Daniel 
131874cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
131974cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
132074cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
132174cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1322abd58f01SBen Widawsky 		} else
1323abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1324abd58f01SBen Widawsky 	}
1325abd58f01SBen Widawsky 
132674cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
132774cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
132874cdb337SChris Wilson 		if (tmp) {
132974cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
133074cdb337SChris Wilson 			ret = IRQ_HANDLED;
133174cdb337SChris Wilson 
133274cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
133374cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
133474cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
133574cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
133674cdb337SChris Wilson 		} else
133774cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
133874cdb337SChris Wilson 	}
133974cdb337SChris Wilson 
13400961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
134174cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
13420961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1343cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13440961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
134538cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1346c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13470961021aSBen Widawsky 		} else
13480961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13490961021aSBen Widawsky 	}
13500961021aSBen Widawsky 
1351abd58f01SBen Widawsky 	return ret;
1352abd58f01SBen Widawsky }
1353abd58f01SBen Widawsky 
1354b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1355b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1356b543fb04SEgbert Eich 
135707c338ceSJani Nikula static int pch_port_to_hotplug_shift(enum port port)
135813cf5504SDave Airlie {
135913cf5504SDave Airlie 	switch (port) {
136013cf5504SDave Airlie 	case PORT_A:
136113cf5504SDave Airlie 	case PORT_E:
136213cf5504SDave Airlie 	default:
136313cf5504SDave Airlie 		return -1;
136413cf5504SDave Airlie 	case PORT_B:
136513cf5504SDave Airlie 		return 0;
136613cf5504SDave Airlie 	case PORT_C:
136713cf5504SDave Airlie 		return 8;
136813cf5504SDave Airlie 	case PORT_D:
136913cf5504SDave Airlie 		return 16;
137013cf5504SDave Airlie 	}
137113cf5504SDave Airlie }
137213cf5504SDave Airlie 
137307c338ceSJani Nikula static int i915_port_to_hotplug_shift(enum port port)
137413cf5504SDave Airlie {
137513cf5504SDave Airlie 	switch (port) {
137613cf5504SDave Airlie 	case PORT_A:
137713cf5504SDave Airlie 	case PORT_E:
137813cf5504SDave Airlie 	default:
137913cf5504SDave Airlie 		return -1;
138013cf5504SDave Airlie 	case PORT_B:
138113cf5504SDave Airlie 		return 17;
138213cf5504SDave Airlie 	case PORT_C:
138313cf5504SDave Airlie 		return 19;
138413cf5504SDave Airlie 	case PORT_D:
138513cf5504SDave Airlie 		return 21;
138613cf5504SDave Airlie 	}
138713cf5504SDave Airlie }
138813cf5504SDave Airlie 
138913cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
139013cf5504SDave Airlie {
139113cf5504SDave Airlie 	switch (pin) {
139213cf5504SDave Airlie 	case HPD_PORT_B:
139313cf5504SDave Airlie 		return PORT_B;
139413cf5504SDave Airlie 	case HPD_PORT_C:
139513cf5504SDave Airlie 		return PORT_C;
139613cf5504SDave Airlie 	case HPD_PORT_D:
139713cf5504SDave Airlie 		return PORT_D;
139813cf5504SDave Airlie 	default:
139913cf5504SDave Airlie 		return PORT_A; /* no hpd */
140013cf5504SDave Airlie 	}
140113cf5504SDave Airlie }
140213cf5504SDave Airlie 
140310a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1404b543fb04SEgbert Eich 					 u32 hotplug_trigger,
140513cf5504SDave Airlie 					 u32 dig_hotplug_reg,
14067c7e10dbSVille Syrjälä 					 const u32 hpd[HPD_NUM_PINS])
1407b543fb04SEgbert Eich {
14082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1409b543fb04SEgbert Eich 	int i;
141013cf5504SDave Airlie 	enum port port;
141110a504deSDaniel Vetter 	bool storm_detected = false;
141213cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
141313cf5504SDave Airlie 	u32 dig_shift;
141413cf5504SDave Airlie 	u32 dig_port_mask = 0;
1415b543fb04SEgbert Eich 
141691d131d2SDaniel Vetter 	if (!hotplug_trigger)
141791d131d2SDaniel Vetter 		return;
141891d131d2SDaniel Vetter 
141913cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
142013cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1421cc9bd499SImre Deak 
1422b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1423b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
142413cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
142513cf5504SDave Airlie 			continue;
1426821450c6SEgbert Eich 
142713cf5504SDave Airlie 		port = get_port_from_pin(i);
142813cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
142913cf5504SDave Airlie 			bool long_hpd;
143013cf5504SDave Airlie 
14316b5ad42fSImre Deak 			if (!HAS_GMCH_DISPLAY(dev_priv)) {
143207c338ceSJani Nikula 				dig_shift = pch_port_to_hotplug_shift(port);
143313cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
143407c338ceSJani Nikula 			} else {
143507c338ceSJani Nikula 				dig_shift = i915_port_to_hotplug_shift(port);
143607c338ceSJani Nikula 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
143713cf5504SDave Airlie 			}
143813cf5504SDave Airlie 
143926fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
144026fbb774SVille Syrjälä 					 port_name(port),
144126fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
144213cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
144313cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
144413cf5504SDave Airlie 			if (long_hpd) {
144513cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
144613cf5504SDave Airlie 				dig_port_mask |= hpd[i];
144713cf5504SDave Airlie 			} else {
144813cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
144913cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
145013cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
145113cf5504SDave Airlie 			}
145213cf5504SDave Airlie 			queue_dig = true;
145313cf5504SDave Airlie 		}
145413cf5504SDave Airlie 	}
145513cf5504SDave Airlie 
145613cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
14573ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
14583ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
14593ff04a16SDaniel Vetter 			/*
14603ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
14613ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
14623ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
14633ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
14643ff04a16SDaniel Vetter 			 */
14653ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1466cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1467cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1468b8f102e8SEgbert Eich 
14693ff04a16SDaniel Vetter 			continue;
14703ff04a16SDaniel Vetter 		}
14713ff04a16SDaniel Vetter 
1472b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1473b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1474b543fb04SEgbert Eich 			continue;
1475b543fb04SEgbert Eich 
147613cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1477bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
147813cf5504SDave Airlie 			queue_hp = true;
147913cf5504SDave Airlie 		}
148013cf5504SDave Airlie 
1481b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1482b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1483b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1484b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1485b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1486b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1487b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1488b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1489142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1490b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
149110a504deSDaniel Vetter 			storm_detected = true;
1492b543fb04SEgbert Eich 		} else {
1493b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1494b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1495b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1496b543fb04SEgbert Eich 		}
1497b543fb04SEgbert Eich 	}
1498b543fb04SEgbert Eich 
149910a504deSDaniel Vetter 	if (storm_detected)
150010a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1501b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
15025876fa0dSDaniel Vetter 
1503645416f5SDaniel Vetter 	/*
1504645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1505645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1506645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1507645416f5SDaniel Vetter 	 * deadlock.
1508645416f5SDaniel Vetter 	 */
150913cf5504SDave Airlie 	if (queue_dig)
15100e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
151113cf5504SDave Airlie 	if (queue_hp)
1512645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1513b543fb04SEgbert Eich }
1514b543fb04SEgbert Eich 
1515515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1516515ac2bbSDaniel Vetter {
15172d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
151828c70f16SDaniel Vetter 
151928c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1520515ac2bbSDaniel Vetter }
1521515ac2bbSDaniel Vetter 
1522ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1523ce99c256SDaniel Vetter {
15242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15259ee32feaSDaniel Vetter 
15269ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1527ce99c256SDaniel Vetter }
1528ce99c256SDaniel Vetter 
15298bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1530277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1531eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1532eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15338bc5e955SDaniel Vetter 					 uint32_t crc4)
15348bf1e9f1SShuang He {
15358bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15368bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15378bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1538ac2300d4SDamien Lespiau 	int head, tail;
1539b2c88f5bSDamien Lespiau 
1540d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1541d538bbdfSDamien Lespiau 
15420c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1543d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
154434273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15450c912c79SDamien Lespiau 		return;
15460c912c79SDamien Lespiau 	}
15470c912c79SDamien Lespiau 
1548d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1549d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1550b2c88f5bSDamien Lespiau 
1551b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1552d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1553b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1554b2c88f5bSDamien Lespiau 		return;
1555b2c88f5bSDamien Lespiau 	}
1556b2c88f5bSDamien Lespiau 
1557b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15588bf1e9f1SShuang He 
15598bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1560eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1561eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1562eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1563eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1564eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1565b2c88f5bSDamien Lespiau 
1566b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1567d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1568d538bbdfSDamien Lespiau 
1569d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
157007144428SDamien Lespiau 
157107144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15728bf1e9f1SShuang He }
1573277de95eSDaniel Vetter #else
1574277de95eSDaniel Vetter static inline void
1575277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1576277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1577277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1578277de95eSDaniel Vetter 			     uint32_t crc4) {}
1579277de95eSDaniel Vetter #endif
1580eba94eb9SDaniel Vetter 
1581277de95eSDaniel Vetter 
1582277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15835a69b89fSDaniel Vetter {
15845a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15855a69b89fSDaniel Vetter 
1586277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15875a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15885a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15895a69b89fSDaniel Vetter }
15905a69b89fSDaniel Vetter 
1591277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1592eba94eb9SDaniel Vetter {
1593eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1594eba94eb9SDaniel Vetter 
1595277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1596eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1597eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1598eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1599eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16008bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1601eba94eb9SDaniel Vetter }
16025b3a856bSDaniel Vetter 
1603277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16045b3a856bSDaniel Vetter {
16055b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16060b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16070b5c5ed0SDaniel Vetter 
16080b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16090b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16100b5c5ed0SDaniel Vetter 	else
16110b5c5ed0SDaniel Vetter 		res1 = 0;
16120b5c5ed0SDaniel Vetter 
16130b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16140b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16150b5c5ed0SDaniel Vetter 	else
16160b5c5ed0SDaniel Vetter 		res2 = 0;
16175b3a856bSDaniel Vetter 
1618277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16190b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16200b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16210b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16220b5c5ed0SDaniel Vetter 				     res1, res2);
16235b3a856bSDaniel Vetter }
16248bf1e9f1SShuang He 
16251403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16261403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16271403c0d4SPaulo Zanoni  * the work queue. */
16281403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1629baf02a1fSBen Widawsky {
1630a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
163159cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1632480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1633d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1634d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16352adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
163641a05a3aSDaniel Vetter 		}
1637d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1638d4d70aa5SImre Deak 	}
1639baf02a1fSBen Widawsky 
1640c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1641c9a9a268SImre Deak 		return;
1642c9a9a268SImre Deak 
16431403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
164412638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
164574cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
164612638c57SBen Widawsky 
1647aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1648aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
164912638c57SBen Widawsky 	}
16501403c0d4SPaulo Zanoni }
1651baf02a1fSBen Widawsky 
16528d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16538d7849dbSVille Syrjälä {
16548d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16558d7849dbSVille Syrjälä 		return false;
16568d7849dbSVille Syrjälä 
16578d7849dbSVille Syrjälä 	return true;
16588d7849dbSVille Syrjälä }
16598d7849dbSVille Syrjälä 
1660c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16617e231dbeSJesse Barnes {
1662c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
166391d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16647e231dbeSJesse Barnes 	int pipe;
16657e231dbeSJesse Barnes 
166658ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1667055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
166891d181ddSImre Deak 		int reg;
1669bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
167091d181ddSImre Deak 
1671bbb5eebfSDaniel Vetter 		/*
1672bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1673bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1674bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1675bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1676bbb5eebfSDaniel Vetter 		 * handle.
1677bbb5eebfSDaniel Vetter 		 */
16780f239f4cSDaniel Vetter 
16790f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16800f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1681bbb5eebfSDaniel Vetter 
1682bbb5eebfSDaniel Vetter 		switch (pipe) {
1683bbb5eebfSDaniel Vetter 		case PIPE_A:
1684bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1685bbb5eebfSDaniel Vetter 			break;
1686bbb5eebfSDaniel Vetter 		case PIPE_B:
1687bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1688bbb5eebfSDaniel Vetter 			break;
16893278f67fSVille Syrjälä 		case PIPE_C:
16903278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16913278f67fSVille Syrjälä 			break;
1692bbb5eebfSDaniel Vetter 		}
1693bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1694bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1695bbb5eebfSDaniel Vetter 
1696bbb5eebfSDaniel Vetter 		if (!mask)
169791d181ddSImre Deak 			continue;
169891d181ddSImre Deak 
169991d181ddSImre Deak 		reg = PIPESTAT(pipe);
1700bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1701bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17027e231dbeSJesse Barnes 
17037e231dbeSJesse Barnes 		/*
17047e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17057e231dbeSJesse Barnes 		 */
170691d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
170791d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17087e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17097e231dbeSJesse Barnes 	}
171058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17117e231dbeSJesse Barnes 
1712055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1713d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1714d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1715d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
171631acc7f5SJesse Barnes 
1717579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
171831acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
171931acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
172031acc7f5SJesse Barnes 		}
17214356d586SDaniel Vetter 
17224356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1723277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17242d9d2b0bSVille Syrjälä 
17251f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17261f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
172731acc7f5SJesse Barnes 	}
172831acc7f5SJesse Barnes 
1729c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1730c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1731c1874ed7SImre Deak }
1732c1874ed7SImre Deak 
173316c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
173416c6c56bSVille Syrjälä {
173516c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
173616c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
173716c6c56bSVille Syrjälä 
17383ff60f89SOscar Mateo 	if (hotplug_status) {
17393ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17403ff60f89SOscar Mateo 		/*
17413ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
17423ff60f89SOscar Mateo 		 * may miss hotplug events.
17433ff60f89SOscar Mateo 		 */
17443ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
17453ff60f89SOscar Mateo 
174616c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
174716c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
174816c6c56bSVille Syrjälä 
174913cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
175016c6c56bSVille Syrjälä 		} else {
175116c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
175216c6c56bSVille Syrjälä 
175313cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
175416c6c56bSVille Syrjälä 		}
175516c6c56bSVille Syrjälä 
175616c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
175716c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
175816c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
17593ff60f89SOscar Mateo 	}
176016c6c56bSVille Syrjälä }
176116c6c56bSVille Syrjälä 
1762c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1763c1874ed7SImre Deak {
176445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1766c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1767c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1768c1874ed7SImre Deak 
17692dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17702dd2a883SImre Deak 		return IRQ_NONE;
17712dd2a883SImre Deak 
1772c1874ed7SImre Deak 	while (true) {
17733ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17743ff60f89SOscar Mateo 
1775c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17763ff60f89SOscar Mateo 		if (gt_iir)
17773ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17783ff60f89SOscar Mateo 
1779c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17803ff60f89SOscar Mateo 		if (pm_iir)
17813ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17823ff60f89SOscar Mateo 
17833ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17843ff60f89SOscar Mateo 		if (iir) {
17853ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17863ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17873ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17883ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17893ff60f89SOscar Mateo 		}
1790c1874ed7SImre Deak 
1791c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1792c1874ed7SImre Deak 			goto out;
1793c1874ed7SImre Deak 
1794c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1795c1874ed7SImre Deak 
17963ff60f89SOscar Mateo 		if (gt_iir)
1797c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
179860611c13SPaulo Zanoni 		if (pm_iir)
1799d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18003ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18013ff60f89SOscar Mateo 		 * signalled in iir */
18023ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18037e231dbeSJesse Barnes 	}
18047e231dbeSJesse Barnes 
18057e231dbeSJesse Barnes out:
18067e231dbeSJesse Barnes 	return ret;
18077e231dbeSJesse Barnes }
18087e231dbeSJesse Barnes 
180943f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
181043f328d7SVille Syrjälä {
181145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
181243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
181343f328d7SVille Syrjälä 	u32 master_ctl, iir;
181443f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
181543f328d7SVille Syrjälä 
18162dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18172dd2a883SImre Deak 		return IRQ_NONE;
18182dd2a883SImre Deak 
18198e5fd599SVille Syrjälä 	for (;;) {
18208e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18213278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18223278f67fSVille Syrjälä 
18233278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18248e5fd599SVille Syrjälä 			break;
182543f328d7SVille Syrjälä 
182627b6c122SOscar Mateo 		ret = IRQ_HANDLED;
182727b6c122SOscar Mateo 
182843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
182943f328d7SVille Syrjälä 
183027b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
183127b6c122SOscar Mateo 
183227b6c122SOscar Mateo 		if (iir) {
183327b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
183427b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
183527b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
183627b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
183727b6c122SOscar Mateo 		}
183827b6c122SOscar Mateo 
183974cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
184043f328d7SVille Syrjälä 
184127b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
184227b6c122SOscar Mateo 		 * signalled in iir */
18433278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
184443f328d7SVille Syrjälä 
184543f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
184643f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18478e5fd599SVille Syrjälä 	}
18483278f67fSVille Syrjälä 
184943f328d7SVille Syrjälä 	return ret;
185043f328d7SVille Syrjälä }
185143f328d7SVille Syrjälä 
185223e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1853776ad806SJesse Barnes {
18542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
18559db4a9c7SJesse Barnes 	int pipe;
1856b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
185713cf5504SDave Airlie 	u32 dig_hotplug_reg;
1858776ad806SJesse Barnes 
185913cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
186013cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
186113cf5504SDave Airlie 
186213cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
186391d131d2SDaniel Vetter 
1864cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1865cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1866776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1867cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1868cfc33bf7SVille Syrjälä 				 port_name(port));
1869cfc33bf7SVille Syrjälä 	}
1870776ad806SJesse Barnes 
1871ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1872ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1873ce99c256SDaniel Vetter 
1874776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1875515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1876776ad806SJesse Barnes 
1877776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1878776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1879776ad806SJesse Barnes 
1880776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1881776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1882776ad806SJesse Barnes 
1883776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1884776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1885776ad806SJesse Barnes 
18869db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1887055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
18889db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18899db4a9c7SJesse Barnes 					 pipe_name(pipe),
18909db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1891776ad806SJesse Barnes 
1892776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1893776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1894776ad806SJesse Barnes 
1895776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1896776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1897776ad806SJesse Barnes 
1898776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18991f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19008664281bSPaulo Zanoni 
19018664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19021f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19038664281bSPaulo Zanoni }
19048664281bSPaulo Zanoni 
19058664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19068664281bSPaulo Zanoni {
19078664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19088664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19095a69b89fSDaniel Vetter 	enum pipe pipe;
19108664281bSPaulo Zanoni 
1911de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1912de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1913de032bf4SPaulo Zanoni 
1914055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19151f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19161f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19178664281bSPaulo Zanoni 
19185a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19195a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1920277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19215a69b89fSDaniel Vetter 			else
1922277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19235a69b89fSDaniel Vetter 		}
19245a69b89fSDaniel Vetter 	}
19258bf1e9f1SShuang He 
19268664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19278664281bSPaulo Zanoni }
19288664281bSPaulo Zanoni 
19298664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19308664281bSPaulo Zanoni {
19318664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19328664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19338664281bSPaulo Zanoni 
1934de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1935de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1936de032bf4SPaulo Zanoni 
19378664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19381f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19398664281bSPaulo Zanoni 
19408664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19411f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19428664281bSPaulo Zanoni 
19438664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19441f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19458664281bSPaulo Zanoni 
19468664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1947776ad806SJesse Barnes }
1948776ad806SJesse Barnes 
194923e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
195023e81d69SAdam Jackson {
19512d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
195223e81d69SAdam Jackson 	int pipe;
1953b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
195413cf5504SDave Airlie 	u32 dig_hotplug_reg;
195523e81d69SAdam Jackson 
195613cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
195713cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
195813cf5504SDave Airlie 
195913cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
196091d131d2SDaniel Vetter 
1961cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1962cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
196323e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1964cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1965cfc33bf7SVille Syrjälä 				 port_name(port));
1966cfc33bf7SVille Syrjälä 	}
196723e81d69SAdam Jackson 
196823e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1969ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
197023e81d69SAdam Jackson 
197123e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1972515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
197323e81d69SAdam Jackson 
197423e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
197523e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
197623e81d69SAdam Jackson 
197723e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
197823e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
197923e81d69SAdam Jackson 
198023e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1981055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
198223e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
198323e81d69SAdam Jackson 					 pipe_name(pipe),
198423e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19858664281bSPaulo Zanoni 
19868664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19878664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
198823e81d69SAdam Jackson }
198923e81d69SAdam Jackson 
1990c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1991c008bc6eSPaulo Zanoni {
1992c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
199340da17c2SDaniel Vetter 	enum pipe pipe;
1994c008bc6eSPaulo Zanoni 
1995c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1996c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1997c008bc6eSPaulo Zanoni 
1998c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1999c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2000c008bc6eSPaulo Zanoni 
2001c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2002c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2003c008bc6eSPaulo Zanoni 
2004055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2005d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2006d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2007d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2008c008bc6eSPaulo Zanoni 
200940da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20101f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2011c008bc6eSPaulo Zanoni 
201240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
201340da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20145b3a856bSDaniel Vetter 
201540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
201640da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
201740da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
201840da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2019c008bc6eSPaulo Zanoni 		}
2020c008bc6eSPaulo Zanoni 	}
2021c008bc6eSPaulo Zanoni 
2022c008bc6eSPaulo Zanoni 	/* check event from PCH */
2023c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2024c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2025c008bc6eSPaulo Zanoni 
2026c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2027c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2028c008bc6eSPaulo Zanoni 		else
2029c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2030c008bc6eSPaulo Zanoni 
2031c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2032c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2033c008bc6eSPaulo Zanoni 	}
2034c008bc6eSPaulo Zanoni 
2035c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2036c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2037c008bc6eSPaulo Zanoni }
2038c008bc6eSPaulo Zanoni 
20399719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20409719fb98SPaulo Zanoni {
20419719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
204207d27e20SDamien Lespiau 	enum pipe pipe;
20439719fb98SPaulo Zanoni 
20449719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20459719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20469719fb98SPaulo Zanoni 
20479719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20489719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20499719fb98SPaulo Zanoni 
20509719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20519719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20529719fb98SPaulo Zanoni 
2053055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2054d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2055d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2056d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
205740da17c2SDaniel Vetter 
205840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
205907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
206007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
206107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20629719fb98SPaulo Zanoni 		}
20639719fb98SPaulo Zanoni 	}
20649719fb98SPaulo Zanoni 
20659719fb98SPaulo Zanoni 	/* check event from PCH */
20669719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20679719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20689719fb98SPaulo Zanoni 
20699719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20709719fb98SPaulo Zanoni 
20719719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20729719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20739719fb98SPaulo Zanoni 	}
20749719fb98SPaulo Zanoni }
20759719fb98SPaulo Zanoni 
207672c90f62SOscar Mateo /*
207772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
207872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
207972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
208072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
208172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
208272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
208372c90f62SOscar Mateo  */
2084f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2085b1f14ad0SJesse Barnes {
208645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2088f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20890e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2090b1f14ad0SJesse Barnes 
20912dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20922dd2a883SImre Deak 		return IRQ_NONE;
20932dd2a883SImre Deak 
20948664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
20958664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2096907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
20978664281bSPaulo Zanoni 
2098b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2099b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2100b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
210123a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21020e43406bSChris Wilson 
210344498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
210444498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
210544498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
210644498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
210744498aeaSPaulo Zanoni 	 * due to its back queue). */
2108ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
210944498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
211044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
211144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2112ab5c608bSBen Widawsky 	}
211344498aeaSPaulo Zanoni 
211472c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
211572c90f62SOscar Mateo 
21160e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21170e43406bSChris Wilson 	if (gt_iir) {
211872c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
211972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2120d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21210e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2122d8fc8a47SPaulo Zanoni 		else
2123d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21240e43406bSChris Wilson 	}
2125b1f14ad0SJesse Barnes 
2126b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21270e43406bSChris Wilson 	if (de_iir) {
212872c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
212972c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2130f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21319719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2132f1af8fc1SPaulo Zanoni 		else
2133f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21340e43406bSChris Wilson 	}
21350e43406bSChris Wilson 
2136f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2137f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21380e43406bSChris Wilson 		if (pm_iir) {
2139b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21400e43406bSChris Wilson 			ret = IRQ_HANDLED;
214172c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21420e43406bSChris Wilson 		}
2143f1af8fc1SPaulo Zanoni 	}
2144b1f14ad0SJesse Barnes 
2145b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2146b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2147ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
214844498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
214944498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2150ab5c608bSBen Widawsky 	}
2151b1f14ad0SJesse Barnes 
2152b1f14ad0SJesse Barnes 	return ret;
2153b1f14ad0SJesse Barnes }
2154b1f14ad0SJesse Barnes 
2155d04a492dSShashank Sharma static void bxt_hpd_handler(struct drm_device *dev, uint32_t iir_status)
2156d04a492dSShashank Sharma {
2157d04a492dSShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
2158d04a492dSShashank Sharma 	uint32_t hp_control;
2159d04a492dSShashank Sharma 	uint32_t hp_trigger;
2160d04a492dSShashank Sharma 
2161d04a492dSShashank Sharma 	/* Get the status */
2162d04a492dSShashank Sharma 	hp_trigger = iir_status & BXT_DE_PORT_HOTPLUG_MASK;
2163d04a492dSShashank Sharma 	hp_control = I915_READ(BXT_HOTPLUG_CTL);
2164d04a492dSShashank Sharma 
2165d04a492dSShashank Sharma 	/* Hotplug not enabled ? */
2166d04a492dSShashank Sharma 	if (!(hp_control & BXT_HOTPLUG_CTL_MASK)) {
2167d04a492dSShashank Sharma 		DRM_ERROR("Interrupt when HPD disabled\n");
2168d04a492dSShashank Sharma 		return;
2169d04a492dSShashank Sharma 	}
2170d04a492dSShashank Sharma 
2171d04a492dSShashank Sharma 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
2172d04a492dSShashank Sharma 		hp_control & BXT_HOTPLUG_CTL_MASK);
2173d04a492dSShashank Sharma 
2174d04a492dSShashank Sharma 	/* Check for HPD storm and schedule bottom half */
2175d04a492dSShashank Sharma 	intel_hpd_irq_handler(dev, hp_trigger, hp_control, hpd_bxt);
2176d04a492dSShashank Sharma 
2177d04a492dSShashank Sharma 	/*
2178d04a492dSShashank Sharma 	 * FIXME: Save the hot plug status for bottom half before
2179d04a492dSShashank Sharma 	 * clearing the sticky status bits, else the status will be
2180d04a492dSShashank Sharma 	 * lost.
2181d04a492dSShashank Sharma 	 */
2182d04a492dSShashank Sharma 
2183d04a492dSShashank Sharma 	/* Clear sticky bits in hpd status */
2184d04a492dSShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hp_control);
2185d04a492dSShashank Sharma }
2186d04a492dSShashank Sharma 
2187abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2188abd58f01SBen Widawsky {
2189abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2190abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2191abd58f01SBen Widawsky 	u32 master_ctl;
2192abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2193abd58f01SBen Widawsky 	uint32_t tmp = 0;
2194c42664ccSDaniel Vetter 	enum pipe pipe;
219588e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
219688e04703SJesse Barnes 
21972dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21982dd2a883SImre Deak 		return IRQ_NONE;
21992dd2a883SImre Deak 
220088e04703SJesse Barnes 	if (IS_GEN9(dev))
220188e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
220288e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2203abd58f01SBen Widawsky 
2204cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2205abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2206abd58f01SBen Widawsky 	if (!master_ctl)
2207abd58f01SBen Widawsky 		return IRQ_NONE;
2208abd58f01SBen Widawsky 
2209cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2210abd58f01SBen Widawsky 
221138cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
221238cc46d7SOscar Mateo 
221374cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2214abd58f01SBen Widawsky 
2215abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2216abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2217abd58f01SBen Widawsky 		if (tmp) {
2218abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2219abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
222038cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
222138cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
222238cc46d7SOscar Mateo 			else
222338cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2224abd58f01SBen Widawsky 		}
222538cc46d7SOscar Mateo 		else
222638cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2227abd58f01SBen Widawsky 	}
2228abd58f01SBen Widawsky 
22296d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22306d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22316d766f02SDaniel Vetter 		if (tmp) {
2232d04a492dSShashank Sharma 			bool found = false;
2233d04a492dSShashank Sharma 
22346d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22356d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
223688e04703SJesse Barnes 
2237d04a492dSShashank Sharma 			if (tmp & aux_mask) {
223838cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
2239d04a492dSShashank Sharma 				found = true;
2240d04a492dSShashank Sharma 			}
2241d04a492dSShashank Sharma 
2242d04a492dSShashank Sharma 			if (IS_BROXTON(dev) && tmp & BXT_DE_PORT_HOTPLUG_MASK) {
2243d04a492dSShashank Sharma 				bxt_hpd_handler(dev, tmp);
2244d04a492dSShashank Sharma 				found = true;
2245d04a492dSShashank Sharma 			}
2246d04a492dSShashank Sharma 
2247d04a492dSShashank Sharma 			if (!found)
224838cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22496d766f02SDaniel Vetter 		}
225038cc46d7SOscar Mateo 		else
225138cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22526d766f02SDaniel Vetter 	}
22536d766f02SDaniel Vetter 
2254055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2255770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2256abd58f01SBen Widawsky 
2257c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2258c42664ccSDaniel Vetter 			continue;
2259c42664ccSDaniel Vetter 
2260abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
226138cc46d7SOscar Mateo 		if (pipe_iir) {
226238cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
226338cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2264770de83dSDamien Lespiau 
2265d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2266d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2267d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2268abd58f01SBen Widawsky 
2269770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2270770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2271770de83dSDamien Lespiau 			else
2272770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2273770de83dSDamien Lespiau 
2274770de83dSDamien Lespiau 			if (flip_done) {
2275abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2276abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2277abd58f01SBen Widawsky 			}
2278abd58f01SBen Widawsky 
22790fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22800fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22810fbe7870SDaniel Vetter 
22821f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22831f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22841f7247c0SDaniel Vetter 								    pipe);
228538d83c96SDaniel Vetter 
2286770de83dSDamien Lespiau 
2287770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2288770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2289770de83dSDamien Lespiau 			else
2290770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2291770de83dSDamien Lespiau 
2292770de83dSDamien Lespiau 			if (fault_errors)
229330100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
229430100f2bSDaniel Vetter 					  pipe_name(pipe),
229530100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2296c42664ccSDaniel Vetter 		} else
2297abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2298abd58f01SBen Widawsky 	}
2299abd58f01SBen Widawsky 
2300*266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev) && !HAS_PCH_NOP(dev) &&
2301*266ea3d9SShashank Sharma 	    master_ctl & GEN8_DE_PCH_IRQ) {
230292d03a80SDaniel Vetter 		/*
230392d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
230492d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
230592d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
230692d03a80SDaniel Vetter 		 */
230792d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
230892d03a80SDaniel Vetter 		if (pch_iir) {
230992d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
231092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
231138cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
231238cc46d7SOscar Mateo 		} else
231338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
231438cc46d7SOscar Mateo 
231592d03a80SDaniel Vetter 	}
231692d03a80SDaniel Vetter 
2317cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2318cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2319abd58f01SBen Widawsky 
2320abd58f01SBen Widawsky 	return ret;
2321abd58f01SBen Widawsky }
2322abd58f01SBen Widawsky 
232317e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
232417e1df07SDaniel Vetter 			       bool reset_completed)
232517e1df07SDaniel Vetter {
2326a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
232717e1df07SDaniel Vetter 	int i;
232817e1df07SDaniel Vetter 
232917e1df07SDaniel Vetter 	/*
233017e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
233117e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
233217e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
233317e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
233417e1df07SDaniel Vetter 	 */
233517e1df07SDaniel Vetter 
233617e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
233717e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
233817e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
233917e1df07SDaniel Vetter 
234017e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
234117e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
234217e1df07SDaniel Vetter 
234317e1df07SDaniel Vetter 	/*
234417e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
234517e1df07SDaniel Vetter 	 * reset state is cleared.
234617e1df07SDaniel Vetter 	 */
234717e1df07SDaniel Vetter 	if (reset_completed)
234817e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
234917e1df07SDaniel Vetter }
235017e1df07SDaniel Vetter 
23518a905236SJesse Barnes /**
2352b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23538a905236SJesse Barnes  *
23548a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23558a905236SJesse Barnes  * was detected.
23568a905236SJesse Barnes  */
2357b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23588a905236SJesse Barnes {
2359b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2360b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2361cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2362cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2363cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
236417e1df07SDaniel Vetter 	int ret;
23658a905236SJesse Barnes 
23665bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23678a905236SJesse Barnes 
23687db0ba24SDaniel Vetter 	/*
23697db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23707db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23717db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23727db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23737db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23747db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23757db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23767db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23777db0ba24SDaniel Vetter 	 */
23787db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
237944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23805bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23817db0ba24SDaniel Vetter 				   reset_event);
23821f83fee0SDaniel Vetter 
238317e1df07SDaniel Vetter 		/*
2384f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2385f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2386f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2387f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2388f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2389f454c694SImre Deak 		 */
2390f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23917514747dSVille Syrjälä 
23927514747dSVille Syrjälä 		intel_prepare_reset(dev);
23937514747dSVille Syrjälä 
2394f454c694SImre Deak 		/*
239517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
239617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
239717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
239817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
239917e1df07SDaniel Vetter 		 */
2400f69061beSDaniel Vetter 		ret = i915_reset(dev);
2401f69061beSDaniel Vetter 
24027514747dSVille Syrjälä 		intel_finish_reset(dev);
240317e1df07SDaniel Vetter 
2404f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2405f454c694SImre Deak 
2406f69061beSDaniel Vetter 		if (ret == 0) {
2407f69061beSDaniel Vetter 			/*
2408f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2409f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2410f69061beSDaniel Vetter 			 * complete.
2411f69061beSDaniel Vetter 			 *
2412f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2413f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2414f69061beSDaniel Vetter 			 * updates before
2415f69061beSDaniel Vetter 			 * the counter increment.
2416f69061beSDaniel Vetter 			 */
24174e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2418f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2419f69061beSDaniel Vetter 
24205bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2421f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24221f83fee0SDaniel Vetter 		} else {
24232ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2424f316a42cSBen Gamari 		}
24251f83fee0SDaniel Vetter 
242617e1df07SDaniel Vetter 		/*
242717e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
242817e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
242917e1df07SDaniel Vetter 		 */
243017e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2431f316a42cSBen Gamari 	}
24328a905236SJesse Barnes }
24338a905236SJesse Barnes 
243435aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2435c0e09200SDave Airlie {
24368a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2437bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
243863eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2439050ee91fSBen Widawsky 	int pipe, i;
244063eeaf38SJesse Barnes 
244135aed2e6SChris Wilson 	if (!eir)
244235aed2e6SChris Wilson 		return;
244363eeaf38SJesse Barnes 
2444a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24458a905236SJesse Barnes 
2446bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2447bd9854f9SBen Widawsky 
24488a905236SJesse Barnes 	if (IS_G4X(dev)) {
24498a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24508a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24518a905236SJesse Barnes 
2452a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2453a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2454050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2455050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2456a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2457a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24588a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24593143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24608a905236SJesse Barnes 		}
24618a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24628a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2463a70491ccSJoe Perches 			pr_err("page table error\n");
2464a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24658a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24663143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24678a905236SJesse Barnes 		}
24688a905236SJesse Barnes 	}
24698a905236SJesse Barnes 
2470a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
247163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
247263eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2473a70491ccSJoe Perches 			pr_err("page table error\n");
2474a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
247563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24763143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
247763eeaf38SJesse Barnes 		}
24788a905236SJesse Barnes 	}
24798a905236SJesse Barnes 
248063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2481a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2482055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2483a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24849db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
248563eeaf38SJesse Barnes 		/* pipestat has already been acked */
248663eeaf38SJesse Barnes 	}
248763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2488a70491ccSJoe Perches 		pr_err("instruction error\n");
2489a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2490050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2491050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2492a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
249363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
249463eeaf38SJesse Barnes 
2495a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2496a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2497a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
249863eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24993143a2bfSChris Wilson 			POSTING_READ(IPEIR);
250063eeaf38SJesse Barnes 		} else {
250163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
250263eeaf38SJesse Barnes 
2503a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2504a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2505a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2506a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
250763eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25083143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
250963eeaf38SJesse Barnes 		}
251063eeaf38SJesse Barnes 	}
251163eeaf38SJesse Barnes 
251263eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25133143a2bfSChris Wilson 	POSTING_READ(EIR);
251463eeaf38SJesse Barnes 	eir = I915_READ(EIR);
251563eeaf38SJesse Barnes 	if (eir) {
251663eeaf38SJesse Barnes 		/*
251763eeaf38SJesse Barnes 		 * some errors might have become stuck,
251863eeaf38SJesse Barnes 		 * mask them.
251963eeaf38SJesse Barnes 		 */
252063eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
252163eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
252263eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
252363eeaf38SJesse Barnes 	}
252435aed2e6SChris Wilson }
252535aed2e6SChris Wilson 
252635aed2e6SChris Wilson /**
2527b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
252835aed2e6SChris Wilson  * @dev: drm device
252935aed2e6SChris Wilson  *
2530b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
253135aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
253235aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
253335aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
253435aed2e6SChris Wilson  * of a ring dump etc.).
253535aed2e6SChris Wilson  */
253658174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
253758174462SMika Kuoppala 		       const char *fmt, ...)
253835aed2e6SChris Wilson {
253935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
254058174462SMika Kuoppala 	va_list args;
254158174462SMika Kuoppala 	char error_msg[80];
254235aed2e6SChris Wilson 
254358174462SMika Kuoppala 	va_start(args, fmt);
254458174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
254558174462SMika Kuoppala 	va_end(args);
254658174462SMika Kuoppala 
254758174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
254835aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25498a905236SJesse Barnes 
2550ba1234d1SBen Gamari 	if (wedged) {
2551f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2552f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2553ba1234d1SBen Gamari 
255411ed50ecSBen Gamari 		/*
2555b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2556b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2557b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
255817e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
255917e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
256017e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
256117e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
256217e1df07SDaniel Vetter 		 *
256317e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
256417e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
256517e1df07SDaniel Vetter 		 * counter atomic_t.
256611ed50ecSBen Gamari 		 */
256717e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
256811ed50ecSBen Gamari 	}
256911ed50ecSBen Gamari 
2570b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25718a905236SJesse Barnes }
25728a905236SJesse Barnes 
257342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
257442f52ef8SKeith Packard  * we use as a pipe index
257542f52ef8SKeith Packard  */
2576f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25770a3e67a4SJesse Barnes {
25782d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2579e9d21d7fSKeith Packard 	unsigned long irqflags;
258071e0ffa5SJesse Barnes 
25811ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2582f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25837c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2584755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25850a3e67a4SJesse Barnes 	else
25867c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2587755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25881ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25898692d00eSChris Wilson 
25900a3e67a4SJesse Barnes 	return 0;
25910a3e67a4SJesse Barnes }
25920a3e67a4SJesse Barnes 
2593f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2594f796cf8fSJesse Barnes {
25952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2596f796cf8fSJesse Barnes 	unsigned long irqflags;
2597b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
259840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2599f796cf8fSJesse Barnes 
2600f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2601b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2602b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2603b1f14ad0SJesse Barnes 
2604b1f14ad0SJesse Barnes 	return 0;
2605b1f14ad0SJesse Barnes }
2606b1f14ad0SJesse Barnes 
26077e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26087e231dbeSJesse Barnes {
26092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26107e231dbeSJesse Barnes 	unsigned long irqflags;
26117e231dbeSJesse Barnes 
26127e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
261331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2614755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26157e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26167e231dbeSJesse Barnes 
26177e231dbeSJesse Barnes 	return 0;
26187e231dbeSJesse Barnes }
26197e231dbeSJesse Barnes 
2620abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2621abd58f01SBen Widawsky {
2622abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2623abd58f01SBen Widawsky 	unsigned long irqflags;
2624abd58f01SBen Widawsky 
2625abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26267167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26277167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2628abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2629abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2630abd58f01SBen Widawsky 	return 0;
2631abd58f01SBen Widawsky }
2632abd58f01SBen Widawsky 
263342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
263442f52ef8SKeith Packard  * we use as a pipe index
263542f52ef8SKeith Packard  */
2636f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26370a3e67a4SJesse Barnes {
26382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2639e9d21d7fSKeith Packard 	unsigned long irqflags;
26400a3e67a4SJesse Barnes 
26411ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26427c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2643755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2644755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26451ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26460a3e67a4SJesse Barnes }
26470a3e67a4SJesse Barnes 
2648f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2649f796cf8fSJesse Barnes {
26502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2651f796cf8fSJesse Barnes 	unsigned long irqflags;
2652b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
265340da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2654f796cf8fSJesse Barnes 
2655f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2656b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2657b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2658b1f14ad0SJesse Barnes }
2659b1f14ad0SJesse Barnes 
26607e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26617e231dbeSJesse Barnes {
26622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26637e231dbeSJesse Barnes 	unsigned long irqflags;
26647e231dbeSJesse Barnes 
26657e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
266631acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2667755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26687e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26697e231dbeSJesse Barnes }
26707e231dbeSJesse Barnes 
2671abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2672abd58f01SBen Widawsky {
2673abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2674abd58f01SBen Widawsky 	unsigned long irqflags;
2675abd58f01SBen Widawsky 
2676abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26777167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26787167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2679abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2680abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2681abd58f01SBen Widawsky }
2682abd58f01SBen Widawsky 
268344cdd6d2SJohn Harrison static struct drm_i915_gem_request *
268444cdd6d2SJohn Harrison ring_last_request(struct intel_engine_cs *ring)
2685852835f3SZou Nan hai {
2686893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
268744cdd6d2SJohn Harrison 			  struct drm_i915_gem_request, list);
2688893eead0SChris Wilson }
2689893eead0SChris Wilson 
26909107e9d2SChris Wilson static bool
269144cdd6d2SJohn Harrison ring_idle(struct intel_engine_cs *ring)
2692893eead0SChris Wilson {
26939107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
26941b5a433aSJohn Harrison 		i915_gem_request_completed(ring_last_request(ring), false));
2695f65d9421SBen Gamari }
2696f65d9421SBen Gamari 
2697a028c4b0SDaniel Vetter static bool
2698a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2699a028c4b0SDaniel Vetter {
2700a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2701a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2702a028c4b0SDaniel Vetter 	} else {
2703a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2704a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2705a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2706a028c4b0SDaniel Vetter 	}
2707a028c4b0SDaniel Vetter }
2708a028c4b0SDaniel Vetter 
2709a4872ba6SOscar Mateo static struct intel_engine_cs *
2710a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2711921d42eaSDaniel Vetter {
2712921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2713a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2714921d42eaSDaniel Vetter 	int i;
2715921d42eaSDaniel Vetter 
2716921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2717a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2718a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2719a6cdb93aSRodrigo Vivi 				continue;
2720a6cdb93aSRodrigo Vivi 
2721a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2722a6cdb93aSRodrigo Vivi 				return signaller;
2723a6cdb93aSRodrigo Vivi 		}
2724921d42eaSDaniel Vetter 	} else {
2725921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2726921d42eaSDaniel Vetter 
2727921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2728921d42eaSDaniel Vetter 			if(ring == signaller)
2729921d42eaSDaniel Vetter 				continue;
2730921d42eaSDaniel Vetter 
2731ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2732921d42eaSDaniel Vetter 				return signaller;
2733921d42eaSDaniel Vetter 		}
2734921d42eaSDaniel Vetter 	}
2735921d42eaSDaniel Vetter 
2736a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2737a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2738921d42eaSDaniel Vetter 
2739921d42eaSDaniel Vetter 	return NULL;
2740921d42eaSDaniel Vetter }
2741921d42eaSDaniel Vetter 
2742a4872ba6SOscar Mateo static struct intel_engine_cs *
2743a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2744a24a11e6SChris Wilson {
2745a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
274688fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2747a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2748a6cdb93aSRodrigo Vivi 	int i, backwards;
2749a24a11e6SChris Wilson 
2750a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2751a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27526274f212SChris Wilson 		return NULL;
2753a24a11e6SChris Wilson 
275488fe429dSDaniel Vetter 	/*
275588fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
275688fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2757a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2758a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
275988fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
276088fe429dSDaniel Vetter 	 * ringbuffer itself.
2761a24a11e6SChris Wilson 	 */
276288fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2763a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
276488fe429dSDaniel Vetter 
2765a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
276688fe429dSDaniel Vetter 		/*
276788fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
276888fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
276988fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
277088fe429dSDaniel Vetter 		 */
2771ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
277288fe429dSDaniel Vetter 
277388fe429dSDaniel Vetter 		/* This here seems to blow up */
2774ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2775a24a11e6SChris Wilson 		if (cmd == ipehr)
2776a24a11e6SChris Wilson 			break;
2777a24a11e6SChris Wilson 
277888fe429dSDaniel Vetter 		head -= 4;
277988fe429dSDaniel Vetter 	}
2780a24a11e6SChris Wilson 
278188fe429dSDaniel Vetter 	if (!i)
278288fe429dSDaniel Vetter 		return NULL;
278388fe429dSDaniel Vetter 
2784ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2785a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2786a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2787a6cdb93aSRodrigo Vivi 		offset <<= 32;
2788a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2789a6cdb93aSRodrigo Vivi 	}
2790a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2791a24a11e6SChris Wilson }
2792a24a11e6SChris Wilson 
2793a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27946274f212SChris Wilson {
27956274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2796a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2797a0d036b0SChris Wilson 	u32 seqno;
27986274f212SChris Wilson 
27994be17381SChris Wilson 	ring->hangcheck.deadlock++;
28006274f212SChris Wilson 
28016274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28024be17381SChris Wilson 	if (signaller == NULL)
28034be17381SChris Wilson 		return -1;
28044be17381SChris Wilson 
28054be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28064be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28076274f212SChris Wilson 		return -1;
28086274f212SChris Wilson 
28094be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28104be17381SChris Wilson 		return 1;
28114be17381SChris Wilson 
2812a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2813a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2814a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28154be17381SChris Wilson 		return -1;
28164be17381SChris Wilson 
28174be17381SChris Wilson 	return 0;
28186274f212SChris Wilson }
28196274f212SChris Wilson 
28206274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28216274f212SChris Wilson {
2822a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28236274f212SChris Wilson 	int i;
28246274f212SChris Wilson 
28256274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28264be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28276274f212SChris Wilson }
28286274f212SChris Wilson 
2829ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2830a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28311ec14ad3SChris Wilson {
28321ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28331ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28349107e9d2SChris Wilson 	u32 tmp;
28359107e9d2SChris Wilson 
2836f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2837f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2838f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2839f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2840f260fe7bSMika Kuoppala 		}
2841f260fe7bSMika Kuoppala 
2842f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2843f260fe7bSMika Kuoppala 	}
28446274f212SChris Wilson 
28459107e9d2SChris Wilson 	if (IS_GEN2(dev))
2846f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28479107e9d2SChris Wilson 
28489107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28499107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28509107e9d2SChris Wilson 	 * and break the hang. This should work on
28519107e9d2SChris Wilson 	 * all but the second generation chipsets.
28529107e9d2SChris Wilson 	 */
28539107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28541ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
285558174462SMika Kuoppala 		i915_handle_error(dev, false,
285658174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28571ec14ad3SChris Wilson 				  ring->name);
28581ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2859f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28601ec14ad3SChris Wilson 	}
2861a24a11e6SChris Wilson 
28626274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28636274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28646274f212SChris Wilson 		default:
2865f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28666274f212SChris Wilson 		case 1:
286758174462SMika Kuoppala 			i915_handle_error(dev, false,
286858174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2869a24a11e6SChris Wilson 					  ring->name);
2870a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2871f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28726274f212SChris Wilson 		case 0:
2873f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28746274f212SChris Wilson 		}
28759107e9d2SChris Wilson 	}
28769107e9d2SChris Wilson 
2877f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2878a24a11e6SChris Wilson }
2879d1e61e7fSChris Wilson 
2880737b1506SChris Wilson /*
2881f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
288205407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
288305407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
288405407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
288505407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
288605407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2887f65d9421SBen Gamari  */
2888737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2889f65d9421SBen Gamari {
2890737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2891737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2892737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2893737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2894a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2895b4519513SChris Wilson 	int i;
289605407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28979107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28989107e9d2SChris Wilson #define BUSY 1
28999107e9d2SChris Wilson #define KICK 5
29009107e9d2SChris Wilson #define HUNG 20
2901893eead0SChris Wilson 
2902d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29033e0dc6b0SBen Widawsky 		return;
29043e0dc6b0SBen Widawsky 
2905b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
290650877445SChris Wilson 		u64 acthd;
290750877445SChris Wilson 		u32 seqno;
29089107e9d2SChris Wilson 		bool busy = true;
2909b4519513SChris Wilson 
29106274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29116274f212SChris Wilson 
291205407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
291305407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
291405407ff8SMika Kuoppala 
291505407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
291644cdd6d2SJohn Harrison 			if (ring_idle(ring)) {
2917da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2918da661464SMika Kuoppala 
29199107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29209107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2921094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2922f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29239107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29249107e9d2SChris Wilson 								  ring->name);
2925f4adcd24SDaniel Vetter 						else
2926f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2927f4adcd24SDaniel Vetter 								 ring->name);
29289107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2929094f9a54SChris Wilson 					}
2930094f9a54SChris Wilson 					/* Safeguard against driver failure */
2931094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29329107e9d2SChris Wilson 				} else
29339107e9d2SChris Wilson 					busy = false;
293405407ff8SMika Kuoppala 			} else {
29356274f212SChris Wilson 				/* We always increment the hangcheck score
29366274f212SChris Wilson 				 * if the ring is busy and still processing
29376274f212SChris Wilson 				 * the same request, so that no single request
29386274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29396274f212SChris Wilson 				 * batches). The only time we do not increment
29406274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29416274f212SChris Wilson 				 * ring is in a legitimate wait for another
29426274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29436274f212SChris Wilson 				 * victim and we want to be sure we catch the
29446274f212SChris Wilson 				 * right culprit. Then every time we do kick
29456274f212SChris Wilson 				 * the ring, add a small increment to the
29466274f212SChris Wilson 				 * score so that we can catch a batch that is
29476274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29486274f212SChris Wilson 				 * for stalling the machine.
29499107e9d2SChris Wilson 				 */
2950ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2951ad8beaeaSMika Kuoppala 								    acthd);
2952ad8beaeaSMika Kuoppala 
2953ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2954da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2955f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2956f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2957f260fe7bSMika Kuoppala 					break;
2958f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2959ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29606274f212SChris Wilson 					break;
2961f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2962ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29636274f212SChris Wilson 					break;
2964f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2965ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29666274f212SChris Wilson 					stuck[i] = true;
29676274f212SChris Wilson 					break;
29686274f212SChris Wilson 				}
296905407ff8SMika Kuoppala 			}
29709107e9d2SChris Wilson 		} else {
2971da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2972da661464SMika Kuoppala 
29739107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29749107e9d2SChris Wilson 			 * attempts across multiple batches.
29759107e9d2SChris Wilson 			 */
29769107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29779107e9d2SChris Wilson 				ring->hangcheck.score--;
2978f260fe7bSMika Kuoppala 
2979f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2980cbb465e7SChris Wilson 		}
2981f65d9421SBen Gamari 
298205407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
298305407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29849107e9d2SChris Wilson 		busy_count += busy;
298505407ff8SMika Kuoppala 	}
298605407ff8SMika Kuoppala 
298705407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2988b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2989b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
299005407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2991a43adf07SChris Wilson 				 ring->name);
2992a43adf07SChris Wilson 			rings_hung++;
299305407ff8SMika Kuoppala 		}
299405407ff8SMika Kuoppala 	}
299505407ff8SMika Kuoppala 
299605407ff8SMika Kuoppala 	if (rings_hung)
299758174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
299805407ff8SMika Kuoppala 
299905407ff8SMika Kuoppala 	if (busy_count)
300005407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
300105407ff8SMika Kuoppala 		 * being added */
300210cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
300310cd45b6SMika Kuoppala }
300410cd45b6SMika Kuoppala 
300510cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
300610cd45b6SMika Kuoppala {
3007737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
3008672e7b7cSChris Wilson 
3009d330a953SJani Nikula 	if (!i915.enable_hangcheck)
301010cd45b6SMika Kuoppala 		return;
301110cd45b6SMika Kuoppala 
3012737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
3013737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
3014737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
3015737b1506SChris Wilson 	 */
3016737b1506SChris Wilson 
3017737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3018737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3019f65d9421SBen Gamari }
3020f65d9421SBen Gamari 
30211c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
302291738a95SPaulo Zanoni {
302391738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
302491738a95SPaulo Zanoni 
302591738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
302691738a95SPaulo Zanoni 		return;
302791738a95SPaulo Zanoni 
3028f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3029105b122eSPaulo Zanoni 
3030105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3031105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3032622364b6SPaulo Zanoni }
3033105b122eSPaulo Zanoni 
303491738a95SPaulo Zanoni /*
3035622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3036622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3037622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3038622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3039622364b6SPaulo Zanoni  *
3040622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
304191738a95SPaulo Zanoni  */
3042622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3043622364b6SPaulo Zanoni {
3044622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3045622364b6SPaulo Zanoni 
3046622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3047622364b6SPaulo Zanoni 		return;
3048622364b6SPaulo Zanoni 
3049622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
305091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
305191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
305291738a95SPaulo Zanoni }
305391738a95SPaulo Zanoni 
30547c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3055d18ea1b5SDaniel Vetter {
3056d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3057d18ea1b5SDaniel Vetter 
3058f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3059a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3060f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3061d18ea1b5SDaniel Vetter }
3062d18ea1b5SDaniel Vetter 
3063c0e09200SDave Airlie /* drm_dma.h hooks
3064c0e09200SDave Airlie */
3065be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3066036a4a7dSZhenyu Wang {
30672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3068036a4a7dSZhenyu Wang 
30690c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3070bdfcdb63SDaniel Vetter 
3071f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3072c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3073c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3074036a4a7dSZhenyu Wang 
30757c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3076c650156aSZhenyu Wang 
30771c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30787d99163dSBen Widawsky }
30797d99163dSBen Widawsky 
308070591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
308170591a41SVille Syrjälä {
308270591a41SVille Syrjälä 	enum pipe pipe;
308370591a41SVille Syrjälä 
308470591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
308570591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
308670591a41SVille Syrjälä 
308770591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
308870591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
308970591a41SVille Syrjälä 
309070591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
309170591a41SVille Syrjälä }
309270591a41SVille Syrjälä 
30937e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30947e231dbeSJesse Barnes {
30952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30967e231dbeSJesse Barnes 
30977e231dbeSJesse Barnes 	/* VLV magic */
30987e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30997e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31007e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31017e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31027e231dbeSJesse Barnes 
31037c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31047e231dbeSJesse Barnes 
31057c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31067e231dbeSJesse Barnes 
310770591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
31087e231dbeSJesse Barnes }
31097e231dbeSJesse Barnes 
3110d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3111d6e3cca3SDaniel Vetter {
3112d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3113d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3114d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3115d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3116d6e3cca3SDaniel Vetter }
3117d6e3cca3SDaniel Vetter 
3118823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3119abd58f01SBen Widawsky {
3120abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3121abd58f01SBen Widawsky 	int pipe;
3122abd58f01SBen Widawsky 
3123abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3124abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3125abd58f01SBen Widawsky 
3126d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3127abd58f01SBen Widawsky 
3128055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3129f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3130813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3131f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3132abd58f01SBen Widawsky 
3133f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3134f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3135f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3136abd58f01SBen Widawsky 
3137*266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
31381c69eb42SPaulo Zanoni 		ibx_irq_reset(dev);
3139abd58f01SBen Widawsky }
3140abd58f01SBen Widawsky 
31414c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31424c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3143d49bdb0eSPaulo Zanoni {
31441180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3145d49bdb0eSPaulo Zanoni 
314613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3147d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3148d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3149d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3150d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31514c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31524c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31534c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31541180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31554c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31564c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31574c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31581180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
315913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3160d49bdb0eSPaulo Zanoni }
3161d49bdb0eSPaulo Zanoni 
316243f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
316343f328d7SVille Syrjälä {
316443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
316543f328d7SVille Syrjälä 
316643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
316743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
316843f328d7SVille Syrjälä 
3169d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
317043f328d7SVille Syrjälä 
317143f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
317243f328d7SVille Syrjälä 
317343f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
317443f328d7SVille Syrjälä 
317570591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
317643f328d7SVille Syrjälä }
317743f328d7SVille Syrjälä 
317882a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
317982a28bcfSDaniel Vetter {
31802d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
318182a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3182fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
318382a28bcfSDaniel Vetter 
318482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3185fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3186b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3187cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3188fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
318982a28bcfSDaniel Vetter 	} else {
3190fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3191b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3192cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3193fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
319482a28bcfSDaniel Vetter 	}
319582a28bcfSDaniel Vetter 
3196fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
319782a28bcfSDaniel Vetter 
31987fe0b973SKeith Packard 	/*
31997fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32007fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
32017fe0b973SKeith Packard 	 *
32027fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
32037fe0b973SKeith Packard 	 */
32047fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32057fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32067fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32077fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32087fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32097fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32107fe0b973SKeith Packard }
32117fe0b973SKeith Packard 
3212e0a20ad7SShashank Sharma static void bxt_hpd_irq_setup(struct drm_device *dev)
3213e0a20ad7SShashank Sharma {
3214e0a20ad7SShashank Sharma 	struct drm_i915_private *dev_priv = dev->dev_private;
3215e0a20ad7SShashank Sharma 	struct intel_encoder *intel_encoder;
3216e0a20ad7SShashank Sharma 	u32 hotplug_port = 0;
3217e0a20ad7SShashank Sharma 	u32 hotplug_ctrl;
3218e0a20ad7SShashank Sharma 
3219e0a20ad7SShashank Sharma 	/* Now, enable HPD */
3220e0a20ad7SShashank Sharma 	for_each_intel_encoder(dev, intel_encoder) {
3221e0a20ad7SShashank Sharma 		if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark
3222e0a20ad7SShashank Sharma 				== HPD_ENABLED)
3223e0a20ad7SShashank Sharma 			hotplug_port |= hpd_bxt[intel_encoder->hpd_pin];
3224e0a20ad7SShashank Sharma 	}
3225e0a20ad7SShashank Sharma 
3226e0a20ad7SShashank Sharma 	/* Mask all HPD control bits */
3227e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(BXT_HOTPLUG_CTL) & ~BXT_HOTPLUG_CTL_MASK;
3228e0a20ad7SShashank Sharma 
3229e0a20ad7SShashank Sharma 	/* Enable requested port in hotplug control */
3230e0a20ad7SShashank Sharma 	/* TODO: implement (short) HPD support on port A */
3231e0a20ad7SShashank Sharma 	WARN_ON_ONCE(hotplug_port & BXT_DE_PORT_HP_DDIA);
3232e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIB)
3233e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIB_HPD_ENABLE;
3234e0a20ad7SShashank Sharma 	if (hotplug_port & BXT_DE_PORT_HP_DDIC)
3235e0a20ad7SShashank Sharma 		hotplug_ctrl |= BXT_DDIC_HPD_ENABLE;
3236e0a20ad7SShashank Sharma 	I915_WRITE(BXT_HOTPLUG_CTL, hotplug_ctrl);
3237e0a20ad7SShashank Sharma 
3238e0a20ad7SShashank Sharma 	/* Unmask DDI hotplug in IMR */
3239e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IMR) & ~hotplug_port;
3240e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IMR, hotplug_ctrl);
3241e0a20ad7SShashank Sharma 
3242e0a20ad7SShashank Sharma 	/* Enable DDI hotplug in IER */
3243e0a20ad7SShashank Sharma 	hotplug_ctrl = I915_READ(GEN8_DE_PORT_IER) | hotplug_port;
3244e0a20ad7SShashank Sharma 	I915_WRITE(GEN8_DE_PORT_IER, hotplug_ctrl);
3245e0a20ad7SShashank Sharma 	POSTING_READ(GEN8_DE_PORT_IER);
3246e0a20ad7SShashank Sharma }
3247e0a20ad7SShashank Sharma 
3248d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3249d46da437SPaulo Zanoni {
32502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
325182a28bcfSDaniel Vetter 	u32 mask;
3252d46da437SPaulo Zanoni 
3253692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3254692a04cfSDaniel Vetter 		return;
3255692a04cfSDaniel Vetter 
3256105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32575c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3258105b122eSPaulo Zanoni 	else
32595c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32608664281bSPaulo Zanoni 
3261337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3262d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3263d46da437SPaulo Zanoni }
3264d46da437SPaulo Zanoni 
32650a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32660a9a8c91SDaniel Vetter {
32670a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32680a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32690a9a8c91SDaniel Vetter 
32700a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32710a9a8c91SDaniel Vetter 
32720a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3273040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32740a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
327535a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
327635a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32770a9a8c91SDaniel Vetter 	}
32780a9a8c91SDaniel Vetter 
32790a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32800a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32810a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32820a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32830a9a8c91SDaniel Vetter 	} else {
32840a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32850a9a8c91SDaniel Vetter 	}
32860a9a8c91SDaniel Vetter 
328735079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32880a9a8c91SDaniel Vetter 
32890a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
329078e68d36SImre Deak 		/*
329178e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
329278e68d36SImre Deak 		 * itself is enabled/disabled.
329378e68d36SImre Deak 		 */
32940a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32950a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32960a9a8c91SDaniel Vetter 
3297605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
329835079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32990a9a8c91SDaniel Vetter 	}
33000a9a8c91SDaniel Vetter }
33010a9a8c91SDaniel Vetter 
3302f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3303036a4a7dSZhenyu Wang {
33042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33058e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33068e76f8dcSPaulo Zanoni 
33078e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33088e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33098e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33108e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33115c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33128e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
33135c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
33148e76f8dcSPaulo Zanoni 	} else {
33158e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3316ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33175b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33185b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33195b3a856bSDaniel Vetter 				DE_POISON);
33205c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
33215c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
33228e76f8dcSPaulo Zanoni 	}
3323036a4a7dSZhenyu Wang 
33241ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3325036a4a7dSZhenyu Wang 
33260c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33270c841212SPaulo Zanoni 
3328622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3329622364b6SPaulo Zanoni 
333035079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3331036a4a7dSZhenyu Wang 
33320a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3333036a4a7dSZhenyu Wang 
3334d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33357fe0b973SKeith Packard 
3336f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33376005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33386005ce42SDaniel Vetter 		 *
33396005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33404bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33414bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3342d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3343f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3344d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3345f97108d1SJesse Barnes 	}
3346f97108d1SJesse Barnes 
3347036a4a7dSZhenyu Wang 	return 0;
3348036a4a7dSZhenyu Wang }
3349036a4a7dSZhenyu Wang 
3350f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3351f8b79e58SImre Deak {
3352f8b79e58SImre Deak 	u32 pipestat_mask;
3353f8b79e58SImre Deak 	u32 iir_mask;
3354120dda4fSVille Syrjälä 	enum pipe pipe;
3355f8b79e58SImre Deak 
3356f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3357f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3358f8b79e58SImre Deak 
3359120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3360120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3361f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3362f8b79e58SImre Deak 
3363f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3364f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3365f8b79e58SImre Deak 
3366120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3367120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3368120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3369f8b79e58SImre Deak 
3370f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3371f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3372f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3373120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3374120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3375f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3376f8b79e58SImre Deak 
3377f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3378f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3379f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
338076e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
338176e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3382f8b79e58SImre Deak }
3383f8b79e58SImre Deak 
3384f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3385f8b79e58SImre Deak {
3386f8b79e58SImre Deak 	u32 pipestat_mask;
3387f8b79e58SImre Deak 	u32 iir_mask;
3388120dda4fSVille Syrjälä 	enum pipe pipe;
3389f8b79e58SImre Deak 
3390f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3391f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33926c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3393120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3394120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3395f8b79e58SImre Deak 
3396f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3397f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
339876e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3399f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3400f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3401f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3402f8b79e58SImre Deak 
3403f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3404f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3405f8b79e58SImre Deak 
3406120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3407120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3408120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3409f8b79e58SImre Deak 
3410f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3411f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3412120dda4fSVille Syrjälä 
3413120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3414120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3415f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3416f8b79e58SImre Deak }
3417f8b79e58SImre Deak 
3418f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3419f8b79e58SImre Deak {
3420f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3421f8b79e58SImre Deak 
3422f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3423f8b79e58SImre Deak 		return;
3424f8b79e58SImre Deak 
3425f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3426f8b79e58SImre Deak 
3427950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3428f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3429f8b79e58SImre Deak }
3430f8b79e58SImre Deak 
3431f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3432f8b79e58SImre Deak {
3433f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3434f8b79e58SImre Deak 
3435f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3436f8b79e58SImre Deak 		return;
3437f8b79e58SImre Deak 
3438f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3439f8b79e58SImre Deak 
3440950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3441f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3442f8b79e58SImre Deak }
3443f8b79e58SImre Deak 
34440e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
34457e231dbeSJesse Barnes {
3446f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34477e231dbeSJesse Barnes 
344820afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
344920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
345020afbda2SDaniel Vetter 
34517e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
345276e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
345376e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
345476e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
345576e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
34567e231dbeSJesse Barnes 
3457b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3458b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3459d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3460f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3461f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3462d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
34630e6c9a9eSVille Syrjälä }
34640e6c9a9eSVille Syrjälä 
34650e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34660e6c9a9eSVille Syrjälä {
34670e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34680e6c9a9eSVille Syrjälä 
34690e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
34707e231dbeSJesse Barnes 
34710a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34727e231dbeSJesse Barnes 
34737e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34747e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34757e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34767e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34777e231dbeSJesse Barnes #endif
34787e231dbeSJesse Barnes 
34797e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
348020afbda2SDaniel Vetter 
348120afbda2SDaniel Vetter 	return 0;
348220afbda2SDaniel Vetter }
348320afbda2SDaniel Vetter 
3484abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3485abd58f01SBen Widawsky {
3486abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3487abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3488abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
348973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3490abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
349173d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
349273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3493abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
349473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
349573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
349673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3497abd58f01SBen Widawsky 		0,
349873d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
349973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3500abd58f01SBen Widawsky 		};
3501abd58f01SBen Widawsky 
35020961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35039a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35049a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
350578e68d36SImre Deak 	/*
350678e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
350778e68d36SImre Deak 	 * is enabled/disabled.
350878e68d36SImre Deak 	 */
350978e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
35109a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3511abd58f01SBen Widawsky }
3512abd58f01SBen Widawsky 
3513abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3514abd58f01SBen Widawsky {
3515770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3516770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3517abd58f01SBen Widawsky 	int pipe;
351888e04703SJesse Barnes 	u32 aux_en = GEN8_AUX_CHANNEL_A;
3519770de83dSDamien Lespiau 
352088e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3521770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3522770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
352388e04703SJesse Barnes 		aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
352488e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
352588e04703SJesse Barnes 	} else
3526770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3527770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3528770de83dSDamien Lespiau 
3529770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3530770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3531770de83dSDamien Lespiau 
353213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
353313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
353413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3535abd58f01SBen Widawsky 
3536055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3537f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3538813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3539813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3540813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
354135079899SPaulo Zanoni 					  de_pipe_enables);
3542abd58f01SBen Widawsky 
354388e04703SJesse Barnes 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
3544abd58f01SBen Widawsky }
3545abd58f01SBen Widawsky 
3546abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3547abd58f01SBen Widawsky {
3548abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3549abd58f01SBen Widawsky 
3550*266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3551622364b6SPaulo Zanoni 		ibx_irq_pre_postinstall(dev);
3552622364b6SPaulo Zanoni 
3553abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3554abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3555abd58f01SBen Widawsky 
3556*266ea3d9SShashank Sharma 	if (HAS_PCH_SPLIT(dev))
3557abd58f01SBen Widawsky 		ibx_irq_postinstall(dev);
3558abd58f01SBen Widawsky 
3559abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3560abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3561abd58f01SBen Widawsky 
3562abd58f01SBen Widawsky 	return 0;
3563abd58f01SBen Widawsky }
3564abd58f01SBen Widawsky 
356543f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
356643f328d7SVille Syrjälä {
356743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
356843f328d7SVille Syrjälä 
3569c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
357043f328d7SVille Syrjälä 
357143f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
357243f328d7SVille Syrjälä 
357343f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
357443f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
357543f328d7SVille Syrjälä 
357643f328d7SVille Syrjälä 	return 0;
357743f328d7SVille Syrjälä }
357843f328d7SVille Syrjälä 
3579abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3580abd58f01SBen Widawsky {
3581abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3582abd58f01SBen Widawsky 
3583abd58f01SBen Widawsky 	if (!dev_priv)
3584abd58f01SBen Widawsky 		return;
3585abd58f01SBen Widawsky 
3586823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3587abd58f01SBen Widawsky }
3588abd58f01SBen Widawsky 
35898ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
35908ea0be4fSVille Syrjälä {
35918ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
35928ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
35938ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35948ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
35958ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
35968ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35978ea0be4fSVille Syrjälä 
35988ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
35998ea0be4fSVille Syrjälä 
3600c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
36018ea0be4fSVille Syrjälä }
36028ea0be4fSVille Syrjälä 
36037e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
36047e231dbeSJesse Barnes {
36052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36067e231dbeSJesse Barnes 
36077e231dbeSJesse Barnes 	if (!dev_priv)
36087e231dbeSJesse Barnes 		return;
36097e231dbeSJesse Barnes 
3610843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3611843d0e7dSImre Deak 
3612893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3613893fce8eSVille Syrjälä 
36147e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3615f8b79e58SImre Deak 
36168ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
36177e231dbeSJesse Barnes }
36187e231dbeSJesse Barnes 
361943f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
362043f328d7SVille Syrjälä {
362143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
362243f328d7SVille Syrjälä 
362343f328d7SVille Syrjälä 	if (!dev_priv)
362443f328d7SVille Syrjälä 		return;
362543f328d7SVille Syrjälä 
362643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
362743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
362843f328d7SVille Syrjälä 
3629a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
363043f328d7SVille Syrjälä 
3631a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
363243f328d7SVille Syrjälä 
3633c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
363443f328d7SVille Syrjälä }
363543f328d7SVille Syrjälä 
3636f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3637036a4a7dSZhenyu Wang {
36382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36394697995bSJesse Barnes 
36404697995bSJesse Barnes 	if (!dev_priv)
36414697995bSJesse Barnes 		return;
36424697995bSJesse Barnes 
3643be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3644036a4a7dSZhenyu Wang }
3645036a4a7dSZhenyu Wang 
3646c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3647c2798b19SChris Wilson {
36482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3649c2798b19SChris Wilson 	int pipe;
3650c2798b19SChris Wilson 
3651055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3652c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3653c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3654c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3655c2798b19SChris Wilson 	POSTING_READ16(IER);
3656c2798b19SChris Wilson }
3657c2798b19SChris Wilson 
3658c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3659c2798b19SChris Wilson {
36602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3661c2798b19SChris Wilson 
3662c2798b19SChris Wilson 	I915_WRITE16(EMR,
3663c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3664c2798b19SChris Wilson 
3665c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3666c2798b19SChris Wilson 	dev_priv->irq_mask =
3667c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3668c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3669c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3670c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3671c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3672c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3673c2798b19SChris Wilson 
3674c2798b19SChris Wilson 	I915_WRITE16(IER,
3675c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3676c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3677c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3678c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3679c2798b19SChris Wilson 	POSTING_READ16(IER);
3680c2798b19SChris Wilson 
3681379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3682379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3683d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3684755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3685755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3686d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3687379ef82dSDaniel Vetter 
3688c2798b19SChris Wilson 	return 0;
3689c2798b19SChris Wilson }
3690c2798b19SChris Wilson 
369190a72f87SVille Syrjälä /*
369290a72f87SVille Syrjälä  * Returns true when a page flip has completed.
369390a72f87SVille Syrjälä  */
369490a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
36951f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
369690a72f87SVille Syrjälä {
36972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36981f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
369990a72f87SVille Syrjälä 
37008d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
370190a72f87SVille Syrjälä 		return false;
370290a72f87SVille Syrjälä 
370390a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3704d6bbafa1SChris Wilson 		goto check_page_flip;
370590a72f87SVille Syrjälä 
370690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
370790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
370890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
370990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
371090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
371190a72f87SVille Syrjälä 	 */
371290a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3713d6bbafa1SChris Wilson 		goto check_page_flip;
371490a72f87SVille Syrjälä 
37157d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
371690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
371790a72f87SVille Syrjälä 	return true;
3718d6bbafa1SChris Wilson 
3719d6bbafa1SChris Wilson check_page_flip:
3720d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3721d6bbafa1SChris Wilson 	return false;
372290a72f87SVille Syrjälä }
372390a72f87SVille Syrjälä 
3724ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3725c2798b19SChris Wilson {
372645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3728c2798b19SChris Wilson 	u16 iir, new_iir;
3729c2798b19SChris Wilson 	u32 pipe_stats[2];
3730c2798b19SChris Wilson 	int pipe;
3731c2798b19SChris Wilson 	u16 flip_mask =
3732c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3733c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3734c2798b19SChris Wilson 
37352dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
37362dd2a883SImre Deak 		return IRQ_NONE;
37372dd2a883SImre Deak 
3738c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3739c2798b19SChris Wilson 	if (iir == 0)
3740c2798b19SChris Wilson 		return IRQ_NONE;
3741c2798b19SChris Wilson 
3742c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3743c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3744c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3745c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3746c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3747c2798b19SChris Wilson 		 */
3748222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3749c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3750aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3751c2798b19SChris Wilson 
3752055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3753c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3754c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3755c2798b19SChris Wilson 
3756c2798b19SChris Wilson 			/*
3757c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3758c2798b19SChris Wilson 			 */
37592d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3760c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3761c2798b19SChris Wilson 		}
3762222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3763c2798b19SChris Wilson 
3764c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3765c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3766c2798b19SChris Wilson 
3767c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
376874cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3769c2798b19SChris Wilson 
3770055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37711f1c2e24SVille Syrjälä 			int plane = pipe;
37723a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
37731f1c2e24SVille Syrjälä 				plane = !plane;
37741f1c2e24SVille Syrjälä 
37754356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37761f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
37771f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3778c2798b19SChris Wilson 
37794356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3780277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37812d9d2b0bSVille Syrjälä 
37821f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37831f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37841f7247c0SDaniel Vetter 								    pipe);
37854356d586SDaniel Vetter 		}
3786c2798b19SChris Wilson 
3787c2798b19SChris Wilson 		iir = new_iir;
3788c2798b19SChris Wilson 	}
3789c2798b19SChris Wilson 
3790c2798b19SChris Wilson 	return IRQ_HANDLED;
3791c2798b19SChris Wilson }
3792c2798b19SChris Wilson 
3793c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3794c2798b19SChris Wilson {
37952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3796c2798b19SChris Wilson 	int pipe;
3797c2798b19SChris Wilson 
3798055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3799c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3800c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3801c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3802c2798b19SChris Wilson 	}
3803c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3804c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3805c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3806c2798b19SChris Wilson }
3807c2798b19SChris Wilson 
3808a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3809a266c7d5SChris Wilson {
38102d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3811a266c7d5SChris Wilson 	int pipe;
3812a266c7d5SChris Wilson 
3813a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3814a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3815a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3816a266c7d5SChris Wilson 	}
3817a266c7d5SChris Wilson 
381800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3819055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3820a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3821a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3822a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3823a266c7d5SChris Wilson 	POSTING_READ(IER);
3824a266c7d5SChris Wilson }
3825a266c7d5SChris Wilson 
3826a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3827a266c7d5SChris Wilson {
38282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
382938bde180SChris Wilson 	u32 enable_mask;
3830a266c7d5SChris Wilson 
383138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
383238bde180SChris Wilson 
383338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
383438bde180SChris Wilson 	dev_priv->irq_mask =
383538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
383638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
383738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
383838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
383938bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
384038bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
384138bde180SChris Wilson 
384238bde180SChris Wilson 	enable_mask =
384338bde180SChris Wilson 		I915_ASLE_INTERRUPT |
384438bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
384538bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
384638bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
384738bde180SChris Wilson 		I915_USER_INTERRUPT;
384838bde180SChris Wilson 
3849a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
385020afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
385120afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
385220afbda2SDaniel Vetter 
3853a266c7d5SChris Wilson 		/* Enable in IER... */
3854a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3855a266c7d5SChris Wilson 		/* and unmask in IMR */
3856a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3857a266c7d5SChris Wilson 	}
3858a266c7d5SChris Wilson 
3859a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3860a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3861a266c7d5SChris Wilson 	POSTING_READ(IER);
3862a266c7d5SChris Wilson 
3863f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
386420afbda2SDaniel Vetter 
3865379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3866379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3867d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3868755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3869755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3870d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3871379ef82dSDaniel Vetter 
387220afbda2SDaniel Vetter 	return 0;
387320afbda2SDaniel Vetter }
387420afbda2SDaniel Vetter 
387590a72f87SVille Syrjälä /*
387690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
387790a72f87SVille Syrjälä  */
387890a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
387990a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
388090a72f87SVille Syrjälä {
38812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
388290a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
388390a72f87SVille Syrjälä 
38848d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
388590a72f87SVille Syrjälä 		return false;
388690a72f87SVille Syrjälä 
388790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3888d6bbafa1SChris Wilson 		goto check_page_flip;
388990a72f87SVille Syrjälä 
389090a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
389190a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
389290a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
389390a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
389490a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
389590a72f87SVille Syrjälä 	 */
389690a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3897d6bbafa1SChris Wilson 		goto check_page_flip;
389890a72f87SVille Syrjälä 
38997d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
390090a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
390190a72f87SVille Syrjälä 	return true;
3902d6bbafa1SChris Wilson 
3903d6bbafa1SChris Wilson check_page_flip:
3904d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3905d6bbafa1SChris Wilson 	return false;
390690a72f87SVille Syrjälä }
390790a72f87SVille Syrjälä 
3908ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3909a266c7d5SChris Wilson {
391045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39128291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
391338bde180SChris Wilson 	u32 flip_mask =
391438bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
391538bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
391638bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3917a266c7d5SChris Wilson 
39182dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
39192dd2a883SImre Deak 		return IRQ_NONE;
39202dd2a883SImre Deak 
3921a266c7d5SChris Wilson 	iir = I915_READ(IIR);
392238bde180SChris Wilson 	do {
392338bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39248291ee90SChris Wilson 		bool blc_event = false;
3925a266c7d5SChris Wilson 
3926a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3927a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3928a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3929a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3930a266c7d5SChris Wilson 		 */
3931222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3932a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3933aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3934a266c7d5SChris Wilson 
3935055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3936a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3937a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3938a266c7d5SChris Wilson 
393938bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3940a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3941a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
394238bde180SChris Wilson 				irq_received = true;
3943a266c7d5SChris Wilson 			}
3944a266c7d5SChris Wilson 		}
3945222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3946a266c7d5SChris Wilson 
3947a266c7d5SChris Wilson 		if (!irq_received)
3948a266c7d5SChris Wilson 			break;
3949a266c7d5SChris Wilson 
3950a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
395116c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
395216c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
395316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3954a266c7d5SChris Wilson 
395538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3956a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3957a266c7d5SChris Wilson 
3958a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
395974cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3960a266c7d5SChris Wilson 
3961055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
396238bde180SChris Wilson 			int plane = pipe;
39633a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
396438bde180SChris Wilson 				plane = !plane;
39655e2032d4SVille Syrjälä 
396690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
396790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
396890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3969a266c7d5SChris Wilson 
3970a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3971a266c7d5SChris Wilson 				blc_event = true;
39724356d586SDaniel Vetter 
39734356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3974277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39752d9d2b0bSVille Syrjälä 
39761f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39771f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39781f7247c0SDaniel Vetter 								    pipe);
3979a266c7d5SChris Wilson 		}
3980a266c7d5SChris Wilson 
3981a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3982a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3983a266c7d5SChris Wilson 
3984a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3985a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3986a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3987a266c7d5SChris Wilson 		 * we would never get another interrupt.
3988a266c7d5SChris Wilson 		 *
3989a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3990a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3991a266c7d5SChris Wilson 		 * another one.
3992a266c7d5SChris Wilson 		 *
3993a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3994a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3995a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3996a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3997a266c7d5SChris Wilson 		 * stray interrupts.
3998a266c7d5SChris Wilson 		 */
399938bde180SChris Wilson 		ret = IRQ_HANDLED;
4000a266c7d5SChris Wilson 		iir = new_iir;
400138bde180SChris Wilson 	} while (iir & ~flip_mask);
4002a266c7d5SChris Wilson 
4003a266c7d5SChris Wilson 	return ret;
4004a266c7d5SChris Wilson }
4005a266c7d5SChris Wilson 
4006a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4007a266c7d5SChris Wilson {
40082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4009a266c7d5SChris Wilson 	int pipe;
4010a266c7d5SChris Wilson 
4011a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4012a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4013a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4014a266c7d5SChris Wilson 	}
4015a266c7d5SChris Wilson 
401600d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4017055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
401855b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4019a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
402055b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
402155b39755SChris Wilson 	}
4022a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4023a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4024a266c7d5SChris Wilson 
4025a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4026a266c7d5SChris Wilson }
4027a266c7d5SChris Wilson 
4028a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4029a266c7d5SChris Wilson {
40302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4031a266c7d5SChris Wilson 	int pipe;
4032a266c7d5SChris Wilson 
4033a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4034a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4035a266c7d5SChris Wilson 
4036a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4037055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4038a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4039a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4040a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4041a266c7d5SChris Wilson 	POSTING_READ(IER);
4042a266c7d5SChris Wilson }
4043a266c7d5SChris Wilson 
4044a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4045a266c7d5SChris Wilson {
40462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4047bbba0a97SChris Wilson 	u32 enable_mask;
4048a266c7d5SChris Wilson 	u32 error_mask;
4049a266c7d5SChris Wilson 
4050a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4051bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4052adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4053bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4054bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4055bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4056bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4057bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4058bbba0a97SChris Wilson 
4059bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
406021ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
406121ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4062bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4063bbba0a97SChris Wilson 
4064bbba0a97SChris Wilson 	if (IS_G4X(dev))
4065bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4066a266c7d5SChris Wilson 
4067b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4068b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4069d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4070755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4071755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4072755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4073d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4074a266c7d5SChris Wilson 
4075a266c7d5SChris Wilson 	/*
4076a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4077a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4078a266c7d5SChris Wilson 	 */
4079a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4080a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4081a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4082a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4083a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4084a266c7d5SChris Wilson 	} else {
4085a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4086a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4087a266c7d5SChris Wilson 	}
4088a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4089a266c7d5SChris Wilson 
4090a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4091a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4092a266c7d5SChris Wilson 	POSTING_READ(IER);
4093a266c7d5SChris Wilson 
409420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
409520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
409620afbda2SDaniel Vetter 
4097f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
409820afbda2SDaniel Vetter 
409920afbda2SDaniel Vetter 	return 0;
410020afbda2SDaniel Vetter }
410120afbda2SDaniel Vetter 
4102bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
410320afbda2SDaniel Vetter {
41042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4105cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
410620afbda2SDaniel Vetter 	u32 hotplug_en;
410720afbda2SDaniel Vetter 
4108b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4109b5ea2d56SDaniel Vetter 
4110bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4111bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4112adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4113e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
4114b2784e15SDamien Lespiau 	for_each_intel_encoder(dev, intel_encoder)
4115cd569aedSEgbert Eich 		if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4116cd569aedSEgbert Eich 			hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4117a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4118a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4119a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4120a266c7d5SChris Wilson 	*/
4121a266c7d5SChris Wilson 	if (IS_G4X(dev))
4122a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
412385fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4124a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4125a266c7d5SChris Wilson 
4126a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4127a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4128a266c7d5SChris Wilson }
4129a266c7d5SChris Wilson 
4130ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4131a266c7d5SChris Wilson {
413245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4134a266c7d5SChris Wilson 	u32 iir, new_iir;
4135a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4136a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
413721ad8330SVille Syrjälä 	u32 flip_mask =
413821ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
413921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4140a266c7d5SChris Wilson 
41412dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
41422dd2a883SImre Deak 		return IRQ_NONE;
41432dd2a883SImre Deak 
4144a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4145a266c7d5SChris Wilson 
4146a266c7d5SChris Wilson 	for (;;) {
4147501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41482c8ba29fSChris Wilson 		bool blc_event = false;
41492c8ba29fSChris Wilson 
4150a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4151a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4152a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4153a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4154a266c7d5SChris Wilson 		 */
4155222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4156a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4157aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4158a266c7d5SChris Wilson 
4159055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4160a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4161a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4162a266c7d5SChris Wilson 
4163a266c7d5SChris Wilson 			/*
4164a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4165a266c7d5SChris Wilson 			 */
4166a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4167a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4168501e01d7SVille Syrjälä 				irq_received = true;
4169a266c7d5SChris Wilson 			}
4170a266c7d5SChris Wilson 		}
4171222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4172a266c7d5SChris Wilson 
4173a266c7d5SChris Wilson 		if (!irq_received)
4174a266c7d5SChris Wilson 			break;
4175a266c7d5SChris Wilson 
4176a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4177a266c7d5SChris Wilson 
4178a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
417916c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
418016c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4181a266c7d5SChris Wilson 
418221ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4183a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4184a266c7d5SChris Wilson 
4185a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
418674cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4187a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
418874cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4189a266c7d5SChris Wilson 
4190055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41912c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
419290a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
419390a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4194a266c7d5SChris Wilson 
4195a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4196a266c7d5SChris Wilson 				blc_event = true;
41974356d586SDaniel Vetter 
41984356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4199277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4200a266c7d5SChris Wilson 
42011f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42021f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42032d9d2b0bSVille Syrjälä 		}
4204a266c7d5SChris Wilson 
4205a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4206a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4207a266c7d5SChris Wilson 
4208515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4209515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4210515ac2bbSDaniel Vetter 
4211a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4212a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4213a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4214a266c7d5SChris Wilson 		 * we would never get another interrupt.
4215a266c7d5SChris Wilson 		 *
4216a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4217a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4218a266c7d5SChris Wilson 		 * another one.
4219a266c7d5SChris Wilson 		 *
4220a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4221a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4222a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4223a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4224a266c7d5SChris Wilson 		 * stray interrupts.
4225a266c7d5SChris Wilson 		 */
4226a266c7d5SChris Wilson 		iir = new_iir;
4227a266c7d5SChris Wilson 	}
4228a266c7d5SChris Wilson 
4229a266c7d5SChris Wilson 	return ret;
4230a266c7d5SChris Wilson }
4231a266c7d5SChris Wilson 
4232a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4233a266c7d5SChris Wilson {
42342d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4235a266c7d5SChris Wilson 	int pipe;
4236a266c7d5SChris Wilson 
4237a266c7d5SChris Wilson 	if (!dev_priv)
4238a266c7d5SChris Wilson 		return;
4239a266c7d5SChris Wilson 
4240a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4241a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4242a266c7d5SChris Wilson 
4243a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4244055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4245a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4246a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4247a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4248a266c7d5SChris Wilson 
4249055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4250a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4251a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4252a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4253a266c7d5SChris Wilson }
4254a266c7d5SChris Wilson 
42554cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4256ac4c16c5SEgbert Eich {
42576323751dSImre Deak 	struct drm_i915_private *dev_priv =
42586323751dSImre Deak 		container_of(work, typeof(*dev_priv),
42596323751dSImre Deak 			     hotplug_reenable_work.work);
4260ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4261ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4262ac4c16c5SEgbert Eich 	int i;
4263ac4c16c5SEgbert Eich 
42646323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
42656323751dSImre Deak 
42664cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4267ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4268ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4269ac4c16c5SEgbert Eich 
4270ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4271ac4c16c5SEgbert Eich 			continue;
4272ac4c16c5SEgbert Eich 
4273ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4274ac4c16c5SEgbert Eich 
4275ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4276ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4277ac4c16c5SEgbert Eich 
4278ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4279ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4280ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4281c23cc417SJani Nikula 							 connector->name);
4282ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4283ac4c16c5SEgbert Eich 				if (!connector->polled)
4284ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4285ac4c16c5SEgbert Eich 			}
4286ac4c16c5SEgbert Eich 		}
4287ac4c16c5SEgbert Eich 	}
4288ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4289ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
42904cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
42916323751dSImre Deak 
42926323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4293ac4c16c5SEgbert Eich }
4294ac4c16c5SEgbert Eich 
4295fca52a55SDaniel Vetter /**
4296fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4297fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4298fca52a55SDaniel Vetter  *
4299fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4300fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4301fca52a55SDaniel Vetter  */
4302b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4303f71d4af4SJesse Barnes {
4304b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43058b2e326dSChris Wilson 
43068b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
430713cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
4308c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4309a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43108b2e326dSChris Wilson 
4311a6706b45SDeepak S 	/* Let's track the enabled rps events */
4312b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43136c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
43146f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
431531685c25SDeepak S 	else
4316a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4317a6706b45SDeepak S 
4318737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4319737b1506SChris Wilson 			  i915_hangcheck_elapsed);
43206323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
43214cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
432261bac78eSDaniel Vetter 
432397a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43249ee32feaSDaniel Vetter 
4325b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43264cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43274cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4328b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4329f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4330f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4331391f75e2SVille Syrjälä 	} else {
4332391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4333391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4334f71d4af4SJesse Barnes 	}
4335f71d4af4SJesse Barnes 
433621da2700SVille Syrjälä 	/*
433721da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
433821da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
433921da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
434021da2700SVille Syrjälä 	 */
4341b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
434221da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
434321da2700SVille Syrjälä 
4344f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4345f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4346f71d4af4SJesse Barnes 
4347b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
434843f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
434943f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
435043f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
435143f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
435243f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
435343f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
435443f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4355b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43567e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43577e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43587e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43597e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43607e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43617e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4362fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4363b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4364abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4365723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4366abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4367abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4368abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4369abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4370e0a20ad7SShashank Sharma 		if (HAS_PCH_SPLIT(dev))
4371abd58f01SBen Widawsky 			dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4372e0a20ad7SShashank Sharma 		else
4373e0a20ad7SShashank Sharma 			dev_priv->display.hpd_irq_setup = bxt_hpd_irq_setup;
4374f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4375f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4376723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4377f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4378f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4379f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4380f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
438182a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4382f71d4af4SJesse Barnes 	} else {
4383b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4384c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4385c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4386c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4387c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4388b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4389a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4390a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4391a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4392a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4393c2798b19SChris Wilson 		} else {
4394a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4395a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4396a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4397a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4398c2798b19SChris Wilson 		}
4399778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4400778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4401f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4402f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4403f71d4af4SJesse Barnes 	}
4404f71d4af4SJesse Barnes }
440520afbda2SDaniel Vetter 
4406fca52a55SDaniel Vetter /**
4407fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4408fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4409fca52a55SDaniel Vetter  *
4410fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4411fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4412fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4413fca52a55SDaniel Vetter  * obeyed.
4414fca52a55SDaniel Vetter  *
4415fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4416fca52a55SDaniel Vetter  * in the driver load and resume code.
4417fca52a55SDaniel Vetter  */
4418b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
441920afbda2SDaniel Vetter {
4420b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4421821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4422821450c6SEgbert Eich 	struct drm_connector *connector;
4423821450c6SEgbert Eich 	int i;
442420afbda2SDaniel Vetter 
4425821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4426821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4427821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4428821450c6SEgbert Eich 	}
4429821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4430821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4431821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
44320e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
44330e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
44340e32b39cSDave Airlie 		if (intel_connector->mst_port)
4435821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4436821450c6SEgbert Eich 	}
4437b5ea2d56SDaniel Vetter 
4438b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4439b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4440d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
444120afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
444220afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4443d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
444420afbda2SDaniel Vetter }
4445c67a470bSPaulo Zanoni 
4446fca52a55SDaniel Vetter /**
4447fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4448fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4449fca52a55SDaniel Vetter  *
4450fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4451fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4452fca52a55SDaniel Vetter  *
4453fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4454fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4455fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4456fca52a55SDaniel Vetter  */
44572aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44582aeb7d3aSDaniel Vetter {
44592aeb7d3aSDaniel Vetter 	/*
44602aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44612aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44622aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44632aeb7d3aSDaniel Vetter 	 */
44642aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44652aeb7d3aSDaniel Vetter 
44662aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44672aeb7d3aSDaniel Vetter }
44682aeb7d3aSDaniel Vetter 
4469fca52a55SDaniel Vetter /**
4470fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4471fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4472fca52a55SDaniel Vetter  *
4473fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4474fca52a55SDaniel Vetter  * resources acquired in the init functions.
4475fca52a55SDaniel Vetter  */
44762aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44772aeb7d3aSDaniel Vetter {
44782aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44792aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44802aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44812aeb7d3aSDaniel Vetter }
44822aeb7d3aSDaniel Vetter 
4483fca52a55SDaniel Vetter /**
4484fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4485fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4486fca52a55SDaniel Vetter  *
4487fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4488fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4489fca52a55SDaniel Vetter  */
4490b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4491c67a470bSPaulo Zanoni {
4492b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44932aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44942dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4495c67a470bSPaulo Zanoni }
4496c67a470bSPaulo Zanoni 
4497fca52a55SDaniel Vetter /**
4498fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4499fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4500fca52a55SDaniel Vetter  *
4501fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4502fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4503fca52a55SDaniel Vetter  */
4504b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4505c67a470bSPaulo Zanoni {
45062aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4507b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4508b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4509c67a470bSPaulo Zanoni }
4510