xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 74cdb337c0fc7d97e7db719051e51abcc7bd9579)
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);
280096fad9eSImre 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 
988*74cdb337SChris Wilson static void notify_ring(struct intel_engine_cs *ring)
989549f7365SChris Wilson {
99093b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
991475553deSChris Wilson 		return;
992475553deSChris Wilson 
993bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9949862e600SChris Wilson 
995549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
996549f7365SChris Wilson }
997549f7365SChris Wilson 
99843cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
99943cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
100031685c25SDeepak S {
100143cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
100243cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
100343cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
100431685c25SDeepak S }
100531685c25SDeepak S 
100643cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
100743cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
100843cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
100943cf3bf0SChris Wilson 			 int threshold)
101031685c25SDeepak S {
101143cf3bf0SChris Wilson 	u64 time, c0;
101231685c25SDeepak S 
101343cf3bf0SChris Wilson 	if (old->cz_clock == 0)
101443cf3bf0SChris Wilson 		return false;
101531685c25SDeepak S 
101643cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
101743cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
101831685c25SDeepak S 
101943cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
102043cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
102143cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
102243cf3bf0SChris Wilson 	 */
102343cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
102443cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
102543cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
102631685c25SDeepak S 
102743cf3bf0SChris Wilson 	return c0 >= time;
102831685c25SDeepak S }
102931685c25SDeepak S 
103043cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
103143cf3bf0SChris Wilson {
103243cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
103343cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
103443cf3bf0SChris Wilson }
103543cf3bf0SChris Wilson 
103643cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
103743cf3bf0SChris Wilson {
103843cf3bf0SChris Wilson 	struct intel_rps_ei now;
103943cf3bf0SChris Wilson 	u32 events = 0;
104043cf3bf0SChris Wilson 
10416f4b12f8SChris Wilson 	if ((pm_iir & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED)) == 0)
104243cf3bf0SChris Wilson 		return 0;
104343cf3bf0SChris Wilson 
104443cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
104543cf3bf0SChris Wilson 	if (now.cz_clock == 0)
104643cf3bf0SChris Wilson 		return 0;
104731685c25SDeepak S 
104843cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
104943cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
105043cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
10518fb55197SChris Wilson 				  dev_priv->rps.down_threshold))
105243cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
105343cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
105431685c25SDeepak S 	}
105531685c25SDeepak S 
105643cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
105743cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
105843cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
10598fb55197SChris Wilson 				 dev_priv->rps.up_threshold))
106043cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
106143cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
106243cf3bf0SChris Wilson 	}
106343cf3bf0SChris Wilson 
106443cf3bf0SChris Wilson 	return events;
106531685c25SDeepak S }
106631685c25SDeepak S 
10674912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10683b8d8d91SJesse Barnes {
10692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10702d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1071edbfdb45SPaulo Zanoni 	u32 pm_iir;
1072dd75fdc8SChris Wilson 	int new_delay, adj;
10733b8d8d91SJesse Barnes 
107459cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1075d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1076d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1077d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1078d4d70aa5SImre Deak 		return;
1079d4d70aa5SImre Deak 	}
1080c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1081c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1082a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1083480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
108459cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10854912d041SBen Widawsky 
108660611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1087a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
108860611c13SPaulo Zanoni 
1089a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
10903b8d8d91SJesse Barnes 		return;
10913b8d8d91SJesse Barnes 
10924fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
10937b9e0ae6SChris Wilson 
109443cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
109543cf3bf0SChris Wilson 
1096dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
1097edcf284bSChris Wilson 	new_delay = dev_priv->rps.cur_freq;
10987425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1099dd75fdc8SChris Wilson 		if (adj > 0)
1100dd75fdc8SChris Wilson 			adj *= 2;
1101edcf284bSChris Wilson 		else /* CHV needs even encode values */
1102edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? 2 : 1;
11037425034aSVille Syrjälä 		/*
11047425034aSVille Syrjälä 		 * For better performance, jump directly
11057425034aSVille Syrjälä 		 * to RPe if we're below it.
11067425034aSVille Syrjälä 		 */
1107edcf284bSChris Wilson 		if (new_delay < dev_priv->rps.efficient_freq - adj) {
1108b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1109edcf284bSChris Wilson 			adj = 0;
1110edcf284bSChris Wilson 		}
1111dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1112b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1113b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1114dd75fdc8SChris Wilson 		else
1115b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1116dd75fdc8SChris Wilson 		adj = 0;
1117dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1118dd75fdc8SChris Wilson 		if (adj < 0)
1119dd75fdc8SChris Wilson 			adj *= 2;
1120edcf284bSChris Wilson 		else /* CHV needs even encode values */
1121edcf284bSChris Wilson 			adj = IS_CHERRYVIEW(dev_priv) ? -2 : -1;
1122dd75fdc8SChris Wilson 	} else { /* unknown event */
1123edcf284bSChris Wilson 		adj = 0;
1124dd75fdc8SChris Wilson 	}
11253b8d8d91SJesse Barnes 
1126edcf284bSChris Wilson 	dev_priv->rps.last_adj = adj;
1127edcf284bSChris Wilson 
112879249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
112979249636SBen Widawsky 	 * interrupt
113079249636SBen Widawsky 	 */
1131edcf284bSChris Wilson 	new_delay += adj;
11321272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1133b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1134b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
113527544369SDeepak S 
1136ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11373b8d8d91SJesse Barnes 
11384fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11393b8d8d91SJesse Barnes }
11403b8d8d91SJesse Barnes 
1141e3689190SBen Widawsky 
1142e3689190SBen Widawsky /**
1143e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1144e3689190SBen Widawsky  * occurred.
1145e3689190SBen Widawsky  * @work: workqueue struct
1146e3689190SBen Widawsky  *
1147e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1148e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1149e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1150e3689190SBen Widawsky  */
1151e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1152e3689190SBen Widawsky {
11532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11542d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1155e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
115635a85ac6SBen Widawsky 	char *parity_event[6];
1157e3689190SBen Widawsky 	uint32_t misccpctl;
115835a85ac6SBen Widawsky 	uint8_t slice = 0;
1159e3689190SBen Widawsky 
1160e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1161e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1162e3689190SBen Widawsky 	 * any time we access those registers.
1163e3689190SBen Widawsky 	 */
1164e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1165e3689190SBen Widawsky 
116635a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
116735a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
116835a85ac6SBen Widawsky 		goto out;
116935a85ac6SBen Widawsky 
1170e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1171e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1172e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1173e3689190SBen Widawsky 
117435a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
117535a85ac6SBen Widawsky 		u32 reg;
117635a85ac6SBen Widawsky 
117735a85ac6SBen Widawsky 		slice--;
117835a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
117935a85ac6SBen Widawsky 			break;
118035a85ac6SBen Widawsky 
118135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
118235a85ac6SBen Widawsky 
118335a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
118435a85ac6SBen Widawsky 
118535a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1186e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1187e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1188e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1189e3689190SBen Widawsky 
119035a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
119135a85ac6SBen Widawsky 		POSTING_READ(reg);
1192e3689190SBen Widawsky 
1193cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1194e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1195e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1196e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
119735a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
119835a85ac6SBen Widawsky 		parity_event[5] = NULL;
1199e3689190SBen Widawsky 
12005bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1201e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1202e3689190SBen Widawsky 
120335a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
120435a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1205e3689190SBen Widawsky 
120635a85ac6SBen Widawsky 		kfree(parity_event[4]);
1207e3689190SBen Widawsky 		kfree(parity_event[3]);
1208e3689190SBen Widawsky 		kfree(parity_event[2]);
1209e3689190SBen Widawsky 		kfree(parity_event[1]);
1210e3689190SBen Widawsky 	}
1211e3689190SBen Widawsky 
121235a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
121335a85ac6SBen Widawsky 
121435a85ac6SBen Widawsky out:
121535a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12164cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1217480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12184cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
121935a85ac6SBen Widawsky 
122035a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
122135a85ac6SBen Widawsky }
122235a85ac6SBen Widawsky 
122335a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1224e3689190SBen Widawsky {
12252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1226e3689190SBen Widawsky 
1227040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1228e3689190SBen Widawsky 		return;
1229e3689190SBen Widawsky 
1230d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1231480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1232d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1233e3689190SBen Widawsky 
123435a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
123535a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
123635a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
123735a85ac6SBen Widawsky 
123835a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
123935a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
124035a85ac6SBen Widawsky 
1241a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1242e3689190SBen Widawsky }
1243e3689190SBen Widawsky 
1244f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1245f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1246f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1247f1af8fc1SPaulo Zanoni {
1248f1af8fc1SPaulo Zanoni 	if (gt_iir &
1249f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1250*74cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1251f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1252*74cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1253f1af8fc1SPaulo Zanoni }
1254f1af8fc1SPaulo Zanoni 
1255e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1256e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1257e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1258e7b4c6b1SDaniel Vetter {
1259e7b4c6b1SDaniel Vetter 
1260cc609d5dSBen Widawsky 	if (gt_iir &
1261cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1262*74cdb337SChris Wilson 		notify_ring(&dev_priv->ring[RCS]);
1263cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1264*74cdb337SChris Wilson 		notify_ring(&dev_priv->ring[VCS]);
1265cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1266*74cdb337SChris Wilson 		notify_ring(&dev_priv->ring[BCS]);
1267e7b4c6b1SDaniel Vetter 
1268cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1269cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1270aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1271aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1272e3689190SBen Widawsky 
127335a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
127435a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1275e7b4c6b1SDaniel Vetter }
1276e7b4c6b1SDaniel Vetter 
1277*74cdb337SChris Wilson static irqreturn_t gen8_gt_irq_handler(struct drm_i915_private *dev_priv,
1278abd58f01SBen Widawsky 				       u32 master_ctl)
1279abd58f01SBen Widawsky {
1280abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1281abd58f01SBen Widawsky 
1282abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1283*74cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(0));
1284abd58f01SBen Widawsky 		if (tmp) {
1285cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(0), tmp);
1286abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1287e981e7b1SThomas Daniel 
1288*74cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
1289*74cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[RCS]);
1290*74cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT))
1291*74cdb337SChris Wilson 				notify_ring(&dev_priv->ring[RCS]);
1292e981e7b1SThomas Daniel 
1293*74cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
1294*74cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[BCS]);
1295*74cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT))
1296*74cdb337SChris Wilson 				notify_ring(&dev_priv->ring[BCS]);
1297abd58f01SBen Widawsky 		} else
1298abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1299abd58f01SBen Widawsky 	}
1300abd58f01SBen Widawsky 
130185f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1302*74cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(1));
1303abd58f01SBen Widawsky 		if (tmp) {
1304cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(1), tmp);
1305abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1306e981e7b1SThomas Daniel 
1307*74cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
1308*74cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS]);
1309*74cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT))
1310*74cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS]);
1311e981e7b1SThomas Daniel 
1312*74cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
1313*74cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VCS2]);
1314*74cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT))
1315*74cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VCS2]);
1316abd58f01SBen Widawsky 		} else
1317abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1318abd58f01SBen Widawsky 	}
1319abd58f01SBen Widawsky 
1320*74cdb337SChris Wilson 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1321*74cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(3));
1322*74cdb337SChris Wilson 		if (tmp) {
1323*74cdb337SChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(3), tmp);
1324*74cdb337SChris Wilson 			ret = IRQ_HANDLED;
1325*74cdb337SChris Wilson 
1326*74cdb337SChris Wilson 			if (tmp & (GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
1327*74cdb337SChris Wilson 				intel_lrc_irq_handler(&dev_priv->ring[VECS]);
1328*74cdb337SChris Wilson 			if (tmp & (GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT))
1329*74cdb337SChris Wilson 				notify_ring(&dev_priv->ring[VECS]);
1330*74cdb337SChris Wilson 		} else
1331*74cdb337SChris Wilson 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1332*74cdb337SChris Wilson 	}
1333*74cdb337SChris Wilson 
13340961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
1335*74cdb337SChris Wilson 		u32 tmp = I915_READ_FW(GEN8_GT_IIR(2));
13360961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
1337cb0d205eSChris Wilson 			I915_WRITE_FW(GEN8_GT_IIR(2),
13380961021aSBen Widawsky 				      tmp & dev_priv->pm_rps_events);
133938cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1340c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13410961021aSBen Widawsky 		} else
13420961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13430961021aSBen Widawsky 	}
13440961021aSBen Widawsky 
1345abd58f01SBen Widawsky 	return ret;
1346abd58f01SBen Widawsky }
1347abd58f01SBen Widawsky 
1348b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1349b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1350b543fb04SEgbert Eich 
135107c338ceSJani Nikula static int pch_port_to_hotplug_shift(enum port port)
135213cf5504SDave Airlie {
135313cf5504SDave Airlie 	switch (port) {
135413cf5504SDave Airlie 	case PORT_A:
135513cf5504SDave Airlie 	case PORT_E:
135613cf5504SDave Airlie 	default:
135713cf5504SDave Airlie 		return -1;
135813cf5504SDave Airlie 	case PORT_B:
135913cf5504SDave Airlie 		return 0;
136013cf5504SDave Airlie 	case PORT_C:
136113cf5504SDave Airlie 		return 8;
136213cf5504SDave Airlie 	case PORT_D:
136313cf5504SDave Airlie 		return 16;
136413cf5504SDave Airlie 	}
136513cf5504SDave Airlie }
136613cf5504SDave Airlie 
136707c338ceSJani Nikula static int i915_port_to_hotplug_shift(enum port port)
136813cf5504SDave Airlie {
136913cf5504SDave Airlie 	switch (port) {
137013cf5504SDave Airlie 	case PORT_A:
137113cf5504SDave Airlie 	case PORT_E:
137213cf5504SDave Airlie 	default:
137313cf5504SDave Airlie 		return -1;
137413cf5504SDave Airlie 	case PORT_B:
137513cf5504SDave Airlie 		return 17;
137613cf5504SDave Airlie 	case PORT_C:
137713cf5504SDave Airlie 		return 19;
137813cf5504SDave Airlie 	case PORT_D:
137913cf5504SDave Airlie 		return 21;
138013cf5504SDave Airlie 	}
138113cf5504SDave Airlie }
138213cf5504SDave Airlie 
138313cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
138413cf5504SDave Airlie {
138513cf5504SDave Airlie 	switch (pin) {
138613cf5504SDave Airlie 	case HPD_PORT_B:
138713cf5504SDave Airlie 		return PORT_B;
138813cf5504SDave Airlie 	case HPD_PORT_C:
138913cf5504SDave Airlie 		return PORT_C;
139013cf5504SDave Airlie 	case HPD_PORT_D:
139113cf5504SDave Airlie 		return PORT_D;
139213cf5504SDave Airlie 	default:
139313cf5504SDave Airlie 		return PORT_A; /* no hpd */
139413cf5504SDave Airlie 	}
139513cf5504SDave Airlie }
139613cf5504SDave Airlie 
139710a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1398b543fb04SEgbert Eich 					 u32 hotplug_trigger,
139913cf5504SDave Airlie 					 u32 dig_hotplug_reg,
14007c7e10dbSVille Syrjälä 					 const u32 hpd[HPD_NUM_PINS])
1401b543fb04SEgbert Eich {
14022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1403b543fb04SEgbert Eich 	int i;
140413cf5504SDave Airlie 	enum port port;
140510a504deSDaniel Vetter 	bool storm_detected = false;
140613cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
140713cf5504SDave Airlie 	u32 dig_shift;
140813cf5504SDave Airlie 	u32 dig_port_mask = 0;
1409b543fb04SEgbert Eich 
141091d131d2SDaniel Vetter 	if (!hotplug_trigger)
141191d131d2SDaniel Vetter 		return;
141291d131d2SDaniel Vetter 
141313cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
141413cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1415cc9bd499SImre Deak 
1416b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1417b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
141813cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
141913cf5504SDave Airlie 			continue;
1420821450c6SEgbert Eich 
142113cf5504SDave Airlie 		port = get_port_from_pin(i);
142213cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
142313cf5504SDave Airlie 			bool long_hpd;
142413cf5504SDave Airlie 
142507c338ceSJani Nikula 			if (HAS_PCH_SPLIT(dev)) {
142607c338ceSJani Nikula 				dig_shift = pch_port_to_hotplug_shift(port);
142713cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
142807c338ceSJani Nikula 			} else {
142907c338ceSJani Nikula 				dig_shift = i915_port_to_hotplug_shift(port);
143007c338ceSJani Nikula 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
143113cf5504SDave Airlie 			}
143213cf5504SDave Airlie 
143326fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
143426fbb774SVille Syrjälä 					 port_name(port),
143526fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
143613cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
143713cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
143813cf5504SDave Airlie 			if (long_hpd) {
143913cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
144013cf5504SDave Airlie 				dig_port_mask |= hpd[i];
144113cf5504SDave Airlie 			} else {
144213cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
144313cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
144413cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
144513cf5504SDave Airlie 			}
144613cf5504SDave Airlie 			queue_dig = true;
144713cf5504SDave Airlie 		}
144813cf5504SDave Airlie 	}
144913cf5504SDave Airlie 
145013cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
14513ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
14523ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
14533ff04a16SDaniel Vetter 			/*
14543ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
14553ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
14563ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
14573ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
14583ff04a16SDaniel Vetter 			 */
14593ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1460cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1461cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1462b8f102e8SEgbert Eich 
14633ff04a16SDaniel Vetter 			continue;
14643ff04a16SDaniel Vetter 		}
14653ff04a16SDaniel Vetter 
1466b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1467b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1468b543fb04SEgbert Eich 			continue;
1469b543fb04SEgbert Eich 
147013cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1471bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
147213cf5504SDave Airlie 			queue_hp = true;
147313cf5504SDave Airlie 		}
147413cf5504SDave Airlie 
1475b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1476b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1477b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1478b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1479b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1480b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1481b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1482b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1483142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1484b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
148510a504deSDaniel Vetter 			storm_detected = true;
1486b543fb04SEgbert Eich 		} else {
1487b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1488b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1489b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1490b543fb04SEgbert Eich 		}
1491b543fb04SEgbert Eich 	}
1492b543fb04SEgbert Eich 
149310a504deSDaniel Vetter 	if (storm_detected)
149410a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1495b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
14965876fa0dSDaniel Vetter 
1497645416f5SDaniel Vetter 	/*
1498645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1499645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1500645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1501645416f5SDaniel Vetter 	 * deadlock.
1502645416f5SDaniel Vetter 	 */
150313cf5504SDave Airlie 	if (queue_dig)
15040e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
150513cf5504SDave Airlie 	if (queue_hp)
1506645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1507b543fb04SEgbert Eich }
1508b543fb04SEgbert Eich 
1509515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1510515ac2bbSDaniel Vetter {
15112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
151228c70f16SDaniel Vetter 
151328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1514515ac2bbSDaniel Vetter }
1515515ac2bbSDaniel Vetter 
1516ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1517ce99c256SDaniel Vetter {
15182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15199ee32feaSDaniel Vetter 
15209ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1521ce99c256SDaniel Vetter }
1522ce99c256SDaniel Vetter 
15238bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1524277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1525eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1526eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15278bc5e955SDaniel Vetter 					 uint32_t crc4)
15288bf1e9f1SShuang He {
15298bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15308bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15318bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1532ac2300d4SDamien Lespiau 	int head, tail;
1533b2c88f5bSDamien Lespiau 
1534d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1535d538bbdfSDamien Lespiau 
15360c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1537d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
153834273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15390c912c79SDamien Lespiau 		return;
15400c912c79SDamien Lespiau 	}
15410c912c79SDamien Lespiau 
1542d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1543d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1544b2c88f5bSDamien Lespiau 
1545b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1546d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1547b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1548b2c88f5bSDamien Lespiau 		return;
1549b2c88f5bSDamien Lespiau 	}
1550b2c88f5bSDamien Lespiau 
1551b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15528bf1e9f1SShuang He 
15538bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1554eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1555eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1556eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1557eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1558eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1559b2c88f5bSDamien Lespiau 
1560b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1561d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1562d538bbdfSDamien Lespiau 
1563d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
156407144428SDamien Lespiau 
156507144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15668bf1e9f1SShuang He }
1567277de95eSDaniel Vetter #else
1568277de95eSDaniel Vetter static inline void
1569277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1570277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1571277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1572277de95eSDaniel Vetter 			     uint32_t crc4) {}
1573277de95eSDaniel Vetter #endif
1574eba94eb9SDaniel Vetter 
1575277de95eSDaniel Vetter 
1576277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15775a69b89fSDaniel Vetter {
15785a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
15795a69b89fSDaniel Vetter 
1580277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
15815a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
15825a69b89fSDaniel Vetter 				     0, 0, 0, 0);
15835a69b89fSDaniel Vetter }
15845a69b89fSDaniel Vetter 
1585277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1586eba94eb9SDaniel Vetter {
1587eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1588eba94eb9SDaniel Vetter 
1589277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1590eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1591eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1592eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1593eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
15948bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1595eba94eb9SDaniel Vetter }
15965b3a856bSDaniel Vetter 
1597277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
15985b3a856bSDaniel Vetter {
15995b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16000b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16010b5c5ed0SDaniel Vetter 
16020b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16030b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16040b5c5ed0SDaniel Vetter 	else
16050b5c5ed0SDaniel Vetter 		res1 = 0;
16060b5c5ed0SDaniel Vetter 
16070b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16080b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16090b5c5ed0SDaniel Vetter 	else
16100b5c5ed0SDaniel Vetter 		res2 = 0;
16115b3a856bSDaniel Vetter 
1612277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16130b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16140b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16150b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16160b5c5ed0SDaniel Vetter 				     res1, res2);
16175b3a856bSDaniel Vetter }
16188bf1e9f1SShuang He 
16191403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16201403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16211403c0d4SPaulo Zanoni  * the work queue. */
16221403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1623baf02a1fSBen Widawsky {
1624a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
162559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1626480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1627d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1628d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16292adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
163041a05a3aSDaniel Vetter 		}
1631d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1632d4d70aa5SImre Deak 	}
1633baf02a1fSBen Widawsky 
1634c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1635c9a9a268SImre Deak 		return;
1636c9a9a268SImre Deak 
16371403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
163812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
1639*74cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VECS]);
164012638c57SBen Widawsky 
1641aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1642aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
164312638c57SBen Widawsky 	}
16441403c0d4SPaulo Zanoni }
1645baf02a1fSBen Widawsky 
16468d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16478d7849dbSVille Syrjälä {
16488d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16498d7849dbSVille Syrjälä 		return false;
16508d7849dbSVille Syrjälä 
16518d7849dbSVille Syrjälä 	return true;
16528d7849dbSVille Syrjälä }
16538d7849dbSVille Syrjälä 
1654c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16557e231dbeSJesse Barnes {
1656c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
165791d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16587e231dbeSJesse Barnes 	int pipe;
16597e231dbeSJesse Barnes 
166058ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1661055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
166291d181ddSImre Deak 		int reg;
1663bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
166491d181ddSImre Deak 
1665bbb5eebfSDaniel Vetter 		/*
1666bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1667bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1668bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1669bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1670bbb5eebfSDaniel Vetter 		 * handle.
1671bbb5eebfSDaniel Vetter 		 */
16720f239f4cSDaniel Vetter 
16730f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
16740f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1675bbb5eebfSDaniel Vetter 
1676bbb5eebfSDaniel Vetter 		switch (pipe) {
1677bbb5eebfSDaniel Vetter 		case PIPE_A:
1678bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1679bbb5eebfSDaniel Vetter 			break;
1680bbb5eebfSDaniel Vetter 		case PIPE_B:
1681bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1682bbb5eebfSDaniel Vetter 			break;
16833278f67fSVille Syrjälä 		case PIPE_C:
16843278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
16853278f67fSVille Syrjälä 			break;
1686bbb5eebfSDaniel Vetter 		}
1687bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1688bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1689bbb5eebfSDaniel Vetter 
1690bbb5eebfSDaniel Vetter 		if (!mask)
169191d181ddSImre Deak 			continue;
169291d181ddSImre Deak 
169391d181ddSImre Deak 		reg = PIPESTAT(pipe);
1694bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1695bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
16967e231dbeSJesse Barnes 
16977e231dbeSJesse Barnes 		/*
16987e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
16997e231dbeSJesse Barnes 		 */
170091d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
170191d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17027e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17037e231dbeSJesse Barnes 	}
170458ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17057e231dbeSJesse Barnes 
1706055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1707d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1708d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1709d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
171031acc7f5SJesse Barnes 
1711579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
171231acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
171331acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
171431acc7f5SJesse Barnes 		}
17154356d586SDaniel Vetter 
17164356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1717277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17182d9d2b0bSVille Syrjälä 
17191f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17201f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
172131acc7f5SJesse Barnes 	}
172231acc7f5SJesse Barnes 
1723c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1724c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1725c1874ed7SImre Deak }
1726c1874ed7SImre Deak 
172716c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
172816c6c56bSVille Syrjälä {
172916c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
173016c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
173116c6c56bSVille Syrjälä 
17323ff60f89SOscar Mateo 	if (hotplug_status) {
17333ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17343ff60f89SOscar Mateo 		/*
17353ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
17363ff60f89SOscar Mateo 		 * may miss hotplug events.
17373ff60f89SOscar Mateo 		 */
17383ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
17393ff60f89SOscar Mateo 
174016c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
174116c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
174216c6c56bSVille Syrjälä 
174313cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
174416c6c56bSVille Syrjälä 		} else {
174516c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
174616c6c56bSVille Syrjälä 
174713cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
174816c6c56bSVille Syrjälä 		}
174916c6c56bSVille Syrjälä 
175016c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
175116c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
175216c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
17533ff60f89SOscar Mateo 	}
175416c6c56bSVille Syrjälä }
175516c6c56bSVille Syrjälä 
1756c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1757c1874ed7SImre Deak {
175845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1760c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1761c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1762c1874ed7SImre Deak 
17632dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17642dd2a883SImre Deak 		return IRQ_NONE;
17652dd2a883SImre Deak 
1766c1874ed7SImre Deak 	while (true) {
17673ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17683ff60f89SOscar Mateo 
1769c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
17703ff60f89SOscar Mateo 		if (gt_iir)
17713ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
17723ff60f89SOscar Mateo 
1773c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
17743ff60f89SOscar Mateo 		if (pm_iir)
17753ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
17763ff60f89SOscar Mateo 
17773ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
17783ff60f89SOscar Mateo 		if (iir) {
17793ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
17803ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
17813ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
17823ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
17833ff60f89SOscar Mateo 		}
1784c1874ed7SImre Deak 
1785c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1786c1874ed7SImre Deak 			goto out;
1787c1874ed7SImre Deak 
1788c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1789c1874ed7SImre Deak 
17903ff60f89SOscar Mateo 		if (gt_iir)
1791c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
179260611c13SPaulo Zanoni 		if (pm_iir)
1793d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
17943ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
17953ff60f89SOscar Mateo 		 * signalled in iir */
17963ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
17977e231dbeSJesse Barnes 	}
17987e231dbeSJesse Barnes 
17997e231dbeSJesse Barnes out:
18007e231dbeSJesse Barnes 	return ret;
18017e231dbeSJesse Barnes }
18027e231dbeSJesse Barnes 
180343f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
180443f328d7SVille Syrjälä {
180545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
180643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
180743f328d7SVille Syrjälä 	u32 master_ctl, iir;
180843f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
180943f328d7SVille Syrjälä 
18102dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18112dd2a883SImre Deak 		return IRQ_NONE;
18122dd2a883SImre Deak 
18138e5fd599SVille Syrjälä 	for (;;) {
18148e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18153278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18163278f67fSVille Syrjälä 
18173278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18188e5fd599SVille Syrjälä 			break;
181943f328d7SVille Syrjälä 
182027b6c122SOscar Mateo 		ret = IRQ_HANDLED;
182127b6c122SOscar Mateo 
182243f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
182343f328d7SVille Syrjälä 
182427b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
182527b6c122SOscar Mateo 
182627b6c122SOscar Mateo 		if (iir) {
182727b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
182827b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
182927b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
183027b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
183127b6c122SOscar Mateo 		}
183227b6c122SOscar Mateo 
1833*74cdb337SChris Wilson 		gen8_gt_irq_handler(dev_priv, master_ctl);
183443f328d7SVille Syrjälä 
183527b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
183627b6c122SOscar Mateo 		 * signalled in iir */
18373278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
183843f328d7SVille Syrjälä 
183943f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
184043f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18418e5fd599SVille Syrjälä 	}
18423278f67fSVille Syrjälä 
184343f328d7SVille Syrjälä 	return ret;
184443f328d7SVille Syrjälä }
184543f328d7SVille Syrjälä 
184623e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1847776ad806SJesse Barnes {
18482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
18499db4a9c7SJesse Barnes 	int pipe;
1850b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
185113cf5504SDave Airlie 	u32 dig_hotplug_reg;
1852776ad806SJesse Barnes 
185313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
185413cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
185513cf5504SDave Airlie 
185613cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
185791d131d2SDaniel Vetter 
1858cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1859cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1860776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1861cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1862cfc33bf7SVille Syrjälä 				 port_name(port));
1863cfc33bf7SVille Syrjälä 	}
1864776ad806SJesse Barnes 
1865ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1866ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1867ce99c256SDaniel Vetter 
1868776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1869515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1870776ad806SJesse Barnes 
1871776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1872776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1873776ad806SJesse Barnes 
1874776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1875776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1876776ad806SJesse Barnes 
1877776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1878776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1879776ad806SJesse Barnes 
18809db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1881055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
18829db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
18839db4a9c7SJesse Barnes 					 pipe_name(pipe),
18849db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1885776ad806SJesse Barnes 
1886776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1887776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1888776ad806SJesse Barnes 
1889776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1890776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1891776ad806SJesse Barnes 
1892776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
18931f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
18948664281bSPaulo Zanoni 
18958664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
18961f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
18978664281bSPaulo Zanoni }
18988664281bSPaulo Zanoni 
18998664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19008664281bSPaulo Zanoni {
19018664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19028664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19035a69b89fSDaniel Vetter 	enum pipe pipe;
19048664281bSPaulo Zanoni 
1905de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1906de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1907de032bf4SPaulo Zanoni 
1908055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19091f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19101f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19118664281bSPaulo Zanoni 
19125a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19135a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1914277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19155a69b89fSDaniel Vetter 			else
1916277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19175a69b89fSDaniel Vetter 		}
19185a69b89fSDaniel Vetter 	}
19198bf1e9f1SShuang He 
19208664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19218664281bSPaulo Zanoni }
19228664281bSPaulo Zanoni 
19238664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19248664281bSPaulo Zanoni {
19258664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19268664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19278664281bSPaulo Zanoni 
1928de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1929de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1930de032bf4SPaulo Zanoni 
19318664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19321f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19338664281bSPaulo Zanoni 
19348664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19351f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19368664281bSPaulo Zanoni 
19378664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19381f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19398664281bSPaulo Zanoni 
19408664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1941776ad806SJesse Barnes }
1942776ad806SJesse Barnes 
194323e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
194423e81d69SAdam Jackson {
19452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
194623e81d69SAdam Jackson 	int pipe;
1947b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
194813cf5504SDave Airlie 	u32 dig_hotplug_reg;
194923e81d69SAdam Jackson 
195013cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
195113cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
195213cf5504SDave Airlie 
195313cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
195491d131d2SDaniel Vetter 
1955cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1956cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
195723e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1958cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1959cfc33bf7SVille Syrjälä 				 port_name(port));
1960cfc33bf7SVille Syrjälä 	}
196123e81d69SAdam Jackson 
196223e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1963ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
196423e81d69SAdam Jackson 
196523e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1966515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
196723e81d69SAdam Jackson 
196823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
196923e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
197023e81d69SAdam Jackson 
197123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
197223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
197323e81d69SAdam Jackson 
197423e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
1975055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
197623e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
197723e81d69SAdam Jackson 					 pipe_name(pipe),
197823e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
19798664281bSPaulo Zanoni 
19808664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
19818664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
198223e81d69SAdam Jackson }
198323e81d69SAdam Jackson 
1984c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
1985c008bc6eSPaulo Zanoni {
1986c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
198740da17c2SDaniel Vetter 	enum pipe pipe;
1988c008bc6eSPaulo Zanoni 
1989c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
1990c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
1991c008bc6eSPaulo Zanoni 
1992c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
1993c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
1994c008bc6eSPaulo Zanoni 
1995c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
1996c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1997c008bc6eSPaulo Zanoni 
1998055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1999d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2000d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2001d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2002c008bc6eSPaulo Zanoni 
200340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20041f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2005c008bc6eSPaulo Zanoni 
200640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
200740da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20085b3a856bSDaniel Vetter 
200940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
201040da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
201140da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
201240da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2013c008bc6eSPaulo Zanoni 		}
2014c008bc6eSPaulo Zanoni 	}
2015c008bc6eSPaulo Zanoni 
2016c008bc6eSPaulo Zanoni 	/* check event from PCH */
2017c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2018c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2019c008bc6eSPaulo Zanoni 
2020c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2021c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2022c008bc6eSPaulo Zanoni 		else
2023c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2024c008bc6eSPaulo Zanoni 
2025c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2026c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2027c008bc6eSPaulo Zanoni 	}
2028c008bc6eSPaulo Zanoni 
2029c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2030c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2031c008bc6eSPaulo Zanoni }
2032c008bc6eSPaulo Zanoni 
20339719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20349719fb98SPaulo Zanoni {
20359719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
203607d27e20SDamien Lespiau 	enum pipe pipe;
20379719fb98SPaulo Zanoni 
20389719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20399719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20409719fb98SPaulo Zanoni 
20419719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20429719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20439719fb98SPaulo Zanoni 
20449719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20459719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20469719fb98SPaulo Zanoni 
2047055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2048d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2049d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2050d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
205140da17c2SDaniel Vetter 
205240da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
205307d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
205407d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
205507d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20569719fb98SPaulo Zanoni 		}
20579719fb98SPaulo Zanoni 	}
20589719fb98SPaulo Zanoni 
20599719fb98SPaulo Zanoni 	/* check event from PCH */
20609719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20619719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20629719fb98SPaulo Zanoni 
20639719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20649719fb98SPaulo Zanoni 
20659719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20669719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20679719fb98SPaulo Zanoni 	}
20689719fb98SPaulo Zanoni }
20699719fb98SPaulo Zanoni 
207072c90f62SOscar Mateo /*
207172c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
207272c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
207372c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
207472c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
207572c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
207672c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
207772c90f62SOscar Mateo  */
2078f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2079b1f14ad0SJesse Barnes {
208045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
20812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2082f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
20830e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2084b1f14ad0SJesse Barnes 
20852dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
20862dd2a883SImre Deak 		return IRQ_NONE;
20872dd2a883SImre Deak 
20888664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
20898664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2090907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
20918664281bSPaulo Zanoni 
2092b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2093b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2094b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
209523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
20960e43406bSChris Wilson 
209744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
209844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
209944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
210044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
210144498aeaSPaulo Zanoni 	 * due to its back queue). */
2102ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
210344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
210444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
210544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2106ab5c608bSBen Widawsky 	}
210744498aeaSPaulo Zanoni 
210872c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
210972c90f62SOscar Mateo 
21100e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21110e43406bSChris Wilson 	if (gt_iir) {
211272c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
211372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2114d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21150e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2116d8fc8a47SPaulo Zanoni 		else
2117d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21180e43406bSChris Wilson 	}
2119b1f14ad0SJesse Barnes 
2120b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21210e43406bSChris Wilson 	if (de_iir) {
212272c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
212372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2124f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21259719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2126f1af8fc1SPaulo Zanoni 		else
2127f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21280e43406bSChris Wilson 	}
21290e43406bSChris Wilson 
2130f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2131f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21320e43406bSChris Wilson 		if (pm_iir) {
2133b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21340e43406bSChris Wilson 			ret = IRQ_HANDLED;
213572c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21360e43406bSChris Wilson 		}
2137f1af8fc1SPaulo Zanoni 	}
2138b1f14ad0SJesse Barnes 
2139b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2140b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2141ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
214244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
214344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2144ab5c608bSBen Widawsky 	}
2145b1f14ad0SJesse Barnes 
2146b1f14ad0SJesse Barnes 	return ret;
2147b1f14ad0SJesse Barnes }
2148b1f14ad0SJesse Barnes 
2149abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2150abd58f01SBen Widawsky {
2151abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2152abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2153abd58f01SBen Widawsky 	u32 master_ctl;
2154abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2155abd58f01SBen Widawsky 	uint32_t tmp = 0;
2156c42664ccSDaniel Vetter 	enum pipe pipe;
215788e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
215888e04703SJesse Barnes 
21592dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21602dd2a883SImre Deak 		return IRQ_NONE;
21612dd2a883SImre Deak 
216288e04703SJesse Barnes 	if (IS_GEN9(dev))
216388e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
216488e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2165abd58f01SBen Widawsky 
2166cb0d205eSChris Wilson 	master_ctl = I915_READ_FW(GEN8_MASTER_IRQ);
2167abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2168abd58f01SBen Widawsky 	if (!master_ctl)
2169abd58f01SBen Widawsky 		return IRQ_NONE;
2170abd58f01SBen Widawsky 
2171cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, 0);
2172abd58f01SBen Widawsky 
217338cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
217438cc46d7SOscar Mateo 
2175*74cdb337SChris Wilson 	ret = gen8_gt_irq_handler(dev_priv, master_ctl);
2176abd58f01SBen Widawsky 
2177abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2178abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2179abd58f01SBen Widawsky 		if (tmp) {
2180abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2181abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
218238cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
218338cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
218438cc46d7SOscar Mateo 			else
218538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2186abd58f01SBen Widawsky 		}
218738cc46d7SOscar Mateo 		else
218838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2189abd58f01SBen Widawsky 	}
2190abd58f01SBen Widawsky 
21916d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
21926d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
21936d766f02SDaniel Vetter 		if (tmp) {
21946d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
21956d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
219688e04703SJesse Barnes 
219788e04703SJesse Barnes 			if (tmp & aux_mask)
219838cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
219938cc46d7SOscar Mateo 			else
220038cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22016d766f02SDaniel Vetter 		}
220238cc46d7SOscar Mateo 		else
220338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22046d766f02SDaniel Vetter 	}
22056d766f02SDaniel Vetter 
2206055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2207770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2208abd58f01SBen Widawsky 
2209c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2210c42664ccSDaniel Vetter 			continue;
2211c42664ccSDaniel Vetter 
2212abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
221338cc46d7SOscar Mateo 		if (pipe_iir) {
221438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
221538cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2216770de83dSDamien Lespiau 
2217d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2218d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2219d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2220abd58f01SBen Widawsky 
2221770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2222770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2223770de83dSDamien Lespiau 			else
2224770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2225770de83dSDamien Lespiau 
2226770de83dSDamien Lespiau 			if (flip_done) {
2227abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2228abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2229abd58f01SBen Widawsky 			}
2230abd58f01SBen Widawsky 
22310fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22320fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22330fbe7870SDaniel Vetter 
22341f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22351f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22361f7247c0SDaniel Vetter 								    pipe);
223738d83c96SDaniel Vetter 
2238770de83dSDamien Lespiau 
2239770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2240770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2241770de83dSDamien Lespiau 			else
2242770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2243770de83dSDamien Lespiau 
2244770de83dSDamien Lespiau 			if (fault_errors)
224530100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
224630100f2bSDaniel Vetter 					  pipe_name(pipe),
224730100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2248c42664ccSDaniel Vetter 		} else
2249abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2250abd58f01SBen Widawsky 	}
2251abd58f01SBen Widawsky 
225292d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
225392d03a80SDaniel Vetter 		/*
225492d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
225592d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
225692d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
225792d03a80SDaniel Vetter 		 */
225892d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
225992d03a80SDaniel Vetter 		if (pch_iir) {
226092d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
226192d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
226238cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
226338cc46d7SOscar Mateo 		} else
226438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
226538cc46d7SOscar Mateo 
226692d03a80SDaniel Vetter 	}
226792d03a80SDaniel Vetter 
2268cb0d205eSChris Wilson 	I915_WRITE_FW(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2269cb0d205eSChris Wilson 	POSTING_READ_FW(GEN8_MASTER_IRQ);
2270abd58f01SBen Widawsky 
2271abd58f01SBen Widawsky 	return ret;
2272abd58f01SBen Widawsky }
2273abd58f01SBen Widawsky 
227417e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
227517e1df07SDaniel Vetter 			       bool reset_completed)
227617e1df07SDaniel Vetter {
2277a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
227817e1df07SDaniel Vetter 	int i;
227917e1df07SDaniel Vetter 
228017e1df07SDaniel Vetter 	/*
228117e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
228217e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
228317e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
228417e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
228517e1df07SDaniel Vetter 	 */
228617e1df07SDaniel Vetter 
228717e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
228817e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
228917e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
229017e1df07SDaniel Vetter 
229117e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
229217e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
229317e1df07SDaniel Vetter 
229417e1df07SDaniel Vetter 	/*
229517e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
229617e1df07SDaniel Vetter 	 * reset state is cleared.
229717e1df07SDaniel Vetter 	 */
229817e1df07SDaniel Vetter 	if (reset_completed)
229917e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
230017e1df07SDaniel Vetter }
230117e1df07SDaniel Vetter 
23028a905236SJesse Barnes /**
2303b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23048a905236SJesse Barnes  *
23058a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23068a905236SJesse Barnes  * was detected.
23078a905236SJesse Barnes  */
2308b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23098a905236SJesse Barnes {
2310b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2311b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2312cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2313cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2314cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
231517e1df07SDaniel Vetter 	int ret;
23168a905236SJesse Barnes 
23175bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23188a905236SJesse Barnes 
23197db0ba24SDaniel Vetter 	/*
23207db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23217db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23227db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23237db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23247db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23257db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23267db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23277db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23287db0ba24SDaniel Vetter 	 */
23297db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
233044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23315bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23327db0ba24SDaniel Vetter 				   reset_event);
23331f83fee0SDaniel Vetter 
233417e1df07SDaniel Vetter 		/*
2335f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2336f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2337f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2338f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2339f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2340f454c694SImre Deak 		 */
2341f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23427514747dSVille Syrjälä 
23437514747dSVille Syrjälä 		intel_prepare_reset(dev);
23447514747dSVille Syrjälä 
2345f454c694SImre Deak 		/*
234617e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
234717e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
234817e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
234917e1df07SDaniel Vetter 		 * deadlocks with the reset work.
235017e1df07SDaniel Vetter 		 */
2351f69061beSDaniel Vetter 		ret = i915_reset(dev);
2352f69061beSDaniel Vetter 
23537514747dSVille Syrjälä 		intel_finish_reset(dev);
235417e1df07SDaniel Vetter 
2355f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2356f454c694SImre Deak 
2357f69061beSDaniel Vetter 		if (ret == 0) {
2358f69061beSDaniel Vetter 			/*
2359f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2360f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2361f69061beSDaniel Vetter 			 * complete.
2362f69061beSDaniel Vetter 			 *
2363f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2364f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2365f69061beSDaniel Vetter 			 * updates before
2366f69061beSDaniel Vetter 			 * the counter increment.
2367f69061beSDaniel Vetter 			 */
23684e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2369f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2370f69061beSDaniel Vetter 
23715bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2372f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
23731f83fee0SDaniel Vetter 		} else {
23742ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2375f316a42cSBen Gamari 		}
23761f83fee0SDaniel Vetter 
237717e1df07SDaniel Vetter 		/*
237817e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
237917e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
238017e1df07SDaniel Vetter 		 */
238117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2382f316a42cSBen Gamari 	}
23838a905236SJesse Barnes }
23848a905236SJesse Barnes 
238535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2386c0e09200SDave Airlie {
23878a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2388bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
238963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2390050ee91fSBen Widawsky 	int pipe, i;
239163eeaf38SJesse Barnes 
239235aed2e6SChris Wilson 	if (!eir)
239335aed2e6SChris Wilson 		return;
239463eeaf38SJesse Barnes 
2395a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
23968a905236SJesse Barnes 
2397bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2398bd9854f9SBen Widawsky 
23998a905236SJesse Barnes 	if (IS_G4X(dev)) {
24008a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24018a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24028a905236SJesse Barnes 
2403a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2404a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2405050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2406050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2407a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2408a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24098a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24103143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24118a905236SJesse Barnes 		}
24128a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24138a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2414a70491ccSJoe Perches 			pr_err("page table error\n");
2415a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24168a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24173143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24188a905236SJesse Barnes 		}
24198a905236SJesse Barnes 	}
24208a905236SJesse Barnes 
2421a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
242263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
242363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2424a70491ccSJoe Perches 			pr_err("page table error\n");
2425a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
242663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24273143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
242863eeaf38SJesse Barnes 		}
24298a905236SJesse Barnes 	}
24308a905236SJesse Barnes 
243163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2432a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2433055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2434a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24359db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
243663eeaf38SJesse Barnes 		/* pipestat has already been acked */
243763eeaf38SJesse Barnes 	}
243863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2439a70491ccSJoe Perches 		pr_err("instruction error\n");
2440a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2441050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2442050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2443a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
244463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
244563eeaf38SJesse Barnes 
2446a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2447a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2448a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
244963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24503143a2bfSChris Wilson 			POSTING_READ(IPEIR);
245163eeaf38SJesse Barnes 		} else {
245263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
245363eeaf38SJesse Barnes 
2454a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2455a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2456a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2457a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
245863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24593143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
246063eeaf38SJesse Barnes 		}
246163eeaf38SJesse Barnes 	}
246263eeaf38SJesse Barnes 
246363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24643143a2bfSChris Wilson 	POSTING_READ(EIR);
246563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
246663eeaf38SJesse Barnes 	if (eir) {
246763eeaf38SJesse Barnes 		/*
246863eeaf38SJesse Barnes 		 * some errors might have become stuck,
246963eeaf38SJesse Barnes 		 * mask them.
247063eeaf38SJesse Barnes 		 */
247163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
247263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
247363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
247463eeaf38SJesse Barnes 	}
247535aed2e6SChris Wilson }
247635aed2e6SChris Wilson 
247735aed2e6SChris Wilson /**
2478b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
247935aed2e6SChris Wilson  * @dev: drm device
248035aed2e6SChris Wilson  *
2481b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
248235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
248335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
248435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
248535aed2e6SChris Wilson  * of a ring dump etc.).
248635aed2e6SChris Wilson  */
248758174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
248858174462SMika Kuoppala 		       const char *fmt, ...)
248935aed2e6SChris Wilson {
249035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
249158174462SMika Kuoppala 	va_list args;
249258174462SMika Kuoppala 	char error_msg[80];
249335aed2e6SChris Wilson 
249458174462SMika Kuoppala 	va_start(args, fmt);
249558174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
249658174462SMika Kuoppala 	va_end(args);
249758174462SMika Kuoppala 
249858174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
249935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25008a905236SJesse Barnes 
2501ba1234d1SBen Gamari 	if (wedged) {
2502f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2503f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2504ba1234d1SBen Gamari 
250511ed50ecSBen Gamari 		/*
2506b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2507b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2508b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
250917e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
251017e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
251117e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
251217e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
251317e1df07SDaniel Vetter 		 *
251417e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
251517e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
251617e1df07SDaniel Vetter 		 * counter atomic_t.
251711ed50ecSBen Gamari 		 */
251817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
251911ed50ecSBen Gamari 	}
252011ed50ecSBen Gamari 
2521b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25228a905236SJesse Barnes }
25238a905236SJesse Barnes 
252442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
252542f52ef8SKeith Packard  * we use as a pipe index
252642f52ef8SKeith Packard  */
2527f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25280a3e67a4SJesse Barnes {
25292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2530e9d21d7fSKeith Packard 	unsigned long irqflags;
253171e0ffa5SJesse Barnes 
25321ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2533f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25347c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2535755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25360a3e67a4SJesse Barnes 	else
25377c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2538755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25391ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25408692d00eSChris Wilson 
25410a3e67a4SJesse Barnes 	return 0;
25420a3e67a4SJesse Barnes }
25430a3e67a4SJesse Barnes 
2544f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2545f796cf8fSJesse Barnes {
25462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2547f796cf8fSJesse Barnes 	unsigned long irqflags;
2548b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
254940da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2550f796cf8fSJesse Barnes 
2551f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2552b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2553b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2554b1f14ad0SJesse Barnes 
2555b1f14ad0SJesse Barnes 	return 0;
2556b1f14ad0SJesse Barnes }
2557b1f14ad0SJesse Barnes 
25587e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
25597e231dbeSJesse Barnes {
25602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25617e231dbeSJesse Barnes 	unsigned long irqflags;
25627e231dbeSJesse Barnes 
25637e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
256431acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2565755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25667e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25677e231dbeSJesse Barnes 
25687e231dbeSJesse Barnes 	return 0;
25697e231dbeSJesse Barnes }
25707e231dbeSJesse Barnes 
2571abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2572abd58f01SBen Widawsky {
2573abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2574abd58f01SBen Widawsky 	unsigned long irqflags;
2575abd58f01SBen Widawsky 
2576abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25777167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
25787167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2579abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2580abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2581abd58f01SBen Widawsky 	return 0;
2582abd58f01SBen Widawsky }
2583abd58f01SBen Widawsky 
258442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
258542f52ef8SKeith Packard  * we use as a pipe index
258642f52ef8SKeith Packard  */
2587f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
25880a3e67a4SJesse Barnes {
25892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2590e9d21d7fSKeith Packard 	unsigned long irqflags;
25910a3e67a4SJesse Barnes 
25921ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
25937c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2594755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2595755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
25961ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25970a3e67a4SJesse Barnes }
25980a3e67a4SJesse Barnes 
2599f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2600f796cf8fSJesse Barnes {
26012d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2602f796cf8fSJesse Barnes 	unsigned long irqflags;
2603b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
260440da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2605f796cf8fSJesse Barnes 
2606f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2607b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2608b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2609b1f14ad0SJesse Barnes }
2610b1f14ad0SJesse Barnes 
26117e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26127e231dbeSJesse Barnes {
26132d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26147e231dbeSJesse Barnes 	unsigned long irqflags;
26157e231dbeSJesse Barnes 
26167e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
261731acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2618755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26197e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26207e231dbeSJesse Barnes }
26217e231dbeSJesse Barnes 
2622abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2623abd58f01SBen Widawsky {
2624abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2625abd58f01SBen Widawsky 	unsigned long irqflags;
2626abd58f01SBen Widawsky 
2627abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26287167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26297167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2630abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2631abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2632abd58f01SBen Widawsky }
2633abd58f01SBen Widawsky 
263444cdd6d2SJohn Harrison static struct drm_i915_gem_request *
263544cdd6d2SJohn Harrison ring_last_request(struct intel_engine_cs *ring)
2636852835f3SZou Nan hai {
2637893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
263844cdd6d2SJohn Harrison 			  struct drm_i915_gem_request, list);
2639893eead0SChris Wilson }
2640893eead0SChris Wilson 
26419107e9d2SChris Wilson static bool
264244cdd6d2SJohn Harrison ring_idle(struct intel_engine_cs *ring)
2643893eead0SChris Wilson {
26449107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
26451b5a433aSJohn Harrison 		i915_gem_request_completed(ring_last_request(ring), false));
2646f65d9421SBen Gamari }
2647f65d9421SBen Gamari 
2648a028c4b0SDaniel Vetter static bool
2649a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2650a028c4b0SDaniel Vetter {
2651a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2652a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2653a028c4b0SDaniel Vetter 	} else {
2654a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2655a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2656a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2657a028c4b0SDaniel Vetter 	}
2658a028c4b0SDaniel Vetter }
2659a028c4b0SDaniel Vetter 
2660a4872ba6SOscar Mateo static struct intel_engine_cs *
2661a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2662921d42eaSDaniel Vetter {
2663921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2664a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2665921d42eaSDaniel Vetter 	int i;
2666921d42eaSDaniel Vetter 
2667921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2668a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2669a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2670a6cdb93aSRodrigo Vivi 				continue;
2671a6cdb93aSRodrigo Vivi 
2672a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2673a6cdb93aSRodrigo Vivi 				return signaller;
2674a6cdb93aSRodrigo Vivi 		}
2675921d42eaSDaniel Vetter 	} else {
2676921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2677921d42eaSDaniel Vetter 
2678921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2679921d42eaSDaniel Vetter 			if(ring == signaller)
2680921d42eaSDaniel Vetter 				continue;
2681921d42eaSDaniel Vetter 
2682ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2683921d42eaSDaniel Vetter 				return signaller;
2684921d42eaSDaniel Vetter 		}
2685921d42eaSDaniel Vetter 	}
2686921d42eaSDaniel Vetter 
2687a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2688a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2689921d42eaSDaniel Vetter 
2690921d42eaSDaniel Vetter 	return NULL;
2691921d42eaSDaniel Vetter }
2692921d42eaSDaniel Vetter 
2693a4872ba6SOscar Mateo static struct intel_engine_cs *
2694a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2695a24a11e6SChris Wilson {
2696a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
269788fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2698a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2699a6cdb93aSRodrigo Vivi 	int i, backwards;
2700a24a11e6SChris Wilson 
2701a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2702a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27036274f212SChris Wilson 		return NULL;
2704a24a11e6SChris Wilson 
270588fe429dSDaniel Vetter 	/*
270688fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
270788fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2708a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2709a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
271088fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
271188fe429dSDaniel Vetter 	 * ringbuffer itself.
2712a24a11e6SChris Wilson 	 */
271388fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2714a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
271588fe429dSDaniel Vetter 
2716a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
271788fe429dSDaniel Vetter 		/*
271888fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
271988fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
272088fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
272188fe429dSDaniel Vetter 		 */
2722ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
272388fe429dSDaniel Vetter 
272488fe429dSDaniel Vetter 		/* This here seems to blow up */
2725ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2726a24a11e6SChris Wilson 		if (cmd == ipehr)
2727a24a11e6SChris Wilson 			break;
2728a24a11e6SChris Wilson 
272988fe429dSDaniel Vetter 		head -= 4;
273088fe429dSDaniel Vetter 	}
2731a24a11e6SChris Wilson 
273288fe429dSDaniel Vetter 	if (!i)
273388fe429dSDaniel Vetter 		return NULL;
273488fe429dSDaniel Vetter 
2735ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2736a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2737a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2738a6cdb93aSRodrigo Vivi 		offset <<= 32;
2739a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2740a6cdb93aSRodrigo Vivi 	}
2741a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2742a24a11e6SChris Wilson }
2743a24a11e6SChris Wilson 
2744a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27456274f212SChris Wilson {
27466274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2747a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2748a0d036b0SChris Wilson 	u32 seqno;
27496274f212SChris Wilson 
27504be17381SChris Wilson 	ring->hangcheck.deadlock++;
27516274f212SChris Wilson 
27526274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
27534be17381SChris Wilson 	if (signaller == NULL)
27544be17381SChris Wilson 		return -1;
27554be17381SChris Wilson 
27564be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
27574be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
27586274f212SChris Wilson 		return -1;
27596274f212SChris Wilson 
27604be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
27614be17381SChris Wilson 		return 1;
27624be17381SChris Wilson 
2763a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2764a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2765a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
27664be17381SChris Wilson 		return -1;
27674be17381SChris Wilson 
27684be17381SChris Wilson 	return 0;
27696274f212SChris Wilson }
27706274f212SChris Wilson 
27716274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
27726274f212SChris Wilson {
2773a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
27746274f212SChris Wilson 	int i;
27756274f212SChris Wilson 
27766274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
27774be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
27786274f212SChris Wilson }
27796274f212SChris Wilson 
2780ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2781a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
27821ec14ad3SChris Wilson {
27831ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
27841ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
27859107e9d2SChris Wilson 	u32 tmp;
27869107e9d2SChris Wilson 
2787f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2788f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2789f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2790f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2791f260fe7bSMika Kuoppala 		}
2792f260fe7bSMika Kuoppala 
2793f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2794f260fe7bSMika Kuoppala 	}
27956274f212SChris Wilson 
27969107e9d2SChris Wilson 	if (IS_GEN2(dev))
2797f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
27989107e9d2SChris Wilson 
27999107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28009107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28019107e9d2SChris Wilson 	 * and break the hang. This should work on
28029107e9d2SChris Wilson 	 * all but the second generation chipsets.
28039107e9d2SChris Wilson 	 */
28049107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28051ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
280658174462SMika Kuoppala 		i915_handle_error(dev, false,
280758174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28081ec14ad3SChris Wilson 				  ring->name);
28091ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2810f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28111ec14ad3SChris Wilson 	}
2812a24a11e6SChris Wilson 
28136274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28146274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28156274f212SChris Wilson 		default:
2816f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28176274f212SChris Wilson 		case 1:
281858174462SMika Kuoppala 			i915_handle_error(dev, false,
281958174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2820a24a11e6SChris Wilson 					  ring->name);
2821a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2822f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28236274f212SChris Wilson 		case 0:
2824f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28256274f212SChris Wilson 		}
28269107e9d2SChris Wilson 	}
28279107e9d2SChris Wilson 
2828f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2829a24a11e6SChris Wilson }
2830d1e61e7fSChris Wilson 
2831737b1506SChris Wilson /*
2832f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
283305407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
283405407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
283505407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
283605407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
283705407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2838f65d9421SBen Gamari  */
2839737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2840f65d9421SBen Gamari {
2841737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2842737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2843737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2844737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2845a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2846b4519513SChris Wilson 	int i;
284705407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28489107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28499107e9d2SChris Wilson #define BUSY 1
28509107e9d2SChris Wilson #define KICK 5
28519107e9d2SChris Wilson #define HUNG 20
2852893eead0SChris Wilson 
2853d330a953SJani Nikula 	if (!i915.enable_hangcheck)
28543e0dc6b0SBen Widawsky 		return;
28553e0dc6b0SBen Widawsky 
2856b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
285750877445SChris Wilson 		u64 acthd;
285850877445SChris Wilson 		u32 seqno;
28599107e9d2SChris Wilson 		bool busy = true;
2860b4519513SChris Wilson 
28616274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
28626274f212SChris Wilson 
286305407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
286405407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
286505407ff8SMika Kuoppala 
286605407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
286744cdd6d2SJohn Harrison 			if (ring_idle(ring)) {
2868da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2869da661464SMika Kuoppala 
28709107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
28719107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2872094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2873f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
28749107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
28759107e9d2SChris Wilson 								  ring->name);
2876f4adcd24SDaniel Vetter 						else
2877f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2878f4adcd24SDaniel Vetter 								 ring->name);
28799107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2880094f9a54SChris Wilson 					}
2881094f9a54SChris Wilson 					/* Safeguard against driver failure */
2882094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
28839107e9d2SChris Wilson 				} else
28849107e9d2SChris Wilson 					busy = false;
288505407ff8SMika Kuoppala 			} else {
28866274f212SChris Wilson 				/* We always increment the hangcheck score
28876274f212SChris Wilson 				 * if the ring is busy and still processing
28886274f212SChris Wilson 				 * the same request, so that no single request
28896274f212SChris Wilson 				 * can run indefinitely (such as a chain of
28906274f212SChris Wilson 				 * batches). The only time we do not increment
28916274f212SChris Wilson 				 * the hangcheck score on this ring, if this
28926274f212SChris Wilson 				 * ring is in a legitimate wait for another
28936274f212SChris Wilson 				 * ring. In that case the waiting ring is a
28946274f212SChris Wilson 				 * victim and we want to be sure we catch the
28956274f212SChris Wilson 				 * right culprit. Then every time we do kick
28966274f212SChris Wilson 				 * the ring, add a small increment to the
28976274f212SChris Wilson 				 * score so that we can catch a batch that is
28986274f212SChris Wilson 				 * being repeatedly kicked and so responsible
28996274f212SChris Wilson 				 * for stalling the machine.
29009107e9d2SChris Wilson 				 */
2901ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2902ad8beaeaSMika Kuoppala 								    acthd);
2903ad8beaeaSMika Kuoppala 
2904ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2905da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2906f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2907f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2908f260fe7bSMika Kuoppala 					break;
2909f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2910ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29116274f212SChris Wilson 					break;
2912f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2913ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29146274f212SChris Wilson 					break;
2915f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2916ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29176274f212SChris Wilson 					stuck[i] = true;
29186274f212SChris Wilson 					break;
29196274f212SChris Wilson 				}
292005407ff8SMika Kuoppala 			}
29219107e9d2SChris Wilson 		} else {
2922da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2923da661464SMika Kuoppala 
29249107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29259107e9d2SChris Wilson 			 * attempts across multiple batches.
29269107e9d2SChris Wilson 			 */
29279107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29289107e9d2SChris Wilson 				ring->hangcheck.score--;
2929f260fe7bSMika Kuoppala 
2930f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2931cbb465e7SChris Wilson 		}
2932f65d9421SBen Gamari 
293305407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
293405407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29359107e9d2SChris Wilson 		busy_count += busy;
293605407ff8SMika Kuoppala 	}
293705407ff8SMika Kuoppala 
293805407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2939b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2940b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
294105407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2942a43adf07SChris Wilson 				 ring->name);
2943a43adf07SChris Wilson 			rings_hung++;
294405407ff8SMika Kuoppala 		}
294505407ff8SMika Kuoppala 	}
294605407ff8SMika Kuoppala 
294705407ff8SMika Kuoppala 	if (rings_hung)
294858174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
294905407ff8SMika Kuoppala 
295005407ff8SMika Kuoppala 	if (busy_count)
295105407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
295205407ff8SMika Kuoppala 		 * being added */
295310cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
295410cd45b6SMika Kuoppala }
295510cd45b6SMika Kuoppala 
295610cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
295710cd45b6SMika Kuoppala {
2958737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2959672e7b7cSChris Wilson 
2960d330a953SJani Nikula 	if (!i915.enable_hangcheck)
296110cd45b6SMika Kuoppala 		return;
296210cd45b6SMika Kuoppala 
2963737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2964737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2965737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2966737b1506SChris Wilson 	 */
2967737b1506SChris Wilson 
2968737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
2969737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
2970f65d9421SBen Gamari }
2971f65d9421SBen Gamari 
29721c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
297391738a95SPaulo Zanoni {
297491738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
297591738a95SPaulo Zanoni 
297691738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
297791738a95SPaulo Zanoni 		return;
297891738a95SPaulo Zanoni 
2979f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
2980105b122eSPaulo Zanoni 
2981105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
2982105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
2983622364b6SPaulo Zanoni }
2984105b122eSPaulo Zanoni 
298591738a95SPaulo Zanoni /*
2986622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
2987622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
2988622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
2989622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
2990622364b6SPaulo Zanoni  *
2991622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
299291738a95SPaulo Zanoni  */
2993622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
2994622364b6SPaulo Zanoni {
2995622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2996622364b6SPaulo Zanoni 
2997622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
2998622364b6SPaulo Zanoni 		return;
2999622364b6SPaulo Zanoni 
3000622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
300191738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
300291738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
300391738a95SPaulo Zanoni }
300491738a95SPaulo Zanoni 
30057c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3006d18ea1b5SDaniel Vetter {
3007d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3008d18ea1b5SDaniel Vetter 
3009f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3010a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3011f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3012d18ea1b5SDaniel Vetter }
3013d18ea1b5SDaniel Vetter 
3014c0e09200SDave Airlie /* drm_dma.h hooks
3015c0e09200SDave Airlie */
3016be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3017036a4a7dSZhenyu Wang {
30182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3019036a4a7dSZhenyu Wang 
30200c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3021bdfcdb63SDaniel Vetter 
3022f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3023c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3024c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3025036a4a7dSZhenyu Wang 
30267c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3027c650156aSZhenyu Wang 
30281c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30297d99163dSBen Widawsky }
30307d99163dSBen Widawsky 
303170591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
303270591a41SVille Syrjälä {
303370591a41SVille Syrjälä 	enum pipe pipe;
303470591a41SVille Syrjälä 
303570591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
303670591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
303770591a41SVille Syrjälä 
303870591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
303970591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
304070591a41SVille Syrjälä 
304170591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
304270591a41SVille Syrjälä }
304370591a41SVille Syrjälä 
30447e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30457e231dbeSJesse Barnes {
30462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30477e231dbeSJesse Barnes 
30487e231dbeSJesse Barnes 	/* VLV magic */
30497e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30507e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30517e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30527e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30537e231dbeSJesse Barnes 
30547c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30557e231dbeSJesse Barnes 
30567c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
30577e231dbeSJesse Barnes 
305870591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
30597e231dbeSJesse Barnes }
30607e231dbeSJesse Barnes 
3061d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3062d6e3cca3SDaniel Vetter {
3063d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3064d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3065d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3066d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3067d6e3cca3SDaniel Vetter }
3068d6e3cca3SDaniel Vetter 
3069823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3070abd58f01SBen Widawsky {
3071abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3072abd58f01SBen Widawsky 	int pipe;
3073abd58f01SBen Widawsky 
3074abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3075abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3076abd58f01SBen Widawsky 
3077d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3078abd58f01SBen Widawsky 
3079055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3080f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3081813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3082f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3083abd58f01SBen Widawsky 
3084f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3085f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3086f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3087abd58f01SBen Widawsky 
30881c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3089abd58f01SBen Widawsky }
3090abd58f01SBen Widawsky 
30914c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
30924c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3093d49bdb0eSPaulo Zanoni {
30941180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3095d49bdb0eSPaulo Zanoni 
309613321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3097d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3098d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3099d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3100d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31014c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31024c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31034c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31041180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31054c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31064c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31074c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31081180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
310913321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3110d49bdb0eSPaulo Zanoni }
3111d49bdb0eSPaulo Zanoni 
311243f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
311343f328d7SVille Syrjälä {
311443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
311543f328d7SVille Syrjälä 
311643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
311743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
311843f328d7SVille Syrjälä 
3119d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
312043f328d7SVille Syrjälä 
312143f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
312243f328d7SVille Syrjälä 
312343f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
312443f328d7SVille Syrjälä 
312570591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
312643f328d7SVille Syrjälä }
312743f328d7SVille Syrjälä 
312882a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
312982a28bcfSDaniel Vetter {
31302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
313182a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3132fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
313382a28bcfSDaniel Vetter 
313482a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3135fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3136b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3137cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3138fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
313982a28bcfSDaniel Vetter 	} else {
3140fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3141b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3142cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3143fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
314482a28bcfSDaniel Vetter 	}
314582a28bcfSDaniel Vetter 
3146fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
314782a28bcfSDaniel Vetter 
31487fe0b973SKeith Packard 	/*
31497fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31507fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
31517fe0b973SKeith Packard 	 *
31527fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
31537fe0b973SKeith Packard 	 */
31547fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31557fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31567fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31577fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31587fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31597fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31607fe0b973SKeith Packard }
31617fe0b973SKeith Packard 
3162d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3163d46da437SPaulo Zanoni {
31642d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
316582a28bcfSDaniel Vetter 	u32 mask;
3166d46da437SPaulo Zanoni 
3167692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3168692a04cfSDaniel Vetter 		return;
3169692a04cfSDaniel Vetter 
3170105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
31715c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3172105b122eSPaulo Zanoni 	else
31735c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
31748664281bSPaulo Zanoni 
3175337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3176d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3177d46da437SPaulo Zanoni }
3178d46da437SPaulo Zanoni 
31790a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
31800a9a8c91SDaniel Vetter {
31810a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
31820a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
31830a9a8c91SDaniel Vetter 
31840a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
31850a9a8c91SDaniel Vetter 
31860a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3187040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
31880a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
318935a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
319035a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
31910a9a8c91SDaniel Vetter 	}
31920a9a8c91SDaniel Vetter 
31930a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
31940a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
31950a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
31960a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
31970a9a8c91SDaniel Vetter 	} else {
31980a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
31990a9a8c91SDaniel Vetter 	}
32000a9a8c91SDaniel Vetter 
320135079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32020a9a8c91SDaniel Vetter 
32030a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
320478e68d36SImre Deak 		/*
320578e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
320678e68d36SImre Deak 		 * itself is enabled/disabled.
320778e68d36SImre Deak 		 */
32080a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32090a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32100a9a8c91SDaniel Vetter 
3211605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
321235079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32130a9a8c91SDaniel Vetter 	}
32140a9a8c91SDaniel Vetter }
32150a9a8c91SDaniel Vetter 
3216f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3217036a4a7dSZhenyu Wang {
32182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
32198e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32208e76f8dcSPaulo Zanoni 
32218e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
32228e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
32238e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
32248e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
32255c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
32268e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
32275c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
32288e76f8dcSPaulo Zanoni 	} else {
32298e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3230ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
32315b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
32325b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
32335b3a856bSDaniel Vetter 				DE_POISON);
32345c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
32355c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
32368e76f8dcSPaulo Zanoni 	}
3237036a4a7dSZhenyu Wang 
32381ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3239036a4a7dSZhenyu Wang 
32400c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
32410c841212SPaulo Zanoni 
3242622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3243622364b6SPaulo Zanoni 
324435079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3245036a4a7dSZhenyu Wang 
32460a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3247036a4a7dSZhenyu Wang 
3248d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
32497fe0b973SKeith Packard 
3250f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
32516005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32526005ce42SDaniel Vetter 		 *
32536005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32544bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32554bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3256d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3257f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3258d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3259f97108d1SJesse Barnes 	}
3260f97108d1SJesse Barnes 
3261036a4a7dSZhenyu Wang 	return 0;
3262036a4a7dSZhenyu Wang }
3263036a4a7dSZhenyu Wang 
3264f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3265f8b79e58SImre Deak {
3266f8b79e58SImre Deak 	u32 pipestat_mask;
3267f8b79e58SImre Deak 	u32 iir_mask;
3268120dda4fSVille Syrjälä 	enum pipe pipe;
3269f8b79e58SImre Deak 
3270f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3271f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3272f8b79e58SImre Deak 
3273120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3274120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3275f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3276f8b79e58SImre Deak 
3277f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3278f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3279f8b79e58SImre Deak 
3280120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3281120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3282120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3283f8b79e58SImre Deak 
3284f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3285f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3286f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3287120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3288120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3289f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3290f8b79e58SImre Deak 
3291f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3292f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3293f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
329476e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
329576e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3296f8b79e58SImre Deak }
3297f8b79e58SImre Deak 
3298f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3299f8b79e58SImre Deak {
3300f8b79e58SImre Deak 	u32 pipestat_mask;
3301f8b79e58SImre Deak 	u32 iir_mask;
3302120dda4fSVille Syrjälä 	enum pipe pipe;
3303f8b79e58SImre Deak 
3304f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3305f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33066c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3307120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3308120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3309f8b79e58SImre Deak 
3310f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3311f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
331276e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3313f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3314f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3315f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3316f8b79e58SImre Deak 
3317f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3318f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3319f8b79e58SImre Deak 
3320120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3321120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3322120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3323f8b79e58SImre Deak 
3324f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3325f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3326120dda4fSVille Syrjälä 
3327120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3328120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3329f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3330f8b79e58SImre Deak }
3331f8b79e58SImre Deak 
3332f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3333f8b79e58SImre Deak {
3334f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3335f8b79e58SImre Deak 
3336f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3337f8b79e58SImre Deak 		return;
3338f8b79e58SImre Deak 
3339f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3340f8b79e58SImre Deak 
3341950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3342f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3343f8b79e58SImre Deak }
3344f8b79e58SImre Deak 
3345f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3346f8b79e58SImre Deak {
3347f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3348f8b79e58SImre Deak 
3349f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3350f8b79e58SImre Deak 		return;
3351f8b79e58SImre Deak 
3352f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3353f8b79e58SImre Deak 
3354950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3355f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3356f8b79e58SImre Deak }
3357f8b79e58SImre Deak 
33580e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33597e231dbeSJesse Barnes {
3360f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
33617e231dbeSJesse Barnes 
336220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
336320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
336420afbda2SDaniel Vetter 
33657e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
336676e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
336776e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
336876e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
336976e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
33707e231dbeSJesse Barnes 
3371b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3372b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3373d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3374f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3375f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3376d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
33770e6c9a9eSVille Syrjälä }
33780e6c9a9eSVille Syrjälä 
33790e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
33800e6c9a9eSVille Syrjälä {
33810e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
33820e6c9a9eSVille Syrjälä 
33830e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
33847e231dbeSJesse Barnes 
33850a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
33867e231dbeSJesse Barnes 
33877e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
33887e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
33897e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
33907e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
33917e231dbeSJesse Barnes #endif
33927e231dbeSJesse Barnes 
33937e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
339420afbda2SDaniel Vetter 
339520afbda2SDaniel Vetter 	return 0;
339620afbda2SDaniel Vetter }
339720afbda2SDaniel Vetter 
3398abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3399abd58f01SBen Widawsky {
3400abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3401abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3402abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
340373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3404abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
340573d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
340673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3407abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
340873d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
340973d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
341073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3411abd58f01SBen Widawsky 		0,
341273d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
341373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3414abd58f01SBen Widawsky 		};
3415abd58f01SBen Widawsky 
34160961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
34179a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34189a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
341978e68d36SImre Deak 	/*
342078e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
342178e68d36SImre Deak 	 * is enabled/disabled.
342278e68d36SImre Deak 	 */
342378e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
34249a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3425abd58f01SBen Widawsky }
3426abd58f01SBen Widawsky 
3427abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3428abd58f01SBen Widawsky {
3429770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3430770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3431abd58f01SBen Widawsky 	int pipe;
343288e04703SJesse Barnes 	u32 aux_en = GEN8_AUX_CHANNEL_A;
3433770de83dSDamien Lespiau 
343488e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3435770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3436770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
343788e04703SJesse Barnes 		aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
343888e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
343988e04703SJesse Barnes 	} else
3440770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3441770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3442770de83dSDamien Lespiau 
3443770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3444770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3445770de83dSDamien Lespiau 
344613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
344713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
344813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3449abd58f01SBen Widawsky 
3450055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3451f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3452813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3453813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3454813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
345535079899SPaulo Zanoni 					  de_pipe_enables);
3456abd58f01SBen Widawsky 
345788e04703SJesse Barnes 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
3458abd58f01SBen Widawsky }
3459abd58f01SBen Widawsky 
3460abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3461abd58f01SBen Widawsky {
3462abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3463abd58f01SBen Widawsky 
3464622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3465622364b6SPaulo Zanoni 
3466abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3467abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3468abd58f01SBen Widawsky 
3469abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3470abd58f01SBen Widawsky 
3471abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3472abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3473abd58f01SBen Widawsky 
3474abd58f01SBen Widawsky 	return 0;
3475abd58f01SBen Widawsky }
3476abd58f01SBen Widawsky 
347743f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
347843f328d7SVille Syrjälä {
347943f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
348043f328d7SVille Syrjälä 
3481c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
348243f328d7SVille Syrjälä 
348343f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
348443f328d7SVille Syrjälä 
348543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
348643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
348743f328d7SVille Syrjälä 
348843f328d7SVille Syrjälä 	return 0;
348943f328d7SVille Syrjälä }
349043f328d7SVille Syrjälä 
3491abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3492abd58f01SBen Widawsky {
3493abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3494abd58f01SBen Widawsky 
3495abd58f01SBen Widawsky 	if (!dev_priv)
3496abd58f01SBen Widawsky 		return;
3497abd58f01SBen Widawsky 
3498823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3499abd58f01SBen Widawsky }
3500abd58f01SBen Widawsky 
35018ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
35028ea0be4fSVille Syrjälä {
35038ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
35048ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
35058ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35068ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
35078ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
35088ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35098ea0be4fSVille Syrjälä 
35108ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
35118ea0be4fSVille Syrjälä 
3512c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
35138ea0be4fSVille Syrjälä }
35148ea0be4fSVille Syrjälä 
35157e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35167e231dbeSJesse Barnes {
35172d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35187e231dbeSJesse Barnes 
35197e231dbeSJesse Barnes 	if (!dev_priv)
35207e231dbeSJesse Barnes 		return;
35217e231dbeSJesse Barnes 
3522843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3523843d0e7dSImre Deak 
3524893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3525893fce8eSVille Syrjälä 
35267e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3527f8b79e58SImre Deak 
35288ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
35297e231dbeSJesse Barnes }
35307e231dbeSJesse Barnes 
353143f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
353243f328d7SVille Syrjälä {
353343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
353443f328d7SVille Syrjälä 
353543f328d7SVille Syrjälä 	if (!dev_priv)
353643f328d7SVille Syrjälä 		return;
353743f328d7SVille Syrjälä 
353843f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
353943f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
354043f328d7SVille Syrjälä 
3541a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
354243f328d7SVille Syrjälä 
3543a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
354443f328d7SVille Syrjälä 
3545c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
354643f328d7SVille Syrjälä }
354743f328d7SVille Syrjälä 
3548f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3549036a4a7dSZhenyu Wang {
35502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35514697995bSJesse Barnes 
35524697995bSJesse Barnes 	if (!dev_priv)
35534697995bSJesse Barnes 		return;
35544697995bSJesse Barnes 
3555be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3556036a4a7dSZhenyu Wang }
3557036a4a7dSZhenyu Wang 
3558c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3559c2798b19SChris Wilson {
35602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3561c2798b19SChris Wilson 	int pipe;
3562c2798b19SChris Wilson 
3563055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3564c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3565c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3566c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3567c2798b19SChris Wilson 	POSTING_READ16(IER);
3568c2798b19SChris Wilson }
3569c2798b19SChris Wilson 
3570c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3571c2798b19SChris Wilson {
35722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3573c2798b19SChris Wilson 
3574c2798b19SChris Wilson 	I915_WRITE16(EMR,
3575c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3576c2798b19SChris Wilson 
3577c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3578c2798b19SChris Wilson 	dev_priv->irq_mask =
3579c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3580c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3581c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3582c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3583c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3584c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3585c2798b19SChris Wilson 
3586c2798b19SChris Wilson 	I915_WRITE16(IER,
3587c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3588c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3589c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3590c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3591c2798b19SChris Wilson 	POSTING_READ16(IER);
3592c2798b19SChris Wilson 
3593379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3594379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3595d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3596755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3597755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3598d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3599379ef82dSDaniel Vetter 
3600c2798b19SChris Wilson 	return 0;
3601c2798b19SChris Wilson }
3602c2798b19SChris Wilson 
360390a72f87SVille Syrjälä /*
360490a72f87SVille Syrjälä  * Returns true when a page flip has completed.
360590a72f87SVille Syrjälä  */
360690a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
36071f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
360890a72f87SVille Syrjälä {
36092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36101f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
361190a72f87SVille Syrjälä 
36128d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
361390a72f87SVille Syrjälä 		return false;
361490a72f87SVille Syrjälä 
361590a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3616d6bbafa1SChris Wilson 		goto check_page_flip;
361790a72f87SVille Syrjälä 
361890a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
361990a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
362090a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
362190a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
362290a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
362390a72f87SVille Syrjälä 	 */
362490a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3625d6bbafa1SChris Wilson 		goto check_page_flip;
362690a72f87SVille Syrjälä 
36277d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
362890a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
362990a72f87SVille Syrjälä 	return true;
3630d6bbafa1SChris Wilson 
3631d6bbafa1SChris Wilson check_page_flip:
3632d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3633d6bbafa1SChris Wilson 	return false;
363490a72f87SVille Syrjälä }
363590a72f87SVille Syrjälä 
3636ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3637c2798b19SChris Wilson {
363845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
36392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3640c2798b19SChris Wilson 	u16 iir, new_iir;
3641c2798b19SChris Wilson 	u32 pipe_stats[2];
3642c2798b19SChris Wilson 	int pipe;
3643c2798b19SChris Wilson 	u16 flip_mask =
3644c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3645c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3646c2798b19SChris Wilson 
36472dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36482dd2a883SImre Deak 		return IRQ_NONE;
36492dd2a883SImre Deak 
3650c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3651c2798b19SChris Wilson 	if (iir == 0)
3652c2798b19SChris Wilson 		return IRQ_NONE;
3653c2798b19SChris Wilson 
3654c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3655c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3656c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3657c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3658c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3659c2798b19SChris Wilson 		 */
3660222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3661c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3662aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3663c2798b19SChris Wilson 
3664055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3665c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3666c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3667c2798b19SChris Wilson 
3668c2798b19SChris Wilson 			/*
3669c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3670c2798b19SChris Wilson 			 */
36712d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3672c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3673c2798b19SChris Wilson 		}
3674222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3675c2798b19SChris Wilson 
3676c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3677c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3678c2798b19SChris Wilson 
3679c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3680*74cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3681c2798b19SChris Wilson 
3682055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
36831f1c2e24SVille Syrjälä 			int plane = pipe;
36843a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
36851f1c2e24SVille Syrjälä 				plane = !plane;
36861f1c2e24SVille Syrjälä 
36874356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
36881f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
36891f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3690c2798b19SChris Wilson 
36914356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3692277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
36932d9d2b0bSVille Syrjälä 
36941f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
36951f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
36961f7247c0SDaniel Vetter 								    pipe);
36974356d586SDaniel Vetter 		}
3698c2798b19SChris Wilson 
3699c2798b19SChris Wilson 		iir = new_iir;
3700c2798b19SChris Wilson 	}
3701c2798b19SChris Wilson 
3702c2798b19SChris Wilson 	return IRQ_HANDLED;
3703c2798b19SChris Wilson }
3704c2798b19SChris Wilson 
3705c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3706c2798b19SChris Wilson {
37072d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3708c2798b19SChris Wilson 	int pipe;
3709c2798b19SChris Wilson 
3710055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3711c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3712c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3713c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3714c2798b19SChris Wilson 	}
3715c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3716c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3717c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3718c2798b19SChris Wilson }
3719c2798b19SChris Wilson 
3720a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3721a266c7d5SChris Wilson {
37222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3723a266c7d5SChris Wilson 	int pipe;
3724a266c7d5SChris Wilson 
3725a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3726a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3727a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3728a266c7d5SChris Wilson 	}
3729a266c7d5SChris Wilson 
373000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3731055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3732a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3733a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3734a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3735a266c7d5SChris Wilson 	POSTING_READ(IER);
3736a266c7d5SChris Wilson }
3737a266c7d5SChris Wilson 
3738a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3739a266c7d5SChris Wilson {
37402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
374138bde180SChris Wilson 	u32 enable_mask;
3742a266c7d5SChris Wilson 
374338bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
374438bde180SChris Wilson 
374538bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
374638bde180SChris Wilson 	dev_priv->irq_mask =
374738bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
374838bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
374938bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
375038bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
375138bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
375238bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
375338bde180SChris Wilson 
375438bde180SChris Wilson 	enable_mask =
375538bde180SChris Wilson 		I915_ASLE_INTERRUPT |
375638bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
375738bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
375838bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
375938bde180SChris Wilson 		I915_USER_INTERRUPT;
376038bde180SChris Wilson 
3761a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
376220afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
376320afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
376420afbda2SDaniel Vetter 
3765a266c7d5SChris Wilson 		/* Enable in IER... */
3766a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3767a266c7d5SChris Wilson 		/* and unmask in IMR */
3768a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3769a266c7d5SChris Wilson 	}
3770a266c7d5SChris Wilson 
3771a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3772a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3773a266c7d5SChris Wilson 	POSTING_READ(IER);
3774a266c7d5SChris Wilson 
3775f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
377620afbda2SDaniel Vetter 
3777379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3778379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3779d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3780755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3781755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3782d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3783379ef82dSDaniel Vetter 
378420afbda2SDaniel Vetter 	return 0;
378520afbda2SDaniel Vetter }
378620afbda2SDaniel Vetter 
378790a72f87SVille Syrjälä /*
378890a72f87SVille Syrjälä  * Returns true when a page flip has completed.
378990a72f87SVille Syrjälä  */
379090a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
379190a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
379290a72f87SVille Syrjälä {
37932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
379490a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
379590a72f87SVille Syrjälä 
37968d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
379790a72f87SVille Syrjälä 		return false;
379890a72f87SVille Syrjälä 
379990a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3800d6bbafa1SChris Wilson 		goto check_page_flip;
380190a72f87SVille Syrjälä 
380290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
380390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
380490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
380590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
380690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
380790a72f87SVille Syrjälä 	 */
380890a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3809d6bbafa1SChris Wilson 		goto check_page_flip;
381090a72f87SVille Syrjälä 
38117d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
381290a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
381390a72f87SVille Syrjälä 	return true;
3814d6bbafa1SChris Wilson 
3815d6bbafa1SChris Wilson check_page_flip:
3816d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3817d6bbafa1SChris Wilson 	return false;
381890a72f87SVille Syrjälä }
381990a72f87SVille Syrjälä 
3820ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3821a266c7d5SChris Wilson {
382245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38232d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38248291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
382538bde180SChris Wilson 	u32 flip_mask =
382638bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
382738bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
382838bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3829a266c7d5SChris Wilson 
38302dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38312dd2a883SImre Deak 		return IRQ_NONE;
38322dd2a883SImre Deak 
3833a266c7d5SChris Wilson 	iir = I915_READ(IIR);
383438bde180SChris Wilson 	do {
383538bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
38368291ee90SChris Wilson 		bool blc_event = false;
3837a266c7d5SChris Wilson 
3838a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3839a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3840a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3841a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3842a266c7d5SChris Wilson 		 */
3843222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3844a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3845aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3846a266c7d5SChris Wilson 
3847055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3848a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3849a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3850a266c7d5SChris Wilson 
385138bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3852a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3853a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
385438bde180SChris Wilson 				irq_received = true;
3855a266c7d5SChris Wilson 			}
3856a266c7d5SChris Wilson 		}
3857222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3858a266c7d5SChris Wilson 
3859a266c7d5SChris Wilson 		if (!irq_received)
3860a266c7d5SChris Wilson 			break;
3861a266c7d5SChris Wilson 
3862a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
386316c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
386416c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
386516c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3866a266c7d5SChris Wilson 
386738bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3868a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3869a266c7d5SChris Wilson 
3870a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3871*74cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
3872a266c7d5SChris Wilson 
3873055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
387438bde180SChris Wilson 			int plane = pipe;
38753a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
387638bde180SChris Wilson 				plane = !plane;
38775e2032d4SVille Syrjälä 
387890a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
387990a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
388090a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3881a266c7d5SChris Wilson 
3882a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3883a266c7d5SChris Wilson 				blc_event = true;
38844356d586SDaniel Vetter 
38854356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3886277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38872d9d2b0bSVille Syrjälä 
38881f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
38891f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
38901f7247c0SDaniel Vetter 								    pipe);
3891a266c7d5SChris Wilson 		}
3892a266c7d5SChris Wilson 
3893a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3894a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3895a266c7d5SChris Wilson 
3896a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3897a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3898a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3899a266c7d5SChris Wilson 		 * we would never get another interrupt.
3900a266c7d5SChris Wilson 		 *
3901a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3902a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3903a266c7d5SChris Wilson 		 * another one.
3904a266c7d5SChris Wilson 		 *
3905a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3906a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3907a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3908a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3909a266c7d5SChris Wilson 		 * stray interrupts.
3910a266c7d5SChris Wilson 		 */
391138bde180SChris Wilson 		ret = IRQ_HANDLED;
3912a266c7d5SChris Wilson 		iir = new_iir;
391338bde180SChris Wilson 	} while (iir & ~flip_mask);
3914a266c7d5SChris Wilson 
3915a266c7d5SChris Wilson 	return ret;
3916a266c7d5SChris Wilson }
3917a266c7d5SChris Wilson 
3918a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3919a266c7d5SChris Wilson {
39202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3921a266c7d5SChris Wilson 	int pipe;
3922a266c7d5SChris Wilson 
3923a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3924a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3925a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3926a266c7d5SChris Wilson 	}
3927a266c7d5SChris Wilson 
392800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
3929055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
393055b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3931a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
393255b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
393355b39755SChris Wilson 	}
3934a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3935a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3936a266c7d5SChris Wilson 
3937a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3938a266c7d5SChris Wilson }
3939a266c7d5SChris Wilson 
3940a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3941a266c7d5SChris Wilson {
39422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3943a266c7d5SChris Wilson 	int pipe;
3944a266c7d5SChris Wilson 
3945a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3946a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3947a266c7d5SChris Wilson 
3948a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3949055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3950a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3951a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3952a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3953a266c7d5SChris Wilson 	POSTING_READ(IER);
3954a266c7d5SChris Wilson }
3955a266c7d5SChris Wilson 
3956a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3957a266c7d5SChris Wilson {
39582d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3959bbba0a97SChris Wilson 	u32 enable_mask;
3960a266c7d5SChris Wilson 	u32 error_mask;
3961a266c7d5SChris Wilson 
3962a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3963bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3964adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3965bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3966bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3967bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3968bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3969bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3970bbba0a97SChris Wilson 
3971bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
397221ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
397321ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
3974bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
3975bbba0a97SChris Wilson 
3976bbba0a97SChris Wilson 	if (IS_G4X(dev))
3977bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
3978a266c7d5SChris Wilson 
3979b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3980b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3981d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3982755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3983755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3984755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3985d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3986a266c7d5SChris Wilson 
3987a266c7d5SChris Wilson 	/*
3988a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
3989a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
3990a266c7d5SChris Wilson 	 */
3991a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
3992a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
3993a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
3994a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
3995a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3996a266c7d5SChris Wilson 	} else {
3997a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
3998a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
3999a266c7d5SChris Wilson 	}
4000a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4001a266c7d5SChris Wilson 
4002a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4003a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4004a266c7d5SChris Wilson 	POSTING_READ(IER);
4005a266c7d5SChris Wilson 
400620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
400720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
400820afbda2SDaniel Vetter 
4009f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
401020afbda2SDaniel Vetter 
401120afbda2SDaniel Vetter 	return 0;
401220afbda2SDaniel Vetter }
401320afbda2SDaniel Vetter 
4014bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
401520afbda2SDaniel Vetter {
40162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4017cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
401820afbda2SDaniel Vetter 	u32 hotplug_en;
401920afbda2SDaniel Vetter 
4020b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4021b5ea2d56SDaniel Vetter 
4022bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4023bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4024adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4025e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
4026b2784e15SDamien Lespiau 	for_each_intel_encoder(dev, intel_encoder)
4027cd569aedSEgbert Eich 		if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4028cd569aedSEgbert Eich 			hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4029a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4030a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4031a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4032a266c7d5SChris Wilson 	*/
4033a266c7d5SChris Wilson 	if (IS_G4X(dev))
4034a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
403585fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4036a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4037a266c7d5SChris Wilson 
4038a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4039a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4040a266c7d5SChris Wilson }
4041a266c7d5SChris Wilson 
4042ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4043a266c7d5SChris Wilson {
404445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4046a266c7d5SChris Wilson 	u32 iir, new_iir;
4047a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4048a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
404921ad8330SVille Syrjälä 	u32 flip_mask =
405021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
405121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4052a266c7d5SChris Wilson 
40532dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40542dd2a883SImre Deak 		return IRQ_NONE;
40552dd2a883SImre Deak 
4056a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4057a266c7d5SChris Wilson 
4058a266c7d5SChris Wilson 	for (;;) {
4059501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
40602c8ba29fSChris Wilson 		bool blc_event = false;
40612c8ba29fSChris Wilson 
4062a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4063a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4064a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4065a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4066a266c7d5SChris Wilson 		 */
4067222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4068a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4069aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4070a266c7d5SChris Wilson 
4071055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4072a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4073a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4074a266c7d5SChris Wilson 
4075a266c7d5SChris Wilson 			/*
4076a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4077a266c7d5SChris Wilson 			 */
4078a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4079a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4080501e01d7SVille Syrjälä 				irq_received = true;
4081a266c7d5SChris Wilson 			}
4082a266c7d5SChris Wilson 		}
4083222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4084a266c7d5SChris Wilson 
4085a266c7d5SChris Wilson 		if (!irq_received)
4086a266c7d5SChris Wilson 			break;
4087a266c7d5SChris Wilson 
4088a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4089a266c7d5SChris Wilson 
4090a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
409116c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
409216c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4093a266c7d5SChris Wilson 
409421ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4095a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4096a266c7d5SChris Wilson 
4097a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4098*74cdb337SChris Wilson 			notify_ring(&dev_priv->ring[RCS]);
4099a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4100*74cdb337SChris Wilson 			notify_ring(&dev_priv->ring[VCS]);
4101a266c7d5SChris Wilson 
4102055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41032c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
410490a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
410590a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4106a266c7d5SChris Wilson 
4107a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4108a266c7d5SChris Wilson 				blc_event = true;
41094356d586SDaniel Vetter 
41104356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4111277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4112a266c7d5SChris Wilson 
41131f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41141f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41152d9d2b0bSVille Syrjälä 		}
4116a266c7d5SChris Wilson 
4117a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4118a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4119a266c7d5SChris Wilson 
4120515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4121515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4122515ac2bbSDaniel Vetter 
4123a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4124a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4125a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4126a266c7d5SChris Wilson 		 * we would never get another interrupt.
4127a266c7d5SChris Wilson 		 *
4128a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4129a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4130a266c7d5SChris Wilson 		 * another one.
4131a266c7d5SChris Wilson 		 *
4132a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4133a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4134a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4135a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4136a266c7d5SChris Wilson 		 * stray interrupts.
4137a266c7d5SChris Wilson 		 */
4138a266c7d5SChris Wilson 		iir = new_iir;
4139a266c7d5SChris Wilson 	}
4140a266c7d5SChris Wilson 
4141a266c7d5SChris Wilson 	return ret;
4142a266c7d5SChris Wilson }
4143a266c7d5SChris Wilson 
4144a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4145a266c7d5SChris Wilson {
41462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4147a266c7d5SChris Wilson 	int pipe;
4148a266c7d5SChris Wilson 
4149a266c7d5SChris Wilson 	if (!dev_priv)
4150a266c7d5SChris Wilson 		return;
4151a266c7d5SChris Wilson 
4152a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4153a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4154a266c7d5SChris Wilson 
4155a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4156055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4157a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4158a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4159a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4160a266c7d5SChris Wilson 
4161055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4162a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4163a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4164a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4165a266c7d5SChris Wilson }
4166a266c7d5SChris Wilson 
41674cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4168ac4c16c5SEgbert Eich {
41696323751dSImre Deak 	struct drm_i915_private *dev_priv =
41706323751dSImre Deak 		container_of(work, typeof(*dev_priv),
41716323751dSImre Deak 			     hotplug_reenable_work.work);
4172ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4173ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4174ac4c16c5SEgbert Eich 	int i;
4175ac4c16c5SEgbert Eich 
41766323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
41776323751dSImre Deak 
41784cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4179ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4180ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4181ac4c16c5SEgbert Eich 
4182ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4183ac4c16c5SEgbert Eich 			continue;
4184ac4c16c5SEgbert Eich 
4185ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4186ac4c16c5SEgbert Eich 
4187ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4188ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4189ac4c16c5SEgbert Eich 
4190ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4191ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4192ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4193c23cc417SJani Nikula 							 connector->name);
4194ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4195ac4c16c5SEgbert Eich 				if (!connector->polled)
4196ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4197ac4c16c5SEgbert Eich 			}
4198ac4c16c5SEgbert Eich 		}
4199ac4c16c5SEgbert Eich 	}
4200ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4201ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
42024cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
42036323751dSImre Deak 
42046323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4205ac4c16c5SEgbert Eich }
4206ac4c16c5SEgbert Eich 
4207fca52a55SDaniel Vetter /**
4208fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4209fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4210fca52a55SDaniel Vetter  *
4211fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4212fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4213fca52a55SDaniel Vetter  */
4214b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4215f71d4af4SJesse Barnes {
4216b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
42178b2e326dSChris Wilson 
42188b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
421913cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
4220c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4221a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42228b2e326dSChris Wilson 
4223a6706b45SDeepak S 	/* Let's track the enabled rps events */
4224b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
42256c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
42266f4b12f8SChris Wilson 		dev_priv->pm_rps_events = GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED;
422731685c25SDeepak S 	else
4228a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4229a6706b45SDeepak S 
4230737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4231737b1506SChris Wilson 			  i915_hangcheck_elapsed);
42326323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
42334cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
423461bac78eSDaniel Vetter 
423597a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
42369ee32feaSDaniel Vetter 
4237b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42384cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
42394cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4240b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4241f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4242f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4243391f75e2SVille Syrjälä 	} else {
4244391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4245391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4246f71d4af4SJesse Barnes 	}
4247f71d4af4SJesse Barnes 
424821da2700SVille Syrjälä 	/*
424921da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
425021da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
425121da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
425221da2700SVille Syrjälä 	 */
4253b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
425421da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
425521da2700SVille Syrjälä 
4256f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4257f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4258f71d4af4SJesse Barnes 
4259b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
426043f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
426143f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
426243f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
426343f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
426443f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
426543f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
426643f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4267b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
42687e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
42697e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
42707e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
42717e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
42727e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
42737e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4274fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4275b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4276abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4277723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4278abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4279abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4280abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4281abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4282abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4283f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4284f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4285723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4286f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4287f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4288f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4289f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
429082a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4291f71d4af4SJesse Barnes 	} else {
4292b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4293c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4294c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4295c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4296c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4297b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4298a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4299a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4300a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4301a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4302c2798b19SChris Wilson 		} else {
4303a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4304a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4305a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4306a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4307c2798b19SChris Wilson 		}
4308778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4309778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4310f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4311f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4312f71d4af4SJesse Barnes 	}
4313f71d4af4SJesse Barnes }
431420afbda2SDaniel Vetter 
4315fca52a55SDaniel Vetter /**
4316fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4317fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4318fca52a55SDaniel Vetter  *
4319fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4320fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4321fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4322fca52a55SDaniel Vetter  * obeyed.
4323fca52a55SDaniel Vetter  *
4324fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4325fca52a55SDaniel Vetter  * in the driver load and resume code.
4326fca52a55SDaniel Vetter  */
4327b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
432820afbda2SDaniel Vetter {
4329b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4330821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4331821450c6SEgbert Eich 	struct drm_connector *connector;
4332821450c6SEgbert Eich 	int i;
433320afbda2SDaniel Vetter 
4334821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4335821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4336821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4337821450c6SEgbert Eich 	}
4338821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4339821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4340821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
43410e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
43420e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
43430e32b39cSDave Airlie 		if (intel_connector->mst_port)
4344821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4345821450c6SEgbert Eich 	}
4346b5ea2d56SDaniel Vetter 
4347b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4348b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4349d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
435020afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
435120afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4352d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
435320afbda2SDaniel Vetter }
4354c67a470bSPaulo Zanoni 
4355fca52a55SDaniel Vetter /**
4356fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4357fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4358fca52a55SDaniel Vetter  *
4359fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4360fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4361fca52a55SDaniel Vetter  *
4362fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4363fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4364fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4365fca52a55SDaniel Vetter  */
43662aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
43672aeb7d3aSDaniel Vetter {
43682aeb7d3aSDaniel Vetter 	/*
43692aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
43702aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
43712aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
43722aeb7d3aSDaniel Vetter 	 */
43732aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
43742aeb7d3aSDaniel Vetter 
43752aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
43762aeb7d3aSDaniel Vetter }
43772aeb7d3aSDaniel Vetter 
4378fca52a55SDaniel Vetter /**
4379fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4380fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4381fca52a55SDaniel Vetter  *
4382fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4383fca52a55SDaniel Vetter  * resources acquired in the init functions.
4384fca52a55SDaniel Vetter  */
43852aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
43862aeb7d3aSDaniel Vetter {
43872aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
43882aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
43892aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
43902aeb7d3aSDaniel Vetter }
43912aeb7d3aSDaniel Vetter 
4392fca52a55SDaniel Vetter /**
4393fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4394fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4395fca52a55SDaniel Vetter  *
4396fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4397fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4398fca52a55SDaniel Vetter  */
4399b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4400c67a470bSPaulo Zanoni {
4401b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44022aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44032dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4404c67a470bSPaulo Zanoni }
4405c67a470bSPaulo Zanoni 
4406fca52a55SDaniel Vetter /**
4407fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4408fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4409fca52a55SDaniel Vetter  *
4410fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4411fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4412fca52a55SDaniel Vetter  */
4413b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4414c67a470bSPaulo Zanoni {
44152aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4416b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4417b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4418c67a470bSPaulo Zanoni }
4419