xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision 43cf3bf084ba097463d67e756ff821505bdaa69d)
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);
2803cc134e3SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
2813cc134e3SImre Deak }
2823cc134e3SImre Deak 
283b900b949SImre Deak void gen6_enable_rps_interrupts(struct drm_device *dev)
284b900b949SImre Deak {
285b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
286b900b949SImre Deak 
287b900b949SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
28878e68d36SImre Deak 
289b900b949SImre Deak 	WARN_ON(dev_priv->rps.pm_iir);
2903cc134e3SImre Deak 	WARN_ON(I915_READ(gen6_pm_iir(dev_priv)) & dev_priv->pm_rps_events);
291d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = true;
29278e68d36SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) |
29378e68d36SImre Deak 				dev_priv->pm_rps_events);
294b900b949SImre Deak 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
29578e68d36SImre Deak 
296b900b949SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
297b900b949SImre Deak }
298b900b949SImre Deak 
29959d02a1fSImre Deak u32 gen6_sanitize_rps_pm_mask(struct drm_i915_private *dev_priv, u32 mask)
30059d02a1fSImre Deak {
30159d02a1fSImre Deak 	/*
302f24eeb19SImre Deak 	 * SNB,IVB can while VLV,CHV may hard hang on looping batchbuffer
30359d02a1fSImre Deak 	 * if GEN6_PM_UP_EI_EXPIRED is masked.
304f24eeb19SImre Deak 	 *
305f24eeb19SImre Deak 	 * TODO: verify if this can be reproduced on VLV,CHV.
30659d02a1fSImre Deak 	 */
30759d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen <= 7 && !IS_HASWELL(dev_priv))
30859d02a1fSImre Deak 		mask &= ~GEN6_PM_RP_UP_EI_EXPIRED;
30959d02a1fSImre Deak 
31059d02a1fSImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
31159d02a1fSImre Deak 		mask &= ~GEN8_PMINTR_REDIRECT_TO_NON_DISP;
31259d02a1fSImre Deak 
31359d02a1fSImre Deak 	return mask;
31459d02a1fSImre Deak }
31559d02a1fSImre Deak 
316b900b949SImre Deak void gen6_disable_rps_interrupts(struct drm_device *dev)
317b900b949SImre Deak {
318b900b949SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
319b900b949SImre Deak 
320d4d70aa5SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
321d4d70aa5SImre Deak 	dev_priv->rps.interrupts_enabled = false;
322d4d70aa5SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
323d4d70aa5SImre Deak 
324d4d70aa5SImre Deak 	cancel_work_sync(&dev_priv->rps.work);
325d4d70aa5SImre Deak 
3269939fba2SImre Deak 	spin_lock_irq(&dev_priv->irq_lock);
3279939fba2SImre Deak 
32859d02a1fSImre Deak 	I915_WRITE(GEN6_PMINTRMSK, gen6_sanitize_rps_pm_mask(dev_priv, ~0));
3299939fba2SImre Deak 
3309939fba2SImre Deak 	__gen6_disable_pm_irq(dev_priv, dev_priv->pm_rps_events);
331b900b949SImre Deak 	I915_WRITE(gen6_pm_ier(dev_priv), I915_READ(gen6_pm_ier(dev_priv)) &
332b900b949SImre Deak 				~dev_priv->pm_rps_events);
333b900b949SImre Deak 	I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
3349939fba2SImre Deak 	I915_WRITE(gen6_pm_iir(dev_priv), dev_priv->pm_rps_events);
3359939fba2SImre Deak 
3369939fba2SImre Deak 	dev_priv->rps.pm_iir = 0;
3379939fba2SImre Deak 
3389939fba2SImre Deak 	spin_unlock_irq(&dev_priv->irq_lock);
339b900b949SImre Deak }
340b900b949SImre Deak 
3410961021aSBen Widawsky /**
342fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
343fee884edSDaniel Vetter  * @dev_priv: driver private
344fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
345fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
346fee884edSDaniel Vetter  */
34747339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
348fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
349fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
350fee884edSDaniel Vetter {
351fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
352fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
353fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
354fee884edSDaniel Vetter 
35515a17aaeSDaniel Vetter 	WARN_ON(enabled_irq_mask & ~interrupt_mask);
35615a17aaeSDaniel Vetter 
357fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
358fee884edSDaniel Vetter 
3599df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
360c67a470bSPaulo Zanoni 		return;
361c67a470bSPaulo Zanoni 
362fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
363fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
364fee884edSDaniel Vetter }
3658664281bSPaulo Zanoni 
366b5ea642aSDaniel Vetter static void
367755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
368755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
3697c463586SKeith Packard {
3709db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
371755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3727c463586SKeith Packard 
373b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
374d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
375b79480baSDaniel Vetter 
37604feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
37704feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
37804feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
37904feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
380755e9019SImre Deak 		return;
381755e9019SImre Deak 
382755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
38346c06a30SVille Syrjälä 		return;
38446c06a30SVille Syrjälä 
38591d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
38691d181ddSImre Deak 
3877c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
388755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
38946c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3903143a2bfSChris Wilson 	POSTING_READ(reg);
3917c463586SKeith Packard }
3927c463586SKeith Packard 
393b5ea642aSDaniel Vetter static void
394755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
395755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
3967c463586SKeith Packard {
3979db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
398755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3997c463586SKeith Packard 
400b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
401d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
402b79480baSDaniel Vetter 
40304feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
40404feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
40504feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
40604feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
40746c06a30SVille Syrjälä 		return;
40846c06a30SVille Syrjälä 
409755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
410755e9019SImre Deak 		return;
411755e9019SImre Deak 
41291d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
41391d181ddSImre Deak 
414755e9019SImre Deak 	pipestat &= ~enable_mask;
41546c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
4163143a2bfSChris Wilson 	POSTING_READ(reg);
4177c463586SKeith Packard }
4187c463586SKeith Packard 
41910c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
42010c59c51SImre Deak {
42110c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
42210c59c51SImre Deak 
42310c59c51SImre Deak 	/*
424724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
425724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
42610c59c51SImre Deak 	 */
42710c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
42810c59c51SImre Deak 		return 0;
429724a6905SVille Syrjälä 	/*
430724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
431724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
432724a6905SVille Syrjälä 	 */
433724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
434724a6905SVille Syrjälä 		return 0;
43510c59c51SImre Deak 
43610c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
43710c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
43810c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
43910c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
44010c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
44110c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
44210c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
44310c59c51SImre Deak 
44410c59c51SImre Deak 	return enable_mask;
44510c59c51SImre Deak }
44610c59c51SImre Deak 
447755e9019SImre Deak void
448755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
449755e9019SImre Deak 		     u32 status_mask)
450755e9019SImre Deak {
451755e9019SImre Deak 	u32 enable_mask;
452755e9019SImre Deak 
45310c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
45410c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
45510c59c51SImre Deak 							   status_mask);
45610c59c51SImre Deak 	else
457755e9019SImre Deak 		enable_mask = status_mask << 16;
458755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
459755e9019SImre Deak }
460755e9019SImre Deak 
461755e9019SImre Deak void
462755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
463755e9019SImre Deak 		      u32 status_mask)
464755e9019SImre Deak {
465755e9019SImre Deak 	u32 enable_mask;
466755e9019SImre Deak 
46710c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
46810c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
46910c59c51SImre Deak 							   status_mask);
47010c59c51SImre Deak 	else
471755e9019SImre Deak 		enable_mask = status_mask << 16;
472755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
473755e9019SImre Deak }
474755e9019SImre Deak 
475c0e09200SDave Airlie /**
476f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
47701c66889SZhao Yakui  */
478f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
47901c66889SZhao Yakui {
4802d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4811ec14ad3SChris Wilson 
482f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
483f49e38ddSJani Nikula 		return;
484f49e38ddSJani Nikula 
48513321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
48601c66889SZhao Yakui 
487755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
488a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4893b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
490755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
4911ec14ad3SChris Wilson 
49213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
49301c66889SZhao Yakui }
49401c66889SZhao Yakui 
495f75f3746SVille Syrjälä /*
496f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
497f75f3746SVille Syrjälä  * around the vertical blanking period.
498f75f3746SVille Syrjälä  *
499f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
500f75f3746SVille Syrjälä  *  vblank_start >= 3
501f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
502f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
503f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
504f75f3746SVille Syrjälä  *
505f75f3746SVille Syrjälä  *           start of vblank:
506f75f3746SVille Syrjälä  *           latch double buffered registers
507f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
508f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
509f75f3746SVille Syrjälä  *           |
510f75f3746SVille Syrjälä  *           |          frame start:
511f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
512f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
513f75f3746SVille Syrjälä  *           |          |
514f75f3746SVille Syrjälä  *           |          |  start of vsync:
515f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
516f75f3746SVille Syrjälä  *           |          |  |
517f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
518f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
519f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
520f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
521f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
522f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
523f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
524f75f3746SVille Syrjälä  *       |          |                                         |
525f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
526f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
527f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
528f75f3746SVille Syrjälä  *
529f75f3746SVille Syrjälä  * x  = horizontal active
530f75f3746SVille Syrjälä  * _  = horizontal blanking
531f75f3746SVille Syrjälä  * hs = horizontal sync
532f75f3746SVille Syrjälä  * va = vertical active
533f75f3746SVille Syrjälä  * vb = vertical blanking
534f75f3746SVille Syrjälä  * vs = vertical sync
535f75f3746SVille Syrjälä  * vbs = vblank_start (number)
536f75f3746SVille Syrjälä  *
537f75f3746SVille Syrjälä  * Summary:
538f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
539f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
540f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
541f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
542f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
543f75f3746SVille Syrjälä  */
544f75f3746SVille Syrjälä 
5454cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5464cdb83ecSVille Syrjälä {
5474cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5484cdb83ecSVille Syrjälä 	return 0;
5494cdb83ecSVille Syrjälä }
5504cdb83ecSVille Syrjälä 
55142f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
55242f52ef8SKeith Packard  * we use as a pipe index
55342f52ef8SKeith Packard  */
554f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5550a3e67a4SJesse Barnes {
5562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5570a3e67a4SJesse Barnes 	unsigned long high_frame;
5580a3e67a4SJesse Barnes 	unsigned long low_frame;
5590b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
560391f75e2SVille Syrjälä 	struct intel_crtc *intel_crtc =
561391f75e2SVille Syrjälä 		to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
562391f75e2SVille Syrjälä 	const struct drm_display_mode *mode =
5636e3c9717SAnder Conselvan de Oliveira 		&intel_crtc->config->base.adjusted_mode;
564391f75e2SVille Syrjälä 
5650b2a8e09SVille Syrjälä 	htotal = mode->crtc_htotal;
5660b2a8e09SVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
5670b2a8e09SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
5680b2a8e09SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5690b2a8e09SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
570391f75e2SVille Syrjälä 
5710b2a8e09SVille Syrjälä 	/* Convert to pixel count */
5720b2a8e09SVille Syrjälä 	vbl_start *= htotal;
5730b2a8e09SVille Syrjälä 
5740b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
5750b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
5760b2a8e09SVille Syrjälä 
5779db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5789db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5795eddb70bSChris Wilson 
5800a3e67a4SJesse Barnes 	/*
5810a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5820a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5830a3e67a4SJesse Barnes 	 * register.
5840a3e67a4SJesse Barnes 	 */
5850a3e67a4SJesse Barnes 	do {
5865eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
587391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5885eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5890a3e67a4SJesse Barnes 	} while (high1 != high2);
5900a3e67a4SJesse Barnes 
5915eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
592391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5935eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
594391f75e2SVille Syrjälä 
595391f75e2SVille Syrjälä 	/*
596391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
597391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
598391f75e2SVille Syrjälä 	 * counter against vblank start.
599391f75e2SVille Syrjälä 	 */
600edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
6010a3e67a4SJesse Barnes }
6020a3e67a4SJesse Barnes 
603f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
6049880b7a5SJesse Barnes {
6052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
6069db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
6079880b7a5SJesse Barnes 
6089880b7a5SJesse Barnes 	return I915_READ(reg);
6099880b7a5SJesse Barnes }
6109880b7a5SJesse Barnes 
611ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
612ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
613ad3543edSMario Kleiner 
614a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
615a225f079SVille Syrjälä {
616a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
617a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
6186e3c9717SAnder Conselvan de Oliveira 	const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
619a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
62080715b2fSVille Syrjälä 	int position, vtotal;
621a225f079SVille Syrjälä 
62280715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
623a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
624a225f079SVille Syrjälä 		vtotal /= 2;
625a225f079SVille Syrjälä 
626a225f079SVille Syrjälä 	if (IS_GEN2(dev))
627a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
628a225f079SVille Syrjälä 	else
629a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
630a225f079SVille Syrjälä 
631a225f079SVille Syrjälä 	/*
63280715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
63380715b2fSVille Syrjälä 	 * scanline_offset adjustment.
634a225f079SVille Syrjälä 	 */
63580715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
636a225f079SVille Syrjälä }
637a225f079SVille Syrjälä 
638f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
639abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
640abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6410af7e4dfSMario Kleiner {
642c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
643c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
644c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6456e3c9717SAnder Conselvan de Oliveira 	const struct drm_display_mode *mode = &intel_crtc->config->base.adjusted_mode;
6463aa18df8SVille Syrjälä 	int position;
64778e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
6480af7e4dfSMario Kleiner 	bool in_vbl = true;
6490af7e4dfSMario Kleiner 	int ret = 0;
650ad3543edSMario Kleiner 	unsigned long irqflags;
6510af7e4dfSMario Kleiner 
652c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6530af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6549db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6550af7e4dfSMario Kleiner 		return 0;
6560af7e4dfSMario Kleiner 	}
6570af7e4dfSMario Kleiner 
658c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
65978e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
660c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
661c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
662c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6630af7e4dfSMario Kleiner 
664d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
665d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
666d31faf65SVille Syrjälä 		vbl_end /= 2;
667d31faf65SVille Syrjälä 		vtotal /= 2;
668d31faf65SVille Syrjälä 	}
669d31faf65SVille Syrjälä 
670c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
671c2baf4b7SVille Syrjälä 
672ad3543edSMario Kleiner 	/*
673ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
674ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
675ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
676ad3543edSMario Kleiner 	 */
677ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
678ad3543edSMario Kleiner 
679ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
680ad3543edSMario Kleiner 
681ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
682ad3543edSMario Kleiner 	if (stime)
683ad3543edSMario Kleiner 		*stime = ktime_get();
684ad3543edSMario Kleiner 
6857c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6860af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6870af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6880af7e4dfSMario Kleiner 		 */
689a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
6900af7e4dfSMario Kleiner 	} else {
6910af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6920af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6930af7e4dfSMario Kleiner 		 * scanout position.
6940af7e4dfSMario Kleiner 		 */
695ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
6960af7e4dfSMario Kleiner 
6973aa18df8SVille Syrjälä 		/* convert to pixel counts */
6983aa18df8SVille Syrjälä 		vbl_start *= htotal;
6993aa18df8SVille Syrjälä 		vbl_end *= htotal;
7003aa18df8SVille Syrjälä 		vtotal *= htotal;
70178e8fc6bSVille Syrjälä 
70278e8fc6bSVille Syrjälä 		/*
7037e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
7047e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
7057e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
7067e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
7077e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
7087e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
7097e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
7107e78f1cbSVille Syrjälä 		 */
7117e78f1cbSVille Syrjälä 		if (position >= vtotal)
7127e78f1cbSVille Syrjälä 			position = vtotal - 1;
7137e78f1cbSVille Syrjälä 
7147e78f1cbSVille Syrjälä 		/*
71578e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
71678e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
71778e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
71878e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
71978e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
72078e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
72178e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
72278e8fc6bSVille Syrjälä 		 */
72378e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7243aa18df8SVille Syrjälä 	}
7253aa18df8SVille Syrjälä 
726ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
727ad3543edSMario Kleiner 	if (etime)
728ad3543edSMario Kleiner 		*etime = ktime_get();
729ad3543edSMario Kleiner 
730ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
731ad3543edSMario Kleiner 
732ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
733ad3543edSMario Kleiner 
7343aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7353aa18df8SVille Syrjälä 
7363aa18df8SVille Syrjälä 	/*
7373aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7383aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7393aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7403aa18df8SVille Syrjälä 	 * up since vbl_end.
7413aa18df8SVille Syrjälä 	 */
7423aa18df8SVille Syrjälä 	if (position >= vbl_start)
7433aa18df8SVille Syrjälä 		position -= vbl_end;
7443aa18df8SVille Syrjälä 	else
7453aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7463aa18df8SVille Syrjälä 
7477c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7483aa18df8SVille Syrjälä 		*vpos = position;
7493aa18df8SVille Syrjälä 		*hpos = 0;
7503aa18df8SVille Syrjälä 	} else {
7510af7e4dfSMario Kleiner 		*vpos = position / htotal;
7520af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7530af7e4dfSMario Kleiner 	}
7540af7e4dfSMario Kleiner 
7550af7e4dfSMario Kleiner 	/* In vblank? */
7560af7e4dfSMario Kleiner 	if (in_vbl)
7573d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
7580af7e4dfSMario Kleiner 
7590af7e4dfSMario Kleiner 	return ret;
7600af7e4dfSMario Kleiner }
7610af7e4dfSMario Kleiner 
762a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
763a225f079SVille Syrjälä {
764a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
765a225f079SVille Syrjälä 	unsigned long irqflags;
766a225f079SVille Syrjälä 	int position;
767a225f079SVille Syrjälä 
768a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
769a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
770a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
771a225f079SVille Syrjälä 
772a225f079SVille Syrjälä 	return position;
773a225f079SVille Syrjälä }
774a225f079SVille Syrjälä 
775f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7760af7e4dfSMario Kleiner 			      int *max_error,
7770af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7780af7e4dfSMario Kleiner 			      unsigned flags)
7790af7e4dfSMario Kleiner {
7804041b853SChris Wilson 	struct drm_crtc *crtc;
7810af7e4dfSMario Kleiner 
7827eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7834041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7840af7e4dfSMario Kleiner 		return -EINVAL;
7850af7e4dfSMario Kleiner 	}
7860af7e4dfSMario Kleiner 
7870af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7884041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7894041b853SChris Wilson 	if (crtc == NULL) {
7904041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7914041b853SChris Wilson 		return -EINVAL;
7924041b853SChris Wilson 	}
7934041b853SChris Wilson 
79483d65738SMatt Roper 	if (!crtc->state->enable) {
7954041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7964041b853SChris Wilson 		return -EBUSY;
7974041b853SChris Wilson 	}
7980af7e4dfSMario Kleiner 
7990af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
8004041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
8014041b853SChris Wilson 						     vblank_time, flags,
8027da903efSVille Syrjälä 						     crtc,
8036e3c9717SAnder Conselvan de Oliveira 						     &to_intel_crtc(crtc)->config->base.adjusted_mode);
8040af7e4dfSMario Kleiner }
8050af7e4dfSMario Kleiner 
80667c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
80767c347ffSJani Nikula 				struct drm_connector *connector)
808321a1b30SEgbert Eich {
809321a1b30SEgbert Eich 	enum drm_connector_status old_status;
810321a1b30SEgbert Eich 
811321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
812321a1b30SEgbert Eich 	old_status = connector->status;
813321a1b30SEgbert Eich 
814321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
81567c347ffSJani Nikula 	if (old_status == connector->status)
81667c347ffSJani Nikula 		return false;
81767c347ffSJani Nikula 
81867c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
819321a1b30SEgbert Eich 		      connector->base.id,
820c23cc417SJani Nikula 		      connector->name,
82167c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
82267c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
82367c347ffSJani Nikula 
82467c347ffSJani Nikula 	return true;
825321a1b30SEgbert Eich }
826321a1b30SEgbert Eich 
82713cf5504SDave Airlie static void i915_digport_work_func(struct work_struct *work)
82813cf5504SDave Airlie {
82913cf5504SDave Airlie 	struct drm_i915_private *dev_priv =
83013cf5504SDave Airlie 		container_of(work, struct drm_i915_private, dig_port_work);
83113cf5504SDave Airlie 	u32 long_port_mask, short_port_mask;
83213cf5504SDave Airlie 	struct intel_digital_port *intel_dig_port;
833b2c5c181SDaniel Vetter 	int i;
83413cf5504SDave Airlie 	u32 old_bits = 0;
83513cf5504SDave Airlie 
8364cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
83713cf5504SDave Airlie 	long_port_mask = dev_priv->long_hpd_port_mask;
83813cf5504SDave Airlie 	dev_priv->long_hpd_port_mask = 0;
83913cf5504SDave Airlie 	short_port_mask = dev_priv->short_hpd_port_mask;
84013cf5504SDave Airlie 	dev_priv->short_hpd_port_mask = 0;
8414cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
84213cf5504SDave Airlie 
84313cf5504SDave Airlie 	for (i = 0; i < I915_MAX_PORTS; i++) {
84413cf5504SDave Airlie 		bool valid = false;
84513cf5504SDave Airlie 		bool long_hpd = false;
84613cf5504SDave Airlie 		intel_dig_port = dev_priv->hpd_irq_port[i];
84713cf5504SDave Airlie 		if (!intel_dig_port || !intel_dig_port->hpd_pulse)
84813cf5504SDave Airlie 			continue;
84913cf5504SDave Airlie 
85013cf5504SDave Airlie 		if (long_port_mask & (1 << i))  {
85113cf5504SDave Airlie 			valid = true;
85213cf5504SDave Airlie 			long_hpd = true;
85313cf5504SDave Airlie 		} else if (short_port_mask & (1 << i))
85413cf5504SDave Airlie 			valid = true;
85513cf5504SDave Airlie 
85613cf5504SDave Airlie 		if (valid) {
857b2c5c181SDaniel Vetter 			enum irqreturn ret;
858b2c5c181SDaniel Vetter 
85913cf5504SDave Airlie 			ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
860b2c5c181SDaniel Vetter 			if (ret == IRQ_NONE) {
861b2c5c181SDaniel Vetter 				/* fall back to old school hpd */
86213cf5504SDave Airlie 				old_bits |= (1 << intel_dig_port->base.hpd_pin);
86313cf5504SDave Airlie 			}
86413cf5504SDave Airlie 		}
86513cf5504SDave Airlie 	}
86613cf5504SDave Airlie 
86713cf5504SDave Airlie 	if (old_bits) {
8684cb21832SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
86913cf5504SDave Airlie 		dev_priv->hpd_event_bits |= old_bits;
8704cb21832SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
87113cf5504SDave Airlie 		schedule_work(&dev_priv->hotplug_work);
87213cf5504SDave Airlie 	}
87313cf5504SDave Airlie }
87413cf5504SDave Airlie 
8755ca58282SJesse Barnes /*
8765ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8775ca58282SJesse Barnes  */
878ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
879ac4c16c5SEgbert Eich 
8805ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8815ca58282SJesse Barnes {
8822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
8832d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
8845ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
885c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
886cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
887cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
888cd569aedSEgbert Eich 	struct drm_connector *connector;
889cd569aedSEgbert Eich 	bool hpd_disabled = false;
890321a1b30SEgbert Eich 	bool changed = false;
891142e2398SEgbert Eich 	u32 hpd_event_bits;
8925ca58282SJesse Barnes 
893a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
894e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
895e67189abSJesse Barnes 
8964cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
897142e2398SEgbert Eich 
898142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
899142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
900cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
901cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
90236cd7444SDave Airlie 		if (!intel_connector->encoder)
90336cd7444SDave Airlie 			continue;
904cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
905cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
906cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
907cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
908cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
909cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
910c23cc417SJani Nikula 				connector->name);
911cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
912cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
913cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
914cd569aedSEgbert Eich 			hpd_disabled = true;
915cd569aedSEgbert Eich 		}
916142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
917142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
918c23cc417SJani Nikula 				      connector->name, intel_encoder->hpd_pin);
919142e2398SEgbert Eich 		}
920cd569aedSEgbert Eich 	}
921cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
922cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
923cd569aedSEgbert Eich 	  * some connectors */
924ac4c16c5SEgbert Eich 	if (hpd_disabled) {
925cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
9266323751dSImre Deak 		mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
9276323751dSImre Deak 				 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
928ac4c16c5SEgbert Eich 	}
929cd569aedSEgbert Eich 
9304cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
931cd569aedSEgbert Eich 
932321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
933321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
93436cd7444SDave Airlie 		if (!intel_connector->encoder)
93536cd7444SDave Airlie 			continue;
936321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
937321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
938cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
939cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
940321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
941321a1b30SEgbert Eich 				changed = true;
942321a1b30SEgbert Eich 		}
943321a1b30SEgbert Eich 	}
94440ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
94540ee3381SKeith Packard 
946321a1b30SEgbert Eich 	if (changed)
947321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9485ca58282SJesse Barnes }
9495ca58282SJesse Barnes 
950d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
951f97108d1SJesse Barnes {
9522d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
953b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9549270388eSDaniel Vetter 	u8 new_delay;
9559270388eSDaniel Vetter 
956d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
957f97108d1SJesse Barnes 
95873edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
95973edd18fSDaniel Vetter 
96020e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9619270388eSDaniel Vetter 
9627648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
963b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
964b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
965f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
966f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
967f97108d1SJesse Barnes 
968f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
969b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
97020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
97120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
97220e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
97320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
974b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
97520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
97620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
97720e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
97820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
979f97108d1SJesse Barnes 	}
980f97108d1SJesse Barnes 
9817648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
98220e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
983f97108d1SJesse Barnes 
984d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9859270388eSDaniel Vetter 
986f97108d1SJesse Barnes 	return;
987f97108d1SJesse Barnes }
988f97108d1SJesse Barnes 
989549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
990a4872ba6SOscar Mateo 			struct intel_engine_cs *ring)
991549f7365SChris Wilson {
99293b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
993475553deSChris Wilson 		return;
994475553deSChris Wilson 
995bcfcc8baSJohn Harrison 	trace_i915_gem_request_notify(ring);
9969862e600SChris Wilson 
997549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
998549f7365SChris Wilson }
999549f7365SChris Wilson 
1000*43cf3bf0SChris Wilson static void vlv_c0_read(struct drm_i915_private *dev_priv,
1001*43cf3bf0SChris Wilson 			struct intel_rps_ei *ei)
100231685c25SDeepak S {
1003*43cf3bf0SChris Wilson 	ei->cz_clock = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
1004*43cf3bf0SChris Wilson 	ei->render_c0 = I915_READ(VLV_RENDER_C0_COUNT);
1005*43cf3bf0SChris Wilson 	ei->media_c0 = I915_READ(VLV_MEDIA_C0_COUNT);
100631685c25SDeepak S }
100731685c25SDeepak S 
1008*43cf3bf0SChris Wilson static bool vlv_c0_above(struct drm_i915_private *dev_priv,
1009*43cf3bf0SChris Wilson 			 const struct intel_rps_ei *old,
1010*43cf3bf0SChris Wilson 			 const struct intel_rps_ei *now,
1011*43cf3bf0SChris Wilson 			 int threshold)
101231685c25SDeepak S {
1013*43cf3bf0SChris Wilson 	u64 time, c0;
101431685c25SDeepak S 
1015*43cf3bf0SChris Wilson 	if (old->cz_clock == 0)
1016*43cf3bf0SChris Wilson 		return false;
101731685c25SDeepak S 
1018*43cf3bf0SChris Wilson 	time = now->cz_clock - old->cz_clock;
1019*43cf3bf0SChris Wilson 	time *= threshold * dev_priv->mem_freq;
102031685c25SDeepak S 
1021*43cf3bf0SChris Wilson 	/* Workload can be split between render + media, e.g. SwapBuffers
1022*43cf3bf0SChris Wilson 	 * being blitted in X after being rendered in mesa. To account for
1023*43cf3bf0SChris Wilson 	 * this we need to combine both engines into our activity counter.
1024*43cf3bf0SChris Wilson 	 */
1025*43cf3bf0SChris Wilson 	c0 = now->render_c0 - old->render_c0;
1026*43cf3bf0SChris Wilson 	c0 += now->media_c0 - old->media_c0;
1027*43cf3bf0SChris Wilson 	c0 *= 100 * VLV_CZ_CLOCK_TO_MILLI_SEC * 4 / 1000;
102831685c25SDeepak S 
1029*43cf3bf0SChris Wilson 	return c0 >= time;
103031685c25SDeepak S }
103131685c25SDeepak S 
1032*43cf3bf0SChris Wilson void gen6_rps_reset_ei(struct drm_i915_private *dev_priv)
1033*43cf3bf0SChris Wilson {
1034*43cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &dev_priv->rps.down_ei);
1035*43cf3bf0SChris Wilson 	dev_priv->rps.up_ei = dev_priv->rps.down_ei;
1036*43cf3bf0SChris Wilson 	dev_priv->rps.ei_interrupt_count = 0;
1037*43cf3bf0SChris Wilson }
1038*43cf3bf0SChris Wilson 
1039*43cf3bf0SChris Wilson static u32 vlv_wa_c0_ei(struct drm_i915_private *dev_priv, u32 pm_iir)
1040*43cf3bf0SChris Wilson {
1041*43cf3bf0SChris Wilson 	struct intel_rps_ei now;
1042*43cf3bf0SChris Wilson 	u32 events = 0;
1043*43cf3bf0SChris Wilson 
1044*43cf3bf0SChris Wilson 	if ((pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) == 0)
1045*43cf3bf0SChris Wilson 		return 0;
1046*43cf3bf0SChris Wilson 
1047*43cf3bf0SChris Wilson 	vlv_c0_read(dev_priv, &now);
1048*43cf3bf0SChris Wilson 	if (now.cz_clock == 0)
1049*43cf3bf0SChris Wilson 		return 0;
105031685c25SDeepak S 
105131685c25SDeepak S 	/*
105231685c25SDeepak S 	 * To down throttle, C0 residency should be less than down threshold
105331685c25SDeepak S 	 * for continous EI intervals. So calculate down EI counters
105431685c25SDeepak S 	 * once in VLV_INT_COUNT_FOR_DOWN_EI
105531685c25SDeepak S 	 */
1056*43cf3bf0SChris Wilson 	if (++dev_priv->rps.ei_interrupt_count >= VLV_INT_COUNT_FOR_DOWN_EI) {
1057*43cf3bf0SChris Wilson 		pm_iir |= GEN6_PM_RP_DOWN_EI_EXPIRED;
105831685c25SDeepak S 		dev_priv->rps.ei_interrupt_count = 0;
105931685c25SDeepak S 	}
106031685c25SDeepak S 
1061*43cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_DOWN_EI_EXPIRED) {
1062*43cf3bf0SChris Wilson 		if (!vlv_c0_above(dev_priv,
1063*43cf3bf0SChris Wilson 				  &dev_priv->rps.down_ei, &now,
1064*43cf3bf0SChris Wilson 				  VLV_RP_DOWN_EI_THRESHOLD))
1065*43cf3bf0SChris Wilson 			events |= GEN6_PM_RP_DOWN_THRESHOLD;
1066*43cf3bf0SChris Wilson 		dev_priv->rps.down_ei = now;
106731685c25SDeepak S 	}
106831685c25SDeepak S 
1069*43cf3bf0SChris Wilson 	if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
1070*43cf3bf0SChris Wilson 		if (vlv_c0_above(dev_priv,
1071*43cf3bf0SChris Wilson 				 &dev_priv->rps.up_ei, &now,
1072*43cf3bf0SChris Wilson 				 VLV_RP_UP_EI_THRESHOLD))
1073*43cf3bf0SChris Wilson 			events |= GEN6_PM_RP_UP_THRESHOLD;
1074*43cf3bf0SChris Wilson 		dev_priv->rps.up_ei = now;
1075*43cf3bf0SChris Wilson 	}
1076*43cf3bf0SChris Wilson 
1077*43cf3bf0SChris Wilson 	return events;
107831685c25SDeepak S }
107931685c25SDeepak S 
10804912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
10813b8d8d91SJesse Barnes {
10822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
10832d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1084edbfdb45SPaulo Zanoni 	u32 pm_iir;
1085dd75fdc8SChris Wilson 	int new_delay, adj;
10863b8d8d91SJesse Barnes 
108759cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1088d4d70aa5SImre Deak 	/* Speed up work cancelation during disabling rps interrupts. */
1089d4d70aa5SImre Deak 	if (!dev_priv->rps.interrupts_enabled) {
1090d4d70aa5SImre Deak 		spin_unlock_irq(&dev_priv->irq_lock);
1091d4d70aa5SImre Deak 		return;
1092d4d70aa5SImre Deak 	}
1093c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1094c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
1095a72fbc3aSImre Deak 	/* Make sure not to corrupt PMIMR state used by ringbuffer on GEN6 */
1096480c8033SDaniel Vetter 	gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
109759cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
10984912d041SBen Widawsky 
109960611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1100a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
110160611c13SPaulo Zanoni 
1102a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
11033b8d8d91SJesse Barnes 		return;
11043b8d8d91SJesse Barnes 
11054fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11067b9e0ae6SChris Wilson 
1107*43cf3bf0SChris Wilson 	pm_iir |= vlv_wa_c0_ei(dev_priv, pm_iir);
1108*43cf3bf0SChris Wilson 
1109dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11107425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1111dd75fdc8SChris Wilson 		if (adj > 0)
1112dd75fdc8SChris Wilson 			adj *= 2;
111313a5660cSDeepak S 		else {
111413a5660cSDeepak S 			/* CHV needs even encode values */
111513a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
111613a5660cSDeepak S 		}
1117b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11187425034aSVille Syrjälä 
11197425034aSVille Syrjälä 		/*
11207425034aSVille Syrjälä 		 * For better performance, jump directly
11217425034aSVille Syrjälä 		 * to RPe if we're below it.
11227425034aSVille Syrjälä 		 */
1123b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1124b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1125dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1126b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1127b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1128dd75fdc8SChris Wilson 		else
1129b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1130dd75fdc8SChris Wilson 		adj = 0;
1131dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1132dd75fdc8SChris Wilson 		if (adj < 0)
1133dd75fdc8SChris Wilson 			adj *= 2;
113413a5660cSDeepak S 		else {
113513a5660cSDeepak S 			/* CHV needs even encode values */
113613a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
113713a5660cSDeepak S 		}
1138b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1139dd75fdc8SChris Wilson 	} else { /* unknown event */
1140b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1141dd75fdc8SChris Wilson 	}
11423b8d8d91SJesse Barnes 
114379249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
114479249636SBen Widawsky 	 * interrupt
114579249636SBen Widawsky 	 */
11461272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1147b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1148b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
114927544369SDeepak S 
1150b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1151dd75fdc8SChris Wilson 
1152ffe02b40SVille Syrjälä 	intel_set_rps(dev_priv->dev, new_delay);
11533b8d8d91SJesse Barnes 
11544fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11553b8d8d91SJesse Barnes }
11563b8d8d91SJesse Barnes 
1157e3689190SBen Widawsky 
1158e3689190SBen Widawsky /**
1159e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1160e3689190SBen Widawsky  * occurred.
1161e3689190SBen Widawsky  * @work: workqueue struct
1162e3689190SBen Widawsky  *
1163e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1164e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1165e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1166e3689190SBen Widawsky  */
1167e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1168e3689190SBen Widawsky {
11692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11702d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1171e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
117235a85ac6SBen Widawsky 	char *parity_event[6];
1173e3689190SBen Widawsky 	uint32_t misccpctl;
117435a85ac6SBen Widawsky 	uint8_t slice = 0;
1175e3689190SBen Widawsky 
1176e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1177e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1178e3689190SBen Widawsky 	 * any time we access those registers.
1179e3689190SBen Widawsky 	 */
1180e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1181e3689190SBen Widawsky 
118235a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
118335a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
118435a85ac6SBen Widawsky 		goto out;
118535a85ac6SBen Widawsky 
1186e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1187e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1188e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1189e3689190SBen Widawsky 
119035a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
119135a85ac6SBen Widawsky 		u32 reg;
119235a85ac6SBen Widawsky 
119335a85ac6SBen Widawsky 		slice--;
119435a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
119535a85ac6SBen Widawsky 			break;
119635a85ac6SBen Widawsky 
119735a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
119835a85ac6SBen Widawsky 
119935a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
120035a85ac6SBen Widawsky 
120135a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1202e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1203e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1204e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1205e3689190SBen Widawsky 
120635a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
120735a85ac6SBen Widawsky 		POSTING_READ(reg);
1208e3689190SBen Widawsky 
1209cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1210e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1211e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1212e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
121335a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
121435a85ac6SBen Widawsky 		parity_event[5] = NULL;
1215e3689190SBen Widawsky 
12165bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1217e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1218e3689190SBen Widawsky 
121935a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
122035a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1221e3689190SBen Widawsky 
122235a85ac6SBen Widawsky 		kfree(parity_event[4]);
1223e3689190SBen Widawsky 		kfree(parity_event[3]);
1224e3689190SBen Widawsky 		kfree(parity_event[2]);
1225e3689190SBen Widawsky 		kfree(parity_event[1]);
1226e3689190SBen Widawsky 	}
1227e3689190SBen Widawsky 
122835a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
122935a85ac6SBen Widawsky 
123035a85ac6SBen Widawsky out:
123135a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12324cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1233480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12344cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
123535a85ac6SBen Widawsky 
123635a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
123735a85ac6SBen Widawsky }
123835a85ac6SBen Widawsky 
123935a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1240e3689190SBen Widawsky {
12412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1242e3689190SBen Widawsky 
1243040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1244e3689190SBen Widawsky 		return;
1245e3689190SBen Widawsky 
1246d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1247480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1248d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1249e3689190SBen Widawsky 
125035a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
125135a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
125235a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
125335a85ac6SBen Widawsky 
125435a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
125535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
125635a85ac6SBen Widawsky 
1257a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1258e3689190SBen Widawsky }
1259e3689190SBen Widawsky 
1260f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1261f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1262f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1263f1af8fc1SPaulo Zanoni {
1264f1af8fc1SPaulo Zanoni 	if (gt_iir &
1265f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1266f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1267f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1268f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1269f1af8fc1SPaulo Zanoni }
1270f1af8fc1SPaulo Zanoni 
1271e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1272e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1273e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1274e7b4c6b1SDaniel Vetter {
1275e7b4c6b1SDaniel Vetter 
1276cc609d5dSBen Widawsky 	if (gt_iir &
1277cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1278e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1279cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1280e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1281cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1282e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1283e7b4c6b1SDaniel Vetter 
1284cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1285cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1286aaecdf61SDaniel Vetter 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT))
1287aaecdf61SDaniel Vetter 		DRM_DEBUG("Command parser error, gt_iir 0x%08x\n", gt_iir);
1288e3689190SBen Widawsky 
128935a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
129035a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1291e7b4c6b1SDaniel Vetter }
1292e7b4c6b1SDaniel Vetter 
1293abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1294abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1295abd58f01SBen Widawsky 				       u32 master_ctl)
1296abd58f01SBen Widawsky {
1297e981e7b1SThomas Daniel 	struct intel_engine_cs *ring;
1298abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1299abd58f01SBen Widawsky 	uint32_t tmp = 0;
1300abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1301abd58f01SBen Widawsky 
1302abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1303abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1304abd58f01SBen Widawsky 		if (tmp) {
130538cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1306abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1307e981e7b1SThomas Daniel 
1308abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1309e981e7b1SThomas Daniel 			ring = &dev_priv->ring[RCS];
1310abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1311e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1312e981e7b1SThomas Daniel 			if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
13133f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1314e981e7b1SThomas Daniel 
1315e981e7b1SThomas Daniel 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1316e981e7b1SThomas Daniel 			ring = &dev_priv->ring[BCS];
1317abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1318e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1319e981e7b1SThomas Daniel 			if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
13203f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1321abd58f01SBen Widawsky 		} else
1322abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1323abd58f01SBen Widawsky 	}
1324abd58f01SBen Widawsky 
132585f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1326abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1327abd58f01SBen Widawsky 		if (tmp) {
132838cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1329abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1330e981e7b1SThomas Daniel 
1331abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1332e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS];
1333abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1334e981e7b1SThomas Daniel 				notify_ring(dev, ring);
133573d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
13363f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1337e981e7b1SThomas Daniel 
133885f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
1339e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS2];
134085f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
1341e981e7b1SThomas Daniel 				notify_ring(dev, ring);
134273d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
13433f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1344abd58f01SBen Widawsky 		} else
1345abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1346abd58f01SBen Widawsky 	}
1347abd58f01SBen Widawsky 
13480961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
13490961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
13500961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
13510961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
13520961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
135338cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
1354c9a9a268SImre Deak 			gen6_rps_irq_handler(dev_priv, tmp);
13550961021aSBen Widawsky 		} else
13560961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
13570961021aSBen Widawsky 	}
13580961021aSBen Widawsky 
1359abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1360abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1361abd58f01SBen Widawsky 		if (tmp) {
136238cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1363abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1364e981e7b1SThomas Daniel 
1365abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1366e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VECS];
1367abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1368e981e7b1SThomas Daniel 				notify_ring(dev, ring);
136973d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
13703f7531c3SDaniel Vetter 				intel_lrc_irq_handler(ring);
1371abd58f01SBen Widawsky 		} else
1372abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1373abd58f01SBen Widawsky 	}
1374abd58f01SBen Widawsky 
1375abd58f01SBen Widawsky 	return ret;
1376abd58f01SBen Widawsky }
1377abd58f01SBen Widawsky 
1378b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1379b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1380b543fb04SEgbert Eich 
138107c338ceSJani Nikula static int pch_port_to_hotplug_shift(enum port port)
138213cf5504SDave Airlie {
138313cf5504SDave Airlie 	switch (port) {
138413cf5504SDave Airlie 	case PORT_A:
138513cf5504SDave Airlie 	case PORT_E:
138613cf5504SDave Airlie 	default:
138713cf5504SDave Airlie 		return -1;
138813cf5504SDave Airlie 	case PORT_B:
138913cf5504SDave Airlie 		return 0;
139013cf5504SDave Airlie 	case PORT_C:
139113cf5504SDave Airlie 		return 8;
139213cf5504SDave Airlie 	case PORT_D:
139313cf5504SDave Airlie 		return 16;
139413cf5504SDave Airlie 	}
139513cf5504SDave Airlie }
139613cf5504SDave Airlie 
139707c338ceSJani Nikula static int i915_port_to_hotplug_shift(enum port port)
139813cf5504SDave Airlie {
139913cf5504SDave Airlie 	switch (port) {
140013cf5504SDave Airlie 	case PORT_A:
140113cf5504SDave Airlie 	case PORT_E:
140213cf5504SDave Airlie 	default:
140313cf5504SDave Airlie 		return -1;
140413cf5504SDave Airlie 	case PORT_B:
140513cf5504SDave Airlie 		return 17;
140613cf5504SDave Airlie 	case PORT_C:
140713cf5504SDave Airlie 		return 19;
140813cf5504SDave Airlie 	case PORT_D:
140913cf5504SDave Airlie 		return 21;
141013cf5504SDave Airlie 	}
141113cf5504SDave Airlie }
141213cf5504SDave Airlie 
141313cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
141413cf5504SDave Airlie {
141513cf5504SDave Airlie 	switch (pin) {
141613cf5504SDave Airlie 	case HPD_PORT_B:
141713cf5504SDave Airlie 		return PORT_B;
141813cf5504SDave Airlie 	case HPD_PORT_C:
141913cf5504SDave Airlie 		return PORT_C;
142013cf5504SDave Airlie 	case HPD_PORT_D:
142113cf5504SDave Airlie 		return PORT_D;
142213cf5504SDave Airlie 	default:
142313cf5504SDave Airlie 		return PORT_A; /* no hpd */
142413cf5504SDave Airlie 	}
142513cf5504SDave Airlie }
142613cf5504SDave Airlie 
142710a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1428b543fb04SEgbert Eich 					 u32 hotplug_trigger,
142913cf5504SDave Airlie 					 u32 dig_hotplug_reg,
14307c7e10dbSVille Syrjälä 					 const u32 hpd[HPD_NUM_PINS])
1431b543fb04SEgbert Eich {
14322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1433b543fb04SEgbert Eich 	int i;
143413cf5504SDave Airlie 	enum port port;
143510a504deSDaniel Vetter 	bool storm_detected = false;
143613cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
143713cf5504SDave Airlie 	u32 dig_shift;
143813cf5504SDave Airlie 	u32 dig_port_mask = 0;
1439b543fb04SEgbert Eich 
144091d131d2SDaniel Vetter 	if (!hotplug_trigger)
144191d131d2SDaniel Vetter 		return;
144291d131d2SDaniel Vetter 
144313cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
144413cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1445cc9bd499SImre Deak 
1446b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1447b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
144813cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
144913cf5504SDave Airlie 			continue;
1450821450c6SEgbert Eich 
145113cf5504SDave Airlie 		port = get_port_from_pin(i);
145213cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
145313cf5504SDave Airlie 			bool long_hpd;
145413cf5504SDave Airlie 
145507c338ceSJani Nikula 			if (HAS_PCH_SPLIT(dev)) {
145607c338ceSJani Nikula 				dig_shift = pch_port_to_hotplug_shift(port);
145713cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
145807c338ceSJani Nikula 			} else {
145907c338ceSJani Nikula 				dig_shift = i915_port_to_hotplug_shift(port);
146007c338ceSJani Nikula 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
146113cf5504SDave Airlie 			}
146213cf5504SDave Airlie 
146326fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
146426fbb774SVille Syrjälä 					 port_name(port),
146526fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
146613cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
146713cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
146813cf5504SDave Airlie 			if (long_hpd) {
146913cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
147013cf5504SDave Airlie 				dig_port_mask |= hpd[i];
147113cf5504SDave Airlie 			} else {
147213cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
147313cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
147413cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
147513cf5504SDave Airlie 			}
147613cf5504SDave Airlie 			queue_dig = true;
147713cf5504SDave Airlie 		}
147813cf5504SDave Airlie 	}
147913cf5504SDave Airlie 
148013cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
14813ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
14823ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
14833ff04a16SDaniel Vetter 			/*
14843ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
14853ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
14863ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
14873ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
14883ff04a16SDaniel Vetter 			 */
14893ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1490cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1491cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1492b8f102e8SEgbert Eich 
14933ff04a16SDaniel Vetter 			continue;
14943ff04a16SDaniel Vetter 		}
14953ff04a16SDaniel Vetter 
1496b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1497b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1498b543fb04SEgbert Eich 			continue;
1499b543fb04SEgbert Eich 
150013cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1501bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
150213cf5504SDave Airlie 			queue_hp = true;
150313cf5504SDave Airlie 		}
150413cf5504SDave Airlie 
1505b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1506b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1507b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1508b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1509b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1510b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1511b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1512b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1513142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1514b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
151510a504deSDaniel Vetter 			storm_detected = true;
1516b543fb04SEgbert Eich 		} else {
1517b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1518b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1519b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1520b543fb04SEgbert Eich 		}
1521b543fb04SEgbert Eich 	}
1522b543fb04SEgbert Eich 
152310a504deSDaniel Vetter 	if (storm_detected)
152410a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1525b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
15265876fa0dSDaniel Vetter 
1527645416f5SDaniel Vetter 	/*
1528645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1529645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1530645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1531645416f5SDaniel Vetter 	 * deadlock.
1532645416f5SDaniel Vetter 	 */
153313cf5504SDave Airlie 	if (queue_dig)
15340e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
153513cf5504SDave Airlie 	if (queue_hp)
1536645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1537b543fb04SEgbert Eich }
1538b543fb04SEgbert Eich 
1539515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1540515ac2bbSDaniel Vetter {
15412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
154228c70f16SDaniel Vetter 
154328c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1544515ac2bbSDaniel Vetter }
1545515ac2bbSDaniel Vetter 
1546ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1547ce99c256SDaniel Vetter {
15482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
15499ee32feaSDaniel Vetter 
15509ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1551ce99c256SDaniel Vetter }
1552ce99c256SDaniel Vetter 
15538bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1554277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1555eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1556eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
15578bc5e955SDaniel Vetter 					 uint32_t crc4)
15588bf1e9f1SShuang He {
15598bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
15608bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
15618bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1562ac2300d4SDamien Lespiau 	int head, tail;
1563b2c88f5bSDamien Lespiau 
1564d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1565d538bbdfSDamien Lespiau 
15660c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1567d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
156834273620SDaniel Vetter 		DRM_DEBUG_KMS("spurious interrupt\n");
15690c912c79SDamien Lespiau 		return;
15700c912c79SDamien Lespiau 	}
15710c912c79SDamien Lespiau 
1572d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1573d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1574b2c88f5bSDamien Lespiau 
1575b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1576d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1577b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1578b2c88f5bSDamien Lespiau 		return;
1579b2c88f5bSDamien Lespiau 	}
1580b2c88f5bSDamien Lespiau 
1581b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
15828bf1e9f1SShuang He 
15838bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1584eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1585eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1586eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1587eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1588eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1589b2c88f5bSDamien Lespiau 
1590b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1591d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1592d538bbdfSDamien Lespiau 
1593d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
159407144428SDamien Lespiau 
159507144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
15968bf1e9f1SShuang He }
1597277de95eSDaniel Vetter #else
1598277de95eSDaniel Vetter static inline void
1599277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1600277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1601277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1602277de95eSDaniel Vetter 			     uint32_t crc4) {}
1603277de95eSDaniel Vetter #endif
1604eba94eb9SDaniel Vetter 
1605277de95eSDaniel Vetter 
1606277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16075a69b89fSDaniel Vetter {
16085a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16095a69b89fSDaniel Vetter 
1610277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16115a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16125a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16135a69b89fSDaniel Vetter }
16145a69b89fSDaniel Vetter 
1615277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1616eba94eb9SDaniel Vetter {
1617eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1618eba94eb9SDaniel Vetter 
1619277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1620eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1621eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1622eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1623eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16248bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1625eba94eb9SDaniel Vetter }
16265b3a856bSDaniel Vetter 
1627277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16285b3a856bSDaniel Vetter {
16295b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16300b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16310b5c5ed0SDaniel Vetter 
16320b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16330b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16340b5c5ed0SDaniel Vetter 	else
16350b5c5ed0SDaniel Vetter 		res1 = 0;
16360b5c5ed0SDaniel Vetter 
16370b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16380b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16390b5c5ed0SDaniel Vetter 	else
16400b5c5ed0SDaniel Vetter 		res2 = 0;
16415b3a856bSDaniel Vetter 
1642277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16430b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16440b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16450b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16460b5c5ed0SDaniel Vetter 				     res1, res2);
16475b3a856bSDaniel Vetter }
16488bf1e9f1SShuang He 
16491403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
16501403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
16511403c0d4SPaulo Zanoni  * the work queue. */
16521403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1653baf02a1fSBen Widawsky {
1654a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
165559cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1656480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
1657d4d70aa5SImre Deak 		if (dev_priv->rps.interrupts_enabled) {
1658d4d70aa5SImre Deak 			dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
16592adbee62SDaniel Vetter 			queue_work(dev_priv->wq, &dev_priv->rps.work);
166041a05a3aSDaniel Vetter 		}
1661d4d70aa5SImre Deak 		spin_unlock(&dev_priv->irq_lock);
1662d4d70aa5SImre Deak 	}
1663baf02a1fSBen Widawsky 
1664c9a9a268SImre Deak 	if (INTEL_INFO(dev_priv)->gen >= 8)
1665c9a9a268SImre Deak 		return;
1666c9a9a268SImre Deak 
16671403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
166812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
166912638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
167012638c57SBen Widawsky 
1671aaecdf61SDaniel Vetter 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT)
1672aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, pm_iir 0x%08x\n", pm_iir);
167312638c57SBen Widawsky 	}
16741403c0d4SPaulo Zanoni }
1675baf02a1fSBen Widawsky 
16768d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
16778d7849dbSVille Syrjälä {
16788d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
16798d7849dbSVille Syrjälä 		return false;
16808d7849dbSVille Syrjälä 
16818d7849dbSVille Syrjälä 	return true;
16828d7849dbSVille Syrjälä }
16838d7849dbSVille Syrjälä 
1684c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
16857e231dbeSJesse Barnes {
1686c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
168791d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
16887e231dbeSJesse Barnes 	int pipe;
16897e231dbeSJesse Barnes 
169058ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1691055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
169291d181ddSImre Deak 		int reg;
1693bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
169491d181ddSImre Deak 
1695bbb5eebfSDaniel Vetter 		/*
1696bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1697bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1698bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1699bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1700bbb5eebfSDaniel Vetter 		 * handle.
1701bbb5eebfSDaniel Vetter 		 */
17020f239f4cSDaniel Vetter 
17030f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17040f239f4cSDaniel Vetter 		mask = PIPE_FIFO_UNDERRUN_STATUS;
1705bbb5eebfSDaniel Vetter 
1706bbb5eebfSDaniel Vetter 		switch (pipe) {
1707bbb5eebfSDaniel Vetter 		case PIPE_A:
1708bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1709bbb5eebfSDaniel Vetter 			break;
1710bbb5eebfSDaniel Vetter 		case PIPE_B:
1711bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1712bbb5eebfSDaniel Vetter 			break;
17133278f67fSVille Syrjälä 		case PIPE_C:
17143278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17153278f67fSVille Syrjälä 			break;
1716bbb5eebfSDaniel Vetter 		}
1717bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1718bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1719bbb5eebfSDaniel Vetter 
1720bbb5eebfSDaniel Vetter 		if (!mask)
172191d181ddSImre Deak 			continue;
172291d181ddSImre Deak 
172391d181ddSImre Deak 		reg = PIPESTAT(pipe);
1724bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1725bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17267e231dbeSJesse Barnes 
17277e231dbeSJesse Barnes 		/*
17287e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17297e231dbeSJesse Barnes 		 */
173091d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
173191d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17327e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17337e231dbeSJesse Barnes 	}
173458ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17357e231dbeSJesse Barnes 
1736055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1737d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1738d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1739d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
174031acc7f5SJesse Barnes 
1741579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
174231acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
174331acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
174431acc7f5SJesse Barnes 		}
17454356d586SDaniel Vetter 
17464356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1747277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17482d9d2b0bSVille Syrjälä 
17491f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
17501f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
175131acc7f5SJesse Barnes 	}
175231acc7f5SJesse Barnes 
1753c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1754c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1755c1874ed7SImre Deak }
1756c1874ed7SImre Deak 
175716c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
175816c6c56bSVille Syrjälä {
175916c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
176016c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
176116c6c56bSVille Syrjälä 
17623ff60f89SOscar Mateo 	if (hotplug_status) {
17633ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
17643ff60f89SOscar Mateo 		/*
17653ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
17663ff60f89SOscar Mateo 		 * may miss hotplug events.
17673ff60f89SOscar Mateo 		 */
17683ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
17693ff60f89SOscar Mateo 
177016c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
177116c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
177216c6c56bSVille Syrjälä 
177313cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
177416c6c56bSVille Syrjälä 		} else {
177516c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
177616c6c56bSVille Syrjälä 
177713cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
177816c6c56bSVille Syrjälä 		}
177916c6c56bSVille Syrjälä 
178016c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
178116c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
178216c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
17833ff60f89SOscar Mateo 	}
178416c6c56bSVille Syrjälä }
178516c6c56bSVille Syrjälä 
1786c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1787c1874ed7SImre Deak {
178845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
17892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1790c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1791c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1792c1874ed7SImre Deak 
17932dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
17942dd2a883SImre Deak 		return IRQ_NONE;
17952dd2a883SImre Deak 
1796c1874ed7SImre Deak 	while (true) {
17973ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
17983ff60f89SOscar Mateo 
1799c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
18003ff60f89SOscar Mateo 		if (gt_iir)
18013ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
18023ff60f89SOscar Mateo 
1803c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18043ff60f89SOscar Mateo 		if (pm_iir)
18053ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
18063ff60f89SOscar Mateo 
18073ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
18083ff60f89SOscar Mateo 		if (iir) {
18093ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
18103ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
18113ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
18123ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
18133ff60f89SOscar Mateo 		}
1814c1874ed7SImre Deak 
1815c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1816c1874ed7SImre Deak 			goto out;
1817c1874ed7SImre Deak 
1818c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1819c1874ed7SImre Deak 
18203ff60f89SOscar Mateo 		if (gt_iir)
1821c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
182260611c13SPaulo Zanoni 		if (pm_iir)
1823d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18243ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18253ff60f89SOscar Mateo 		 * signalled in iir */
18263ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18277e231dbeSJesse Barnes 	}
18287e231dbeSJesse Barnes 
18297e231dbeSJesse Barnes out:
18307e231dbeSJesse Barnes 	return ret;
18317e231dbeSJesse Barnes }
18327e231dbeSJesse Barnes 
183343f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
183443f328d7SVille Syrjälä {
183545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
183643f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
183743f328d7SVille Syrjälä 	u32 master_ctl, iir;
183843f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
183943f328d7SVille Syrjälä 
18402dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
18412dd2a883SImre Deak 		return IRQ_NONE;
18422dd2a883SImre Deak 
18438e5fd599SVille Syrjälä 	for (;;) {
18448e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18453278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18463278f67fSVille Syrjälä 
18473278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18488e5fd599SVille Syrjälä 			break;
184943f328d7SVille Syrjälä 
185027b6c122SOscar Mateo 		ret = IRQ_HANDLED;
185127b6c122SOscar Mateo 
185243f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
185343f328d7SVille Syrjälä 
185427b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
185527b6c122SOscar Mateo 
185627b6c122SOscar Mateo 		if (iir) {
185727b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
185827b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
185927b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
186027b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
186127b6c122SOscar Mateo 		}
186227b6c122SOscar Mateo 
18633278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
186443f328d7SVille Syrjälä 
186527b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
186627b6c122SOscar Mateo 		 * signalled in iir */
18673278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
186843f328d7SVille Syrjälä 
186943f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
187043f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
18718e5fd599SVille Syrjälä 	}
18723278f67fSVille Syrjälä 
187343f328d7SVille Syrjälä 	return ret;
187443f328d7SVille Syrjälä }
187543f328d7SVille Syrjälä 
187623e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1877776ad806SJesse Barnes {
18782d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
18799db4a9c7SJesse Barnes 	int pipe;
1880b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
188113cf5504SDave Airlie 	u32 dig_hotplug_reg;
1882776ad806SJesse Barnes 
188313cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
188413cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
188513cf5504SDave Airlie 
188613cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
188791d131d2SDaniel Vetter 
1888cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1889cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1890776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1891cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1892cfc33bf7SVille Syrjälä 				 port_name(port));
1893cfc33bf7SVille Syrjälä 	}
1894776ad806SJesse Barnes 
1895ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1896ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1897ce99c256SDaniel Vetter 
1898776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1899515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1900776ad806SJesse Barnes 
1901776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1902776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1903776ad806SJesse Barnes 
1904776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1905776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1906776ad806SJesse Barnes 
1907776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1908776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1909776ad806SJesse Barnes 
19109db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1911055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19129db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19139db4a9c7SJesse Barnes 					 pipe_name(pipe),
19149db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1915776ad806SJesse Barnes 
1916776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1917776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1918776ad806SJesse Barnes 
1919776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1920776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1921776ad806SJesse Barnes 
1922776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19231f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19248664281bSPaulo Zanoni 
19258664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19261f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19278664281bSPaulo Zanoni }
19288664281bSPaulo Zanoni 
19298664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19308664281bSPaulo Zanoni {
19318664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19328664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19335a69b89fSDaniel Vetter 	enum pipe pipe;
19348664281bSPaulo Zanoni 
1935de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1936de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1937de032bf4SPaulo Zanoni 
1938055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19391f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19401f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19418664281bSPaulo Zanoni 
19425a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19435a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1944277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19455a69b89fSDaniel Vetter 			else
1946277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19475a69b89fSDaniel Vetter 		}
19485a69b89fSDaniel Vetter 	}
19498bf1e9f1SShuang He 
19508664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19518664281bSPaulo Zanoni }
19528664281bSPaulo Zanoni 
19538664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19548664281bSPaulo Zanoni {
19558664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19568664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19578664281bSPaulo Zanoni 
1958de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1959de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1960de032bf4SPaulo Zanoni 
19618664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
19621f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19638664281bSPaulo Zanoni 
19648664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
19651f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19668664281bSPaulo Zanoni 
19678664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
19681f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
19698664281bSPaulo Zanoni 
19708664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
1971776ad806SJesse Barnes }
1972776ad806SJesse Barnes 
197323e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
197423e81d69SAdam Jackson {
19752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
197623e81d69SAdam Jackson 	int pipe;
1977b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
197813cf5504SDave Airlie 	u32 dig_hotplug_reg;
197923e81d69SAdam Jackson 
198013cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
198113cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
198213cf5504SDave Airlie 
198313cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
198491d131d2SDaniel Vetter 
1985cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
1986cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
198723e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
1988cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
1989cfc33bf7SVille Syrjälä 				 port_name(port));
1990cfc33bf7SVille Syrjälä 	}
199123e81d69SAdam Jackson 
199223e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
1993ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
199423e81d69SAdam Jackson 
199523e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
1996515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
199723e81d69SAdam Jackson 
199823e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
199923e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
200023e81d69SAdam Jackson 
200123e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
200223e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
200323e81d69SAdam Jackson 
200423e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2005055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
200623e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
200723e81d69SAdam Jackson 					 pipe_name(pipe),
200823e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20098664281bSPaulo Zanoni 
20108664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20118664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
201223e81d69SAdam Jackson }
201323e81d69SAdam Jackson 
2014c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2015c008bc6eSPaulo Zanoni {
2016c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
201740da17c2SDaniel Vetter 	enum pipe pipe;
2018c008bc6eSPaulo Zanoni 
2019c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2020c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2021c008bc6eSPaulo Zanoni 
2022c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2023c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2024c008bc6eSPaulo Zanoni 
2025c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2026c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2027c008bc6eSPaulo Zanoni 
2028055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2029d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2030d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2031d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2032c008bc6eSPaulo Zanoni 
203340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20341f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2035c008bc6eSPaulo Zanoni 
203640da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
203740da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20385b3a856bSDaniel Vetter 
203940da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
204040da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
204140da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
204240da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2043c008bc6eSPaulo Zanoni 		}
2044c008bc6eSPaulo Zanoni 	}
2045c008bc6eSPaulo Zanoni 
2046c008bc6eSPaulo Zanoni 	/* check event from PCH */
2047c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2048c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2049c008bc6eSPaulo Zanoni 
2050c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2051c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2052c008bc6eSPaulo Zanoni 		else
2053c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2054c008bc6eSPaulo Zanoni 
2055c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2056c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2057c008bc6eSPaulo Zanoni 	}
2058c008bc6eSPaulo Zanoni 
2059c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2060c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2061c008bc6eSPaulo Zanoni }
2062c008bc6eSPaulo Zanoni 
20639719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
20649719fb98SPaulo Zanoni {
20659719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
206607d27e20SDamien Lespiau 	enum pipe pipe;
20679719fb98SPaulo Zanoni 
20689719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
20699719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
20709719fb98SPaulo Zanoni 
20719719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
20729719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
20739719fb98SPaulo Zanoni 
20749719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
20759719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
20769719fb98SPaulo Zanoni 
2077055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2078d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2079d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2080d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
208140da17c2SDaniel Vetter 
208240da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
208307d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
208407d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
208507d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
20869719fb98SPaulo Zanoni 		}
20879719fb98SPaulo Zanoni 	}
20889719fb98SPaulo Zanoni 
20899719fb98SPaulo Zanoni 	/* check event from PCH */
20909719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
20919719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
20929719fb98SPaulo Zanoni 
20939719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
20949719fb98SPaulo Zanoni 
20959719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
20969719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
20979719fb98SPaulo Zanoni 	}
20989719fb98SPaulo Zanoni }
20999719fb98SPaulo Zanoni 
210072c90f62SOscar Mateo /*
210172c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
210272c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
210372c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
210472c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
210572c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
210672c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
210772c90f62SOscar Mateo  */
2108f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2109b1f14ad0SJesse Barnes {
211045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21112d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2112f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21130e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2114b1f14ad0SJesse Barnes 
21152dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21162dd2a883SImre Deak 		return IRQ_NONE;
21172dd2a883SImre Deak 
21188664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21198664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2120907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21218664281bSPaulo Zanoni 
2122b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2123b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2124b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
212523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21260e43406bSChris Wilson 
212744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
212844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
212944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
213044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
213144498aeaSPaulo Zanoni 	 * due to its back queue). */
2132ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
213344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
213444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
213544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2136ab5c608bSBen Widawsky 	}
213744498aeaSPaulo Zanoni 
213872c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
213972c90f62SOscar Mateo 
21400e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21410e43406bSChris Wilson 	if (gt_iir) {
214272c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
214372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2144d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21450e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2146d8fc8a47SPaulo Zanoni 		else
2147d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21480e43406bSChris Wilson 	}
2149b1f14ad0SJesse Barnes 
2150b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21510e43406bSChris Wilson 	if (de_iir) {
215272c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
215372c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2154f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21559719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2156f1af8fc1SPaulo Zanoni 		else
2157f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21580e43406bSChris Wilson 	}
21590e43406bSChris Wilson 
2160f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2161f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21620e43406bSChris Wilson 		if (pm_iir) {
2163b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21640e43406bSChris Wilson 			ret = IRQ_HANDLED;
216572c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
21660e43406bSChris Wilson 		}
2167f1af8fc1SPaulo Zanoni 	}
2168b1f14ad0SJesse Barnes 
2169b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2170b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2171ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
217244498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
217344498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2174ab5c608bSBen Widawsky 	}
2175b1f14ad0SJesse Barnes 
2176b1f14ad0SJesse Barnes 	return ret;
2177b1f14ad0SJesse Barnes }
2178b1f14ad0SJesse Barnes 
2179abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2180abd58f01SBen Widawsky {
2181abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2182abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2183abd58f01SBen Widawsky 	u32 master_ctl;
2184abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2185abd58f01SBen Widawsky 	uint32_t tmp = 0;
2186c42664ccSDaniel Vetter 	enum pipe pipe;
218788e04703SJesse Barnes 	u32 aux_mask = GEN8_AUX_CHANNEL_A;
218888e04703SJesse Barnes 
21892dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
21902dd2a883SImre Deak 		return IRQ_NONE;
21912dd2a883SImre Deak 
219288e04703SJesse Barnes 	if (IS_GEN9(dev))
219388e04703SJesse Barnes 		aux_mask |=  GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
219488e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
2195abd58f01SBen Widawsky 
2196abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2197abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2198abd58f01SBen Widawsky 	if (!master_ctl)
2199abd58f01SBen Widawsky 		return IRQ_NONE;
2200abd58f01SBen Widawsky 
2201abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2202abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2203abd58f01SBen Widawsky 
220438cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
220538cc46d7SOscar Mateo 
2206abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2207abd58f01SBen Widawsky 
2208abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2209abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2210abd58f01SBen Widawsky 		if (tmp) {
2211abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2212abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
221338cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
221438cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
221538cc46d7SOscar Mateo 			else
221638cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2217abd58f01SBen Widawsky 		}
221838cc46d7SOscar Mateo 		else
221938cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2220abd58f01SBen Widawsky 	}
2221abd58f01SBen Widawsky 
22226d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22236d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22246d766f02SDaniel Vetter 		if (tmp) {
22256d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22266d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
222788e04703SJesse Barnes 
222888e04703SJesse Barnes 			if (tmp & aux_mask)
222938cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
223038cc46d7SOscar Mateo 			else
223138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22326d766f02SDaniel Vetter 		}
223338cc46d7SOscar Mateo 		else
223438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22356d766f02SDaniel Vetter 	}
22366d766f02SDaniel Vetter 
2237055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2238770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2239abd58f01SBen Widawsky 
2240c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2241c42664ccSDaniel Vetter 			continue;
2242c42664ccSDaniel Vetter 
2243abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
224438cc46d7SOscar Mateo 		if (pipe_iir) {
224538cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
224638cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2247770de83dSDamien Lespiau 
2248d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2249d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2250d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2251abd58f01SBen Widawsky 
2252770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2253770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2254770de83dSDamien Lespiau 			else
2255770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2256770de83dSDamien Lespiau 
2257770de83dSDamien Lespiau 			if (flip_done) {
2258abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2259abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2260abd58f01SBen Widawsky 			}
2261abd58f01SBen Widawsky 
22620fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22630fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22640fbe7870SDaniel Vetter 
22651f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22661f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22671f7247c0SDaniel Vetter 								    pipe);
226838d83c96SDaniel Vetter 
2269770de83dSDamien Lespiau 
2270770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2271770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2272770de83dSDamien Lespiau 			else
2273770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2274770de83dSDamien Lespiau 
2275770de83dSDamien Lespiau 			if (fault_errors)
227630100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
227730100f2bSDaniel Vetter 					  pipe_name(pipe),
227830100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2279c42664ccSDaniel Vetter 		} else
2280abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2281abd58f01SBen Widawsky 	}
2282abd58f01SBen Widawsky 
228392d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
228492d03a80SDaniel Vetter 		/*
228592d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
228692d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
228792d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
228892d03a80SDaniel Vetter 		 */
228992d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
229092d03a80SDaniel Vetter 		if (pch_iir) {
229192d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
229292d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
229338cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
229438cc46d7SOscar Mateo 		} else
229538cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
229638cc46d7SOscar Mateo 
229792d03a80SDaniel Vetter 	}
229892d03a80SDaniel Vetter 
2299abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2300abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2301abd58f01SBen Widawsky 
2302abd58f01SBen Widawsky 	return ret;
2303abd58f01SBen Widawsky }
2304abd58f01SBen Widawsky 
230517e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
230617e1df07SDaniel Vetter 			       bool reset_completed)
230717e1df07SDaniel Vetter {
2308a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
230917e1df07SDaniel Vetter 	int i;
231017e1df07SDaniel Vetter 
231117e1df07SDaniel Vetter 	/*
231217e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
231317e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
231417e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
231517e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
231617e1df07SDaniel Vetter 	 */
231717e1df07SDaniel Vetter 
231817e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
231917e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
232017e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
232117e1df07SDaniel Vetter 
232217e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
232317e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
232417e1df07SDaniel Vetter 
232517e1df07SDaniel Vetter 	/*
232617e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
232717e1df07SDaniel Vetter 	 * reset state is cleared.
232817e1df07SDaniel Vetter 	 */
232917e1df07SDaniel Vetter 	if (reset_completed)
233017e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
233117e1df07SDaniel Vetter }
233217e1df07SDaniel Vetter 
23338a905236SJesse Barnes /**
2334b8d24a06SMika Kuoppala  * i915_reset_and_wakeup - do process context error handling work
23358a905236SJesse Barnes  *
23368a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23378a905236SJesse Barnes  * was detected.
23388a905236SJesse Barnes  */
2339b8d24a06SMika Kuoppala static void i915_reset_and_wakeup(struct drm_device *dev)
23408a905236SJesse Barnes {
2341b8d24a06SMika Kuoppala 	struct drm_i915_private *dev_priv = to_i915(dev);
2342b8d24a06SMika Kuoppala 	struct i915_gpu_error *error = &dev_priv->gpu_error;
2343cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2344cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2345cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
234617e1df07SDaniel Vetter 	int ret;
23478a905236SJesse Barnes 
23485bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23498a905236SJesse Barnes 
23507db0ba24SDaniel Vetter 	/*
23517db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23527db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23537db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23547db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23557db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23567db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23577db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23587db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23597db0ba24SDaniel Vetter 	 */
23607db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
236144d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23625bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23637db0ba24SDaniel Vetter 				   reset_event);
23641f83fee0SDaniel Vetter 
236517e1df07SDaniel Vetter 		/*
2366f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2367f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2368f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2369f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2370f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2371f454c694SImre Deak 		 */
2372f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
23737514747dSVille Syrjälä 
23747514747dSVille Syrjälä 		intel_prepare_reset(dev);
23757514747dSVille Syrjälä 
2376f454c694SImre Deak 		/*
237717e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
237817e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
237917e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
238017e1df07SDaniel Vetter 		 * deadlocks with the reset work.
238117e1df07SDaniel Vetter 		 */
2382f69061beSDaniel Vetter 		ret = i915_reset(dev);
2383f69061beSDaniel Vetter 
23847514747dSVille Syrjälä 		intel_finish_reset(dev);
238517e1df07SDaniel Vetter 
2386f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2387f454c694SImre Deak 
2388f69061beSDaniel Vetter 		if (ret == 0) {
2389f69061beSDaniel Vetter 			/*
2390f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2391f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2392f69061beSDaniel Vetter 			 * complete.
2393f69061beSDaniel Vetter 			 *
2394f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2395f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2396f69061beSDaniel Vetter 			 * updates before
2397f69061beSDaniel Vetter 			 * the counter increment.
2398f69061beSDaniel Vetter 			 */
23994e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2400f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2401f69061beSDaniel Vetter 
24025bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2403f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24041f83fee0SDaniel Vetter 		} else {
24052ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2406f316a42cSBen Gamari 		}
24071f83fee0SDaniel Vetter 
240817e1df07SDaniel Vetter 		/*
240917e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
241017e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
241117e1df07SDaniel Vetter 		 */
241217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2413f316a42cSBen Gamari 	}
24148a905236SJesse Barnes }
24158a905236SJesse Barnes 
241635aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2417c0e09200SDave Airlie {
24188a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2419bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
242063eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2421050ee91fSBen Widawsky 	int pipe, i;
242263eeaf38SJesse Barnes 
242335aed2e6SChris Wilson 	if (!eir)
242435aed2e6SChris Wilson 		return;
242563eeaf38SJesse Barnes 
2426a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24278a905236SJesse Barnes 
2428bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2429bd9854f9SBen Widawsky 
24308a905236SJesse Barnes 	if (IS_G4X(dev)) {
24318a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24328a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24338a905236SJesse Barnes 
2434a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2435a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2436050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2437050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2438a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2439a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24408a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24413143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24428a905236SJesse Barnes 		}
24438a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24448a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2445a70491ccSJoe Perches 			pr_err("page table error\n");
2446a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24478a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24483143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24498a905236SJesse Barnes 		}
24508a905236SJesse Barnes 	}
24518a905236SJesse Barnes 
2452a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
245363eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
245463eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2455a70491ccSJoe Perches 			pr_err("page table error\n");
2456a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
245763eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24583143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
245963eeaf38SJesse Barnes 		}
24608a905236SJesse Barnes 	}
24618a905236SJesse Barnes 
246263eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2463a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2464055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2465a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24669db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
246763eeaf38SJesse Barnes 		/* pipestat has already been acked */
246863eeaf38SJesse Barnes 	}
246963eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2470a70491ccSJoe Perches 		pr_err("instruction error\n");
2471a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2472050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2473050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2474a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
247563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
247663eeaf38SJesse Barnes 
2477a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2478a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2479a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
248063eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24813143a2bfSChris Wilson 			POSTING_READ(IPEIR);
248263eeaf38SJesse Barnes 		} else {
248363eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
248463eeaf38SJesse Barnes 
2485a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2486a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2487a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2488a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
248963eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24903143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
249163eeaf38SJesse Barnes 		}
249263eeaf38SJesse Barnes 	}
249363eeaf38SJesse Barnes 
249463eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
24953143a2bfSChris Wilson 	POSTING_READ(EIR);
249663eeaf38SJesse Barnes 	eir = I915_READ(EIR);
249763eeaf38SJesse Barnes 	if (eir) {
249863eeaf38SJesse Barnes 		/*
249963eeaf38SJesse Barnes 		 * some errors might have become stuck,
250063eeaf38SJesse Barnes 		 * mask them.
250163eeaf38SJesse Barnes 		 */
250263eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
250363eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
250463eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
250563eeaf38SJesse Barnes 	}
250635aed2e6SChris Wilson }
250735aed2e6SChris Wilson 
250835aed2e6SChris Wilson /**
2509b8d24a06SMika Kuoppala  * i915_handle_error - handle a gpu error
251035aed2e6SChris Wilson  * @dev: drm device
251135aed2e6SChris Wilson  *
2512b8d24a06SMika Kuoppala  * Do some basic checking of regsiter state at error time and
251335aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
251435aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
251535aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
251635aed2e6SChris Wilson  * of a ring dump etc.).
251735aed2e6SChris Wilson  */
251858174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
251958174462SMika Kuoppala 		       const char *fmt, ...)
252035aed2e6SChris Wilson {
252135aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
252258174462SMika Kuoppala 	va_list args;
252358174462SMika Kuoppala 	char error_msg[80];
252435aed2e6SChris Wilson 
252558174462SMika Kuoppala 	va_start(args, fmt);
252658174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
252758174462SMika Kuoppala 	va_end(args);
252858174462SMika Kuoppala 
252958174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
253035aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25318a905236SJesse Barnes 
2532ba1234d1SBen Gamari 	if (wedged) {
2533f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2534f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2535ba1234d1SBen Gamari 
253611ed50ecSBen Gamari 		/*
2537b8d24a06SMika Kuoppala 		 * Wakeup waiting processes so that the reset function
2538b8d24a06SMika Kuoppala 		 * i915_reset_and_wakeup doesn't deadlock trying to grab
2539b8d24a06SMika Kuoppala 		 * various locks. By bumping the reset counter first, the woken
254017e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
254117e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
254217e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
254317e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
254417e1df07SDaniel Vetter 		 *
254517e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
254617e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
254717e1df07SDaniel Vetter 		 * counter atomic_t.
254811ed50ecSBen Gamari 		 */
254917e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
255011ed50ecSBen Gamari 	}
255111ed50ecSBen Gamari 
2552b8d24a06SMika Kuoppala 	i915_reset_and_wakeup(dev);
25538a905236SJesse Barnes }
25548a905236SJesse Barnes 
255542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
255642f52ef8SKeith Packard  * we use as a pipe index
255742f52ef8SKeith Packard  */
2558f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25590a3e67a4SJesse Barnes {
25602d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2561e9d21d7fSKeith Packard 	unsigned long irqflags;
256271e0ffa5SJesse Barnes 
25631ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2564f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
25657c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2566755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
25670a3e67a4SJesse Barnes 	else
25687c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2569755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
25701ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25718692d00eSChris Wilson 
25720a3e67a4SJesse Barnes 	return 0;
25730a3e67a4SJesse Barnes }
25740a3e67a4SJesse Barnes 
2575f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2576f796cf8fSJesse Barnes {
25772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2578f796cf8fSJesse Barnes 	unsigned long irqflags;
2579b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
258040da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2581f796cf8fSJesse Barnes 
2582f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2583b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2584b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2585b1f14ad0SJesse Barnes 
2586b1f14ad0SJesse Barnes 	return 0;
2587b1f14ad0SJesse Barnes }
2588b1f14ad0SJesse Barnes 
25897e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
25907e231dbeSJesse Barnes {
25912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25927e231dbeSJesse Barnes 	unsigned long irqflags;
25937e231dbeSJesse Barnes 
25947e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
259531acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2596755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
25977e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
25987e231dbeSJesse Barnes 
25997e231dbeSJesse Barnes 	return 0;
26007e231dbeSJesse Barnes }
26017e231dbeSJesse Barnes 
2602abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2603abd58f01SBen Widawsky {
2604abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2605abd58f01SBen Widawsky 	unsigned long irqflags;
2606abd58f01SBen Widawsky 
2607abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26087167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26097167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2610abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2611abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2612abd58f01SBen Widawsky 	return 0;
2613abd58f01SBen Widawsky }
2614abd58f01SBen Widawsky 
261542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
261642f52ef8SKeith Packard  * we use as a pipe index
261742f52ef8SKeith Packard  */
2618f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26190a3e67a4SJesse Barnes {
26202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2621e9d21d7fSKeith Packard 	unsigned long irqflags;
26220a3e67a4SJesse Barnes 
26231ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26247c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2625755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2626755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26271ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26280a3e67a4SJesse Barnes }
26290a3e67a4SJesse Barnes 
2630f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2631f796cf8fSJesse Barnes {
26322d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2633f796cf8fSJesse Barnes 	unsigned long irqflags;
2634b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
263540da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2636f796cf8fSJesse Barnes 
2637f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2638b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2639b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2640b1f14ad0SJesse Barnes }
2641b1f14ad0SJesse Barnes 
26427e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26437e231dbeSJesse Barnes {
26442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26457e231dbeSJesse Barnes 	unsigned long irqflags;
26467e231dbeSJesse Barnes 
26477e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
264831acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2649755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26507e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26517e231dbeSJesse Barnes }
26527e231dbeSJesse Barnes 
2653abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2654abd58f01SBen Widawsky {
2655abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2656abd58f01SBen Widawsky 	unsigned long irqflags;
2657abd58f01SBen Widawsky 
2658abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26597167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
26607167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2661abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2662abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2663abd58f01SBen Widawsky }
2664abd58f01SBen Widawsky 
266544cdd6d2SJohn Harrison static struct drm_i915_gem_request *
266644cdd6d2SJohn Harrison ring_last_request(struct intel_engine_cs *ring)
2667852835f3SZou Nan hai {
2668893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
266944cdd6d2SJohn Harrison 			  struct drm_i915_gem_request, list);
2670893eead0SChris Wilson }
2671893eead0SChris Wilson 
26729107e9d2SChris Wilson static bool
267344cdd6d2SJohn Harrison ring_idle(struct intel_engine_cs *ring)
2674893eead0SChris Wilson {
26759107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
26761b5a433aSJohn Harrison 		i915_gem_request_completed(ring_last_request(ring), false));
2677f65d9421SBen Gamari }
2678f65d9421SBen Gamari 
2679a028c4b0SDaniel Vetter static bool
2680a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2681a028c4b0SDaniel Vetter {
2682a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2683a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2684a028c4b0SDaniel Vetter 	} else {
2685a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2686a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2687a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2688a028c4b0SDaniel Vetter 	}
2689a028c4b0SDaniel Vetter }
2690a028c4b0SDaniel Vetter 
2691a4872ba6SOscar Mateo static struct intel_engine_cs *
2692a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2693921d42eaSDaniel Vetter {
2694921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2695a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2696921d42eaSDaniel Vetter 	int i;
2697921d42eaSDaniel Vetter 
2698921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2699a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2700a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2701a6cdb93aSRodrigo Vivi 				continue;
2702a6cdb93aSRodrigo Vivi 
2703a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2704a6cdb93aSRodrigo Vivi 				return signaller;
2705a6cdb93aSRodrigo Vivi 		}
2706921d42eaSDaniel Vetter 	} else {
2707921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2708921d42eaSDaniel Vetter 
2709921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2710921d42eaSDaniel Vetter 			if(ring == signaller)
2711921d42eaSDaniel Vetter 				continue;
2712921d42eaSDaniel Vetter 
2713ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2714921d42eaSDaniel Vetter 				return signaller;
2715921d42eaSDaniel Vetter 		}
2716921d42eaSDaniel Vetter 	}
2717921d42eaSDaniel Vetter 
2718a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2719a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2720921d42eaSDaniel Vetter 
2721921d42eaSDaniel Vetter 	return NULL;
2722921d42eaSDaniel Vetter }
2723921d42eaSDaniel Vetter 
2724a4872ba6SOscar Mateo static struct intel_engine_cs *
2725a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2726a24a11e6SChris Wilson {
2727a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
272888fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2729a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2730a6cdb93aSRodrigo Vivi 	int i, backwards;
2731a24a11e6SChris Wilson 
2732a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2733a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27346274f212SChris Wilson 		return NULL;
2735a24a11e6SChris Wilson 
273688fe429dSDaniel Vetter 	/*
273788fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
273888fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2739a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2740a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
274188fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
274288fe429dSDaniel Vetter 	 * ringbuffer itself.
2743a24a11e6SChris Wilson 	 */
274488fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2745a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
274688fe429dSDaniel Vetter 
2747a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
274888fe429dSDaniel Vetter 		/*
274988fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
275088fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
275188fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
275288fe429dSDaniel Vetter 		 */
2753ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
275488fe429dSDaniel Vetter 
275588fe429dSDaniel Vetter 		/* This here seems to blow up */
2756ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2757a24a11e6SChris Wilson 		if (cmd == ipehr)
2758a24a11e6SChris Wilson 			break;
2759a24a11e6SChris Wilson 
276088fe429dSDaniel Vetter 		head -= 4;
276188fe429dSDaniel Vetter 	}
2762a24a11e6SChris Wilson 
276388fe429dSDaniel Vetter 	if (!i)
276488fe429dSDaniel Vetter 		return NULL;
276588fe429dSDaniel Vetter 
2766ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2767a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2768a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2769a6cdb93aSRodrigo Vivi 		offset <<= 32;
2770a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2771a6cdb93aSRodrigo Vivi 	}
2772a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2773a24a11e6SChris Wilson }
2774a24a11e6SChris Wilson 
2775a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
27766274f212SChris Wilson {
27776274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2778a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2779a0d036b0SChris Wilson 	u32 seqno;
27806274f212SChris Wilson 
27814be17381SChris Wilson 	ring->hangcheck.deadlock++;
27826274f212SChris Wilson 
27836274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
27844be17381SChris Wilson 	if (signaller == NULL)
27854be17381SChris Wilson 		return -1;
27864be17381SChris Wilson 
27874be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
27884be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
27896274f212SChris Wilson 		return -1;
27906274f212SChris Wilson 
27914be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
27924be17381SChris Wilson 		return 1;
27934be17381SChris Wilson 
2794a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2795a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2796a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
27974be17381SChris Wilson 		return -1;
27984be17381SChris Wilson 
27994be17381SChris Wilson 	return 0;
28006274f212SChris Wilson }
28016274f212SChris Wilson 
28026274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28036274f212SChris Wilson {
2804a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28056274f212SChris Wilson 	int i;
28066274f212SChris Wilson 
28076274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28084be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28096274f212SChris Wilson }
28106274f212SChris Wilson 
2811ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2812a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28131ec14ad3SChris Wilson {
28141ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28151ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28169107e9d2SChris Wilson 	u32 tmp;
28179107e9d2SChris Wilson 
2818f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2819f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2820f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2821f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2822f260fe7bSMika Kuoppala 		}
2823f260fe7bSMika Kuoppala 
2824f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2825f260fe7bSMika Kuoppala 	}
28266274f212SChris Wilson 
28279107e9d2SChris Wilson 	if (IS_GEN2(dev))
2828f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28299107e9d2SChris Wilson 
28309107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28319107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28329107e9d2SChris Wilson 	 * and break the hang. This should work on
28339107e9d2SChris Wilson 	 * all but the second generation chipsets.
28349107e9d2SChris Wilson 	 */
28359107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28361ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
283758174462SMika Kuoppala 		i915_handle_error(dev, false,
283858174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28391ec14ad3SChris Wilson 				  ring->name);
28401ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2841f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28421ec14ad3SChris Wilson 	}
2843a24a11e6SChris Wilson 
28446274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28456274f212SChris Wilson 		switch (semaphore_passed(ring)) {
28466274f212SChris Wilson 		default:
2847f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
28486274f212SChris Wilson 		case 1:
284958174462SMika Kuoppala 			i915_handle_error(dev, false,
285058174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2851a24a11e6SChris Wilson 					  ring->name);
2852a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2853f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
28546274f212SChris Wilson 		case 0:
2855f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
28566274f212SChris Wilson 		}
28579107e9d2SChris Wilson 	}
28589107e9d2SChris Wilson 
2859f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2860a24a11e6SChris Wilson }
2861d1e61e7fSChris Wilson 
2862737b1506SChris Wilson /*
2863f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
286405407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
286505407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
286605407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
286705407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
286805407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2869f65d9421SBen Gamari  */
2870737b1506SChris Wilson static void i915_hangcheck_elapsed(struct work_struct *work)
2871f65d9421SBen Gamari {
2872737b1506SChris Wilson 	struct drm_i915_private *dev_priv =
2873737b1506SChris Wilson 		container_of(work, typeof(*dev_priv),
2874737b1506SChris Wilson 			     gpu_error.hangcheck_work.work);
2875737b1506SChris Wilson 	struct drm_device *dev = dev_priv->dev;
2876a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2877b4519513SChris Wilson 	int i;
287805407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
28799107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
28809107e9d2SChris Wilson #define BUSY 1
28819107e9d2SChris Wilson #define KICK 5
28829107e9d2SChris Wilson #define HUNG 20
2883893eead0SChris Wilson 
2884d330a953SJani Nikula 	if (!i915.enable_hangcheck)
28853e0dc6b0SBen Widawsky 		return;
28863e0dc6b0SBen Widawsky 
2887b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
288850877445SChris Wilson 		u64 acthd;
288950877445SChris Wilson 		u32 seqno;
28909107e9d2SChris Wilson 		bool busy = true;
2891b4519513SChris Wilson 
28926274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
28936274f212SChris Wilson 
289405407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
289505407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
289605407ff8SMika Kuoppala 
289705407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
289844cdd6d2SJohn Harrison 			if (ring_idle(ring)) {
2899da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2900da661464SMika Kuoppala 
29019107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29029107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2903094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2904f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29059107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29069107e9d2SChris Wilson 								  ring->name);
2907f4adcd24SDaniel Vetter 						else
2908f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2909f4adcd24SDaniel Vetter 								 ring->name);
29109107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2911094f9a54SChris Wilson 					}
2912094f9a54SChris Wilson 					/* Safeguard against driver failure */
2913094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29149107e9d2SChris Wilson 				} else
29159107e9d2SChris Wilson 					busy = false;
291605407ff8SMika Kuoppala 			} else {
29176274f212SChris Wilson 				/* We always increment the hangcheck score
29186274f212SChris Wilson 				 * if the ring is busy and still processing
29196274f212SChris Wilson 				 * the same request, so that no single request
29206274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29216274f212SChris Wilson 				 * batches). The only time we do not increment
29226274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29236274f212SChris Wilson 				 * ring is in a legitimate wait for another
29246274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29256274f212SChris Wilson 				 * victim and we want to be sure we catch the
29266274f212SChris Wilson 				 * right culprit. Then every time we do kick
29276274f212SChris Wilson 				 * the ring, add a small increment to the
29286274f212SChris Wilson 				 * score so that we can catch a batch that is
29296274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29306274f212SChris Wilson 				 * for stalling the machine.
29319107e9d2SChris Wilson 				 */
2932ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2933ad8beaeaSMika Kuoppala 								    acthd);
2934ad8beaeaSMika Kuoppala 
2935ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2936da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2937f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2938f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2939f260fe7bSMika Kuoppala 					break;
2940f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2941ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29426274f212SChris Wilson 					break;
2943f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2944ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29456274f212SChris Wilson 					break;
2946f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2947ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
29486274f212SChris Wilson 					stuck[i] = true;
29496274f212SChris Wilson 					break;
29506274f212SChris Wilson 				}
295105407ff8SMika Kuoppala 			}
29529107e9d2SChris Wilson 		} else {
2953da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
2954da661464SMika Kuoppala 
29559107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
29569107e9d2SChris Wilson 			 * attempts across multiple batches.
29579107e9d2SChris Wilson 			 */
29589107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
29599107e9d2SChris Wilson 				ring->hangcheck.score--;
2960f260fe7bSMika Kuoppala 
2961f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
2962cbb465e7SChris Wilson 		}
2963f65d9421SBen Gamari 
296405407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
296505407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
29669107e9d2SChris Wilson 		busy_count += busy;
296705407ff8SMika Kuoppala 	}
296805407ff8SMika Kuoppala 
296905407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
2970b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
2971b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
297205407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
2973a43adf07SChris Wilson 				 ring->name);
2974a43adf07SChris Wilson 			rings_hung++;
297505407ff8SMika Kuoppala 		}
297605407ff8SMika Kuoppala 	}
297705407ff8SMika Kuoppala 
297805407ff8SMika Kuoppala 	if (rings_hung)
297958174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
298005407ff8SMika Kuoppala 
298105407ff8SMika Kuoppala 	if (busy_count)
298205407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
298305407ff8SMika Kuoppala 		 * being added */
298410cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
298510cd45b6SMika Kuoppala }
298610cd45b6SMika Kuoppala 
298710cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
298810cd45b6SMika Kuoppala {
2989737b1506SChris Wilson 	struct i915_gpu_error *e = &to_i915(dev)->gpu_error;
2990672e7b7cSChris Wilson 
2991d330a953SJani Nikula 	if (!i915.enable_hangcheck)
299210cd45b6SMika Kuoppala 		return;
299310cd45b6SMika Kuoppala 
2994737b1506SChris Wilson 	/* Don't continually defer the hangcheck so that it is always run at
2995737b1506SChris Wilson 	 * least once after work has been scheduled on any ring. Otherwise,
2996737b1506SChris Wilson 	 * we will ignore a hung ring if a second ring is kept busy.
2997737b1506SChris Wilson 	 */
2998737b1506SChris Wilson 
2999737b1506SChris Wilson 	queue_delayed_work(e->hangcheck_wq, &e->hangcheck_work,
3000737b1506SChris Wilson 			   round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES));
3001f65d9421SBen Gamari }
3002f65d9421SBen Gamari 
30031c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
300491738a95SPaulo Zanoni {
300591738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
300691738a95SPaulo Zanoni 
300791738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
300891738a95SPaulo Zanoni 		return;
300991738a95SPaulo Zanoni 
3010f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3011105b122eSPaulo Zanoni 
3012105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3013105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3014622364b6SPaulo Zanoni }
3015105b122eSPaulo Zanoni 
301691738a95SPaulo Zanoni /*
3017622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3018622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3019622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3020622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3021622364b6SPaulo Zanoni  *
3022622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
302391738a95SPaulo Zanoni  */
3024622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3025622364b6SPaulo Zanoni {
3026622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3027622364b6SPaulo Zanoni 
3028622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3029622364b6SPaulo Zanoni 		return;
3030622364b6SPaulo Zanoni 
3031622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
303291738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
303391738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
303491738a95SPaulo Zanoni }
303591738a95SPaulo Zanoni 
30367c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3037d18ea1b5SDaniel Vetter {
3038d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3039d18ea1b5SDaniel Vetter 
3040f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3041a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3042f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3043d18ea1b5SDaniel Vetter }
3044d18ea1b5SDaniel Vetter 
3045c0e09200SDave Airlie /* drm_dma.h hooks
3046c0e09200SDave Airlie */
3047be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3048036a4a7dSZhenyu Wang {
30492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3050036a4a7dSZhenyu Wang 
30510c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3052bdfcdb63SDaniel Vetter 
3053f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3054c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3055c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3056036a4a7dSZhenyu Wang 
30577c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3058c650156aSZhenyu Wang 
30591c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
30607d99163dSBen Widawsky }
30617d99163dSBen Widawsky 
306270591a41SVille Syrjälä static void vlv_display_irq_reset(struct drm_i915_private *dev_priv)
306370591a41SVille Syrjälä {
306470591a41SVille Syrjälä 	enum pipe pipe;
306570591a41SVille Syrjälä 
306670591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
306770591a41SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
306870591a41SVille Syrjälä 
306970591a41SVille Syrjälä 	for_each_pipe(dev_priv, pipe)
307070591a41SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
307170591a41SVille Syrjälä 
307270591a41SVille Syrjälä 	GEN5_IRQ_RESET(VLV_);
307370591a41SVille Syrjälä }
307470591a41SVille Syrjälä 
30757e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
30767e231dbeSJesse Barnes {
30772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
30787e231dbeSJesse Barnes 
30797e231dbeSJesse Barnes 	/* VLV magic */
30807e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
30817e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
30827e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
30837e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
30847e231dbeSJesse Barnes 
30857c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
30867e231dbeSJesse Barnes 
30877c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
30887e231dbeSJesse Barnes 
308970591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
30907e231dbeSJesse Barnes }
30917e231dbeSJesse Barnes 
3092d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3093d6e3cca3SDaniel Vetter {
3094d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3095d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3096d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3097d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3098d6e3cca3SDaniel Vetter }
3099d6e3cca3SDaniel Vetter 
3100823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3101abd58f01SBen Widawsky {
3102abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3103abd58f01SBen Widawsky 	int pipe;
3104abd58f01SBen Widawsky 
3105abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3106abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3107abd58f01SBen Widawsky 
3108d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3109abd58f01SBen Widawsky 
3110055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3111f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3112813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3113f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3114abd58f01SBen Widawsky 
3115f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3116f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3117f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3118abd58f01SBen Widawsky 
31191c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3120abd58f01SBen Widawsky }
3121abd58f01SBen Widawsky 
31224c6c03beSDamien Lespiau void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv,
31234c6c03beSDamien Lespiau 				     unsigned int pipe_mask)
3124d49bdb0eSPaulo Zanoni {
31251180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3126d49bdb0eSPaulo Zanoni 
312713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3128d14c0343SDamien Lespiau 	if (pipe_mask & 1 << PIPE_A)
3129d14c0343SDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_A,
3130d14c0343SDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_A],
3131d14c0343SDamien Lespiau 				  ~dev_priv->de_irq_mask[PIPE_A] | extra_ier);
31324c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_B)
31334c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B,
31344c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_B],
31351180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
31364c6c03beSDamien Lespiau 	if (pipe_mask & 1 << PIPE_C)
31374c6c03beSDamien Lespiau 		GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C,
31384c6c03beSDamien Lespiau 				  dev_priv->de_irq_mask[PIPE_C],
31391180e206SPaulo Zanoni 				  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
314013321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3141d49bdb0eSPaulo Zanoni }
3142d49bdb0eSPaulo Zanoni 
314343f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
314443f328d7SVille Syrjälä {
314543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
314643f328d7SVille Syrjälä 
314743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
314843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
314943f328d7SVille Syrjälä 
3150d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
315143f328d7SVille Syrjälä 
315243f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
315343f328d7SVille Syrjälä 
315443f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
315543f328d7SVille Syrjälä 
315670591a41SVille Syrjälä 	vlv_display_irq_reset(dev_priv);
315743f328d7SVille Syrjälä }
315843f328d7SVille Syrjälä 
315982a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
316082a28bcfSDaniel Vetter {
31612d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
316282a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3163fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
316482a28bcfSDaniel Vetter 
316582a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3166fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3167b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3168cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3169fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
317082a28bcfSDaniel Vetter 	} else {
3171fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3172b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3173cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3174fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
317582a28bcfSDaniel Vetter 	}
317682a28bcfSDaniel Vetter 
3177fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
317882a28bcfSDaniel Vetter 
31797fe0b973SKeith Packard 	/*
31807fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
31817fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
31827fe0b973SKeith Packard 	 *
31837fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
31847fe0b973SKeith Packard 	 */
31857fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
31867fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
31877fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
31887fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
31897fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
31907fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
31917fe0b973SKeith Packard }
31927fe0b973SKeith Packard 
3193d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3194d46da437SPaulo Zanoni {
31952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
319682a28bcfSDaniel Vetter 	u32 mask;
3197d46da437SPaulo Zanoni 
3198692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3199692a04cfSDaniel Vetter 		return;
3200692a04cfSDaniel Vetter 
3201105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32025c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3203105b122eSPaulo Zanoni 	else
32045c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32058664281bSPaulo Zanoni 
3206337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3207d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3208d46da437SPaulo Zanoni }
3209d46da437SPaulo Zanoni 
32100a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32110a9a8c91SDaniel Vetter {
32120a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32130a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32140a9a8c91SDaniel Vetter 
32150a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32160a9a8c91SDaniel Vetter 
32170a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3218040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32190a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
322035a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
322135a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32220a9a8c91SDaniel Vetter 	}
32230a9a8c91SDaniel Vetter 
32240a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32250a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32260a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32270a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32280a9a8c91SDaniel Vetter 	} else {
32290a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32300a9a8c91SDaniel Vetter 	}
32310a9a8c91SDaniel Vetter 
323235079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32330a9a8c91SDaniel Vetter 
32340a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
323578e68d36SImre Deak 		/*
323678e68d36SImre Deak 		 * RPS interrupts will get enabled/disabled on demand when RPS
323778e68d36SImre Deak 		 * itself is enabled/disabled.
323878e68d36SImre Deak 		 */
32390a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32400a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32410a9a8c91SDaniel Vetter 
3242605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
324335079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32440a9a8c91SDaniel Vetter 	}
32450a9a8c91SDaniel Vetter }
32460a9a8c91SDaniel Vetter 
3247f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3248036a4a7dSZhenyu Wang {
32492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
32508e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32518e76f8dcSPaulo Zanoni 
32528e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
32538e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
32548e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
32558e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
32565c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
32578e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
32585c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
32598e76f8dcSPaulo Zanoni 	} else {
32608e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3261ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
32625b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
32635b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
32645b3a856bSDaniel Vetter 				DE_POISON);
32655c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
32665c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
32678e76f8dcSPaulo Zanoni 	}
3268036a4a7dSZhenyu Wang 
32691ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3270036a4a7dSZhenyu Wang 
32710c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
32720c841212SPaulo Zanoni 
3273622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3274622364b6SPaulo Zanoni 
327535079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3276036a4a7dSZhenyu Wang 
32770a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3278036a4a7dSZhenyu Wang 
3279d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
32807fe0b973SKeith Packard 
3281f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
32826005ce42SDaniel Vetter 		/* Enable PCU event interrupts
32836005ce42SDaniel Vetter 		 *
32846005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
32854bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
32864bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3287d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3288f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3289d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3290f97108d1SJesse Barnes 	}
3291f97108d1SJesse Barnes 
3292036a4a7dSZhenyu Wang 	return 0;
3293036a4a7dSZhenyu Wang }
3294036a4a7dSZhenyu Wang 
3295f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3296f8b79e58SImre Deak {
3297f8b79e58SImre Deak 	u32 pipestat_mask;
3298f8b79e58SImre Deak 	u32 iir_mask;
3299120dda4fSVille Syrjälä 	enum pipe pipe;
3300f8b79e58SImre Deak 
3301f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3302f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3303f8b79e58SImre Deak 
3304120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3305120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3306f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3307f8b79e58SImre Deak 
3308f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3309f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3310f8b79e58SImre Deak 
3311120dda4fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3312120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3313120dda4fSVille Syrjälä 		      i915_enable_pipestat(dev_priv, pipe, pipestat_mask);
3314f8b79e58SImre Deak 
3315f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3316f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3317f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3318120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3319120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3320f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3321f8b79e58SImre Deak 
3322f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3323f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3324f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
332576e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
332676e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
3327f8b79e58SImre Deak }
3328f8b79e58SImre Deak 
3329f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3330f8b79e58SImre Deak {
3331f8b79e58SImre Deak 	u32 pipestat_mask;
3332f8b79e58SImre Deak 	u32 iir_mask;
3333120dda4fSVille Syrjälä 	enum pipe pipe;
3334f8b79e58SImre Deak 
3335f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3336f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33376c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3338120dda4fSVille Syrjälä 	if (IS_CHERRYVIEW(dev_priv))
3339120dda4fSVille Syrjälä 		iir_mask |= I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
3340f8b79e58SImre Deak 
3341f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3342f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
334376e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3344f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3345f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3346f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3347f8b79e58SImre Deak 
3348f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3349f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3350f8b79e58SImre Deak 
3351120dda4fSVille Syrjälä 	i915_disable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3352120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3353120dda4fSVille Syrjälä 		i915_disable_pipestat(dev_priv, pipe, pipestat_mask);
3354f8b79e58SImre Deak 
3355f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3356f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3357120dda4fSVille Syrjälä 
3358120dda4fSVille Syrjälä 	for_each_pipe(dev_priv, pipe)
3359120dda4fSVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), pipestat_mask);
3360f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3361f8b79e58SImre Deak }
3362f8b79e58SImre Deak 
3363f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3364f8b79e58SImre Deak {
3365f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3366f8b79e58SImre Deak 
3367f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3368f8b79e58SImre Deak 		return;
3369f8b79e58SImre Deak 
3370f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3371f8b79e58SImre Deak 
3372950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3373f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3374f8b79e58SImre Deak }
3375f8b79e58SImre Deak 
3376f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3377f8b79e58SImre Deak {
3378f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3379f8b79e58SImre Deak 
3380f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3381f8b79e58SImre Deak 		return;
3382f8b79e58SImre Deak 
3383f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3384f8b79e58SImre Deak 
3385950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3386f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3387f8b79e58SImre Deak }
3388f8b79e58SImre Deak 
33890e6c9a9eSVille Syrjälä static void vlv_display_irq_postinstall(struct drm_i915_private *dev_priv)
33907e231dbeSJesse Barnes {
3391f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
33927e231dbeSJesse Barnes 
339320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
339420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
339520afbda2SDaniel Vetter 
33967e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
339776e41860SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
339876e41860SVille Syrjälä 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
339976e41860SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
340076e41860SVille Syrjälä 	POSTING_READ(VLV_IMR);
34017e231dbeSJesse Barnes 
3402b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3403b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3404d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3405f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3406f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3407d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
34080e6c9a9eSVille Syrjälä }
34090e6c9a9eSVille Syrjälä 
34100e6c9a9eSVille Syrjälä static int valleyview_irq_postinstall(struct drm_device *dev)
34110e6c9a9eSVille Syrjälä {
34120e6c9a9eSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
34130e6c9a9eSVille Syrjälä 
34140e6c9a9eSVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
34157e231dbeSJesse Barnes 
34160a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34177e231dbeSJesse Barnes 
34187e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34197e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34207e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34217e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34227e231dbeSJesse Barnes #endif
34237e231dbeSJesse Barnes 
34247e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
342520afbda2SDaniel Vetter 
342620afbda2SDaniel Vetter 	return 0;
342720afbda2SDaniel Vetter }
342820afbda2SDaniel Vetter 
3429abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3430abd58f01SBen Widawsky {
3431abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3432abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3433abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
343473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3435abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
343673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
343773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3438abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
343973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
344073d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
344173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3442abd58f01SBen Widawsky 		0,
344373d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
344473d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3445abd58f01SBen Widawsky 		};
3446abd58f01SBen Widawsky 
34470961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
34489a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34499a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
345078e68d36SImre Deak 	/*
345178e68d36SImre Deak 	 * RPS interrupts will get enabled/disabled on demand when RPS itself
345278e68d36SImre Deak 	 * is enabled/disabled.
345378e68d36SImre Deak 	 */
345478e68d36SImre Deak 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, 0);
34559a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3456abd58f01SBen Widawsky }
3457abd58f01SBen Widawsky 
3458abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3459abd58f01SBen Widawsky {
3460770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3461770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3462abd58f01SBen Widawsky 	int pipe;
346388e04703SJesse Barnes 	u32 aux_en = GEN8_AUX_CHANNEL_A;
3464770de83dSDamien Lespiau 
346588e04703SJesse Barnes 	if (IS_GEN9(dev_priv)) {
3466770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3467770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
346888e04703SJesse Barnes 		aux_en |= GEN9_AUX_CHANNEL_B | GEN9_AUX_CHANNEL_C |
346988e04703SJesse Barnes 			GEN9_AUX_CHANNEL_D;
347088e04703SJesse Barnes 	} else
3471770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3472770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3473770de83dSDamien Lespiau 
3474770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3475770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3476770de83dSDamien Lespiau 
347713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
347813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
347913b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3480abd58f01SBen Widawsky 
3481055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3482f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3483813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3484813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3485813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
348635079899SPaulo Zanoni 					  de_pipe_enables);
3487abd58f01SBen Widawsky 
348888e04703SJesse Barnes 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~aux_en, aux_en);
3489abd58f01SBen Widawsky }
3490abd58f01SBen Widawsky 
3491abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3492abd58f01SBen Widawsky {
3493abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3494abd58f01SBen Widawsky 
3495622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3496622364b6SPaulo Zanoni 
3497abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3498abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3499abd58f01SBen Widawsky 
3500abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3501abd58f01SBen Widawsky 
3502abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3503abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3504abd58f01SBen Widawsky 
3505abd58f01SBen Widawsky 	return 0;
3506abd58f01SBen Widawsky }
3507abd58f01SBen Widawsky 
350843f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
350943f328d7SVille Syrjälä {
351043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
351143f328d7SVille Syrjälä 
3512c2b66797SVille Syrjälä 	vlv_display_irq_postinstall(dev_priv);
351343f328d7SVille Syrjälä 
351443f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
351543f328d7SVille Syrjälä 
351643f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
351743f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
351843f328d7SVille Syrjälä 
351943f328d7SVille Syrjälä 	return 0;
352043f328d7SVille Syrjälä }
352143f328d7SVille Syrjälä 
3522abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3523abd58f01SBen Widawsky {
3524abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3525abd58f01SBen Widawsky 
3526abd58f01SBen Widawsky 	if (!dev_priv)
3527abd58f01SBen Widawsky 		return;
3528abd58f01SBen Widawsky 
3529823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3530abd58f01SBen Widawsky }
3531abd58f01SBen Widawsky 
35328ea0be4fSVille Syrjälä static void vlv_display_irq_uninstall(struct drm_i915_private *dev_priv)
35338ea0be4fSVille Syrjälä {
35348ea0be4fSVille Syrjälä 	/* Interrupt setup is already guaranteed to be single-threaded, this is
35358ea0be4fSVille Syrjälä 	 * just to make the assert_spin_locked check happy. */
35368ea0be4fSVille Syrjälä 	spin_lock_irq(&dev_priv->irq_lock);
35378ea0be4fSVille Syrjälä 	if (dev_priv->display_irqs_enabled)
35388ea0be4fSVille Syrjälä 		valleyview_display_irqs_uninstall(dev_priv);
35398ea0be4fSVille Syrjälä 	spin_unlock_irq(&dev_priv->irq_lock);
35408ea0be4fSVille Syrjälä 
35418ea0be4fSVille Syrjälä 	vlv_display_irq_reset(dev_priv);
35428ea0be4fSVille Syrjälä 
3543c352d1baSImre Deak 	dev_priv->irq_mask = ~0;
35448ea0be4fSVille Syrjälä }
35458ea0be4fSVille Syrjälä 
35467e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35477e231dbeSJesse Barnes {
35482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35497e231dbeSJesse Barnes 
35507e231dbeSJesse Barnes 	if (!dev_priv)
35517e231dbeSJesse Barnes 		return;
35527e231dbeSJesse Barnes 
3553843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3554843d0e7dSImre Deak 
3555893fce8eSVille Syrjälä 	gen5_gt_irq_reset(dev);
3556893fce8eSVille Syrjälä 
35577e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
3558f8b79e58SImre Deak 
35598ea0be4fSVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
35607e231dbeSJesse Barnes }
35617e231dbeSJesse Barnes 
356243f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
356343f328d7SVille Syrjälä {
356443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
356543f328d7SVille Syrjälä 
356643f328d7SVille Syrjälä 	if (!dev_priv)
356743f328d7SVille Syrjälä 		return;
356843f328d7SVille Syrjälä 
356943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
357043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
357143f328d7SVille Syrjälä 
3572a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
357343f328d7SVille Syrjälä 
3574a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
357543f328d7SVille Syrjälä 
3576c2b66797SVille Syrjälä 	vlv_display_irq_uninstall(dev_priv);
357743f328d7SVille Syrjälä }
357843f328d7SVille Syrjälä 
3579f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3580036a4a7dSZhenyu Wang {
35812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35824697995bSJesse Barnes 
35834697995bSJesse Barnes 	if (!dev_priv)
35844697995bSJesse Barnes 		return;
35854697995bSJesse Barnes 
3586be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3587036a4a7dSZhenyu Wang }
3588036a4a7dSZhenyu Wang 
3589c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3590c2798b19SChris Wilson {
35912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3592c2798b19SChris Wilson 	int pipe;
3593c2798b19SChris Wilson 
3594055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3595c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3596c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3597c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3598c2798b19SChris Wilson 	POSTING_READ16(IER);
3599c2798b19SChris Wilson }
3600c2798b19SChris Wilson 
3601c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3602c2798b19SChris Wilson {
36032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3604c2798b19SChris Wilson 
3605c2798b19SChris Wilson 	I915_WRITE16(EMR,
3606c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3607c2798b19SChris Wilson 
3608c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3609c2798b19SChris Wilson 	dev_priv->irq_mask =
3610c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3611c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3612c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3613c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3614c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3615c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3616c2798b19SChris Wilson 
3617c2798b19SChris Wilson 	I915_WRITE16(IER,
3618c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3619c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3620c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3621c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3622c2798b19SChris Wilson 	POSTING_READ16(IER);
3623c2798b19SChris Wilson 
3624379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3625379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3626d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3627755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3628755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3629d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3630379ef82dSDaniel Vetter 
3631c2798b19SChris Wilson 	return 0;
3632c2798b19SChris Wilson }
3633c2798b19SChris Wilson 
363490a72f87SVille Syrjälä /*
363590a72f87SVille Syrjälä  * Returns true when a page flip has completed.
363690a72f87SVille Syrjälä  */
363790a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
36381f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
363990a72f87SVille Syrjälä {
36402d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36411f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
364290a72f87SVille Syrjälä 
36438d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
364490a72f87SVille Syrjälä 		return false;
364590a72f87SVille Syrjälä 
364690a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3647d6bbafa1SChris Wilson 		goto check_page_flip;
364890a72f87SVille Syrjälä 
364990a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
365090a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
365190a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
365290a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
365390a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
365490a72f87SVille Syrjälä 	 */
365590a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3656d6bbafa1SChris Wilson 		goto check_page_flip;
365790a72f87SVille Syrjälä 
36587d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
365990a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
366090a72f87SVille Syrjälä 	return true;
3661d6bbafa1SChris Wilson 
3662d6bbafa1SChris Wilson check_page_flip:
3663d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3664d6bbafa1SChris Wilson 	return false;
366590a72f87SVille Syrjälä }
366690a72f87SVille Syrjälä 
3667ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3668c2798b19SChris Wilson {
366945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
36702d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3671c2798b19SChris Wilson 	u16 iir, new_iir;
3672c2798b19SChris Wilson 	u32 pipe_stats[2];
3673c2798b19SChris Wilson 	int pipe;
3674c2798b19SChris Wilson 	u16 flip_mask =
3675c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3676c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3677c2798b19SChris Wilson 
36782dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
36792dd2a883SImre Deak 		return IRQ_NONE;
36802dd2a883SImre Deak 
3681c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3682c2798b19SChris Wilson 	if (iir == 0)
3683c2798b19SChris Wilson 		return IRQ_NONE;
3684c2798b19SChris Wilson 
3685c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3686c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3687c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3688c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3689c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3690c2798b19SChris Wilson 		 */
3691222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3692c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3693aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3694c2798b19SChris Wilson 
3695055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3696c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3697c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3698c2798b19SChris Wilson 
3699c2798b19SChris Wilson 			/*
3700c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3701c2798b19SChris Wilson 			 */
37022d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3703c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3704c2798b19SChris Wilson 		}
3705222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3706c2798b19SChris Wilson 
3707c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3708c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3709c2798b19SChris Wilson 
3710c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3711c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3712c2798b19SChris Wilson 
3713055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37141f1c2e24SVille Syrjälä 			int plane = pipe;
37153a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
37161f1c2e24SVille Syrjälä 				plane = !plane;
37171f1c2e24SVille Syrjälä 
37184356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37191f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
37201f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3721c2798b19SChris Wilson 
37224356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3723277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37242d9d2b0bSVille Syrjälä 
37251f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37261f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37271f7247c0SDaniel Vetter 								    pipe);
37284356d586SDaniel Vetter 		}
3729c2798b19SChris Wilson 
3730c2798b19SChris Wilson 		iir = new_iir;
3731c2798b19SChris Wilson 	}
3732c2798b19SChris Wilson 
3733c2798b19SChris Wilson 	return IRQ_HANDLED;
3734c2798b19SChris Wilson }
3735c2798b19SChris Wilson 
3736c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3737c2798b19SChris Wilson {
37382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3739c2798b19SChris Wilson 	int pipe;
3740c2798b19SChris Wilson 
3741055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3742c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3743c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3744c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3745c2798b19SChris Wilson 	}
3746c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3747c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3748c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3749c2798b19SChris Wilson }
3750c2798b19SChris Wilson 
3751a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3752a266c7d5SChris Wilson {
37532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3754a266c7d5SChris Wilson 	int pipe;
3755a266c7d5SChris Wilson 
3756a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3757a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3758a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3759a266c7d5SChris Wilson 	}
3760a266c7d5SChris Wilson 
376100d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3762055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3763a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3764a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3765a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3766a266c7d5SChris Wilson 	POSTING_READ(IER);
3767a266c7d5SChris Wilson }
3768a266c7d5SChris Wilson 
3769a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3770a266c7d5SChris Wilson {
37712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
377238bde180SChris Wilson 	u32 enable_mask;
3773a266c7d5SChris Wilson 
377438bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
377538bde180SChris Wilson 
377638bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
377738bde180SChris Wilson 	dev_priv->irq_mask =
377838bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
377938bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
378038bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
378138bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
378238bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
378338bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
378438bde180SChris Wilson 
378538bde180SChris Wilson 	enable_mask =
378638bde180SChris Wilson 		I915_ASLE_INTERRUPT |
378738bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
378838bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
378938bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
379038bde180SChris Wilson 		I915_USER_INTERRUPT;
379138bde180SChris Wilson 
3792a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
379320afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
379420afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
379520afbda2SDaniel Vetter 
3796a266c7d5SChris Wilson 		/* Enable in IER... */
3797a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3798a266c7d5SChris Wilson 		/* and unmask in IMR */
3799a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3800a266c7d5SChris Wilson 	}
3801a266c7d5SChris Wilson 
3802a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3803a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3804a266c7d5SChris Wilson 	POSTING_READ(IER);
3805a266c7d5SChris Wilson 
3806f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
380720afbda2SDaniel Vetter 
3808379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3809379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3810d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3811755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3812755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3813d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3814379ef82dSDaniel Vetter 
381520afbda2SDaniel Vetter 	return 0;
381620afbda2SDaniel Vetter }
381720afbda2SDaniel Vetter 
381890a72f87SVille Syrjälä /*
381990a72f87SVille Syrjälä  * Returns true when a page flip has completed.
382090a72f87SVille Syrjälä  */
382190a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
382290a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
382390a72f87SVille Syrjälä {
38242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
382590a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
382690a72f87SVille Syrjälä 
38278d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
382890a72f87SVille Syrjälä 		return false;
382990a72f87SVille Syrjälä 
383090a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3831d6bbafa1SChris Wilson 		goto check_page_flip;
383290a72f87SVille Syrjälä 
383390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
383490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
383590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
383690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
383790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
383890a72f87SVille Syrjälä 	 */
383990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3840d6bbafa1SChris Wilson 		goto check_page_flip;
384190a72f87SVille Syrjälä 
38427d47559eSVille Syrjälä 	intel_prepare_page_flip(dev, plane);
384390a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
384490a72f87SVille Syrjälä 	return true;
3845d6bbafa1SChris Wilson 
3846d6bbafa1SChris Wilson check_page_flip:
3847d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3848d6bbafa1SChris Wilson 	return false;
384990a72f87SVille Syrjälä }
385090a72f87SVille Syrjälä 
3851ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3852a266c7d5SChris Wilson {
385345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
38542d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
38558291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
385638bde180SChris Wilson 	u32 flip_mask =
385738bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
385838bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
385938bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3860a266c7d5SChris Wilson 
38612dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
38622dd2a883SImre Deak 		return IRQ_NONE;
38632dd2a883SImre Deak 
3864a266c7d5SChris Wilson 	iir = I915_READ(IIR);
386538bde180SChris Wilson 	do {
386638bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
38678291ee90SChris Wilson 		bool blc_event = false;
3868a266c7d5SChris Wilson 
3869a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3870a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3871a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3872a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3873a266c7d5SChris Wilson 		 */
3874222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3875a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
3876aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
3877a266c7d5SChris Wilson 
3878055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3879a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3880a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3881a266c7d5SChris Wilson 
388238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3883a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3884a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
388538bde180SChris Wilson 				irq_received = true;
3886a266c7d5SChris Wilson 			}
3887a266c7d5SChris Wilson 		}
3888222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3889a266c7d5SChris Wilson 
3890a266c7d5SChris Wilson 		if (!irq_received)
3891a266c7d5SChris Wilson 			break;
3892a266c7d5SChris Wilson 
3893a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
389416c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
389516c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
389616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3897a266c7d5SChris Wilson 
389838bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3899a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3900a266c7d5SChris Wilson 
3901a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3902a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3903a266c7d5SChris Wilson 
3904055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
390538bde180SChris Wilson 			int plane = pipe;
39063a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
390738bde180SChris Wilson 				plane = !plane;
39085e2032d4SVille Syrjälä 
390990a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
391090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
391190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3912a266c7d5SChris Wilson 
3913a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3914a266c7d5SChris Wilson 				blc_event = true;
39154356d586SDaniel Vetter 
39164356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3917277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39182d9d2b0bSVille Syrjälä 
39191f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39201f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39211f7247c0SDaniel Vetter 								    pipe);
3922a266c7d5SChris Wilson 		}
3923a266c7d5SChris Wilson 
3924a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3925a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3926a266c7d5SChris Wilson 
3927a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3928a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3929a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3930a266c7d5SChris Wilson 		 * we would never get another interrupt.
3931a266c7d5SChris Wilson 		 *
3932a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
3933a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
3934a266c7d5SChris Wilson 		 * another one.
3935a266c7d5SChris Wilson 		 *
3936a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
3937a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
3938a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
3939a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
3940a266c7d5SChris Wilson 		 * stray interrupts.
3941a266c7d5SChris Wilson 		 */
394238bde180SChris Wilson 		ret = IRQ_HANDLED;
3943a266c7d5SChris Wilson 		iir = new_iir;
394438bde180SChris Wilson 	} while (iir & ~flip_mask);
3945a266c7d5SChris Wilson 
3946a266c7d5SChris Wilson 	return ret;
3947a266c7d5SChris Wilson }
3948a266c7d5SChris Wilson 
3949a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
3950a266c7d5SChris Wilson {
39512d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3952a266c7d5SChris Wilson 	int pipe;
3953a266c7d5SChris Wilson 
3954a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3955a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3956a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3957a266c7d5SChris Wilson 	}
3958a266c7d5SChris Wilson 
395900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
3960055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
396155b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
3962a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
396355b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
396455b39755SChris Wilson 	}
3965a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3966a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3967a266c7d5SChris Wilson 
3968a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
3969a266c7d5SChris Wilson }
3970a266c7d5SChris Wilson 
3971a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
3972a266c7d5SChris Wilson {
39732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3974a266c7d5SChris Wilson 	int pipe;
3975a266c7d5SChris Wilson 
3976a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
3977a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3978a266c7d5SChris Wilson 
3979a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
3980055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3981a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3982a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3983a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3984a266c7d5SChris Wilson 	POSTING_READ(IER);
3985a266c7d5SChris Wilson }
3986a266c7d5SChris Wilson 
3987a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
3988a266c7d5SChris Wilson {
39892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3990bbba0a97SChris Wilson 	u32 enable_mask;
3991a266c7d5SChris Wilson 	u32 error_mask;
3992a266c7d5SChris Wilson 
3993a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
3994bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
3995adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
3996bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3997bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3998bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3999bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4000bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4001bbba0a97SChris Wilson 
4002bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
400321ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
400421ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4005bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4006bbba0a97SChris Wilson 
4007bbba0a97SChris Wilson 	if (IS_G4X(dev))
4008bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4009a266c7d5SChris Wilson 
4010b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4011b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4012d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4013755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4014755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4015755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4016d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4017a266c7d5SChris Wilson 
4018a266c7d5SChris Wilson 	/*
4019a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4020a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4021a266c7d5SChris Wilson 	 */
4022a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4023a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4024a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4025a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4026a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4027a266c7d5SChris Wilson 	} else {
4028a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4029a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4030a266c7d5SChris Wilson 	}
4031a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4032a266c7d5SChris Wilson 
4033a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4034a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4035a266c7d5SChris Wilson 	POSTING_READ(IER);
4036a266c7d5SChris Wilson 
403720afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
403820afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
403920afbda2SDaniel Vetter 
4040f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
404120afbda2SDaniel Vetter 
404220afbda2SDaniel Vetter 	return 0;
404320afbda2SDaniel Vetter }
404420afbda2SDaniel Vetter 
4045bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
404620afbda2SDaniel Vetter {
40472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4048cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
404920afbda2SDaniel Vetter 	u32 hotplug_en;
405020afbda2SDaniel Vetter 
4051b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4052b5ea2d56SDaniel Vetter 
4053bac56d5bSEgbert Eich 	hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4054bac56d5bSEgbert Eich 	hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4055adca4730SChris Wilson 	/* Note HDMI and DP share hotplug bits */
4056e5868a31SEgbert Eich 	/* enable bits are the same for all generations */
4057b2784e15SDamien Lespiau 	for_each_intel_encoder(dev, intel_encoder)
4058cd569aedSEgbert Eich 		if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4059cd569aedSEgbert Eich 			hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4060a266c7d5SChris Wilson 	/* Programming the CRT detection parameters tends
4061a266c7d5SChris Wilson 	   to generate a spurious hotplug event about three
4062a266c7d5SChris Wilson 	   seconds later.  So just do it once.
4063a266c7d5SChris Wilson 	*/
4064a266c7d5SChris Wilson 	if (IS_G4X(dev))
4065a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
406685fc95baSDaniel Vetter 	hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4067a266c7d5SChris Wilson 	hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4068a266c7d5SChris Wilson 
4069a266c7d5SChris Wilson 	/* Ignore TV since it's buggy */
4070a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4071a266c7d5SChris Wilson }
4072a266c7d5SChris Wilson 
4073ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4074a266c7d5SChris Wilson {
407545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
40762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4077a266c7d5SChris Wilson 	u32 iir, new_iir;
4078a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4079a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
408021ad8330SVille Syrjälä 	u32 flip_mask =
408121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
408221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4083a266c7d5SChris Wilson 
40842dd2a883SImre Deak 	if (!intel_irqs_enabled(dev_priv))
40852dd2a883SImre Deak 		return IRQ_NONE;
40862dd2a883SImre Deak 
4087a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4088a266c7d5SChris Wilson 
4089a266c7d5SChris Wilson 	for (;;) {
4090501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
40912c8ba29fSChris Wilson 		bool blc_event = false;
40922c8ba29fSChris Wilson 
4093a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4094a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4095a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4096a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4097a266c7d5SChris Wilson 		 */
4098222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4099a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
4100aaecdf61SDaniel Vetter 			DRM_DEBUG("Command parser error, iir 0x%08x\n", iir);
4101a266c7d5SChris Wilson 
4102055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4103a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4104a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4105a266c7d5SChris Wilson 
4106a266c7d5SChris Wilson 			/*
4107a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4108a266c7d5SChris Wilson 			 */
4109a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4110a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4111501e01d7SVille Syrjälä 				irq_received = true;
4112a266c7d5SChris Wilson 			}
4113a266c7d5SChris Wilson 		}
4114222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4115a266c7d5SChris Wilson 
4116a266c7d5SChris Wilson 		if (!irq_received)
4117a266c7d5SChris Wilson 			break;
4118a266c7d5SChris Wilson 
4119a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4120a266c7d5SChris Wilson 
4121a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
412216c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
412316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4124a266c7d5SChris Wilson 
412521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4126a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4127a266c7d5SChris Wilson 
4128a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4129a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4130a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4131a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4132a266c7d5SChris Wilson 
4133055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
41342c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
413590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
413690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4137a266c7d5SChris Wilson 
4138a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4139a266c7d5SChris Wilson 				blc_event = true;
41404356d586SDaniel Vetter 
41414356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4142277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4143a266c7d5SChris Wilson 
41441f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
41451f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
41462d9d2b0bSVille Syrjälä 		}
4147a266c7d5SChris Wilson 
4148a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4149a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4150a266c7d5SChris Wilson 
4151515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4152515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4153515ac2bbSDaniel Vetter 
4154a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4155a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4156a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4157a266c7d5SChris Wilson 		 * we would never get another interrupt.
4158a266c7d5SChris Wilson 		 *
4159a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4160a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4161a266c7d5SChris Wilson 		 * another one.
4162a266c7d5SChris Wilson 		 *
4163a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4164a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4165a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4166a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4167a266c7d5SChris Wilson 		 * stray interrupts.
4168a266c7d5SChris Wilson 		 */
4169a266c7d5SChris Wilson 		iir = new_iir;
4170a266c7d5SChris Wilson 	}
4171a266c7d5SChris Wilson 
4172a266c7d5SChris Wilson 	return ret;
4173a266c7d5SChris Wilson }
4174a266c7d5SChris Wilson 
4175a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4176a266c7d5SChris Wilson {
41772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4178a266c7d5SChris Wilson 	int pipe;
4179a266c7d5SChris Wilson 
4180a266c7d5SChris Wilson 	if (!dev_priv)
4181a266c7d5SChris Wilson 		return;
4182a266c7d5SChris Wilson 
4183a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4184a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4185a266c7d5SChris Wilson 
4186a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4187055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4188a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4189a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4190a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4191a266c7d5SChris Wilson 
4192055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4193a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4194a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4195a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4196a266c7d5SChris Wilson }
4197a266c7d5SChris Wilson 
41984cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4199ac4c16c5SEgbert Eich {
42006323751dSImre Deak 	struct drm_i915_private *dev_priv =
42016323751dSImre Deak 		container_of(work, typeof(*dev_priv),
42026323751dSImre Deak 			     hotplug_reenable_work.work);
4203ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4204ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4205ac4c16c5SEgbert Eich 	int i;
4206ac4c16c5SEgbert Eich 
42076323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
42086323751dSImre Deak 
42094cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4210ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4211ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4212ac4c16c5SEgbert Eich 
4213ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4214ac4c16c5SEgbert Eich 			continue;
4215ac4c16c5SEgbert Eich 
4216ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4217ac4c16c5SEgbert Eich 
4218ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4219ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4220ac4c16c5SEgbert Eich 
4221ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4222ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4223ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4224c23cc417SJani Nikula 							 connector->name);
4225ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4226ac4c16c5SEgbert Eich 				if (!connector->polled)
4227ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4228ac4c16c5SEgbert Eich 			}
4229ac4c16c5SEgbert Eich 		}
4230ac4c16c5SEgbert Eich 	}
4231ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4232ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
42334cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
42346323751dSImre Deak 
42356323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4236ac4c16c5SEgbert Eich }
4237ac4c16c5SEgbert Eich 
4238fca52a55SDaniel Vetter /**
4239fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4240fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4241fca52a55SDaniel Vetter  *
4242fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4243fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4244fca52a55SDaniel Vetter  */
4245b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4246f71d4af4SJesse Barnes {
4247b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
42488b2e326dSChris Wilson 
42498b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
425013cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
4251c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4252a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
42538b2e326dSChris Wilson 
4254a6706b45SDeepak S 	/* Let's track the enabled rps events */
4255b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
42566c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
425731685c25SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
425831685c25SDeepak S 	else
4259a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4260a6706b45SDeepak S 
4261737b1506SChris Wilson 	INIT_DELAYED_WORK(&dev_priv->gpu_error.hangcheck_work,
4262737b1506SChris Wilson 			  i915_hangcheck_elapsed);
42636323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
42644cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
426561bac78eSDaniel Vetter 
426697a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
42679ee32feaSDaniel Vetter 
4268b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
42694cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
42704cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4271b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4272f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4273f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4274391f75e2SVille Syrjälä 	} else {
4275391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4276391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4277f71d4af4SJesse Barnes 	}
4278f71d4af4SJesse Barnes 
427921da2700SVille Syrjälä 	/*
428021da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
428121da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
428221da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
428321da2700SVille Syrjälä 	 */
4284b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
428521da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
428621da2700SVille Syrjälä 
4287f71d4af4SJesse Barnes 	dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4288f71d4af4SJesse Barnes 	dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4289f71d4af4SJesse Barnes 
4290b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
429143f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
429243f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
429343f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
429443f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
429543f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
429643f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
429743f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4298b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
42997e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43007e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43017e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43027e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43037e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43047e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4305fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4306b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4307abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4308723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4309abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4310abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4311abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4312abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4313abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4314f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4315f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4316723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4317f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4318f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4319f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4320f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
432182a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4322f71d4af4SJesse Barnes 	} else {
4323b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4324c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4325c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4326c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4327c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4328b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4329a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4330a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4331a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4332a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
4333c2798b19SChris Wilson 		} else {
4334a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4335a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4336a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4337a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4338c2798b19SChris Wilson 		}
4339778eb334SVille Syrjälä 		if (I915_HAS_HOTPLUG(dev_priv))
4340778eb334SVille Syrjälä 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4341f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4342f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4343f71d4af4SJesse Barnes 	}
4344f71d4af4SJesse Barnes }
434520afbda2SDaniel Vetter 
4346fca52a55SDaniel Vetter /**
4347fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4348fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4349fca52a55SDaniel Vetter  *
4350fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4351fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4352fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4353fca52a55SDaniel Vetter  * obeyed.
4354fca52a55SDaniel Vetter  *
4355fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4356fca52a55SDaniel Vetter  * in the driver load and resume code.
4357fca52a55SDaniel Vetter  */
4358b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
435920afbda2SDaniel Vetter {
4360b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4361821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4362821450c6SEgbert Eich 	struct drm_connector *connector;
4363821450c6SEgbert Eich 	int i;
436420afbda2SDaniel Vetter 
4365821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4366821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4367821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4368821450c6SEgbert Eich 	}
4369821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4370821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4371821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
43720e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
43730e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
43740e32b39cSDave Airlie 		if (intel_connector->mst_port)
4375821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4376821450c6SEgbert Eich 	}
4377b5ea2d56SDaniel Vetter 
4378b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4379b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4380d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
438120afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
438220afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4383d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
438420afbda2SDaniel Vetter }
4385c67a470bSPaulo Zanoni 
4386fca52a55SDaniel Vetter /**
4387fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4388fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4389fca52a55SDaniel Vetter  *
4390fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4391fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4392fca52a55SDaniel Vetter  *
4393fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4394fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4395fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4396fca52a55SDaniel Vetter  */
43972aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
43982aeb7d3aSDaniel Vetter {
43992aeb7d3aSDaniel Vetter 	/*
44002aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44012aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44022aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44032aeb7d3aSDaniel Vetter 	 */
44042aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44052aeb7d3aSDaniel Vetter 
44062aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44072aeb7d3aSDaniel Vetter }
44082aeb7d3aSDaniel Vetter 
4409fca52a55SDaniel Vetter /**
4410fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4411fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4412fca52a55SDaniel Vetter  *
4413fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4414fca52a55SDaniel Vetter  * resources acquired in the init functions.
4415fca52a55SDaniel Vetter  */
44162aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44172aeb7d3aSDaniel Vetter {
44182aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44192aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44202aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44212aeb7d3aSDaniel Vetter }
44222aeb7d3aSDaniel Vetter 
4423fca52a55SDaniel Vetter /**
4424fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4425fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4426fca52a55SDaniel Vetter  *
4427fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4428fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4429fca52a55SDaniel Vetter  */
4430b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4431c67a470bSPaulo Zanoni {
4432b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
44332aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44342dd2a883SImre Deak 	synchronize_irq(dev_priv->dev->irq);
4435c67a470bSPaulo Zanoni }
4436c67a470bSPaulo Zanoni 
4437fca52a55SDaniel Vetter /**
4438fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4439fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4440fca52a55SDaniel Vetter  *
4441fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4442fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4443fca52a55SDaniel Vetter  */
4444b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4445c67a470bSPaulo Zanoni {
44462aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4447b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4448b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4449c67a470bSPaulo Zanoni }
4450