xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 096fad9ebfadb4922332f98895ea948e8d7fe6a7)
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 
915c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
92f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
935c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
945c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
955c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
965c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
975c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
985c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
995c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
1005c502442SPaulo Zanoni } while (0)
1015c502442SPaulo Zanoni 
102f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
103a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
1045c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
105a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
1065c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1075c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1085c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1095c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
110a9d356a6SPaulo Zanoni } while (0)
111a9d356a6SPaulo Zanoni 
112337ba017SPaulo Zanoni /*
113337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
114337ba017SPaulo Zanoni  */
115337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
116337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
117337ba017SPaulo Zanoni 	if (val) { \
118337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
119337ba017SPaulo Zanoni 		     (reg), val); \
120337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
121337ba017SPaulo Zanoni 		POSTING_READ(reg); \
122337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
123337ba017SPaulo Zanoni 		POSTING_READ(reg); \
124337ba017SPaulo Zanoni 	} \
125337ba017SPaulo Zanoni } while (0)
126337ba017SPaulo Zanoni 
12735079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
128337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
12935079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
1307d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1317d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
13235079899SPaulo Zanoni } while (0)
13335079899SPaulo Zanoni 
13435079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
135337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
13635079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1377d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1387d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
13935079899SPaulo Zanoni } while (0)
14035079899SPaulo Zanoni 
141c9a9a268SImre Deak static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir);
142c9a9a268SImre Deak 
143036a4a7dSZhenyu Wang /* For display hotplug interrupt */
14447339cd9SDaniel Vetter void
1452d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
146036a4a7dSZhenyu Wang {
1474bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1484bc9d430SDaniel Vetter 
1499df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
150c67a470bSPaulo Zanoni 		return;
151c67a470bSPaulo Zanoni 
1521ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1531ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1541ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1553143a2bfSChris Wilson 		POSTING_READ(DEIMR);
156036a4a7dSZhenyu Wang 	}
157036a4a7dSZhenyu Wang }
158036a4a7dSZhenyu Wang 
15947339cd9SDaniel Vetter void
1602d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
161036a4a7dSZhenyu Wang {
1624bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1634bc9d430SDaniel Vetter 
16406ffc778SPaulo Zanoni 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
165c67a470bSPaulo Zanoni 		return;
166c67a470bSPaulo Zanoni 
1671ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1681ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1691ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1703143a2bfSChris Wilson 		POSTING_READ(DEIMR);
171036a4a7dSZhenyu Wang 	}
172036a4a7dSZhenyu Wang }
173036a4a7dSZhenyu Wang 
17443eaea13SPaulo Zanoni /**
17543eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
17643eaea13SPaulo Zanoni  * @dev_priv: driver private
17743eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
17843eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
17943eaea13SPaulo Zanoni  */
18043eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
18143eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
18243eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
18343eaea13SPaulo Zanoni {
18443eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
18543eaea13SPaulo Zanoni 
18615a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
18715a17aaeSDaniel Vetter 
1889df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
189c67a470bSPaulo Zanoni 		return;
190c67a470bSPaulo Zanoni 
19143eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
19243eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
19343eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
19443eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
19543eaea13SPaulo Zanoni }
19643eaea13SPaulo Zanoni 
197480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19843eaea13SPaulo Zanoni {
19943eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
20043eaea13SPaulo Zanoni }
20143eaea13SPaulo Zanoni 
202480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
20343eaea13SPaulo Zanoni {
20443eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
20543eaea13SPaulo Zanoni }
20643eaea13SPaulo Zanoni 
207b900b949SImre Deak static u32 gen6_pm_iir(struct drm_i915_private *dev_priv)
208b900b949SImre Deak {
209b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IIR(2) : GEN6_PMIIR;
210b900b949SImre Deak }
211b900b949SImre Deak 
212a72fbc3aSImre Deak static u32 gen6_pm_imr(struct drm_i915_private *dev_priv)
213a72fbc3aSImre Deak {
214a72fbc3aSImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IMR(2) : GEN6_PMIMR;
215a72fbc3aSImre Deak }
216a72fbc3aSImre Deak 
217b900b949SImre Deak static u32 gen6_pm_ier(struct drm_i915_private *dev_priv)
218b900b949SImre Deak {
219b900b949SImre Deak 	return INTEL_INFO(dev_priv)->gen >= 8 ? GEN8_GT_IER(2) : GEN6_PMIER;
220b900b949SImre Deak }
221b900b949SImre Deak 
222edbfdb45SPaulo Zanoni /**
223edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
224edbfdb45SPaulo Zanoni   * @dev_priv: driver private
225edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
226edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
227edbfdb45SPaulo Zanoni   */
228edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
229edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
230edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
231edbfdb45SPaulo Zanoni {
232605cd25bSPaulo Zanoni 	uint32_t new_val;
233edbfdb45SPaulo Zanoni 
23415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
23515a17aaeSDaniel Vetter 
236edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
237edbfdb45SPaulo Zanoni 
238605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
239f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
240f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
241f52ecbcfSPaulo Zanoni 
242605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
243605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
244a72fbc3aSImre Deak 		I915_WRITE(gen6_pm_imr(dev_priv), dev_priv->pm_irq_mask);
245a72fbc3aSImre Deak 		POSTING_READ(gen6_pm_imr(dev_priv));
246edbfdb45SPaulo Zanoni 	}
247f52ecbcfSPaulo Zanoni }
248edbfdb45SPaulo Zanoni 
249480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
250edbfdb45SPaulo Zanoni {
2519939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2529939fba2SImre Deak 		return;
2539939fba2SImre Deak 
254edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
255edbfdb45SPaulo Zanoni }
256edbfdb45SPaulo Zanoni 
2579939fba2SImre Deak static void __gen6_disable_pm_irq(struct drm_i915_private *dev_priv,
2589939fba2SImre Deak 				  uint32_t mask)
2599939fba2SImre Deak {
2609939fba2SImre Deak 	snb_update_pm_irq(dev_priv, mask, 0);
2619939fba2SImre Deak }
2629939fba2SImre Deak 
263480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
264edbfdb45SPaulo Zanoni {
2659939fba2SImre Deak 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2669939fba2SImre Deak 		return;
2679939fba2SImre Deak 
2689939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, mask);
269edbfdb45SPaulo Zanoni }
270edbfdb45SPaulo Zanoni 
2713cc134e3SImre Deak void gen6_reset_rps_interrupts(struct drm_device *dev)
2723cc134e3SImre Deak {
2733cc134e3SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
2743cc134e3SImre Deak 	uint32_t reg = gen6_pm_iir(dev_priv);
2753cc134e3SImre Deak 
2763cc134e3SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
2773cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2783cc134e3SImre Deak 	I915_WRITE(reg, dev_priv->pm_rps_events);
2793cc134e3SImre Deak 	POSTING_READ(reg);
280*096fad9eSImre Deak 	dev_priv->rps.pm_iir = 0;
2813cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
2823cc134e3SImre Deak }
2833cc134e3SImre Deak 
284b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
285b900b949SImre Deak {
286b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
287b900b949SImre Deak 
288b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
28978e68d36SImre Deak 
290b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
2913cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
292d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
29378e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
29478e68d36SImre Deak 				dev_priv->pm_rps_events);
295b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
29678e68d36SImre Deak 
297b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
298b900b949SImre Deak }
299b900b949SImre Deak 
30059d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
30159d02a1fSImre Deak {
30259d02a1fSImre Deak 	/*
303f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
30459d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
305f24eeb19SImre Deak 	 *
306f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
30759d02a1fSImre Deak 	 */
30859d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
30959d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
31059d02a1fSImre Deak 
31159d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
31259d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
31359d02a1fSImre Deak 
31459d02a1fSImre Deak 	return mask;
31559d02a1fSImre Deak }
31659d02a1fSImre Deak 
317b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
318b900b949SImre Deak {
319b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
320b900b949SImre Deak 
321d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
322d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
323d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
324d4d70aa5SImre Deak 
325d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
326d4d70aa5SImre Deak 
3279939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3289939fba2SImre Deak 
32959d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3309939fba2SImre Deak 
3319939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
332b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
333b900b949SImre Deak 				~dev_priv->pm_rps_events);
33458072ccbSImre Deak 
33558072ccbSImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
33658072ccbSImre Deak 
33758072ccbSImre Deak 	synchronize_irq(dev->irq);
338b900b949SImre Deak }
339b900b949SImre Deak 
3400961021aSBen Widawsky /**
341fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
342fee884edSDaniel Vetter  * @dev_priv: driver private
343fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
344fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
345fee884edSDaniel Vetter  */
34647339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
347fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
348fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
349fee884edSDaniel Vetter {
350fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
351fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
352fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
353fee884edSDaniel Vetter 
35415a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
35515a17aaeSDaniel Vetter 
356fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
357fee884edSDaniel Vetter 
3589df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
359c67a470bSPaulo Zanoni 		return;
360c67a470bSPaulo Zanoni 
361fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
362fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
363fee884edSDaniel Vetter }
3648664281bSPaulo Zanoni 
365b5ea642aSDaniel Vetter static void
366755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
367755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
3687c463586SKeith Packard {
3699db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
370755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3717c463586SKeith Packard 
372b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
373d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
374b79480baSDaniel Vetter 
37504feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
37604feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
37704feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
37804feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
379755e9019SImre Deak 		return;
380755e9019SImre Deak 
381755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
38246c06a30SVille Syrjälä 		return;
38346c06a30SVille Syrjälä 
38491d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
38591d181ddSImre Deak 
3867c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
387755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
38846c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3893143a2bfSChris Wilson 	POSTING_READ(reg);
3907c463586SKeith Packard }
3917c463586SKeith Packard 
392b5ea642aSDaniel Vetter static void
393755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
394755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
3957c463586SKeith Packard {
3969db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
397755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3987c463586SKeith Packard 
399b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
400d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
401b79480baSDaniel Vetter 
40204feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
40304feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
40404feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
40504feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
40646c06a30SVille Syrjälä 		return;
40746c06a30SVille Syrjälä 
408755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
409755e9019SImre Deak 		return;
410755e9019SImre Deak 
41191d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
41291d181ddSImre Deak 
413755e9019SImre Deak 	pipestat &= ~enable_mask;
41446c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4153143a2bfSChris Wilson 	POSTING_READ(reg);
4167c463586SKeith Packard }
4177c463586SKeith Packard 
41810c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
41910c59c51SImre Deak {
42010c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
42110c59c51SImre Deak 
42210c59c51SImre Deak 	/*
423724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
424724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
42510c59c51SImre Deak 	 */
42610c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
42710c59c51SImre Deak 		return 0;
428724a6905SVille Syrjälä 	/*
429724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
430724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
431724a6905SVille Syrjälä 	 */
432724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
433724a6905SVille Syrjälä 		return 0;
43410c59c51SImre Deak 
43510c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
43610c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
43710c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
43810c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
43910c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
44010c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
44110c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
44210c59c51SImre Deak 
44310c59c51SImre Deak 	return enable_mask;
44410c59c51SImre Deak }
44510c59c51SImre Deak 
446755e9019SImre Deak void
447755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
448755e9019SImre Deak 		     u32 status_mask)
449755e9019SImre Deak {
450755e9019SImre Deak 	u32 enable_mask;
451755e9019SImre Deak 
45210c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
45310c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
45410c59c51SImre Deak 							   status_mask);
45510c59c51SImre Deak 	else
456755e9019SImre Deak 		enable_mask = status_mask << 16;
457755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
458755e9019SImre Deak }
459755e9019SImre Deak 
460755e9019SImre Deak void
461755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
462755e9019SImre Deak 		      u32 status_mask)
463755e9019SImre Deak {
464755e9019SImre Deak 	u32 enable_mask;
465755e9019SImre Deak 
46610c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
46710c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
46810c59c51SImre Deak 							   status_mask);
46910c59c51SImre Deak 	else
470755e9019SImre Deak 		enable_mask = status_mask << 16;
471755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
472755e9019SImre Deak }
473755e9019SImre Deak 
474c0e09200SDave Airlie /**
475f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
47601c66889SZhao Yakui  */
477f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
47801c66889SZhao Yakui {
4792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4801ec14ad3SChris Wilson 
481f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
482f49e38ddSJani Nikula 		return;
483f49e38ddSJani Nikula 
48413321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
48501c66889SZhao Yakui 
486755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
487a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4883b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
489755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
4901ec14ad3SChris Wilson 
49113321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
49201c66889SZhao Yakui }
49301c66889SZhao Yakui 
494f75f3746SVille Syrjälä /*
495f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
496f75f3746SVille Syrjälä  * around the vertical blanking period.
497f75f3746SVille Syrjälä  *
498f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
499f75f3746SVille Syrjälä  *  vblank_start >= 3
500f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
501f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
502f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
503f75f3746SVille Syrjälä  *
504f75f3746SVille Syrjälä  *           start of vblank:
505f75f3746SVille Syrjälä  *           latch double buffered registers
506f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
507f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
508f75f3746SVille Syrjälä  *           |
509f75f3746SVille Syrjälä  *           |          frame start:
510f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
511f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
512f75f3746SVille Syrjälä  *           |          |
513f75f3746SVille Syrjälä  *           |          |  start of vsync:
514f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
515f75f3746SVille Syrjälä  *           |          |  |
516f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
517f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
518f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
519f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
520f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
521f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
522f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
523f75f3746SVille Syrjälä  *       |          |                                         |
524f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
525f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
526f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
527f75f3746SVille Syrjälä  *
528f75f3746SVille Syrjälä  * x  = horizontal active
529f75f3746SVille Syrjälä  * _  = horizontal blanking
530f75f3746SVille Syrjälä  * hs = horizontal sync
531f75f3746SVille Syrjälä  * va = vertical active
532f75f3746SVille Syrjälä  * vb = vertical blanking
533f75f3746SVille Syrjälä  * vs = vertical sync
534f75f3746SVille Syrjälä  * vbs = vblank_start (number)
535f75f3746SVille Syrjälä  *
536f75f3746SVille Syrjälä  * Summary:
537f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
538f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
539f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
540f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
541f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
542f75f3746SVille Syrjälä  */
543f75f3746SVille Syrjälä 
5444cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5454cdb83ecSVille Syrjälä {
5464cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5474cdb83ecSVille Syrjälä 	return 0;
5484cdb83ecSVille Syrjälä }
5494cdb83ecSVille Syrjälä 
55042f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
55142f52ef8SKeith Packard  * we use as a pipe index
55242f52ef8SKeith Packard  */
553f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5540a3e67a4SJesse Barnes {
5552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5560a3e67a4SJesse Barnes 	unsigned long high_frame;
5570a3e67a4SJesse Barnes 	unsigned long low_frame;
5580b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
559391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
560391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
561391f75e2SVille Syrjälä 	const struct drm_display_mode *mode =
5626e3c9717SAnder Conselvan de Oliveira 		&intel_crtc->config->base.adjusted_mode;
563391f75e2SVille Syrjälä 
5640b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
5650b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
5660b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
5670b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5680b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
569391f75e2SVille Syrjälä 
5700b2a8e09SVille Syrjälä 	/* Convert to pixel count */
5710b2a8e09SVille Syrjälä 	vbl_start *= htotal;
5720b2a8e09SVille Syrjälä 
5730b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
5740b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
5750b2a8e09SVille Syrjälä 
5769db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5779db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5785eddb70bSChris Wilson 
5790a3e67a4SJesse Barnes 	/*
5800a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5810a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5820a3e67a4SJesse Barnes 	 * register.
5830a3e67a4SJesse Barnes 	 */
5840a3e67a4SJesse Barnes 	do {
5855eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
586391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5875eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5880a3e67a4SJesse Barnes 	} while (high1 != high2);
5890a3e67a4SJesse Barnes 
5905eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
591391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5925eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
593391f75e2SVille Syrjälä 
594391f75e2SVille Syrjälä 	/*
595391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
596391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
597391f75e2SVille Syrjälä 	 * counter against vblank start.
598391f75e2SVille Syrjälä 	 */
599edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6000a3e67a4SJesse Barnes }
6010a3e67a4SJesse Barnes 
602f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6039880b7a5SJesse Barnes {
6042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6059db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6069880b7a5SJesse Barnes 
6079880b7a5SJesse Barnes 	return I915_READ(reg);
6089880b7a5SJesse Barnes }
6099880b7a5SJesse Barnes 
610ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
611ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
612ad3543edSMario Kleiner 
613a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
614a225f079SVille Syrjälä {
615a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
616a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
6176e3c9717SAnder Conselvan de Oliveira 	const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
618a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
61980715b2fSVille Syrjälä 	int position, vtotal;
620a225f079SVille Syrjälä 
62180715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
622a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
623a225f079SVille Syrjälä 		vtotal /= 2;
624a225f079SVille Syrjälä 
625a225f079SVille Syrjälä 	if (IS_GEN2(dev))
626a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
627a225f079SVille Syrjälä 	else
628a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
629a225f079SVille Syrjälä 
630a225f079SVille Syrjälä 	/*
63180715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
63280715b2fSVille Syrjälä 	 * scanline_offset adjustment.
633a225f079SVille Syrjälä 	 */
63480715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
635a225f079SVille Syrjälä }
636a225f079SVille Syrjälä 
637f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
638abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
639abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6400af7e4dfSMario Kleiner {
641c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
642c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
643c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6446e3c9717SAnder Conselvan de Oliveira 	const struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
6453aa18df8SVille Syrjälä 	int position;
64678e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
6470af7e4dfSMario Kleiner 	bool in_vbl = true;
6480af7e4dfSMario Kleiner 	int ret = 0;
649ad3543edSMario Kleiner 	unsigned long irqflags;
6500af7e4dfSMario Kleiner 
651c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6520af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6539db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6540af7e4dfSMario Kleiner 		return 0;
6550af7e4dfSMario Kleiner 	}
6560af7e4dfSMario Kleiner 
657c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
65878e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
659c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
660c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
661c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6620af7e4dfSMario Kleiner 
663d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
664d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
665d31faf65SVille Syrjälä 		vbl_end /= 2;
666d31faf65SVille Syrjälä 		vtotal /= 2;
667d31faf65SVille Syrjälä 	}
668d31faf65SVille Syrjälä 
669c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
670c2baf4b7SVille Syrjälä 
671ad3543edSMario Kleiner 	/*
672ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
673ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
674ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
675ad3543edSMario Kleiner 	 */
676ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
677ad3543edSMario Kleiner 
678ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
679ad3543edSMario Kleiner 
680ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
681ad3543edSMario Kleiner 	if (stime)
682ad3543edSMario Kleiner 		*stime = ktime_get();
683ad3543edSMario Kleiner 
6847c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6850af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6860af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6870af7e4dfSMario Kleiner 		 */
688a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
6890af7e4dfSMario Kleiner 	} else {
6900af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6910af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6920af7e4dfSMario Kleiner 		 * scanout position.
6930af7e4dfSMario Kleiner 		 */
694ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
6950af7e4dfSMario Kleiner 
6963aa18df8SVille Syrjälä 		/* convert to pixel counts */
6973aa18df8SVille Syrjälä 		vbl_start *= htotal;
6983aa18df8SVille Syrjälä 		vbl_end *= htotal;
6993aa18df8SVille Syrjälä 		vtotal *= htotal;
70078e8fc6bSVille Syrjälä 
70178e8fc6bSVille Syrjälä 		/*
7027e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7037e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7047e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7057e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7067e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7077e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7087e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7097e78f1cbSVille Syrjälä 		 */
7107e78f1cbSVille Syrjälä 		if (position >= vtotal)
7117e78f1cbSVille Syrjälä 			position = vtotal - 1;
7127e78f1cbSVille Syrjälä 
7137e78f1cbSVille Syrjälä 		/*
71478e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
71578e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
71678e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
71778e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
71878e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
71978e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
72078e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
72178e8fc6bSVille Syrjälä 		 */
72278e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7233aa18df8SVille Syrjälä 	}
7243aa18df8SVille Syrjälä 
725ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
726ad3543edSMario Kleiner 	if (etime)
727ad3543edSMario Kleiner 		*etime = ktime_get();
728ad3543edSMario Kleiner 
729ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
730ad3543edSMario Kleiner 
731ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
732ad3543edSMario Kleiner 
7333aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7343aa18df8SVille Syrjälä 
7353aa18df8SVille Syrjälä 	/*
7363aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7373aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7383aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7393aa18df8SVille Syrjälä 	 * up since vbl_end.
7403aa18df8SVille Syrjälä 	 */
7413aa18df8SVille Syrjälä 	if (position >= vbl_start)
7423aa18df8SVille Syrjälä 		position -= vbl_end;
7433aa18df8SVille Syrjälä 	else
7443aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7453aa18df8SVille Syrjälä 
7467c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7473aa18df8SVille Syrjälä 		*vpos = position;
7483aa18df8SVille Syrjälä 		*hpos = 0;
7493aa18df8SVille Syrjälä 	} else {
7500af7e4dfSMario Kleiner 		*vpos = position / htotal;
7510af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7520af7e4dfSMario Kleiner 	}
7530af7e4dfSMario Kleiner 
7540af7e4dfSMario Kleiner 	/* In vblank? */
7550af7e4dfSMario Kleiner 	if (in_vbl)
7563d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
7570af7e4dfSMario Kleiner 
7580af7e4dfSMario Kleiner 	return ret;
7590af7e4dfSMario Kleiner }
7600af7e4dfSMario Kleiner 
761a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
762a225f079SVille Syrjälä {
763a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
764a225f079SVille Syrjälä 	unsigned long irqflags;
765a225f079SVille Syrjälä 	int position;
766a225f079SVille Syrjälä 
767a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
768a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
769a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
770a225f079SVille Syrjälä 
771a225f079SVille Syrjälä 	return position;
772a225f079SVille Syrjälä }
773a225f079SVille Syrjälä 
774f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7750af7e4dfSMario Kleiner 			      int *max_error,
7760af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7770af7e4dfSMario Kleiner 			      unsigned flags)
7780af7e4dfSMario Kleiner {
7794041b853SChris Wilson 	struct drm_crtc *crtc;
7800af7e4dfSMario Kleiner 
7817eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7824041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7830af7e4dfSMario Kleiner 		return -EINVAL;
7840af7e4dfSMario Kleiner 	}
7850af7e4dfSMario Kleiner 
7860af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7874041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7884041b853SChris Wilson 	if (crtc == NULL) {
7894041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7904041b853SChris Wilson 		return -EINVAL;
7914041b853SChris Wilson 	}
7924041b853SChris Wilson 
79383d65738SMatt Roper 	if (!crtc->state->enable) {
7944041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7954041b853SChris Wilson 		return -EBUSY;
7964041b853SChris Wilson 	}
7970af7e4dfSMario Kleiner 
7980af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
7994041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8004041b853SChris Wilson 						     vblank_time, flags,
8017da903efSVille Syrjälä 						     crtc,
8026e3c9717SAnder Conselvan de Oliveira 						     &to_intel_crtc(crtc)->config->base.adjusted_mode);
8030af7e4dfSMario Kleiner }
8040af7e4dfSMario Kleiner 
80567c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
80667c347ffSJani Nikula 				struct drm_connector *connector)
807321a1b30SEgbert Eich {
808321a1b30SEgbert Eich 	enum drm_connector_status old_status;
809321a1b30SEgbert Eich 
810321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
811321a1b30SEgbert Eich 	old_status = connector->status;
812321a1b30SEgbert Eich 
813321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
81467c347ffSJani Nikula 	if (old_status == connector->status)
81567c347ffSJani Nikula 		return false;
81667c347ffSJani Nikula 
81767c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
818321a1b30SEgbert Eich 		      connector->base.id,
819c23cc417SJani Nikula 		      connector->name,
82067c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
82167c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
82267c347ffSJani Nikula 
82367c347ffSJani Nikula 	return true;
824321a1b30SEgbert Eich }
825321a1b30SEgbert Eich 
82613cf5504SDave Airlie static void i915_digport_work_func(struct work_struct *work)
82713cf5504SDave Airlie {
82813cf5504SDave Airlie 	struct drm_i915_private *dev_priv =
82913cf5504SDave Airlie 		container_of(work, struct drm_i915_private, dig_port_work);
83013cf5504SDave Airlie 	u32 long_port_mask, short_port_mask;
83113cf5504SDave Airlie 	struct intel_digital_port *intel_dig_port;
832b2c5c181SDaniel Vetter 	int i;
83313cf5504SDave Airlie 	u32 old_bits = 0;
83413cf5504SDave Airlie 
8354cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
83613cf5504SDave Airlie 	long_port_mask = dev_priv->long_hpd_port_mask;
83713cf5504SDave Airlie 	dev_priv->long_hpd_port_mask = 0;
83813cf5504SDave Airlie 	short_port_mask = dev_priv->short_hpd_port_mask;
83913cf5504SDave Airlie 	dev_priv->short_hpd_port_mask = 0;
8404cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
84113cf5504SDave Airlie 
84213cf5504SDave Airlie 	for (i = 0; i < I915_MAX_PORTS; i++) {
84313cf5504SDave Airlie 		bool valid = false;
84413cf5504SDave Airlie 		bool long_hpd = false;
84513cf5504SDave Airlie 		intel_dig_port = dev_priv->hpd_irq_port[i];
84613cf5504SDave Airlie 		if (!intel_dig_port || !intel_dig_port->hpd_pulse)
84713cf5504SDave Airlie 			continue;
84813cf5504SDave Airlie 
84913cf5504SDave Airlie 		if (long_port_mask & (1 << i))  {
85013cf5504SDave Airlie 			valid = true;
85113cf5504SDave Airlie 			long_hpd = true;
85213cf5504SDave Airlie 		} else if (short_port_mask & (1 << i))
85313cf5504SDave Airlie 			valid = true;
85413cf5504SDave Airlie 
85513cf5504SDave Airlie 		if (valid) {
856b2c5c181SDaniel Vetter 			enum irqreturn ret;
857b2c5c181SDaniel Vetter 
85813cf5504SDave Airlie 			ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
859b2c5c181SDaniel Vetter 			if (ret == IRQ_NONE) {
860b2c5c181SDaniel Vetter 				/* fall back to old school hpd */
86113cf5504SDave Airlie 				old_bits |= (1 << intel_dig_port->base.hpd_pin);
86213cf5504SDave Airlie 			}
86313cf5504SDave Airlie 		}
86413cf5504SDave Airlie 	}
86513cf5504SDave Airlie 
86613cf5504SDave Airlie 	if (old_bits) {
8674cb21832SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
86813cf5504SDave Airlie 		dev_priv->hpd_event_bits |= old_bits;
8694cb21832SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
87013cf5504SDave Airlie 		schedule_work(&dev_priv->hotplug_work);
87113cf5504SDave Airlie 	}
87213cf5504SDave Airlie }
87313cf5504SDave Airlie 
8745ca58282SJesse Barnes /*
8755ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8765ca58282SJesse Barnes  */
877ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
878ac4c16c5SEgbert Eich 
8795ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8805ca58282SJesse Barnes {
8812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
8822d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
8835ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
884c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
885cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
886cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
887cd569aedSEgbert Eich 	struct drm_connector *connector;
888cd569aedSEgbert Eich 	bool hpd_disabled = false;
889321a1b30SEgbert Eich 	bool changed = false;
890142e2398SEgbert Eich 	u32 hpd_event_bits;
8915ca58282SJesse Barnes 
892a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
893e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
894e67189abSJesse Barnes 
8954cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
896142e2398SEgbert Eich 
897142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
898142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
899cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
900cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
90136cd7444SDave Airlie 		if (!intel_connector->encoder)
90236cd7444SDave Airlie 			continue;
903cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
904cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
905cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
906cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
907cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
908cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
909c23cc417SJani Nikula 				connector->name);
910cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
911cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
912cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
913cd569aedSEgbert Eich 			hpd_disabled = true;
914cd569aedSEgbert Eich 		}
915142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
916142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
917c23cc417SJani Nikula 				      connector->name, intel_encoder->hpd_pin);
918142e2398SEgbert Eich 		}
919cd569aedSEgbert Eich 	}
920cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
921cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
922cd569aedSEgbert Eich 	  * some connectors */
923ac4c16c5SEgbert Eich 	if (hpd_disabled) {
924cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
9256323751dSImre Deak 		mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
9266323751dSImre Deak 				 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
927ac4c16c5SEgbert Eich 	}
928cd569aedSEgbert Eich 
9294cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
930cd569aedSEgbert Eich 
931321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
932321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
93336cd7444SDave Airlie 		if (!intel_connector->encoder)
93436cd7444SDave Airlie 			continue;
935321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
936321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
937cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
938cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
939321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
940321a1b30SEgbert Eich 				changed = true;
941321a1b30SEgbert Eich 		}
942321a1b30SEgbert Eich 	}
94340ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
94440ee3381SKeith Packard 
945321a1b30SEgbert Eich 	if (changed)
946321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9475ca58282SJesse Barnes }
9485ca58282SJesse Barnes 
949d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
950f97108d1SJesse Barnes {
9512d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
952b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9539270388eSDaniel Vetter 	u8 new_delay;
9549270388eSDaniel Vetter 
955d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
956f97108d1SJesse Barnes 
95773edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
95873edd18fSDaniel Vetter 
95920e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9609270388eSDaniel Vetter 
9617648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
962b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
963b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
964f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
965f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
966f97108d1SJesse Barnes 
967f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
968b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
96920e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
97020e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
97120e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
97220e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
973b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
97420e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
97520e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
97620e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
97720e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
978f97108d1SJesse Barnes 	}
979f97108d1SJesse Barnes 
9807648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
98120e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
982f97108d1SJesse Barnes 
983d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9849270388eSDaniel Vetter 
985f97108d1SJesse Barnes 	return;
986f97108d1SJesse Barnes }
987f97108d1SJesse Barnes 
988549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
989a4872ba6SOscar Mateo 			struct intel_engine_cs *ring)
990549f7365SChris Wilson {
99193b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
992475553deSChris Wilson 		return;
993475553deSChris Wilson 
994bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9959862e600SChris Wilson 
996549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
997549f7365SChris Wilson }
998549f7365SChris Wilson 
99943cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
100043cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
100131685c25SDeepak S {
100243cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
100343cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
100443cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
100531685c25SDeepak S }
100631685c25SDeepak S 
100743cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
100843cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
100943cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
101043cf3bf0SChris Wilson 			 int threshold)
101131685c25SDeepak S {
101243cf3bf0SChris Wilson 	u64 time, c0;
101331685c25SDeepak S 
101443cf3bf0SChris Wilson 	if (old->cz_clock == 0)
101543cf3bf0SChris Wilson 		return false;
101631685c25SDeepak S 
101743cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
101843cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
101931685c25SDeepak S 
102043cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
102143cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
102243cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
102343cf3bf0SChris Wilson 	 */
102443cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
102543cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
102643cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
102731685c25SDeepak S 
102843cf3bf0SChris Wilson 	return c0 >= time;
102931685c25SDeepak S }
103031685c25SDeepak S 
103143cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
103243cf3bf0SChris Wilson {
103343cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
103443cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
103543cf3bf0SChris Wilson }
103643cf3bf0SChris Wilson 
103743cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
103843cf3bf0SChris Wilson {
103943cf3bf0SChris Wilson 	struct intel_rps_ei now;
104043cf3bf0SChris Wilson 	u32 events = 0;
104143cf3bf0SChris Wilson 
10426f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
104343cf3bf0SChris Wilson 		return 0;
104443cf3bf0SChris Wilson 
104543cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
104643cf3bf0SChris Wilson 	if (now.cz_clock == 0)
104743cf3bf0SChris Wilson 		return 0;
104831685c25SDeepak S 
104943cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
105043cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
105143cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
105243cf3bf0SChris Wilson 				  VLV_RP_DOWN_EI_THRESHOLD))
105343cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
105443cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
105531685c25SDeepak S 	}
105631685c25SDeepak S 
105743cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
105843cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
105943cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
106043cf3bf0SChris Wilson 				 VLV_RP_UP_EI_THRESHOLD))
106143cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
106243cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
106343cf3bf0SChris Wilson 	}
106443cf3bf0SChris Wilson 
106543cf3bf0SChris Wilson 	return events;
106631685c25SDeepak S }
106731685c25SDeepak S 
10684912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10693b8d8d91SJesse Barnes {
10702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10712d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1072edbfdb45SPaulo Zanoni 	u32 pm_iir;
1073dd75fdc8SChris Wilson 	int new_delay, adj;
10743b8d8d91SJesse Barnes 
107559cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1076d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1077d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1078d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1079d4d70aa5SImre Deak 		return;
1080d4d70aa5SImre Deak 	}
1081c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1082c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1083a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1084480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
108559cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10864912d041SBen Widawsky 
108760611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1088a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
108960611c13SPaulo Zanoni 
1090a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
10913b8d8d91SJesse Barnes 		return;
10923b8d8d91SJesse Barnes 
10934fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10947b9e0ae6SChris Wilson 
109543cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
109643cf3bf0SChris Wilson 
1097dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
10987425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1099dd75fdc8SChris Wilson 		if (adj > 0)
1100dd75fdc8SChris Wilson 			adj *= 2;
110113a5660cSDeepak S 		else {
110213a5660cSDeepak S 			/* CHV needs even encode values */
110313a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
110413a5660cSDeepak S 		}
1105b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11067425034aSVille Syrjälä 
11077425034aSVille Syrjälä 		/*
11087425034aSVille Syrjälä 		 * For better performance, jump directly
11097425034aSVille Syrjälä 		 * to RPe if we're below it.
11107425034aSVille Syrjälä 		 */
1111b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1112b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1113dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1114b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1115b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1116dd75fdc8SChris Wilson 		else
1117b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1118dd75fdc8SChris Wilson 		adj = 0;
1119dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1120dd75fdc8SChris Wilson 		if (adj < 0)
1121dd75fdc8SChris Wilson 			adj *= 2;
112213a5660cSDeepak S 		else {
112313a5660cSDeepak S 			/* CHV needs even encode values */
112413a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
112513a5660cSDeepak S 		}
1126b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1127dd75fdc8SChris Wilson 	} else { /* unknown event */
1128b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1129dd75fdc8SChris Wilson 	}
11303b8d8d91SJesse Barnes 
113179249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
113279249636SBen Widawsky 	 * interrupt
113379249636SBen Widawsky 	 */
11341272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1135b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1136b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
113727544369SDeepak S 
1138b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1139dd75fdc8SChris Wilson 
1140ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11413b8d8d91SJesse Barnes 
11424fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11433b8d8d91SJesse Barnes }
11443b8d8d91SJesse Barnes 
1145e3689190SBen Widawsky 
1146e3689190SBen Widawsky /**
1147e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1148e3689190SBen Widawsky  * occurred.
1149e3689190SBen Widawsky  * @work: workqueue struct
1150e3689190SBen Widawsky  *
1151e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1152e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1153e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1154e3689190SBen Widawsky  */
1155e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1156e3689190SBen Widawsky {
11572d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11582d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1159e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
116035a85ac6SBen Widawsky 	char *parity_event[6];
1161e3689190SBen Widawsky 	uint32_t misccpctl;
116235a85ac6SBen Widawsky 	uint8_t slice = 0;
1163e3689190SBen Widawsky 
1164e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1165e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1166e3689190SBen Widawsky 	 * any time we access those registers.
1167e3689190SBen Widawsky 	 */
1168e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1169e3689190SBen Widawsky 
117035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
117135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
117235a85ac6SBen Widawsky 		goto out;
117335a85ac6SBen Widawsky 
1174e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1175e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1176e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1177e3689190SBen Widawsky 
117835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
117935a85ac6SBen Widawsky 		u32 reg;
118035a85ac6SBen Widawsky 
118135a85ac6SBen Widawsky 		slice--;
118235a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
118335a85ac6SBen Widawsky 			break;
118435a85ac6SBen Widawsky 
118535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
118635a85ac6SBen Widawsky 
118735a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
118835a85ac6SBen Widawsky 
118935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1190e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1191e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1192e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1193e3689190SBen Widawsky 
119435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
119535a85ac6SBen Widawsky 		POSTING_READ(reg);
1196e3689190SBen Widawsky 
1197cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1198e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1199e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1200e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
120135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
120235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1203e3689190SBen Widawsky 
12045bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1205e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1206e3689190SBen Widawsky 
120735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
120835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1209e3689190SBen Widawsky 
121035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1211e3689190SBen Widawsky 		kfree(parity_event[3]);
1212e3689190SBen Widawsky 		kfree(parity_event[2]);
1213e3689190SBen Widawsky 		kfree(parity_event[1]);
1214e3689190SBen Widawsky 	}
1215e3689190SBen Widawsky 
121635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
121735a85ac6SBen Widawsky 
121835a85ac6SBen Widawsky out:
121935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12204cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1221480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12224cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
122335a85ac6SBen Widawsky 
122435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
122535a85ac6SBen Widawsky }
122635a85ac6SBen Widawsky 
122735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1228e3689190SBen Widawsky {
12292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1230e3689190SBen Widawsky 
1231040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1232e3689190SBen Widawsky 		return;
1233e3689190SBen Widawsky 
1234d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1235480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1236d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1237e3689190SBen Widawsky 
123835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
123935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
124035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
124135a85ac6SBen Widawsky 
124235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
124335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
124435a85ac6SBen Widawsky 
1245a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1246e3689190SBen Widawsky }
1247e3689190SBen Widawsky 
1248f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1249f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1250f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1251f1af8fc1SPaulo Zanoni {
1252f1af8fc1SPaulo Zanoni 	if (gt_iir &
1253f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1254f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1255f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1256f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1257f1af8fc1SPaulo Zanoni }
1258f1af8fc1SPaulo Zanoni 
1259e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1260e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1261e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1262e7b4c6b1SDaniel Vetter {
1263e7b4c6b1SDaniel Vetter 
1264cc609d5dSBen Widawsky 	if (gt_iir &
1265cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1266e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1267cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1268e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1269cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1270e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1271e7b4c6b1SDaniel Vetter 
1272cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1273cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1274aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1275aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1276e3689190SBen Widawsky 
127735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
127835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1279e7b4c6b1SDaniel Vetter }
1280e7b4c6b1SDaniel Vetter 
1281abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1282abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1283abd58f01SBen Widawsky 				       u32 master_ctl)
1284abd58f01SBen Widawsky {
1285e981e7b1SThomas Daniel 	struct intel_engine_cs *ring;
1286abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1287abd58f01SBen Widawsky 	uint32_t tmp = 0;
1288abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1289abd58f01SBen Widawsky 
1290abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1291abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1292abd58f01SBen Widawsky 		if (tmp) {
129338cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1294abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1295e981e7b1SThomas Daniel 
1296abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1297e981e7b1SThomas Daniel 			ring = &dev_priv->ring[RCS];
1298abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1299e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1300e981e7b1SThomas Daniel 			if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
13013f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1302e981e7b1SThomas Daniel 
1303e981e7b1SThomas Daniel 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1304e981e7b1SThomas Daniel 			ring = &dev_priv->ring[BCS];
1305abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1306e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1307e981e7b1SThomas Daniel 			if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
13083f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1309abd58f01SBen Widawsky 		} else
1310abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1311abd58f01SBen Widawsky 	}
1312abd58f01SBen Widawsky 
131385f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1314abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1315abd58f01SBen Widawsky 		if (tmp) {
131638cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1317abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1318e981e7b1SThomas Daniel 
1319abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1320e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS];
1321abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1322e981e7b1SThomas Daniel 				notify_ring(dev, ring);
132373d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
13243f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1325e981e7b1SThomas Daniel 
132685f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
1327e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS2];
132885f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
1329e981e7b1SThomas Daniel 				notify_ring(dev, ring);
133073d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
13313f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1332abd58f01SBen Widawsky 		} else
1333abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1334abd58f01SBen Widawsky 	}
1335abd58f01SBen Widawsky 
13360961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
13370961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
13380961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
13390961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
13400961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
134138cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1342c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13430961021aSBen Widawsky 		} else
13440961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13450961021aSBen Widawsky 	}
13460961021aSBen Widawsky 
1347abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1348abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1349abd58f01SBen Widawsky 		if (tmp) {
135038cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1351abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1352e981e7b1SThomas Daniel 
1353abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1354e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VECS];
1355abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1356e981e7b1SThomas Daniel 				notify_ring(dev, ring);
135773d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
13583f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1359abd58f01SBen Widawsky 		} else
1360abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1361abd58f01SBen Widawsky 	}
1362abd58f01SBen Widawsky 
1363abd58f01SBen Widawsky 	return ret;
1364abd58f01SBen Widawsky }
1365abd58f01SBen Widawsky 
1366b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1367b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1368b543fb04SEgbert Eich 
136907c338ceSJani Nikula static int pch_port_to_hotplug_shift(enum port port)
137013cf5504SDave Airlie {
137113cf5504SDave Airlie 	switch (port) {
137213cf5504SDave Airlie 	case PORT_A:
137313cf5504SDave Airlie 	case PORT_E:
137413cf5504SDave Airlie 	default:
137513cf5504SDave Airlie 		return -1;
137613cf5504SDave Airlie 	case PORT_B:
137713cf5504SDave Airlie 		return 0;
137813cf5504SDave Airlie 	case PORT_C:
137913cf5504SDave Airlie 		return 8;
138013cf5504SDave Airlie 	case PORT_D:
138113cf5504SDave Airlie 		return 16;
138213cf5504SDave Airlie 	}
138313cf5504SDave Airlie }
138413cf5504SDave Airlie 
138507c338ceSJani Nikula static int i915_port_to_hotplug_shift(enum port port)
138613cf5504SDave Airlie {
138713cf5504SDave Airlie 	switch (port) {
138813cf5504SDave Airlie 	case PORT_A:
138913cf5504SDave Airlie 	case PORT_E:
139013cf5504SDave Airlie 	default:
139113cf5504SDave Airlie 		return -1;
139213cf5504SDave Airlie 	case PORT_B:
139313cf5504SDave Airlie 		return 17;
139413cf5504SDave Airlie 	case PORT_C:
139513cf5504SDave Airlie 		return 19;
139613cf5504SDave Airlie 	case PORT_D:
139713cf5504SDave Airlie 		return 21;
139813cf5504SDave Airlie 	}
139913cf5504SDave Airlie }
140013cf5504SDave Airlie 
140113cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
140213cf5504SDave Airlie {
140313cf5504SDave Airlie 	switch (pin) {
140413cf5504SDave Airlie 	case HPD_PORT_B:
140513cf5504SDave Airlie 		return PORT_B;
140613cf5504SDave Airlie 	case HPD_PORT_C:
140713cf5504SDave Airlie 		return PORT_C;
140813cf5504SDave Airlie 	case HPD_PORT_D:
140913cf5504SDave Airlie 		return PORT_D;
141013cf5504SDave Airlie 	default:
141113cf5504SDave Airlie 		return PORT_A; /* no hpd */
141213cf5504SDave Airlie 	}
141313cf5504SDave Airlie }
141413cf5504SDave Airlie 
141510a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1416b543fb04SEgbert Eich 					 u32 hotplug_trigger,
141713cf5504SDave Airlie 					 u32 dig_hotplug_reg,
14187c7e10dbSVille Syrjälä 					 const u32 hpd[HPD_NUM_PINS])
1419b543fb04SEgbert Eich {
14202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1421b543fb04SEgbert Eich 	int i;
142213cf5504SDave Airlie 	enum port port;
142310a504deSDaniel Vetter 	bool storm_detected = false;
142413cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
142513cf5504SDave Airlie 	u32 dig_shift;
142613cf5504SDave Airlie 	u32 dig_port_mask = 0;
1427b543fb04SEgbert Eich 
142891d131d2SDaniel Vetter 	if (!hotplug_trigger)
142991d131d2SDaniel Vetter 		return;
143091d131d2SDaniel Vetter 
143113cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
143213cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1433cc9bd499SImre Deak 
1434b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1435b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
143613cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
143713cf5504SDave Airlie 			continue;
1438821450c6SEgbert Eich 
143913cf5504SDave Airlie 		port = get_port_from_pin(i);
144013cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
144113cf5504SDave Airlie 			bool long_hpd;
144213cf5504SDave Airlie 
144307c338ceSJani Nikula 			if (HAS_PCH_SPLIT(dev)) {
144407c338ceSJani Nikula 				dig_shift = pch_port_to_hotplug_shift(port);
144513cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
144607c338ceSJani Nikula 			} else {
144707c338ceSJani Nikula 				dig_shift = i915_port_to_hotplug_shift(port);
144807c338ceSJani Nikula 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
144913cf5504SDave Airlie 			}
145013cf5504SDave Airlie 
145126fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
145226fbb774SVille Syrjälä 					 port_name(port),
145326fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
145413cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
145513cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
145613cf5504SDave Airlie 			if (long_hpd) {
145713cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
145813cf5504SDave Airlie 				dig_port_mask |= hpd[i];
145913cf5504SDave Airlie 			} else {
146013cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
146113cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
146213cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
146313cf5504SDave Airlie 			}
146413cf5504SDave Airlie 			queue_dig = true;
146513cf5504SDave Airlie 		}
146613cf5504SDave Airlie 	}
146713cf5504SDave Airlie 
146813cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
14693ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
14703ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
14713ff04a16SDaniel Vetter 			/*
14723ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
14733ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
14743ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
14753ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
14763ff04a16SDaniel Vetter 			 */
14773ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1478cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1479cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1480b8f102e8SEgbert Eich 
14813ff04a16SDaniel Vetter 			continue;
14823ff04a16SDaniel Vetter 		}
14833ff04a16SDaniel Vetter 
1484b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1485b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1486b543fb04SEgbert Eich 			continue;
1487b543fb04SEgbert Eich 
148813cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1489bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
149013cf5504SDave Airlie 			queue_hp = true;
149113cf5504SDave Airlie 		}
149213cf5504SDave Airlie 
1493b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1494b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1495b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1496b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1497b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1498b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1499b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1500b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1501142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1502b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
150310a504deSDaniel Vetter 			storm_detected = true;
1504b543fb04SEgbert Eich 		} else {
1505b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1506b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1507b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1508b543fb04SEgbert Eich 		}
1509b543fb04SEgbert Eich 	}
1510b543fb04SEgbert Eich 
151110a504deSDaniel Vetter 	if (storm_detected)
151210a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1513b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
15145876fa0dSDaniel Vetter 
1515645416f5SDaniel Vetter 	/*
1516645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1517645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1518645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1519645416f5SDaniel Vetter 	 * deadlock.
1520645416f5SDaniel Vetter 	 */
152113cf5504SDave Airlie 	if (queue_dig)
15220e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
152313cf5504SDave Airlie 	if (queue_hp)
1524645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1525b543fb04SEgbert Eich }
1526b543fb04SEgbert Eich 
1527515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1528515ac2bbSDaniel Vetter {
15292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
153028c70f16SDaniel Vetter 
153128c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1532515ac2bbSDaniel Vetter }
1533515ac2bbSDaniel Vetter 
1534ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1535ce99c256SDaniel Vetter {
15362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15379ee32feaSDaniel Vetter 
15389ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1539ce99c256SDaniel Vetter }
1540ce99c256SDaniel Vetter 
15418bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1542277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1543eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1544eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15458bc5e955SDaniel Vetter 					 uint32_t crc4)
15468bf1e9f1SShuang He {
15478bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15488bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15498bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1550ac2300d4SDamien Lespiau 	int head, tail;
1551b2c88f5bSDamien Lespiau 
1552d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1553d538bbdfSDamien Lespiau 
15540c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1555d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
155634273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15570c912c79SDamien Lespiau 		return;
15580c912c79SDamien Lespiau 	}
15590c912c79SDamien Lespiau 
1560d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1561d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1562b2c88f5bSDamien Lespiau 
1563b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1564d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1565b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1566b2c88f5bSDamien Lespiau 		return;
1567b2c88f5bSDamien Lespiau 	}
1568b2c88f5bSDamien Lespiau 
1569b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15708bf1e9f1SShuang He 
15718bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1572eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1573eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1574eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1575eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1576eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1577b2c88f5bSDamien Lespiau 
1578b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1579d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1580d538bbdfSDamien Lespiau 
1581d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
158207144428SDamien Lespiau 
158307144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15848bf1e9f1SShuang He }
1585277de95eSDaniel Vetter #else
1586277de95eSDaniel Vetter static inline void
1587277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1588277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1589277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1590277de95eSDaniel Vetter 			     uint32_t crc4) {}
1591277de95eSDaniel Vetter #endif
1592eba94eb9SDaniel Vetter 
1593277de95eSDaniel Vetter 
1594277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15955a69b89fSDaniel Vetter {
15965a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15975a69b89fSDaniel Vetter 
1598277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15995a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16005a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16015a69b89fSDaniel Vetter }
16025a69b89fSDaniel Vetter 
1603277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1604eba94eb9SDaniel Vetter {
1605eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1606eba94eb9SDaniel Vetter 
1607277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1608eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1609eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1610eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1611eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16128bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1613eba94eb9SDaniel Vetter }
16145b3a856bSDaniel Vetter 
1615277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16165b3a856bSDaniel Vetter {
16175b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16180b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16190b5c5ed0SDaniel Vetter 
16200b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16210b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16220b5c5ed0SDaniel Vetter 	else
16230b5c5ed0SDaniel Vetter 		res1 = 0;
16240b5c5ed0SDaniel Vetter 
16250b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16260b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16270b5c5ed0SDaniel Vetter 	else
16280b5c5ed0SDaniel Vetter 		res2 = 0;
16295b3a856bSDaniel Vetter 
1630277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16310b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16320b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16330b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16340b5c5ed0SDaniel Vetter 				     res1, res2);
16355b3a856bSDaniel Vetter }
16368bf1e9f1SShuang He 
16371403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16381403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16391403c0d4SPaulo Zanoni  * the work queue. */
16401403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1641baf02a1fSBen Widawsky {
1642a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
164359cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1644480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1645d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1646d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16472adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
164841a05a3aSDaniel Vetter 		}
1649d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1650d4d70aa5SImre Deak 	}
1651baf02a1fSBen Widawsky 
1652c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1653c9a9a268SImre Deak 		return;
1654c9a9a268SImre Deak 
16551403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
165612638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
165712638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
165812638c57SBen Widawsky 
1659aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1660aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
166112638c57SBen Widawsky 	}
16621403c0d4SPaulo Zanoni }
1663baf02a1fSBen Widawsky 
16648d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16658d7849dbSVille Syrjälä {
16668d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16678d7849dbSVille Syrjälä 		return false;
16688d7849dbSVille Syrjälä 
16698d7849dbSVille Syrjälä 	return true;
16708d7849dbSVille Syrjälä }
16718d7849dbSVille Syrjälä 
1672c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16737e231dbeSJesse Barnes {
1674c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
167591d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16767e231dbeSJesse Barnes 	int pipe;
16777e231dbeSJesse Barnes 
167858ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1679055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
168091d181ddSImre Deak 		int reg;
1681bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
168291d181ddSImre Deak 
1683bbb5eebfSDaniel Vetter 		/*
1684bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1685bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1686bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1687bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1688bbb5eebfSDaniel Vetter 		 * handle.
1689bbb5eebfSDaniel Vetter 		 */
16900f239f4cSDaniel Vetter 
16910f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16920f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1693bbb5eebfSDaniel Vetter 
1694bbb5eebfSDaniel Vetter 		switch (pipe) {
1695bbb5eebfSDaniel Vetter 		case PIPE_A:
1696bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1697bbb5eebfSDaniel Vetter 			break;
1698bbb5eebfSDaniel Vetter 		case PIPE_B:
1699bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1700bbb5eebfSDaniel Vetter 			break;
17013278f67fSVille Syrjälä 		case PIPE_C:
17023278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17033278f67fSVille Syrjälä 			break;
1704bbb5eebfSDaniel Vetter 		}
1705bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1706bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1707bbb5eebfSDaniel Vetter 
1708bbb5eebfSDaniel Vetter 		if (!mask)
170991d181ddSImre Deak 			continue;
171091d181ddSImre Deak 
171191d181ddSImre Deak 		reg = PIPESTAT(pipe);
1712bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1713bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17147e231dbeSJesse Barnes 
17157e231dbeSJesse Barnes 		/*
17167e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17177e231dbeSJesse Barnes 		 */
171891d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
171991d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17207e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17217e231dbeSJesse Barnes 	}
172258ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17237e231dbeSJesse Barnes 
1724055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1725d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1726d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1727d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
172831acc7f5SJesse Barnes 
1729579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
173031acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
173131acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
173231acc7f5SJesse Barnes 		}
17334356d586SDaniel Vetter 
17344356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1735277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17362d9d2b0bSVille Syrjälä 
17371f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17381f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
173931acc7f5SJesse Barnes 	}
174031acc7f5SJesse Barnes 
1741c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1742c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1743c1874ed7SImre Deak }
1744c1874ed7SImre Deak 
174516c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
174616c6c56bSVille Syrjälä {
174716c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
174816c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
174916c6c56bSVille Syrjälä 
17503ff60f89SOscar Mateo 	if (hotplug_status) {
17513ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17523ff60f89SOscar Mateo 		/*
17533ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
17543ff60f89SOscar Mateo 		 * may miss hotplug events.
17553ff60f89SOscar Mateo 		 */
17563ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
17573ff60f89SOscar Mateo 
175816c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
175916c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
176016c6c56bSVille Syrjälä 
176113cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
176216c6c56bSVille Syrjälä 		} else {
176316c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
176416c6c56bSVille Syrjälä 
176513cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
176616c6c56bSVille Syrjälä 		}
176716c6c56bSVille Syrjälä 
176816c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
176916c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
177016c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
17713ff60f89SOscar Mateo 	}
177216c6c56bSVille Syrjälä }
177316c6c56bSVille Syrjälä 
1774c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1775c1874ed7SImre Deak {
177645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1778c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1779c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1780c1874ed7SImre Deak 
17812dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17822dd2a883SImre Deak 		return IRQ_NONE;
17832dd2a883SImre Deak 
1784c1874ed7SImre Deak 	while (true) {
17853ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17863ff60f89SOscar Mateo 
1787c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17883ff60f89SOscar Mateo 		if (gt_iir)
17893ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17903ff60f89SOscar Mateo 
1791c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17923ff60f89SOscar Mateo 		if (pm_iir)
17933ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17943ff60f89SOscar Mateo 
17953ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17963ff60f89SOscar Mateo 		if (iir) {
17973ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17983ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17993ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
18003ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
18013ff60f89SOscar Mateo 		}
1802c1874ed7SImre Deak 
1803c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1804c1874ed7SImre Deak 			goto out;
1805c1874ed7SImre Deak 
1806c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1807c1874ed7SImre Deak 
18083ff60f89SOscar Mateo 		if (gt_iir)
1809c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
181060611c13SPaulo Zanoni 		if (pm_iir)
1811d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18123ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18133ff60f89SOscar Mateo 		 * signalled in iir */
18143ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18157e231dbeSJesse Barnes 	}
18167e231dbeSJesse Barnes 
18177e231dbeSJesse Barnes out:
18187e231dbeSJesse Barnes 	return ret;
18197e231dbeSJesse Barnes }
18207e231dbeSJesse Barnes 
182143f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
182243f328d7SVille Syrjälä {
182345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
182443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
182543f328d7SVille Syrjälä 	u32 master_ctl, iir;
182643f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
182743f328d7SVille Syrjälä 
18282dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18292dd2a883SImre Deak 		return IRQ_NONE;
18302dd2a883SImre Deak 
18318e5fd599SVille Syrjälä 	for (;;) {
18328e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18333278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18343278f67fSVille Syrjälä 
18353278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18368e5fd599SVille Syrjälä 			break;
183743f328d7SVille Syrjälä 
183827b6c122SOscar Mateo 		ret = IRQ_HANDLED;
183927b6c122SOscar Mateo 
184043f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
184143f328d7SVille Syrjälä 
184227b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
184327b6c122SOscar Mateo 
184427b6c122SOscar Mateo 		if (iir) {
184527b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
184627b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
184727b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
184827b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
184927b6c122SOscar Mateo 		}
185027b6c122SOscar Mateo 
18513278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
185243f328d7SVille Syrjälä 
185327b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
185427b6c122SOscar Mateo 		 * signalled in iir */
18553278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
185643f328d7SVille Syrjälä 
185743f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
185843f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18598e5fd599SVille Syrjälä 	}
18603278f67fSVille Syrjälä 
186143f328d7SVille Syrjälä 	return ret;
186243f328d7SVille Syrjälä }
186343f328d7SVille Syrjälä 
186423e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1865776ad806SJesse Barnes {
18662d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
18679db4a9c7SJesse Barnes 	int pipe;
1868b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
186913cf5504SDave Airlie 	u32 dig_hotplug_reg;
1870776ad806SJesse Barnes 
187113cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
187213cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
187313cf5504SDave Airlie 
187413cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
187591d131d2SDaniel Vetter 
1876cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1877cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1878776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1879cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1880cfc33bf7SVille Syrjälä 				 port_name(port));
1881cfc33bf7SVille Syrjälä 	}
1882776ad806SJesse Barnes 
1883ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1884ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1885ce99c256SDaniel Vetter 
1886776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1887515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1888776ad806SJesse Barnes 
1889776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1890776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1891776ad806SJesse Barnes 
1892776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1893776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1894776ad806SJesse Barnes 
1895776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1896776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1897776ad806SJesse Barnes 
18989db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1899055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19009db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19019db4a9c7SJesse Barnes 					 pipe_name(pipe),
19029db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1903776ad806SJesse Barnes 
1904776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1905776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1906776ad806SJesse Barnes 
1907776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1908776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1909776ad806SJesse Barnes 
1910776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19111f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19128664281bSPaulo Zanoni 
19138664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19141f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19158664281bSPaulo Zanoni }
19168664281bSPaulo Zanoni 
19178664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19188664281bSPaulo Zanoni {
19198664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19208664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19215a69b89fSDaniel Vetter 	enum pipe pipe;
19228664281bSPaulo Zanoni 
1923de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1924de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1925de032bf4SPaulo Zanoni 
1926055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19271f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19281f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19298664281bSPaulo Zanoni 
19305a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19315a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1932277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19335a69b89fSDaniel Vetter 			else
1934277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19355a69b89fSDaniel Vetter 		}
19365a69b89fSDaniel Vetter 	}
19378bf1e9f1SShuang He 
19388664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19398664281bSPaulo Zanoni }
19408664281bSPaulo Zanoni 
19418664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19428664281bSPaulo Zanoni {
19438664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19448664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19458664281bSPaulo Zanoni 
1946de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1947de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1948de032bf4SPaulo Zanoni 
19498664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19501f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19518664281bSPaulo Zanoni 
19528664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19531f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19548664281bSPaulo Zanoni 
19558664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19561f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19578664281bSPaulo Zanoni 
19588664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1959776ad806SJesse Barnes }
1960776ad806SJesse Barnes 
196123e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
196223e81d69SAdam Jackson {
19632d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
196423e81d69SAdam Jackson 	int pipe;
1965b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
196613cf5504SDave Airlie 	u32 dig_hotplug_reg;
196723e81d69SAdam Jackson 
196813cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
196913cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
197013cf5504SDave Airlie 
197113cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
197291d131d2SDaniel Vetter 
1973cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1974cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
197523e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1976cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1977cfc33bf7SVille Syrjälä 				 port_name(port));
1978cfc33bf7SVille Syrjälä 	}
197923e81d69SAdam Jackson 
198023e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1981ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
198223e81d69SAdam Jackson 
198323e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1984515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
198523e81d69SAdam Jackson 
198623e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
198723e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
198823e81d69SAdam Jackson 
198923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
199023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
199123e81d69SAdam Jackson 
199223e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1993055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
199423e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
199523e81d69SAdam Jackson 					 pipe_name(pipe),
199623e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19978664281bSPaulo Zanoni 
19988664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19998664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
200023e81d69SAdam Jackson }
200123e81d69SAdam Jackson 
2002c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2003c008bc6eSPaulo Zanoni {
2004c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
200540da17c2SDaniel Vetter 	enum pipe pipe;
2006c008bc6eSPaulo Zanoni 
2007c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2008c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2009c008bc6eSPaulo Zanoni 
2010c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2011c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2012c008bc6eSPaulo Zanoni 
2013c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2014c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2015c008bc6eSPaulo Zanoni 
2016055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2017d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2018d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2019d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2020c008bc6eSPaulo Zanoni 
202140da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20221f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2023c008bc6eSPaulo Zanoni 
202440da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
202540da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20265b3a856bSDaniel Vetter 
202740da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
202840da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
202940da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
203040da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2031c008bc6eSPaulo Zanoni 		}
2032c008bc6eSPaulo Zanoni 	}
2033c008bc6eSPaulo Zanoni 
2034c008bc6eSPaulo Zanoni 	/* check event from PCH */
2035c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2036c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2037c008bc6eSPaulo Zanoni 
2038c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2039c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2040c008bc6eSPaulo Zanoni 		else
2041c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2042c008bc6eSPaulo Zanoni 
2043c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2044c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2045c008bc6eSPaulo Zanoni 	}
2046c008bc6eSPaulo Zanoni 
2047c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2048c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2049c008bc6eSPaulo Zanoni }
2050c008bc6eSPaulo Zanoni 
20519719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20529719fb98SPaulo Zanoni {
20539719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
205407d27e20SDamien Lespiau 	enum pipe pipe;
20559719fb98SPaulo Zanoni 
20569719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20579719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20589719fb98SPaulo Zanoni 
20599719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20609719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20619719fb98SPaulo Zanoni 
20629719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20639719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20649719fb98SPaulo Zanoni 
2065055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2066d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2067d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2068d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
206940da17c2SDaniel Vetter 
207040da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
207107d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
207207d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
207307d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20749719fb98SPaulo Zanoni 		}
20759719fb98SPaulo Zanoni 	}
20769719fb98SPaulo Zanoni 
20779719fb98SPaulo Zanoni 	/* check event from PCH */
20789719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20799719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20809719fb98SPaulo Zanoni 
20819719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20829719fb98SPaulo Zanoni 
20839719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20849719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20859719fb98SPaulo Zanoni 	}
20869719fb98SPaulo Zanoni }
20879719fb98SPaulo Zanoni 
208872c90f62SOscar Mateo /*
208972c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
209072c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
209172c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
209272c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
209372c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
209472c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
209572c90f62SOscar Mateo  */
2096f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2097b1f14ad0SJesse Barnes {
209845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2100f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21010e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2102b1f14ad0SJesse Barnes 
21032dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21042dd2a883SImre Deak 		return IRQ_NONE;
21052dd2a883SImre Deak 
21068664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21078664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2108907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21098664281bSPaulo Zanoni 
2110b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2111b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2112b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
211323a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21140e43406bSChris Wilson 
211544498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
211644498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
211744498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
211844498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
211944498aeaSPaulo Zanoni 	 * due to its back queue). */
2120ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
212144498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
212244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
212344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2124ab5c608bSBen Widawsky 	}
212544498aeaSPaulo Zanoni 
212672c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
212772c90f62SOscar Mateo 
21280e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21290e43406bSChris Wilson 	if (gt_iir) {
213072c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
213172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2132d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21330e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2134d8fc8a47SPaulo Zanoni 		else
2135d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21360e43406bSChris Wilson 	}
2137b1f14ad0SJesse Barnes 
2138b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21390e43406bSChris Wilson 	if (de_iir) {
214072c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
214172c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2142f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21439719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2144f1af8fc1SPaulo Zanoni 		else
2145f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21460e43406bSChris Wilson 	}
21470e43406bSChris Wilson 
2148f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2149f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21500e43406bSChris Wilson 		if (pm_iir) {
2151b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21520e43406bSChris Wilson 			ret = IRQ_HANDLED;
215372c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21540e43406bSChris Wilson 		}
2155f1af8fc1SPaulo Zanoni 	}
2156b1f14ad0SJesse Barnes 
2157b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2158b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2159ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
216044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
216144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2162ab5c608bSBen Widawsky 	}
2163b1f14ad0SJesse Barnes 
2164b1f14ad0SJesse Barnes 	return ret;
2165b1f14ad0SJesse Barnes }
2166b1f14ad0SJesse Barnes 
2167abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2168abd58f01SBen Widawsky {
2169abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2170abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2171abd58f01SBen Widawsky 	u32 master_ctl;
2172abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2173abd58f01SBen Widawsky 	uint32_t tmp = 0;
2174c42664ccSDaniel Vetter 	enum pipe pipe;
217588e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
217688e04703SJesse Barnes 
21772dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21782dd2a883SImre Deak 		return IRQ_NONE;
21792dd2a883SImre Deak 
218088e04703SJesse Barnes 	if (IS_GEN9(dev))
218188e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
218288e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2183abd58f01SBen Widawsky 
2184abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2185abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2186abd58f01SBen Widawsky 	if (!master_ctl)
2187abd58f01SBen Widawsky 		return IRQ_NONE;
2188abd58f01SBen Widawsky 
2189abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2190abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2191abd58f01SBen Widawsky 
219238cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
219338cc46d7SOscar Mateo 
2194abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2195abd58f01SBen Widawsky 
2196abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2197abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2198abd58f01SBen Widawsky 		if (tmp) {
2199abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2200abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
220138cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
220238cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
220338cc46d7SOscar Mateo 			else
220438cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2205abd58f01SBen Widawsky 		}
220638cc46d7SOscar Mateo 		else
220738cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2208abd58f01SBen Widawsky 	}
2209abd58f01SBen Widawsky 
22106d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22116d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22126d766f02SDaniel Vetter 		if (tmp) {
22136d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22146d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
221588e04703SJesse Barnes 
221688e04703SJesse Barnes 			if (tmp & aux_mask)
221738cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
221838cc46d7SOscar Mateo 			else
221938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22206d766f02SDaniel Vetter 		}
222138cc46d7SOscar Mateo 		else
222238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22236d766f02SDaniel Vetter 	}
22246d766f02SDaniel Vetter 
2225055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2226770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2227abd58f01SBen Widawsky 
2228c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2229c42664ccSDaniel Vetter 			continue;
2230c42664ccSDaniel Vetter 
2231abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
223238cc46d7SOscar Mateo 		if (pipe_iir) {
223338cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
223438cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2235770de83dSDamien Lespiau 
2236d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2237d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2238d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2239abd58f01SBen Widawsky 
2240770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2241770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2242770de83dSDamien Lespiau 			else
2243770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2244770de83dSDamien Lespiau 
2245770de83dSDamien Lespiau 			if (flip_done) {
2246abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2247abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2248abd58f01SBen Widawsky 			}
2249abd58f01SBen Widawsky 
22500fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22510fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22520fbe7870SDaniel Vetter 
22531f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22541f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22551f7247c0SDaniel Vetter 								    pipe);
225638d83c96SDaniel Vetter 
2257770de83dSDamien Lespiau 
2258770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2259770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2260770de83dSDamien Lespiau 			else
2261770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2262770de83dSDamien Lespiau 
2263770de83dSDamien Lespiau 			if (fault_errors)
226430100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
226530100f2bSDaniel Vetter 					  pipe_name(pipe),
226630100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2267c42664ccSDaniel Vetter 		} else
2268abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2269abd58f01SBen Widawsky 	}
2270abd58f01SBen Widawsky 
227192d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
227292d03a80SDaniel Vetter 		/*
227392d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
227492d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
227592d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
227692d03a80SDaniel Vetter 		 */
227792d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
227892d03a80SDaniel Vetter 		if (pch_iir) {
227992d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
228092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
228138cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
228238cc46d7SOscar Mateo 		} else
228338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
228438cc46d7SOscar Mateo 
228592d03a80SDaniel Vetter 	}
228692d03a80SDaniel Vetter 
2287abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2288abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2289abd58f01SBen Widawsky 
2290abd58f01SBen Widawsky 	return ret;
2291abd58f01SBen Widawsky }
2292abd58f01SBen Widawsky 
229317e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
229417e1df07SDaniel Vetter 			       bool reset_completed)
229517e1df07SDaniel Vetter {
2296a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
229717e1df07SDaniel Vetter 	int i;
229817e1df07SDaniel Vetter 
229917e1df07SDaniel Vetter 	/*
230017e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
230117e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
230217e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
230317e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
230417e1df07SDaniel Vetter 	 */
230517e1df07SDaniel Vetter 
230617e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
230717e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
230817e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
230917e1df07SDaniel Vetter 
231017e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
231117e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
231217e1df07SDaniel Vetter 
231317e1df07SDaniel Vetter 	/*
231417e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
231517e1df07SDaniel Vetter 	 * reset state is cleared.
231617e1df07SDaniel Vetter 	 */
231717e1df07SDaniel Vetter 	if (reset_completed)
231817e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
231917e1df07SDaniel Vetter }
232017e1df07SDaniel Vetter 
23218a905236SJesse Barnes /**
2322b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23238a905236SJesse Barnes  *
23248a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23258a905236SJesse Barnes  * was detected.
23268a905236SJesse Barnes  */
2327b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23288a905236SJesse Barnes {
2329b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2330b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2331cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2332cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2333cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
233417e1df07SDaniel Vetter 	int ret;
23358a905236SJesse Barnes 
23365bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23378a905236SJesse Barnes 
23387db0ba24SDaniel Vetter 	/*
23397db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23407db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23417db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23427db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23437db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23447db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23457db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23467db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23477db0ba24SDaniel Vetter 	 */
23487db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
234944d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23505bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23517db0ba24SDaniel Vetter 				   reset_event);
23521f83fee0SDaniel Vetter 
235317e1df07SDaniel Vetter 		/*
2354f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2355f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2356f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2357f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2358f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2359f454c694SImre Deak 		 */
2360f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23617514747dSVille Syrjälä 
23627514747dSVille Syrjälä 		intel_prepare_reset(dev);
23637514747dSVille Syrjälä 
2364f454c694SImre Deak 		/*
236517e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
236617e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
236717e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
236817e1df07SDaniel Vetter 		 * deadlocks with the reset work.
236917e1df07SDaniel Vetter 		 */
2370f69061beSDaniel Vetter 		ret = i915_reset(dev);
2371f69061beSDaniel Vetter 
23727514747dSVille Syrjälä 		intel_finish_reset(dev);
237317e1df07SDaniel Vetter 
2374f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2375f454c694SImre Deak 
2376f69061beSDaniel Vetter 		if (ret == 0) {
2377f69061beSDaniel Vetter 			/*
2378f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2379f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2380f69061beSDaniel Vetter 			 * complete.
2381f69061beSDaniel Vetter 			 *
2382f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2383f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2384f69061beSDaniel Vetter 			 * updates before
2385f69061beSDaniel Vetter 			 * the counter increment.
2386f69061beSDaniel Vetter 			 */
23874e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2388f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2389f69061beSDaniel Vetter 
23905bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2391f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
23921f83fee0SDaniel Vetter 		} else {
23932ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2394f316a42cSBen Gamari 		}
23951f83fee0SDaniel Vetter 
239617e1df07SDaniel Vetter 		/*
239717e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
239817e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
239917e1df07SDaniel Vetter 		 */
240017e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2401f316a42cSBen Gamari 	}
24028a905236SJesse Barnes }
24038a905236SJesse Barnes 
240435aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2405c0e09200SDave Airlie {
24068a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2407bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
240863eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2409050ee91fSBen Widawsky 	int pipe, i;
241063eeaf38SJesse Barnes 
241135aed2e6SChris Wilson 	if (!eir)
241235aed2e6SChris Wilson 		return;
241363eeaf38SJesse Barnes 
2414a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24158a905236SJesse Barnes 
2416bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2417bd9854f9SBen Widawsky 
24188a905236SJesse Barnes 	if (IS_G4X(dev)) {
24198a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24208a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24218a905236SJesse Barnes 
2422a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2423a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2424050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2425050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2426a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2427a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24288a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24293143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24308a905236SJesse Barnes 		}
24318a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24328a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2433a70491ccSJoe Perches 			pr_err("page table error\n");
2434a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24358a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24363143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24378a905236SJesse Barnes 		}
24388a905236SJesse Barnes 	}
24398a905236SJesse Barnes 
2440a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
244163eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
244263eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2443a70491ccSJoe Perches 			pr_err("page table error\n");
2444a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
244563eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24463143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
244763eeaf38SJesse Barnes 		}
24488a905236SJesse Barnes 	}
24498a905236SJesse Barnes 
245063eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2451a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2452055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2453a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24549db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
245563eeaf38SJesse Barnes 		/* pipestat has already been acked */
245663eeaf38SJesse Barnes 	}
245763eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2458a70491ccSJoe Perches 		pr_err("instruction error\n");
2459a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2460050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2461050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2462a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
246363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
246463eeaf38SJesse Barnes 
2465a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2466a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2467a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
246863eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24693143a2bfSChris Wilson 			POSTING_READ(IPEIR);
247063eeaf38SJesse Barnes 		} else {
247163eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
247263eeaf38SJesse Barnes 
2473a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2474a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2475a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2476a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
247763eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24783143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
247963eeaf38SJesse Barnes 		}
248063eeaf38SJesse Barnes 	}
248163eeaf38SJesse Barnes 
248263eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24833143a2bfSChris Wilson 	POSTING_READ(EIR);
248463eeaf38SJesse Barnes 	eir = I915_READ(EIR);
248563eeaf38SJesse Barnes 	if (eir) {
248663eeaf38SJesse Barnes 		/*
248763eeaf38SJesse Barnes 		 * some errors might have become stuck,
248863eeaf38SJesse Barnes 		 * mask them.
248963eeaf38SJesse Barnes 		 */
249063eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
249163eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
249263eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
249363eeaf38SJesse Barnes 	}
249435aed2e6SChris Wilson }
249535aed2e6SChris Wilson 
249635aed2e6SChris Wilson /**
2497b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
249835aed2e6SChris Wilson  * @dev: drm device
249935aed2e6SChris Wilson  *
2500b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
250135aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
250235aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
250335aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
250435aed2e6SChris Wilson  * of a ring dump etc.).
250535aed2e6SChris Wilson  */
250658174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
250758174462SMika Kuoppala 		       const char *fmt, ...)
250835aed2e6SChris Wilson {
250935aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
251058174462SMika Kuoppala 	va_list args;
251158174462SMika Kuoppala 	char error_msg[80];
251235aed2e6SChris Wilson 
251358174462SMika Kuoppala 	va_start(args, fmt);
251458174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
251558174462SMika Kuoppala 	va_end(args);
251658174462SMika Kuoppala 
251758174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
251835aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25198a905236SJesse Barnes 
2520ba1234d1SBen Gamari 	if (wedged) {
2521f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2522f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2523ba1234d1SBen Gamari 
252411ed50ecSBen Gamari 		/*
2525b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2526b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2527b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
252817e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
252917e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
253017e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
253117e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
253217e1df07SDaniel Vetter 		 *
253317e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
253417e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
253517e1df07SDaniel Vetter 		 * counter atomic_t.
253611ed50ecSBen Gamari 		 */
253717e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
253811ed50ecSBen Gamari 	}
253911ed50ecSBen Gamari 
2540b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25418a905236SJesse Barnes }
25428a905236SJesse Barnes 
254342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
254442f52ef8SKeith Packard  * we use as a pipe index
254542f52ef8SKeith Packard  */
2546f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25470a3e67a4SJesse Barnes {
25482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2549e9d21d7fSKeith Packard 	unsigned long irqflags;
255071e0ffa5SJesse Barnes 
25511ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2552f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25537c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2554755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25550a3e67a4SJesse Barnes 	else
25567c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2557755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25581ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25598692d00eSChris Wilson 
25600a3e67a4SJesse Barnes 	return 0;
25610a3e67a4SJesse Barnes }
25620a3e67a4SJesse Barnes 
2563f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2564f796cf8fSJesse Barnes {
25652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2566f796cf8fSJesse Barnes 	unsigned long irqflags;
2567b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
256840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2569f796cf8fSJesse Barnes 
2570f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2571b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2572b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2573b1f14ad0SJesse Barnes 
2574b1f14ad0SJesse Barnes 	return 0;
2575b1f14ad0SJesse Barnes }
2576b1f14ad0SJesse Barnes 
25777e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
25787e231dbeSJesse Barnes {
25792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25807e231dbeSJesse Barnes 	unsigned long irqflags;
25817e231dbeSJesse Barnes 
25827e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
258331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2584755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25857e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25867e231dbeSJesse Barnes 
25877e231dbeSJesse Barnes 	return 0;
25887e231dbeSJesse Barnes }
25897e231dbeSJesse Barnes 
2590abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2591abd58f01SBen Widawsky {
2592abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2593abd58f01SBen Widawsky 	unsigned long irqflags;
2594abd58f01SBen Widawsky 
2595abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25967167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
25977167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2598abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2599abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2600abd58f01SBen Widawsky 	return 0;
2601abd58f01SBen Widawsky }
2602abd58f01SBen Widawsky 
260342f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
260442f52ef8SKeith Packard  * we use as a pipe index
260542f52ef8SKeith Packard  */
2606f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26070a3e67a4SJesse Barnes {
26082d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2609e9d21d7fSKeith Packard 	unsigned long irqflags;
26100a3e67a4SJesse Barnes 
26111ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26127c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2613755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2614755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26151ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26160a3e67a4SJesse Barnes }
26170a3e67a4SJesse Barnes 
2618f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2619f796cf8fSJesse Barnes {
26202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2621f796cf8fSJesse Barnes 	unsigned long irqflags;
2622b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
262340da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2624f796cf8fSJesse Barnes 
2625f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2626b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2627b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2628b1f14ad0SJesse Barnes }
2629b1f14ad0SJesse Barnes 
26307e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26317e231dbeSJesse Barnes {
26322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26337e231dbeSJesse Barnes 	unsigned long irqflags;
26347e231dbeSJesse Barnes 
26357e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
263631acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2637755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26387e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26397e231dbeSJesse Barnes }
26407e231dbeSJesse Barnes 
2641abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2642abd58f01SBen Widawsky {
2643abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2644abd58f01SBen Widawsky 	unsigned long irqflags;
2645abd58f01SBen Widawsky 
2646abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26477167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26487167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2649abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2650abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2651abd58f01SBen Widawsky }
2652abd58f01SBen Widawsky 
265344cdd6d2SJohn Harrison static struct drm_i915_gem_request *
265444cdd6d2SJohn Harrison ring_last_request(struct intel_engine_cs *ring)
2655852835f3SZou Nan hai {
2656893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
265744cdd6d2SJohn Harrison 			  struct drm_i915_gem_request, list);
2658893eead0SChris Wilson }
2659893eead0SChris Wilson 
26609107e9d2SChris Wilson static bool
266144cdd6d2SJohn Harrison ring_idle(struct intel_engine_cs *ring)
2662893eead0SChris Wilson {
26639107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
26641b5a433aSJohn Harrison 		i915_gem_request_completed(ring_last_request(ring), false));
2665f65d9421SBen Gamari }
2666f65d9421SBen Gamari 
2667a028c4b0SDaniel Vetter static bool
2668a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2669a028c4b0SDaniel Vetter {
2670a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2671a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2672a028c4b0SDaniel Vetter 	} else {
2673a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2674a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2675a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2676a028c4b0SDaniel Vetter 	}
2677a028c4b0SDaniel Vetter }
2678a028c4b0SDaniel Vetter 
2679a4872ba6SOscar Mateo static struct intel_engine_cs *
2680a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2681921d42eaSDaniel Vetter {
2682921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2683a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2684921d42eaSDaniel Vetter 	int i;
2685921d42eaSDaniel Vetter 
2686921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2687a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2688a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2689a6cdb93aSRodrigo Vivi 				continue;
2690a6cdb93aSRodrigo Vivi 
2691a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2692a6cdb93aSRodrigo Vivi 				return signaller;
2693a6cdb93aSRodrigo Vivi 		}
2694921d42eaSDaniel Vetter 	} else {
2695921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2696921d42eaSDaniel Vetter 
2697921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2698921d42eaSDaniel Vetter 			if(ring == signaller)
2699921d42eaSDaniel Vetter 				continue;
2700921d42eaSDaniel Vetter 
2701ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2702921d42eaSDaniel Vetter 				return signaller;
2703921d42eaSDaniel Vetter 		}
2704921d42eaSDaniel Vetter 	}
2705921d42eaSDaniel Vetter 
2706a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2707a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2708921d42eaSDaniel Vetter 
2709921d42eaSDaniel Vetter 	return NULL;
2710921d42eaSDaniel Vetter }
2711921d42eaSDaniel Vetter 
2712a4872ba6SOscar Mateo static struct intel_engine_cs *
2713a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2714a24a11e6SChris Wilson {
2715a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
271688fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2717a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2718a6cdb93aSRodrigo Vivi 	int i, backwards;
2719a24a11e6SChris Wilson 
2720a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2721a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27226274f212SChris Wilson 		return NULL;
2723a24a11e6SChris Wilson 
272488fe429dSDaniel Vetter 	/*
272588fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
272688fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2727a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2728a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
272988fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
273088fe429dSDaniel Vetter 	 * ringbuffer itself.
2731a24a11e6SChris Wilson 	 */
273288fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2733a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
273488fe429dSDaniel Vetter 
2735a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
273688fe429dSDaniel Vetter 		/*
273788fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
273888fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
273988fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
274088fe429dSDaniel Vetter 		 */
2741ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
274288fe429dSDaniel Vetter 
274388fe429dSDaniel Vetter 		/* This here seems to blow up */
2744ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2745a24a11e6SChris Wilson 		if (cmd == ipehr)
2746a24a11e6SChris Wilson 			break;
2747a24a11e6SChris Wilson 
274888fe429dSDaniel Vetter 		head -= 4;
274988fe429dSDaniel Vetter 	}
2750a24a11e6SChris Wilson 
275188fe429dSDaniel Vetter 	if (!i)
275288fe429dSDaniel Vetter 		return NULL;
275388fe429dSDaniel Vetter 
2754ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2755a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2756a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2757a6cdb93aSRodrigo Vivi 		offset <<= 32;
2758a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2759a6cdb93aSRodrigo Vivi 	}
2760a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2761a24a11e6SChris Wilson }
2762a24a11e6SChris Wilson 
2763a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27646274f212SChris Wilson {
27656274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2766a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2767a0d036b0SChris Wilson 	u32 seqno;
27686274f212SChris Wilson 
27694be17381SChris Wilson 	ring->hangcheck.deadlock++;
27706274f212SChris Wilson 
27716274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
27724be17381SChris Wilson 	if (signaller == NULL)
27734be17381SChris Wilson 		return -1;
27744be17381SChris Wilson 
27754be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
27764be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
27776274f212SChris Wilson 		return -1;
27786274f212SChris Wilson 
27794be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
27804be17381SChris Wilson 		return 1;
27814be17381SChris Wilson 
2782a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2783a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2784a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
27854be17381SChris Wilson 		return -1;
27864be17381SChris Wilson 
27874be17381SChris Wilson 	return 0;
27886274f212SChris Wilson }
27896274f212SChris Wilson 
27906274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
27916274f212SChris Wilson {
2792a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
27936274f212SChris Wilson 	int i;
27946274f212SChris Wilson 
27956274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
27964be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
27976274f212SChris Wilson }
27986274f212SChris Wilson 
2799ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2800a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28011ec14ad3SChris Wilson {
28021ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28031ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28049107e9d2SChris Wilson 	u32 tmp;
28059107e9d2SChris Wilson 
2806f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2807f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2808f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2809f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2810f260fe7bSMika Kuoppala 		}
2811f260fe7bSMika Kuoppala 
2812f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2813f260fe7bSMika Kuoppala 	}
28146274f212SChris Wilson 
28159107e9d2SChris Wilson 	if (IS_GEN2(dev))
2816f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28179107e9d2SChris Wilson 
28189107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28199107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28209107e9d2SChris Wilson 	 * and break the hang. This should work on
28219107e9d2SChris Wilson 	 * all but the second generation chipsets.
28229107e9d2SChris Wilson 	 */
28239107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28241ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
282558174462SMika Kuoppala 		i915_handle_error(dev, false,
282658174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28271ec14ad3SChris Wilson 				  ring->name);
28281ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2829f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28301ec14ad3SChris Wilson 	}
2831a24a11e6SChris Wilson 
28326274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28336274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28346274f212SChris Wilson 		default:
2835f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28366274f212SChris Wilson 		case 1:
283758174462SMika Kuoppala 			i915_handle_error(dev, false,
283858174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2839a24a11e6SChris Wilson 					  ring->name);
2840a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2841f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28426274f212SChris Wilson 		case 0:
2843f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28446274f212SChris Wilson 		}
28459107e9d2SChris Wilson 	}
28469107e9d2SChris Wilson 
2847f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2848a24a11e6SChris Wilson }
2849d1e61e7fSChris Wilson 
2850737b1506SChris Wilson /*
2851f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
285205407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
285305407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
285405407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
285505407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
285605407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2857f65d9421SBen Gamari  */
2858737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2859f65d9421SBen Gamari {
2860737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2861737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2862737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2863737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2864a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2865b4519513SChris Wilson 	int i;
286605407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28679107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28689107e9d2SChris Wilson #define BUSY 1
28699107e9d2SChris Wilson #define KICK 5
28709107e9d2SChris Wilson #define HUNG 20
2871893eead0SChris Wilson 
2872d330a953SJani Nikula 	if (!i915.enable_hangcheck)
28733e0dc6b0SBen Widawsky 		return;
28743e0dc6b0SBen Widawsky 
2875b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
287650877445SChris Wilson 		u64 acthd;
287750877445SChris Wilson 		u32 seqno;
28789107e9d2SChris Wilson 		bool busy = true;
2879b4519513SChris Wilson 
28806274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
28816274f212SChris Wilson 
288205407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
288305407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
288405407ff8SMika Kuoppala 
288505407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
288644cdd6d2SJohn Harrison 			if (ring_idle(ring)) {
2887da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2888da661464SMika Kuoppala 
28899107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
28909107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2891094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2892f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
28939107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
28949107e9d2SChris Wilson 								  ring->name);
2895f4adcd24SDaniel Vetter 						else
2896f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2897f4adcd24SDaniel Vetter 								 ring->name);
28989107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2899094f9a54SChris Wilson 					}
2900094f9a54SChris Wilson 					/* Safeguard against driver failure */
2901094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29029107e9d2SChris Wilson 				} else
29039107e9d2SChris Wilson 					busy = false;
290405407ff8SMika Kuoppala 			} else {
29056274f212SChris Wilson 				/* We always increment the hangcheck score
29066274f212SChris Wilson 				 * if the ring is busy and still processing
29076274f212SChris Wilson 				 * the same request, so that no single request
29086274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29096274f212SChris Wilson 				 * batches). The only time we do not increment
29106274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29116274f212SChris Wilson 				 * ring is in a legitimate wait for another
29126274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29136274f212SChris Wilson 				 * victim and we want to be sure we catch the
29146274f212SChris Wilson 				 * right culprit. Then every time we do kick
29156274f212SChris Wilson 				 * the ring, add a small increment to the
29166274f212SChris Wilson 				 * score so that we can catch a batch that is
29176274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29186274f212SChris Wilson 				 * for stalling the machine.
29199107e9d2SChris Wilson 				 */
2920ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2921ad8beaeaSMika Kuoppala 								    acthd);
2922ad8beaeaSMika Kuoppala 
2923ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2924da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2925f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2926f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2927f260fe7bSMika Kuoppala 					break;
2928f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2929ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29306274f212SChris Wilson 					break;
2931f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2932ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29336274f212SChris Wilson 					break;
2934f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2935ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29366274f212SChris Wilson 					stuck[i] = true;
29376274f212SChris Wilson 					break;
29386274f212SChris Wilson 				}
293905407ff8SMika Kuoppala 			}
29409107e9d2SChris Wilson 		} else {
2941da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2942da661464SMika Kuoppala 
29439107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29449107e9d2SChris Wilson 			 * attempts across multiple batches.
29459107e9d2SChris Wilson 			 */
29469107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29479107e9d2SChris Wilson 				ring->hangcheck.score--;
2948f260fe7bSMika Kuoppala 
2949f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2950cbb465e7SChris Wilson 		}
2951f65d9421SBen Gamari 
295205407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
295305407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29549107e9d2SChris Wilson 		busy_count += busy;
295505407ff8SMika Kuoppala 	}
295605407ff8SMika Kuoppala 
295705407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2958b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2959b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
296005407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2961a43adf07SChris Wilson 				 ring->name);
2962a43adf07SChris Wilson 			rings_hung++;
296305407ff8SMika Kuoppala 		}
296405407ff8SMika Kuoppala 	}
296505407ff8SMika Kuoppala 
296605407ff8SMika Kuoppala 	if (rings_hung)
296758174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
296805407ff8SMika Kuoppala 
296905407ff8SMika Kuoppala 	if (busy_count)
297005407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
297105407ff8SMika Kuoppala 		 * being added */
297210cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
297310cd45b6SMika Kuoppala }
297410cd45b6SMika Kuoppala 
297510cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
297610cd45b6SMika Kuoppala {
2977737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2978672e7b7cSChris Wilson 
2979d330a953SJani Nikula 	if (!i915.enable_hangcheck)
298010cd45b6SMika Kuoppala 		return;
298110cd45b6SMika Kuoppala 
2982737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2983737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2984737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2985737b1506SChris Wilson 	 */
2986737b1506SChris Wilson 
2987737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
2988737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
2989f65d9421SBen Gamari }
2990f65d9421SBen Gamari 
29911c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
299291738a95SPaulo Zanoni {
299391738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
299491738a95SPaulo Zanoni 
299591738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
299691738a95SPaulo Zanoni 		return;
299791738a95SPaulo Zanoni 
2998f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2999105b122eSPaulo Zanoni 
3000105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3001105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3002622364b6SPaulo Zanoni }
3003105b122eSPaulo Zanoni 
300491738a95SPaulo Zanoni /*
3005622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3006622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3007622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3008622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3009622364b6SPaulo Zanoni  *
3010622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
301191738a95SPaulo Zanoni  */
3012622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3013622364b6SPaulo Zanoni {
3014622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3015622364b6SPaulo Zanoni 
3016622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3017622364b6SPaulo Zanoni 		return;
3018622364b6SPaulo Zanoni 
3019622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
302091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
302191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
302291738a95SPaulo Zanoni }
302391738a95SPaulo Zanoni 
30247c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3025d18ea1b5SDaniel Vetter {
3026d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3027d18ea1b5SDaniel Vetter 
3028f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3029a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3030f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3031d18ea1b5SDaniel Vetter }
3032d18ea1b5SDaniel Vetter 
3033c0e09200SDave Airlie /* drm_dma.h hooks
3034c0e09200SDave Airlie */
3035be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3036036a4a7dSZhenyu Wang {
30372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3038036a4a7dSZhenyu Wang 
30390c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3040bdfcdb63SDaniel Vetter 
3041f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3042c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3043c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3044036a4a7dSZhenyu Wang 
30457c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3046c650156aSZhenyu Wang 
30471c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30487d99163dSBen Widawsky }
30497d99163dSBen Widawsky 
305070591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
305170591a41SVille Syrjälä {
305270591a41SVille Syrjälä 	enum pipe pipe;
305370591a41SVille Syrjälä 
305470591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
305570591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
305670591a41SVille Syrjälä 
305770591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
305870591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
305970591a41SVille Syrjälä 
306070591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
306170591a41SVille Syrjälä }
306270591a41SVille Syrjälä 
30637e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30647e231dbeSJesse Barnes {
30652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30667e231dbeSJesse Barnes 
30677e231dbeSJesse Barnes 	/* VLV magic */
30687e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30697e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30707e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30717e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30727e231dbeSJesse Barnes 
30737c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30747e231dbeSJesse Barnes 
30757c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
30767e231dbeSJesse Barnes 
307770591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
30787e231dbeSJesse Barnes }
30797e231dbeSJesse Barnes 
3080d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3081d6e3cca3SDaniel Vetter {
3082d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3083d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3084d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3085d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3086d6e3cca3SDaniel Vetter }
3087d6e3cca3SDaniel Vetter 
3088823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3089abd58f01SBen Widawsky {
3090abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3091abd58f01SBen Widawsky 	int pipe;
3092abd58f01SBen Widawsky 
3093abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3094abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3095abd58f01SBen Widawsky 
3096d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3097abd58f01SBen Widawsky 
3098055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3099f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3100813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3101f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3102abd58f01SBen Widawsky 
3103f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3104f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3105f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3106abd58f01SBen Widawsky 
31071c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3108abd58f01SBen Widawsky }
3109abd58f01SBen Widawsky 
31104c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31114c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3112d49bdb0eSPaulo Zanoni {
31131180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3114d49bdb0eSPaulo Zanoni 
311513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3116d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3117d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3118d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3119d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31204c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31214c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31224c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31231180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31244c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31254c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31264c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31271180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
312813321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3129d49bdb0eSPaulo Zanoni }
3130d49bdb0eSPaulo Zanoni 
313143f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
313243f328d7SVille Syrjälä {
313343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
313443f328d7SVille Syrjälä 
313543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
313643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
313743f328d7SVille Syrjälä 
3138d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
313943f328d7SVille Syrjälä 
314043f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
314143f328d7SVille Syrjälä 
314243f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
314343f328d7SVille Syrjälä 
314470591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
314543f328d7SVille Syrjälä }
314643f328d7SVille Syrjälä 
314782a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
314882a28bcfSDaniel Vetter {
31492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
315082a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3151fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
315282a28bcfSDaniel Vetter 
315382a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3154fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3155b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3156cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3157fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
315882a28bcfSDaniel Vetter 	} else {
3159fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3160b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3161cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3162fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
316382a28bcfSDaniel Vetter 	}
316482a28bcfSDaniel Vetter 
3165fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
316682a28bcfSDaniel Vetter 
31677fe0b973SKeith Packard 	/*
31687fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31697fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
31707fe0b973SKeith Packard 	 *
31717fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
31727fe0b973SKeith Packard 	 */
31737fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31747fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31757fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31767fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31777fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31787fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31797fe0b973SKeith Packard }
31807fe0b973SKeith Packard 
3181d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3182d46da437SPaulo Zanoni {
31832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
318482a28bcfSDaniel Vetter 	u32 mask;
3185d46da437SPaulo Zanoni 
3186692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3187692a04cfSDaniel Vetter 		return;
3188692a04cfSDaniel Vetter 
3189105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
31905c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3191105b122eSPaulo Zanoni 	else
31925c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
31938664281bSPaulo Zanoni 
3194337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3195d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3196d46da437SPaulo Zanoni }
3197d46da437SPaulo Zanoni 
31980a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
31990a9a8c91SDaniel Vetter {
32000a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32010a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32020a9a8c91SDaniel Vetter 
32030a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32040a9a8c91SDaniel Vetter 
32050a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3206040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32070a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
320835a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
320935a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32100a9a8c91SDaniel Vetter 	}
32110a9a8c91SDaniel Vetter 
32120a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32130a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32140a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32150a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32160a9a8c91SDaniel Vetter 	} else {
32170a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32180a9a8c91SDaniel Vetter 	}
32190a9a8c91SDaniel Vetter 
322035079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32210a9a8c91SDaniel Vetter 
32220a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
322378e68d36SImre Deak 		/*
322478e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
322578e68d36SImre Deak 		 * itself is enabled/disabled.
322678e68d36SImre Deak 		 */
32270a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32280a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32290a9a8c91SDaniel Vetter 
3230605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
323135079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32320a9a8c91SDaniel Vetter 	}
32330a9a8c91SDaniel Vetter }
32340a9a8c91SDaniel Vetter 
3235f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3236036a4a7dSZhenyu Wang {
32372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
32388e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32398e76f8dcSPaulo Zanoni 
32408e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
32418e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
32428e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
32438e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
32445c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
32458e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
32465c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
32478e76f8dcSPaulo Zanoni 	} else {
32488e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3249ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
32505b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
32515b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
32525b3a856bSDaniel Vetter 				DE_POISON);
32535c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
32545c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
32558e76f8dcSPaulo Zanoni 	}
3256036a4a7dSZhenyu Wang 
32571ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3258036a4a7dSZhenyu Wang 
32590c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
32600c841212SPaulo Zanoni 
3261622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3262622364b6SPaulo Zanoni 
326335079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3264036a4a7dSZhenyu Wang 
32650a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3266036a4a7dSZhenyu Wang 
3267d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
32687fe0b973SKeith Packard 
3269f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
32706005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32716005ce42SDaniel Vetter 		 *
32726005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32734bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32744bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3275d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3276f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3277d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3278f97108d1SJesse Barnes 	}
3279f97108d1SJesse Barnes 
3280036a4a7dSZhenyu Wang 	return 0;
3281036a4a7dSZhenyu Wang }
3282036a4a7dSZhenyu Wang 
3283f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3284f8b79e58SImre Deak {
3285f8b79e58SImre Deak 	u32 pipestat_mask;
3286f8b79e58SImre Deak 	u32 iir_mask;
3287120dda4fSVille Syrjälä 	enum pipe pipe;
3288f8b79e58SImre Deak 
3289f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3290f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3291f8b79e58SImre Deak 
3292120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3293120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3294f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3295f8b79e58SImre Deak 
3296f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3297f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3298f8b79e58SImre Deak 
3299120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3300120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3301120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3302f8b79e58SImre Deak 
3303f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3304f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3305f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3306120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3307120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3308f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3309f8b79e58SImre Deak 
3310f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3311f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3312f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
331376e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
331476e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3315f8b79e58SImre Deak }
3316f8b79e58SImre Deak 
3317f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3318f8b79e58SImre Deak {
3319f8b79e58SImre Deak 	u32 pipestat_mask;
3320f8b79e58SImre Deak 	u32 iir_mask;
3321120dda4fSVille Syrjälä 	enum pipe pipe;
3322f8b79e58SImre Deak 
3323f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3324f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33256c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3326120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3327120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3328f8b79e58SImre Deak 
3329f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3330f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
333176e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3332f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3333f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3334f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3335f8b79e58SImre Deak 
3336f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3337f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3338f8b79e58SImre Deak 
3339120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3340120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3341120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3342f8b79e58SImre Deak 
3343f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3344f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3345120dda4fSVille Syrjälä 
3346120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3347120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3348f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3349f8b79e58SImre Deak }
3350f8b79e58SImre Deak 
3351f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3352f8b79e58SImre Deak {
3353f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3354f8b79e58SImre Deak 
3355f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3356f8b79e58SImre Deak 		return;
3357f8b79e58SImre Deak 
3358f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3359f8b79e58SImre Deak 
3360950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3361f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3362f8b79e58SImre Deak }
3363f8b79e58SImre Deak 
3364f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3365f8b79e58SImre Deak {
3366f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3367f8b79e58SImre Deak 
3368f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3369f8b79e58SImre Deak 		return;
3370f8b79e58SImre Deak 
3371f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3372f8b79e58SImre Deak 
3373950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3374f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3375f8b79e58SImre Deak }
3376f8b79e58SImre Deak 
33770e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33787e231dbeSJesse Barnes {
3379f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
33807e231dbeSJesse Barnes 
338120afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
338220afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
338320afbda2SDaniel Vetter 
33847e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
338576e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
338676e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
338776e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
338876e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
33897e231dbeSJesse Barnes 
3390b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3391b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3392d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3393f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3394f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3395d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
33960e6c9a9eSVille Syrjälä }
33970e6c9a9eSVille Syrjälä 
33980e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
33990e6c9a9eSVille Syrjälä {
34000e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34010e6c9a9eSVille Syrjälä 
34020e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
34037e231dbeSJesse Barnes 
34040a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34057e231dbeSJesse Barnes 
34067e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34077e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34087e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34097e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34107e231dbeSJesse Barnes #endif
34117e231dbeSJesse Barnes 
34127e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
341320afbda2SDaniel Vetter 
341420afbda2SDaniel Vetter 	return 0;
341520afbda2SDaniel Vetter }
341620afbda2SDaniel Vetter 
3417abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3418abd58f01SBen Widawsky {
3419abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3420abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3421abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
342273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3423abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
342473d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
342573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3426abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
342773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
342873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
342973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3430abd58f01SBen Widawsky 		0,
343173d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
343273d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3433abd58f01SBen Widawsky 		};
3434abd58f01SBen Widawsky 
34350961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
34369a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34379a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
343878e68d36SImre Deak 	/*
343978e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
344078e68d36SImre Deak 	 * is enabled/disabled.
344178e68d36SImre Deak 	 */
344278e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
34439a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3444abd58f01SBen Widawsky }
3445abd58f01SBen Widawsky 
3446abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3447abd58f01SBen Widawsky {
3448770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3449770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3450abd58f01SBen Widawsky 	int pipe;
345188e04703SJesse Barnes 	u32 aux_en = GEN8_AUX_CHANNEL_A;
3452770de83dSDamien Lespiau 
345388e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3454770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3455770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
345688e04703SJesse Barnes 		aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
345788e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
345888e04703SJesse Barnes 	} else
3459770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3460770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3461770de83dSDamien Lespiau 
3462770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3463770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3464770de83dSDamien Lespiau 
346513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
346613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
346713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3468abd58f01SBen Widawsky 
3469055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3470f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3471813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3472813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3473813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
347435079899SPaulo Zanoni 					  de_pipe_enables);
3475abd58f01SBen Widawsky 
347688e04703SJesse Barnes 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
3477abd58f01SBen Widawsky }
3478abd58f01SBen Widawsky 
3479abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3480abd58f01SBen Widawsky {
3481abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3482abd58f01SBen Widawsky 
3483622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3484622364b6SPaulo Zanoni 
3485abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3486abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3487abd58f01SBen Widawsky 
3488abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3489abd58f01SBen Widawsky 
3490abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3491abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3492abd58f01SBen Widawsky 
3493abd58f01SBen Widawsky 	return 0;
3494abd58f01SBen Widawsky }
3495abd58f01SBen Widawsky 
349643f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
349743f328d7SVille Syrjälä {
349843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
349943f328d7SVille Syrjälä 
3500c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
350143f328d7SVille Syrjälä 
350243f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
350343f328d7SVille Syrjälä 
350443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
350543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
350643f328d7SVille Syrjälä 
350743f328d7SVille Syrjälä 	return 0;
350843f328d7SVille Syrjälä }
350943f328d7SVille Syrjälä 
3510abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3511abd58f01SBen Widawsky {
3512abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3513abd58f01SBen Widawsky 
3514abd58f01SBen Widawsky 	if (!dev_priv)
3515abd58f01SBen Widawsky 		return;
3516abd58f01SBen Widawsky 
3517823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3518abd58f01SBen Widawsky }
3519abd58f01SBen Widawsky 
35208ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
35218ea0be4fSVille Syrjälä {
35228ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
35238ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
35248ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35258ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
35268ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
35278ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35288ea0be4fSVille Syrjälä 
35298ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
35308ea0be4fSVille Syrjälä 
3531c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
35328ea0be4fSVille Syrjälä }
35338ea0be4fSVille Syrjälä 
35347e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35357e231dbeSJesse Barnes {
35362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35377e231dbeSJesse Barnes 
35387e231dbeSJesse Barnes 	if (!dev_priv)
35397e231dbeSJesse Barnes 		return;
35407e231dbeSJesse Barnes 
3541843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3542843d0e7dSImre Deak 
3543893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3544893fce8eSVille Syrjälä 
35457e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3546f8b79e58SImre Deak 
35478ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
35487e231dbeSJesse Barnes }
35497e231dbeSJesse Barnes 
355043f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
355143f328d7SVille Syrjälä {
355243f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
355343f328d7SVille Syrjälä 
355443f328d7SVille Syrjälä 	if (!dev_priv)
355543f328d7SVille Syrjälä 		return;
355643f328d7SVille Syrjälä 
355743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
355843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
355943f328d7SVille Syrjälä 
3560a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
356143f328d7SVille Syrjälä 
3562a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
356343f328d7SVille Syrjälä 
3564c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
356543f328d7SVille Syrjälä }
356643f328d7SVille Syrjälä 
3567f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3568036a4a7dSZhenyu Wang {
35692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35704697995bSJesse Barnes 
35714697995bSJesse Barnes 	if (!dev_priv)
35724697995bSJesse Barnes 		return;
35734697995bSJesse Barnes 
3574be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3575036a4a7dSZhenyu Wang }
3576036a4a7dSZhenyu Wang 
3577c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3578c2798b19SChris Wilson {
35792d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3580c2798b19SChris Wilson 	int pipe;
3581c2798b19SChris Wilson 
3582055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3583c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3584c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3585c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3586c2798b19SChris Wilson 	POSTING_READ16(IER);
3587c2798b19SChris Wilson }
3588c2798b19SChris Wilson 
3589c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3590c2798b19SChris Wilson {
35912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3592c2798b19SChris Wilson 
3593c2798b19SChris Wilson 	I915_WRITE16(EMR,
3594c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3595c2798b19SChris Wilson 
3596c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3597c2798b19SChris Wilson 	dev_priv->irq_mask =
3598c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3599c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3600c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3601c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3602c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3603c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3604c2798b19SChris Wilson 
3605c2798b19SChris Wilson 	I915_WRITE16(IER,
3606c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3607c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3608c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3609c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3610c2798b19SChris Wilson 	POSTING_READ16(IER);
3611c2798b19SChris Wilson 
3612379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3613379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3614d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3615755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3616755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3617d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3618379ef82dSDaniel Vetter 
3619c2798b19SChris Wilson 	return 0;
3620c2798b19SChris Wilson }
3621c2798b19SChris Wilson 
362290a72f87SVille Syrjälä /*
362390a72f87SVille Syrjälä  * Returns true when a page flip has completed.
362490a72f87SVille Syrjälä  */
362590a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
36261f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
362790a72f87SVille Syrjälä {
36282d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36291f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
363090a72f87SVille Syrjälä 
36318d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
363290a72f87SVille Syrjälä 		return false;
363390a72f87SVille Syrjälä 
363490a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3635d6bbafa1SChris Wilson 		goto check_page_flip;
363690a72f87SVille Syrjälä 
363790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
363890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
363990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
364090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
364190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
364290a72f87SVille Syrjälä 	 */
364390a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3644d6bbafa1SChris Wilson 		goto check_page_flip;
364590a72f87SVille Syrjälä 
36467d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
364790a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
364890a72f87SVille Syrjälä 	return true;
3649d6bbafa1SChris Wilson 
3650d6bbafa1SChris Wilson check_page_flip:
3651d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3652d6bbafa1SChris Wilson 	return false;
365390a72f87SVille Syrjälä }
365490a72f87SVille Syrjälä 
3655ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3656c2798b19SChris Wilson {
365745a83f84SDaniel Vetter 	struct drm_device *dev = arg;
36582d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3659c2798b19SChris Wilson 	u16 iir, new_iir;
3660c2798b19SChris Wilson 	u32 pipe_stats[2];
3661c2798b19SChris Wilson 	int pipe;
3662c2798b19SChris Wilson 	u16 flip_mask =
3663c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3664c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3665c2798b19SChris Wilson 
36662dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36672dd2a883SImre Deak 		return IRQ_NONE;
36682dd2a883SImre Deak 
3669c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3670c2798b19SChris Wilson 	if (iir == 0)
3671c2798b19SChris Wilson 		return IRQ_NONE;
3672c2798b19SChris Wilson 
3673c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3674c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3675c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3676c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3677c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3678c2798b19SChris Wilson 		 */
3679222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3680c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3681aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3682c2798b19SChris Wilson 
3683055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3684c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3685c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3686c2798b19SChris Wilson 
3687c2798b19SChris Wilson 			/*
3688c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3689c2798b19SChris Wilson 			 */
36902d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3691c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3692c2798b19SChris Wilson 		}
3693222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3694c2798b19SChris Wilson 
3695c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3696c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3697c2798b19SChris Wilson 
3698c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3699c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3700c2798b19SChris Wilson 
3701055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37021f1c2e24SVille Syrjälä 			int plane = pipe;
37033a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
37041f1c2e24SVille Syrjälä 				plane = !plane;
37051f1c2e24SVille Syrjälä 
37064356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37071f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
37081f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3709c2798b19SChris Wilson 
37104356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3711277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37122d9d2b0bSVille Syrjälä 
37131f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37141f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37151f7247c0SDaniel Vetter 								    pipe);
37164356d586SDaniel Vetter 		}
3717c2798b19SChris Wilson 
3718c2798b19SChris Wilson 		iir = new_iir;
3719c2798b19SChris Wilson 	}
3720c2798b19SChris Wilson 
3721c2798b19SChris Wilson 	return IRQ_HANDLED;
3722c2798b19SChris Wilson }
3723c2798b19SChris Wilson 
3724c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3725c2798b19SChris Wilson {
37262d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3727c2798b19SChris Wilson 	int pipe;
3728c2798b19SChris Wilson 
3729055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3730c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3731c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3732c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3733c2798b19SChris Wilson 	}
3734c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3735c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3736c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3737c2798b19SChris Wilson }
3738c2798b19SChris Wilson 
3739a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3740a266c7d5SChris Wilson {
37412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3742a266c7d5SChris Wilson 	int pipe;
3743a266c7d5SChris Wilson 
3744a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3745a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3746a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3747a266c7d5SChris Wilson 	}
3748a266c7d5SChris Wilson 
374900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3750055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3751a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3752a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3753a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3754a266c7d5SChris Wilson 	POSTING_READ(IER);
3755a266c7d5SChris Wilson }
3756a266c7d5SChris Wilson 
3757a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3758a266c7d5SChris Wilson {
37592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
376038bde180SChris Wilson 	u32 enable_mask;
3761a266c7d5SChris Wilson 
376238bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
376338bde180SChris Wilson 
376438bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
376538bde180SChris Wilson 	dev_priv->irq_mask =
376638bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
376738bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
376838bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
376938bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
377038bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
377138bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
377238bde180SChris Wilson 
377338bde180SChris Wilson 	enable_mask =
377438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
377538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
377638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
377738bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
377838bde180SChris Wilson 		I915_USER_INTERRUPT;
377938bde180SChris Wilson 
3780a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
378120afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
378220afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
378320afbda2SDaniel Vetter 
3784a266c7d5SChris Wilson 		/* Enable in IER... */
3785a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3786a266c7d5SChris Wilson 		/* and unmask in IMR */
3787a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3788a266c7d5SChris Wilson 	}
3789a266c7d5SChris Wilson 
3790a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3791a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3792a266c7d5SChris Wilson 	POSTING_READ(IER);
3793a266c7d5SChris Wilson 
3794f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
379520afbda2SDaniel Vetter 
3796379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3797379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3798d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3799755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3800755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3801d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3802379ef82dSDaniel Vetter 
380320afbda2SDaniel Vetter 	return 0;
380420afbda2SDaniel Vetter }
380520afbda2SDaniel Vetter 
380690a72f87SVille Syrjälä /*
380790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
380890a72f87SVille Syrjälä  */
380990a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
381090a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
381190a72f87SVille Syrjälä {
38122d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
381390a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
381490a72f87SVille Syrjälä 
38158d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
381690a72f87SVille Syrjälä 		return false;
381790a72f87SVille Syrjälä 
381890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3819d6bbafa1SChris Wilson 		goto check_page_flip;
382090a72f87SVille Syrjälä 
382190a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
382290a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
382390a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
382490a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
382590a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
382690a72f87SVille Syrjälä 	 */
382790a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3828d6bbafa1SChris Wilson 		goto check_page_flip;
382990a72f87SVille Syrjälä 
38307d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
383190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
383290a72f87SVille Syrjälä 	return true;
3833d6bbafa1SChris Wilson 
3834d6bbafa1SChris Wilson check_page_flip:
3835d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3836d6bbafa1SChris Wilson 	return false;
383790a72f87SVille Syrjälä }
383890a72f87SVille Syrjälä 
3839ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3840a266c7d5SChris Wilson {
384145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38438291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
384438bde180SChris Wilson 	u32 flip_mask =
384538bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
384638bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
384738bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3848a266c7d5SChris Wilson 
38492dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38502dd2a883SImre Deak 		return IRQ_NONE;
38512dd2a883SImre Deak 
3852a266c7d5SChris Wilson 	iir = I915_READ(IIR);
385338bde180SChris Wilson 	do {
385438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
38558291ee90SChris Wilson 		bool blc_event = false;
3856a266c7d5SChris Wilson 
3857a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3858a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3859a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3860a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3861a266c7d5SChris Wilson 		 */
3862222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3863a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3864aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3865a266c7d5SChris Wilson 
3866055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3867a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3868a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3869a266c7d5SChris Wilson 
387038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3871a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3872a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
387338bde180SChris Wilson 				irq_received = true;
3874a266c7d5SChris Wilson 			}
3875a266c7d5SChris Wilson 		}
3876222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3877a266c7d5SChris Wilson 
3878a266c7d5SChris Wilson 		if (!irq_received)
3879a266c7d5SChris Wilson 			break;
3880a266c7d5SChris Wilson 
3881a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
388216c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
388316c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
388416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3885a266c7d5SChris Wilson 
388638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3887a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3888a266c7d5SChris Wilson 
3889a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3890a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3891a266c7d5SChris Wilson 
3892055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
389338bde180SChris Wilson 			int plane = pipe;
38943a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
389538bde180SChris Wilson 				plane = !plane;
38965e2032d4SVille Syrjälä 
389790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
389890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
389990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3900a266c7d5SChris Wilson 
3901a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3902a266c7d5SChris Wilson 				blc_event = true;
39034356d586SDaniel Vetter 
39044356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3905277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39062d9d2b0bSVille Syrjälä 
39071f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39081f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39091f7247c0SDaniel Vetter 								    pipe);
3910a266c7d5SChris Wilson 		}
3911a266c7d5SChris Wilson 
3912a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3913a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3914a266c7d5SChris Wilson 
3915a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3916a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3917a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3918a266c7d5SChris Wilson 		 * we would never get another interrupt.
3919a266c7d5SChris Wilson 		 *
3920a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3921a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3922a266c7d5SChris Wilson 		 * another one.
3923a266c7d5SChris Wilson 		 *
3924a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3925a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3926a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3927a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3928a266c7d5SChris Wilson 		 * stray interrupts.
3929a266c7d5SChris Wilson 		 */
393038bde180SChris Wilson 		ret = IRQ_HANDLED;
3931a266c7d5SChris Wilson 		iir = new_iir;
393238bde180SChris Wilson 	} while (iir & ~flip_mask);
3933a266c7d5SChris Wilson 
3934a266c7d5SChris Wilson 	return ret;
3935a266c7d5SChris Wilson }
3936a266c7d5SChris Wilson 
3937a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3938a266c7d5SChris Wilson {
39392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3940a266c7d5SChris Wilson 	int pipe;
3941a266c7d5SChris Wilson 
3942a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3943a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3944a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3945a266c7d5SChris Wilson 	}
3946a266c7d5SChris Wilson 
394700d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
3948055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
394955b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3950a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
395155b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
395255b39755SChris Wilson 	}
3953a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3954a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3955a266c7d5SChris Wilson 
3956a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3957a266c7d5SChris Wilson }
3958a266c7d5SChris Wilson 
3959a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3960a266c7d5SChris Wilson {
39612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3962a266c7d5SChris Wilson 	int pipe;
3963a266c7d5SChris Wilson 
3964a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3965a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3966a266c7d5SChris Wilson 
3967a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3968055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3969a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3970a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3971a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3972a266c7d5SChris Wilson 	POSTING_READ(IER);
3973a266c7d5SChris Wilson }
3974a266c7d5SChris Wilson 
3975a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3976a266c7d5SChris Wilson {
39772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3978bbba0a97SChris Wilson 	u32 enable_mask;
3979a266c7d5SChris Wilson 	u32 error_mask;
3980a266c7d5SChris Wilson 
3981a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3982bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3983adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3984bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3985bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3986bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3987bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3988bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3989bbba0a97SChris Wilson 
3990bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
399121ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
399221ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3993bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3994bbba0a97SChris Wilson 
3995bbba0a97SChris Wilson 	if (IS_G4X(dev))
3996bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3997a266c7d5SChris Wilson 
3998b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3999b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4000d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4001755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4002755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4003755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4004d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4005a266c7d5SChris Wilson 
4006a266c7d5SChris Wilson 	/*
4007a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4008a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4009a266c7d5SChris Wilson 	 */
4010a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4011a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4012a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4013a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4014a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4015a266c7d5SChris Wilson 	} else {
4016a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4017a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4018a266c7d5SChris Wilson 	}
4019a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4020a266c7d5SChris Wilson 
4021a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4022a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4023a266c7d5SChris Wilson 	POSTING_READ(IER);
4024a266c7d5SChris Wilson 
402520afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
402620afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
402720afbda2SDaniel Vetter 
4028f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
402920afbda2SDaniel Vetter 
403020afbda2SDaniel Vetter 	return 0;
403120afbda2SDaniel Vetter }
403220afbda2SDaniel Vetter 
4033bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
403420afbda2SDaniel Vetter {
40352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4036cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
403720afbda2SDaniel Vetter 	u32 hotplug_en;
403820afbda2SDaniel Vetter 
4039b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4040b5ea2d56SDaniel Vetter 
4041bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4042bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4043adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4044e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
4045b2784e15SDamien Lespiau 	for_each_intel_encoder(dev, intel_encoder)
4046cd569aedSEgbert Eich 		if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4047cd569aedSEgbert Eich 			hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4048a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4049a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4050a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4051a266c7d5SChris Wilson 	*/
4052a266c7d5SChris Wilson 	if (IS_G4X(dev))
4053a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
405485fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4055a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4056a266c7d5SChris Wilson 
4057a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4058a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4059a266c7d5SChris Wilson }
4060a266c7d5SChris Wilson 
4061ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4062a266c7d5SChris Wilson {
406345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4065a266c7d5SChris Wilson 	u32 iir, new_iir;
4066a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4067a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
406821ad8330SVille Syrjälä 	u32 flip_mask =
406921ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
407021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4071a266c7d5SChris Wilson 
40722dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40732dd2a883SImre Deak 		return IRQ_NONE;
40742dd2a883SImre Deak 
4075a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4076a266c7d5SChris Wilson 
4077a266c7d5SChris Wilson 	for (;;) {
4078501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
40792c8ba29fSChris Wilson 		bool blc_event = false;
40802c8ba29fSChris Wilson 
4081a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4082a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4083a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4084a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4085a266c7d5SChris Wilson 		 */
4086222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4087a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4088aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4089a266c7d5SChris Wilson 
4090055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4091a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4092a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4093a266c7d5SChris Wilson 
4094a266c7d5SChris Wilson 			/*
4095a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4096a266c7d5SChris Wilson 			 */
4097a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4098a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4099501e01d7SVille Syrjälä 				irq_received = true;
4100a266c7d5SChris Wilson 			}
4101a266c7d5SChris Wilson 		}
4102222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4103a266c7d5SChris Wilson 
4104a266c7d5SChris Wilson 		if (!irq_received)
4105a266c7d5SChris Wilson 			break;
4106a266c7d5SChris Wilson 
4107a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4108a266c7d5SChris Wilson 
4109a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
411016c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
411116c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4112a266c7d5SChris Wilson 
411321ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4114a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4115a266c7d5SChris Wilson 
4116a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4117a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4118a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4119a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4120a266c7d5SChris Wilson 
4121055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41222c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
412390a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
412490a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4125a266c7d5SChris Wilson 
4126a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4127a266c7d5SChris Wilson 				blc_event = true;
41284356d586SDaniel Vetter 
41294356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4130277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4131a266c7d5SChris Wilson 
41321f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41331f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41342d9d2b0bSVille Syrjälä 		}
4135a266c7d5SChris Wilson 
4136a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4137a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4138a266c7d5SChris Wilson 
4139515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4140515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4141515ac2bbSDaniel Vetter 
4142a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4143a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4144a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4145a266c7d5SChris Wilson 		 * we would never get another interrupt.
4146a266c7d5SChris Wilson 		 *
4147a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4148a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4149a266c7d5SChris Wilson 		 * another one.
4150a266c7d5SChris Wilson 		 *
4151a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4152a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4153a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4154a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4155a266c7d5SChris Wilson 		 * stray interrupts.
4156a266c7d5SChris Wilson 		 */
4157a266c7d5SChris Wilson 		iir = new_iir;
4158a266c7d5SChris Wilson 	}
4159a266c7d5SChris Wilson 
4160a266c7d5SChris Wilson 	return ret;
4161a266c7d5SChris Wilson }
4162a266c7d5SChris Wilson 
4163a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4164a266c7d5SChris Wilson {
41652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4166a266c7d5SChris Wilson 	int pipe;
4167a266c7d5SChris Wilson 
4168a266c7d5SChris Wilson 	if (!dev_priv)
4169a266c7d5SChris Wilson 		return;
4170a266c7d5SChris Wilson 
4171a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4172a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4173a266c7d5SChris Wilson 
4174a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4175055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4176a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4177a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4178a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4179a266c7d5SChris Wilson 
4180055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4181a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4182a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4183a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4184a266c7d5SChris Wilson }
4185a266c7d5SChris Wilson 
41864cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4187ac4c16c5SEgbert Eich {
41886323751dSImre Deak 	struct drm_i915_private *dev_priv =
41896323751dSImre Deak 		container_of(work, typeof(*dev_priv),
41906323751dSImre Deak 			     hotplug_reenable_work.work);
4191ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4192ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4193ac4c16c5SEgbert Eich 	int i;
4194ac4c16c5SEgbert Eich 
41956323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
41966323751dSImre Deak 
41974cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4198ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4199ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4200ac4c16c5SEgbert Eich 
4201ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4202ac4c16c5SEgbert Eich 			continue;
4203ac4c16c5SEgbert Eich 
4204ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4205ac4c16c5SEgbert Eich 
4206ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4207ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4208ac4c16c5SEgbert Eich 
4209ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4210ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4211ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4212c23cc417SJani Nikula 							 connector->name);
4213ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4214ac4c16c5SEgbert Eich 				if (!connector->polled)
4215ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4216ac4c16c5SEgbert Eich 			}
4217ac4c16c5SEgbert Eich 		}
4218ac4c16c5SEgbert Eich 	}
4219ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4220ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
42214cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
42226323751dSImre Deak 
42236323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4224ac4c16c5SEgbert Eich }
4225ac4c16c5SEgbert Eich 
4226fca52a55SDaniel Vetter /**
4227fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4228fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4229fca52a55SDaniel Vetter  *
4230fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4231fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4232fca52a55SDaniel Vetter  */
4233b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4234f71d4af4SJesse Barnes {
4235b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
42368b2e326dSChris Wilson 
42378b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
423813cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
4239c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4240a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42418b2e326dSChris Wilson 
4242a6706b45SDeepak S 	/* Let's track the enabled rps events */
4243b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
42446c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
42456f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
424631685c25SDeepak S 	else
4247a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4248a6706b45SDeepak S 
4249737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4250737b1506SChris Wilson 			  i915_hangcheck_elapsed);
42516323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
42524cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
425361bac78eSDaniel Vetter 
425497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
42559ee32feaSDaniel Vetter 
4256b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42574cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
42584cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4259b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4260f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4261f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4262391f75e2SVille Syrjälä 	} else {
4263391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4264391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4265f71d4af4SJesse Barnes 	}
4266f71d4af4SJesse Barnes 
426721da2700SVille Syrjälä 	/*
426821da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
426921da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
427021da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
427121da2700SVille Syrjälä 	 */
4272b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
427321da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
427421da2700SVille Syrjälä 
4275f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4276f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4277f71d4af4SJesse Barnes 
4278b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
427943f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
428043f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
428143f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
428243f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
428343f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
428443f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
428543f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4286b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
42877e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
42887e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
42897e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
42907e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
42917e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
42927e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4293fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4294b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4295abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4296723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4297abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4298abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4299abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4300abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4301abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4302f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4303f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4304723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4305f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4306f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4307f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4308f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
430982a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4310f71d4af4SJesse Barnes 	} else {
4311b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4312c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4313c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4314c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4315c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4316b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4317a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4318a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4319a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4320a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4321c2798b19SChris Wilson 		} else {
4322a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4323a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4324a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4325a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4326c2798b19SChris Wilson 		}
4327778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4328778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4329f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4330f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4331f71d4af4SJesse Barnes 	}
4332f71d4af4SJesse Barnes }
433320afbda2SDaniel Vetter 
4334fca52a55SDaniel Vetter /**
4335fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4336fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4337fca52a55SDaniel Vetter  *
4338fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4339fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4340fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4341fca52a55SDaniel Vetter  * obeyed.
4342fca52a55SDaniel Vetter  *
4343fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4344fca52a55SDaniel Vetter  * in the driver load and resume code.
4345fca52a55SDaniel Vetter  */
4346b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
434720afbda2SDaniel Vetter {
4348b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4349821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4350821450c6SEgbert Eich 	struct drm_connector *connector;
4351821450c6SEgbert Eich 	int i;
435220afbda2SDaniel Vetter 
4353821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4354821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4355821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4356821450c6SEgbert Eich 	}
4357821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4358821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4359821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
43600e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
43610e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
43620e32b39cSDave Airlie 		if (intel_connector->mst_port)
4363821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4364821450c6SEgbert Eich 	}
4365b5ea2d56SDaniel Vetter 
4366b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4367b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4368d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
436920afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
437020afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4371d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
437220afbda2SDaniel Vetter }
4373c67a470bSPaulo Zanoni 
4374fca52a55SDaniel Vetter /**
4375fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4376fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4377fca52a55SDaniel Vetter  *
4378fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4379fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4380fca52a55SDaniel Vetter  *
4381fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4382fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4383fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4384fca52a55SDaniel Vetter  */
43852aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
43862aeb7d3aSDaniel Vetter {
43872aeb7d3aSDaniel Vetter 	/*
43882aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
43892aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
43902aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
43912aeb7d3aSDaniel Vetter 	 */
43922aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
43932aeb7d3aSDaniel Vetter 
43942aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
43952aeb7d3aSDaniel Vetter }
43962aeb7d3aSDaniel Vetter 
4397fca52a55SDaniel Vetter /**
4398fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4399fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4400fca52a55SDaniel Vetter  *
4401fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4402fca52a55SDaniel Vetter  * resources acquired in the init functions.
4403fca52a55SDaniel Vetter  */
44042aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44052aeb7d3aSDaniel Vetter {
44062aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44072aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44082aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44092aeb7d3aSDaniel Vetter }
44102aeb7d3aSDaniel Vetter 
4411fca52a55SDaniel Vetter /**
4412fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4413fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4414fca52a55SDaniel Vetter  *
4415fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4416fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4417fca52a55SDaniel Vetter  */
4418b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4419c67a470bSPaulo Zanoni {
4420b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44212aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44222dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4423c67a470bSPaulo Zanoni }
4424c67a470bSPaulo Zanoni 
4425fca52a55SDaniel Vetter /**
4426fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4427fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4428fca52a55SDaniel Vetter  *
4429fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4430fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4431fca52a55SDaniel Vetter  */
4432b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4433c67a470bSPaulo Zanoni {
44342aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4435b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4436b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4437c67a470bSPaulo Zanoni }
4438