xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision a72e4c9f9a11d44458a12928cc776921ae43a616)
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 
48e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
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 
56e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
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 
64e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
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 
73704cfb87SDaniel Vetter static const u32 hpd_status_g4x[] = {
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 
82e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* 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##_IMR(which), (imr_val)); \
13035079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
13135079899SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IER(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##IMR, (imr_val)); \
13735079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
13835079899SPaulo Zanoni 	POSTING_READ(type##IER); \
13935079899SPaulo Zanoni } while (0)
14035079899SPaulo Zanoni 
141036a4a7dSZhenyu Wang /* For display hotplug interrupt */
14247339cd9SDaniel Vetter void
1432d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
144036a4a7dSZhenyu Wang {
1454bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1464bc9d430SDaniel Vetter 
1479df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
148c67a470bSPaulo Zanoni 		return;
149c67a470bSPaulo Zanoni 
1501ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1511ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1521ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1533143a2bfSChris Wilson 		POSTING_READ(DEIMR);
154036a4a7dSZhenyu Wang 	}
155036a4a7dSZhenyu Wang }
156036a4a7dSZhenyu Wang 
15747339cd9SDaniel Vetter void
1582d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
159036a4a7dSZhenyu Wang {
1604bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1614bc9d430SDaniel Vetter 
16206ffc778SPaulo Zanoni 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
163c67a470bSPaulo Zanoni 		return;
164c67a470bSPaulo Zanoni 
1651ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1661ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1671ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1683143a2bfSChris Wilson 		POSTING_READ(DEIMR);
169036a4a7dSZhenyu Wang 	}
170036a4a7dSZhenyu Wang }
171036a4a7dSZhenyu Wang 
17243eaea13SPaulo Zanoni /**
17343eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
17443eaea13SPaulo Zanoni  * @dev_priv: driver private
17543eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
17643eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
17743eaea13SPaulo Zanoni  */
17843eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
17943eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
18043eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
18143eaea13SPaulo Zanoni {
18243eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
18343eaea13SPaulo Zanoni 
1849df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
185c67a470bSPaulo Zanoni 		return;
186c67a470bSPaulo Zanoni 
18743eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
18843eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
18943eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
19043eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
19143eaea13SPaulo Zanoni }
19243eaea13SPaulo Zanoni 
193480c8033SDaniel Vetter void gen5_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19443eaea13SPaulo Zanoni {
19543eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
19643eaea13SPaulo Zanoni }
19743eaea13SPaulo Zanoni 
198480c8033SDaniel Vetter void gen5_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19943eaea13SPaulo Zanoni {
20043eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
20143eaea13SPaulo Zanoni }
20243eaea13SPaulo Zanoni 
203edbfdb45SPaulo Zanoni /**
204edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
205edbfdb45SPaulo Zanoni   * @dev_priv: driver private
206edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
207edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
208edbfdb45SPaulo Zanoni   */
209edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
210edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
211edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
212edbfdb45SPaulo Zanoni {
213605cd25bSPaulo Zanoni 	uint32_t new_val;
214edbfdb45SPaulo Zanoni 
215edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
216edbfdb45SPaulo Zanoni 
2179df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
218c67a470bSPaulo Zanoni 		return;
219c67a470bSPaulo Zanoni 
220605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
221f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
222f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
223f52ecbcfSPaulo Zanoni 
224605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
225605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
226605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
227edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
228edbfdb45SPaulo Zanoni 	}
229f52ecbcfSPaulo Zanoni }
230edbfdb45SPaulo Zanoni 
231480c8033SDaniel Vetter void gen6_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
232edbfdb45SPaulo Zanoni {
233edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
234edbfdb45SPaulo Zanoni }
235edbfdb45SPaulo Zanoni 
236480c8033SDaniel Vetter void gen6_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
237edbfdb45SPaulo Zanoni {
238edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
239edbfdb45SPaulo Zanoni }
240edbfdb45SPaulo Zanoni 
2410961021aSBen Widawsky /**
2420961021aSBen Widawsky   * bdw_update_pm_irq - update GT interrupt 2
2430961021aSBen Widawsky   * @dev_priv: driver private
2440961021aSBen Widawsky   * @interrupt_mask: mask of interrupt bits to update
2450961021aSBen Widawsky   * @enabled_irq_mask: mask of interrupt bits to enable
2460961021aSBen Widawsky   *
2470961021aSBen Widawsky   * Copied from the snb function, updated with relevant register offsets
2480961021aSBen Widawsky   */
2490961021aSBen Widawsky static void bdw_update_pm_irq(struct drm_i915_private *dev_priv,
2500961021aSBen Widawsky 			      uint32_t interrupt_mask,
2510961021aSBen Widawsky 			      uint32_t enabled_irq_mask)
2520961021aSBen Widawsky {
2530961021aSBen Widawsky 	uint32_t new_val;
2540961021aSBen Widawsky 
2550961021aSBen Widawsky 	assert_spin_locked(&dev_priv->irq_lock);
2560961021aSBen Widawsky 
2579df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
2580961021aSBen Widawsky 		return;
2590961021aSBen Widawsky 
2600961021aSBen Widawsky 	new_val = dev_priv->pm_irq_mask;
2610961021aSBen Widawsky 	new_val &= ~interrupt_mask;
2620961021aSBen Widawsky 	new_val |= (~enabled_irq_mask & interrupt_mask);
2630961021aSBen Widawsky 
2640961021aSBen Widawsky 	if (new_val != dev_priv->pm_irq_mask) {
2650961021aSBen Widawsky 		dev_priv->pm_irq_mask = new_val;
2660961021aSBen Widawsky 		I915_WRITE(GEN8_GT_IMR(2), dev_priv->pm_irq_mask);
2670961021aSBen Widawsky 		POSTING_READ(GEN8_GT_IMR(2));
2680961021aSBen Widawsky 	}
2690961021aSBen Widawsky }
2700961021aSBen Widawsky 
271480c8033SDaniel Vetter void gen8_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
2720961021aSBen Widawsky {
2730961021aSBen Widawsky 	bdw_update_pm_irq(dev_priv, mask, mask);
2740961021aSBen Widawsky }
2750961021aSBen Widawsky 
276480c8033SDaniel Vetter void gen8_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
2770961021aSBen Widawsky {
2780961021aSBen Widawsky 	bdw_update_pm_irq(dev_priv, mask, 0);
2790961021aSBen Widawsky }
2800961021aSBen Widawsky 
281fee884edSDaniel Vetter /**
282fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
283fee884edSDaniel Vetter  * @dev_priv: driver private
284fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
285fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
286fee884edSDaniel Vetter  */
28747339cd9SDaniel Vetter void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
288fee884edSDaniel Vetter 				  uint32_t interrupt_mask,
289fee884edSDaniel Vetter 				  uint32_t enabled_irq_mask)
290fee884edSDaniel Vetter {
291fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
292fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
293fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
294fee884edSDaniel Vetter 
295fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
296fee884edSDaniel Vetter 
2979df7575fSJesse Barnes 	if (WARN_ON(!intel_irqs_enabled(dev_priv)))
298c67a470bSPaulo Zanoni 		return;
299c67a470bSPaulo Zanoni 
300fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
301fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
302fee884edSDaniel Vetter }
3038664281bSPaulo Zanoni 
304b5ea642aSDaniel Vetter static void
305755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
306755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
3077c463586SKeith Packard {
3089db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
309755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3107c463586SKeith Packard 
311b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
312d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
313b79480baSDaniel Vetter 
31404feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
31504feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
31604feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
31704feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
318755e9019SImre Deak 		return;
319755e9019SImre Deak 
320755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
32146c06a30SVille Syrjälä 		return;
32246c06a30SVille Syrjälä 
32391d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
32491d181ddSImre Deak 
3257c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
326755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
32746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3283143a2bfSChris Wilson 	POSTING_READ(reg);
3297c463586SKeith Packard }
3307c463586SKeith Packard 
331b5ea642aSDaniel Vetter static void
332755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
333755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
3347c463586SKeith Packard {
3359db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
336755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
3377c463586SKeith Packard 
338b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
339d518ce50SDaniel Vetter 	WARN_ON(!intel_irqs_enabled(dev_priv));
340b79480baSDaniel Vetter 
34104feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
34204feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
34304feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
34404feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
34546c06a30SVille Syrjälä 		return;
34646c06a30SVille Syrjälä 
347755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
348755e9019SImre Deak 		return;
349755e9019SImre Deak 
35091d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
35191d181ddSImre Deak 
352755e9019SImre Deak 	pipestat &= ~enable_mask;
35346c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
3543143a2bfSChris Wilson 	POSTING_READ(reg);
3557c463586SKeith Packard }
3567c463586SKeith Packard 
35710c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
35810c59c51SImre Deak {
35910c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
36010c59c51SImre Deak 
36110c59c51SImre Deak 	/*
362724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
363724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
36410c59c51SImre Deak 	 */
36510c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
36610c59c51SImre Deak 		return 0;
367724a6905SVille Syrjälä 	/*
368724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
369724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
370724a6905SVille Syrjälä 	 */
371724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
372724a6905SVille Syrjälä 		return 0;
37310c59c51SImre Deak 
37410c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
37510c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
37610c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
37710c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
37810c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
37910c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
38010c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
38110c59c51SImre Deak 
38210c59c51SImre Deak 	return enable_mask;
38310c59c51SImre Deak }
38410c59c51SImre Deak 
385755e9019SImre Deak void
386755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
387755e9019SImre Deak 		     u32 status_mask)
388755e9019SImre Deak {
389755e9019SImre Deak 	u32 enable_mask;
390755e9019SImre Deak 
39110c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
39210c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
39310c59c51SImre Deak 							   status_mask);
39410c59c51SImre Deak 	else
395755e9019SImre Deak 		enable_mask = status_mask << 16;
396755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
397755e9019SImre Deak }
398755e9019SImre Deak 
399755e9019SImre Deak void
400755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
401755e9019SImre Deak 		      u32 status_mask)
402755e9019SImre Deak {
403755e9019SImre Deak 	u32 enable_mask;
404755e9019SImre Deak 
40510c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
40610c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
40710c59c51SImre Deak 							   status_mask);
40810c59c51SImre Deak 	else
409755e9019SImre Deak 		enable_mask = status_mask << 16;
410755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
411755e9019SImre Deak }
412755e9019SImre Deak 
413c0e09200SDave Airlie /**
414f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
41501c66889SZhao Yakui  */
416f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
41701c66889SZhao Yakui {
4182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4191ec14ad3SChris Wilson 
420f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
421f49e38ddSJani Nikula 		return;
422f49e38ddSJani Nikula 
42313321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
42401c66889SZhao Yakui 
425755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
426a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
4273b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
428755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
4291ec14ad3SChris Wilson 
43013321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
43101c66889SZhao Yakui }
43201c66889SZhao Yakui 
43301c66889SZhao Yakui /**
4340a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
4350a3e67a4SJesse Barnes  * @dev: DRM device
4360a3e67a4SJesse Barnes  * @pipe: pipe to check
4370a3e67a4SJesse Barnes  *
4380a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
4390a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
4400a3e67a4SJesse Barnes  * before reading such registers if unsure.
4410a3e67a4SJesse Barnes  */
4420a3e67a4SJesse Barnes static int
4430a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
4440a3e67a4SJesse Barnes {
4452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
446702e7a56SPaulo Zanoni 
447a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
448a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
449a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
450a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
45171f8ba6bSPaulo Zanoni 
452a01025afSDaniel Vetter 		return intel_crtc->active;
453a01025afSDaniel Vetter 	} else {
454a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
455a01025afSDaniel Vetter 	}
4560a3e67a4SJesse Barnes }
4570a3e67a4SJesse Barnes 
458f75f3746SVille Syrjälä /*
459f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
460f75f3746SVille Syrjälä  * around the vertical blanking period.
461f75f3746SVille Syrjälä  *
462f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
463f75f3746SVille Syrjälä  *  vblank_start >= 3
464f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
465f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
466f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
467f75f3746SVille Syrjälä  *
468f75f3746SVille Syrjälä  *           start of vblank:
469f75f3746SVille Syrjälä  *           latch double buffered registers
470f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
471f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
472f75f3746SVille Syrjälä  *           |
473f75f3746SVille Syrjälä  *           |          frame start:
474f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
475f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
476f75f3746SVille Syrjälä  *           |          |
477f75f3746SVille Syrjälä  *           |          |  start of vsync:
478f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
479f75f3746SVille Syrjälä  *           |          |  |
480f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
481f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
482f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
483f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
484f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
485f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
486f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
487f75f3746SVille Syrjälä  *       |          |                                         |
488f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
489f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
490f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
491f75f3746SVille Syrjälä  *
492f75f3746SVille Syrjälä  * x  = horizontal active
493f75f3746SVille Syrjälä  * _  = horizontal blanking
494f75f3746SVille Syrjälä  * hs = horizontal sync
495f75f3746SVille Syrjälä  * va = vertical active
496f75f3746SVille Syrjälä  * vb = vertical blanking
497f75f3746SVille Syrjälä  * vs = vertical sync
498f75f3746SVille Syrjälä  * vbs = vblank_start (number)
499f75f3746SVille Syrjälä  *
500f75f3746SVille Syrjälä  * Summary:
501f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
502f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
503f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
504f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
505f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
506f75f3746SVille Syrjälä  */
507f75f3746SVille Syrjälä 
5084cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
5094cdb83ecSVille Syrjälä {
5104cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
5114cdb83ecSVille Syrjälä 	return 0;
5124cdb83ecSVille Syrjälä }
5134cdb83ecSVille Syrjälä 
51442f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
51542f52ef8SKeith Packard  * we use as a pipe index
51642f52ef8SKeith Packard  */
517f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
5180a3e67a4SJesse Barnes {
5192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5200a3e67a4SJesse Barnes 	unsigned long high_frame;
5210a3e67a4SJesse Barnes 	unsigned long low_frame;
5220b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
5230a3e67a4SJesse Barnes 
5240a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
52544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5269db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
5270a3e67a4SJesse Barnes 		return 0;
5280a3e67a4SJesse Barnes 	}
5290a3e67a4SJesse Barnes 
530391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
531391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
532391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
533391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
534391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
535391f75e2SVille Syrjälä 
5360b2a8e09SVille Syrjälä 		htotal = mode->crtc_htotal;
5370b2a8e09SVille Syrjälä 		hsync_start = mode->crtc_hsync_start;
5380b2a8e09SVille Syrjälä 		vbl_start = mode->crtc_vblank_start;
5390b2a8e09SVille Syrjälä 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
5400b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
541391f75e2SVille Syrjälä 	} else {
542a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
543391f75e2SVille Syrjälä 
544391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
5450b2a8e09SVille Syrjälä 		hsync_start = (I915_READ(HSYNC(cpu_transcoder))  & 0x1fff) + 1;
546391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
5470b2a8e09SVille Syrjälä 		if ((I915_READ(PIPECONF(cpu_transcoder)) &
5480b2a8e09SVille Syrjälä 		     PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
5490b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
550391f75e2SVille Syrjälä 	}
551391f75e2SVille Syrjälä 
5520b2a8e09SVille Syrjälä 	/* Convert to pixel count */
5530b2a8e09SVille Syrjälä 	vbl_start *= htotal;
5540b2a8e09SVille Syrjälä 
5550b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
5560b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
5570b2a8e09SVille Syrjälä 
5589db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
5599db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
5605eddb70bSChris Wilson 
5610a3e67a4SJesse Barnes 	/*
5620a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
5630a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
5640a3e67a4SJesse Barnes 	 * register.
5650a3e67a4SJesse Barnes 	 */
5660a3e67a4SJesse Barnes 	do {
5675eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
568391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
5695eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
5700a3e67a4SJesse Barnes 	} while (high1 != high2);
5710a3e67a4SJesse Barnes 
5725eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
573391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
5745eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
575391f75e2SVille Syrjälä 
576391f75e2SVille Syrjälä 	/*
577391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
578391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
579391f75e2SVille Syrjälä 	 * counter against vblank start.
580391f75e2SVille Syrjälä 	 */
581edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
5820a3e67a4SJesse Barnes }
5830a3e67a4SJesse Barnes 
584f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
5859880b7a5SJesse Barnes {
5862d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
5879db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
5889880b7a5SJesse Barnes 
5899880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
59044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
5919db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
5929880b7a5SJesse Barnes 		return 0;
5939880b7a5SJesse Barnes 	}
5949880b7a5SJesse Barnes 
5959880b7a5SJesse Barnes 	return I915_READ(reg);
5969880b7a5SJesse Barnes }
5979880b7a5SJesse Barnes 
598ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
599ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
600ad3543edSMario Kleiner 
601a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
602a225f079SVille Syrjälä {
603a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
604a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
605a225f079SVille Syrjälä 	const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
606a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
60780715b2fSVille Syrjälä 	int position, vtotal;
608a225f079SVille Syrjälä 
60980715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
610a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
611a225f079SVille Syrjälä 		vtotal /= 2;
612a225f079SVille Syrjälä 
613a225f079SVille Syrjälä 	if (IS_GEN2(dev))
614a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
615a225f079SVille Syrjälä 	else
616a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
617a225f079SVille Syrjälä 
618a225f079SVille Syrjälä 	/*
61980715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
62080715b2fSVille Syrjälä 	 * scanline_offset adjustment.
621a225f079SVille Syrjälä 	 */
62280715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
623a225f079SVille Syrjälä }
624a225f079SVille Syrjälä 
625f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
626abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
627abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
6280af7e4dfSMario Kleiner {
629c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
630c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
631c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
632c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
6333aa18df8SVille Syrjälä 	int position;
63478e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
6350af7e4dfSMario Kleiner 	bool in_vbl = true;
6360af7e4dfSMario Kleiner 	int ret = 0;
637ad3543edSMario Kleiner 	unsigned long irqflags;
6380af7e4dfSMario Kleiner 
639c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
6400af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
6419db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
6420af7e4dfSMario Kleiner 		return 0;
6430af7e4dfSMario Kleiner 	}
6440af7e4dfSMario Kleiner 
645c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
64678e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
647c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
648c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
649c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
6500af7e4dfSMario Kleiner 
651d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
652d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
653d31faf65SVille Syrjälä 		vbl_end /= 2;
654d31faf65SVille Syrjälä 		vtotal /= 2;
655d31faf65SVille Syrjälä 	}
656d31faf65SVille Syrjälä 
657c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
658c2baf4b7SVille Syrjälä 
659ad3543edSMario Kleiner 	/*
660ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
661ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
662ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
663ad3543edSMario Kleiner 	 */
664ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
665ad3543edSMario Kleiner 
666ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
667ad3543edSMario Kleiner 
668ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
669ad3543edSMario Kleiner 	if (stime)
670ad3543edSMario Kleiner 		*stime = ktime_get();
671ad3543edSMario Kleiner 
6727c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
6730af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
6740af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
6750af7e4dfSMario Kleiner 		 */
676a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
6770af7e4dfSMario Kleiner 	} else {
6780af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
6790af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
6800af7e4dfSMario Kleiner 		 * scanout position.
6810af7e4dfSMario Kleiner 		 */
682ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
6830af7e4dfSMario Kleiner 
6843aa18df8SVille Syrjälä 		/* convert to pixel counts */
6853aa18df8SVille Syrjälä 		vbl_start *= htotal;
6863aa18df8SVille Syrjälä 		vbl_end *= htotal;
6873aa18df8SVille Syrjälä 		vtotal *= htotal;
68878e8fc6bSVille Syrjälä 
68978e8fc6bSVille Syrjälä 		/*
6907e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
6917e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
6927e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
6937e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
6947e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
6957e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
6967e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
6977e78f1cbSVille Syrjälä 		 */
6987e78f1cbSVille Syrjälä 		if (position >= vtotal)
6997e78f1cbSVille Syrjälä 			position = vtotal - 1;
7007e78f1cbSVille Syrjälä 
7017e78f1cbSVille Syrjälä 		/*
70278e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
70378e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
70478e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
70578e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
70678e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
70778e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
70878e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
70978e8fc6bSVille Syrjälä 		 */
71078e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
7113aa18df8SVille Syrjälä 	}
7123aa18df8SVille Syrjälä 
713ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
714ad3543edSMario Kleiner 	if (etime)
715ad3543edSMario Kleiner 		*etime = ktime_get();
716ad3543edSMario Kleiner 
717ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
718ad3543edSMario Kleiner 
719ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
720ad3543edSMario Kleiner 
7213aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
7223aa18df8SVille Syrjälä 
7233aa18df8SVille Syrjälä 	/*
7243aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
7253aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
7263aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
7273aa18df8SVille Syrjälä 	 * up since vbl_end.
7283aa18df8SVille Syrjälä 	 */
7293aa18df8SVille Syrjälä 	if (position >= vbl_start)
7303aa18df8SVille Syrjälä 		position -= vbl_end;
7313aa18df8SVille Syrjälä 	else
7323aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
7333aa18df8SVille Syrjälä 
7347c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
7353aa18df8SVille Syrjälä 		*vpos = position;
7363aa18df8SVille Syrjälä 		*hpos = 0;
7373aa18df8SVille Syrjälä 	} else {
7380af7e4dfSMario Kleiner 		*vpos = position / htotal;
7390af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
7400af7e4dfSMario Kleiner 	}
7410af7e4dfSMario Kleiner 
7420af7e4dfSMario Kleiner 	/* In vblank? */
7430af7e4dfSMario Kleiner 	if (in_vbl)
7443d3cbd84SDaniel Vetter 		ret |= DRM_SCANOUTPOS_IN_VBLANK;
7450af7e4dfSMario Kleiner 
7460af7e4dfSMario Kleiner 	return ret;
7470af7e4dfSMario Kleiner }
7480af7e4dfSMario Kleiner 
749a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
750a225f079SVille Syrjälä {
751a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
752a225f079SVille Syrjälä 	unsigned long irqflags;
753a225f079SVille Syrjälä 	int position;
754a225f079SVille Syrjälä 
755a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
756a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
757a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
758a225f079SVille Syrjälä 
759a225f079SVille Syrjälä 	return position;
760a225f079SVille Syrjälä }
761a225f079SVille Syrjälä 
762f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
7630af7e4dfSMario Kleiner 			      int *max_error,
7640af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
7650af7e4dfSMario Kleiner 			      unsigned flags)
7660af7e4dfSMario Kleiner {
7674041b853SChris Wilson 	struct drm_crtc *crtc;
7680af7e4dfSMario Kleiner 
7697eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
7704041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7710af7e4dfSMario Kleiner 		return -EINVAL;
7720af7e4dfSMario Kleiner 	}
7730af7e4dfSMario Kleiner 
7740af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
7754041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
7764041b853SChris Wilson 	if (crtc == NULL) {
7774041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
7784041b853SChris Wilson 		return -EINVAL;
7794041b853SChris Wilson 	}
7804041b853SChris Wilson 
7814041b853SChris Wilson 	if (!crtc->enabled) {
7824041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
7834041b853SChris Wilson 		return -EBUSY;
7844041b853SChris Wilson 	}
7850af7e4dfSMario Kleiner 
7860af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
7874041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
7884041b853SChris Wilson 						     vblank_time, flags,
7897da903efSVille Syrjälä 						     crtc,
7907da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
7910af7e4dfSMario Kleiner }
7920af7e4dfSMario Kleiner 
79367c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
79467c347ffSJani Nikula 				struct drm_connector *connector)
795321a1b30SEgbert Eich {
796321a1b30SEgbert Eich 	enum drm_connector_status old_status;
797321a1b30SEgbert Eich 
798321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
799321a1b30SEgbert Eich 	old_status = connector->status;
800321a1b30SEgbert Eich 
801321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
80267c347ffSJani Nikula 	if (old_status == connector->status)
80367c347ffSJani Nikula 		return false;
80467c347ffSJani Nikula 
80567c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
806321a1b30SEgbert Eich 		      connector->base.id,
807c23cc417SJani Nikula 		      connector->name,
80867c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
80967c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
81067c347ffSJani Nikula 
81167c347ffSJani Nikula 	return true;
812321a1b30SEgbert Eich }
813321a1b30SEgbert Eich 
81413cf5504SDave Airlie static void i915_digport_work_func(struct work_struct *work)
81513cf5504SDave Airlie {
81613cf5504SDave Airlie 	struct drm_i915_private *dev_priv =
81713cf5504SDave Airlie 		container_of(work, struct drm_i915_private, dig_port_work);
81813cf5504SDave Airlie 	u32 long_port_mask, short_port_mask;
81913cf5504SDave Airlie 	struct intel_digital_port *intel_dig_port;
82013cf5504SDave Airlie 	int i, ret;
82113cf5504SDave Airlie 	u32 old_bits = 0;
82213cf5504SDave Airlie 
8234cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
82413cf5504SDave Airlie 	long_port_mask = dev_priv->long_hpd_port_mask;
82513cf5504SDave Airlie 	dev_priv->long_hpd_port_mask = 0;
82613cf5504SDave Airlie 	short_port_mask = dev_priv->short_hpd_port_mask;
82713cf5504SDave Airlie 	dev_priv->short_hpd_port_mask = 0;
8284cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
82913cf5504SDave Airlie 
83013cf5504SDave Airlie 	for (i = 0; i < I915_MAX_PORTS; i++) {
83113cf5504SDave Airlie 		bool valid = false;
83213cf5504SDave Airlie 		bool long_hpd = false;
83313cf5504SDave Airlie 		intel_dig_port = dev_priv->hpd_irq_port[i];
83413cf5504SDave Airlie 		if (!intel_dig_port || !intel_dig_port->hpd_pulse)
83513cf5504SDave Airlie 			continue;
83613cf5504SDave Airlie 
83713cf5504SDave Airlie 		if (long_port_mask & (1 << i))  {
83813cf5504SDave Airlie 			valid = true;
83913cf5504SDave Airlie 			long_hpd = true;
84013cf5504SDave Airlie 		} else if (short_port_mask & (1 << i))
84113cf5504SDave Airlie 			valid = true;
84213cf5504SDave Airlie 
84313cf5504SDave Airlie 		if (valid) {
84413cf5504SDave Airlie 			ret = intel_dig_port->hpd_pulse(intel_dig_port, long_hpd);
84513cf5504SDave Airlie 			if (ret == true) {
84613cf5504SDave Airlie 				/* if we get true fallback to old school hpd */
84713cf5504SDave Airlie 				old_bits |= (1 << intel_dig_port->base.hpd_pin);
84813cf5504SDave Airlie 			}
84913cf5504SDave Airlie 		}
85013cf5504SDave Airlie 	}
85113cf5504SDave Airlie 
85213cf5504SDave Airlie 	if (old_bits) {
8534cb21832SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
85413cf5504SDave Airlie 		dev_priv->hpd_event_bits |= old_bits;
8554cb21832SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
85613cf5504SDave Airlie 		schedule_work(&dev_priv->hotplug_work);
85713cf5504SDave Airlie 	}
85813cf5504SDave Airlie }
85913cf5504SDave Airlie 
8605ca58282SJesse Barnes /*
8615ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
8625ca58282SJesse Barnes  */
863ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
864ac4c16c5SEgbert Eich 
8655ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
8665ca58282SJesse Barnes {
8672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
8682d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
8695ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
870c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
871cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
872cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
873cd569aedSEgbert Eich 	struct drm_connector *connector;
874cd569aedSEgbert Eich 	bool hpd_disabled = false;
875321a1b30SEgbert Eich 	bool changed = false;
876142e2398SEgbert Eich 	u32 hpd_event_bits;
8775ca58282SJesse Barnes 
878a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
879e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
880e67189abSJesse Barnes 
8814cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
882142e2398SEgbert Eich 
883142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
884142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
885cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
886cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
88736cd7444SDave Airlie 		if (!intel_connector->encoder)
88836cd7444SDave Airlie 			continue;
889cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
890cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
891cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
892cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
893cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
894cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
895c23cc417SJani Nikula 				connector->name);
896cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
897cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
898cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
899cd569aedSEgbert Eich 			hpd_disabled = true;
900cd569aedSEgbert Eich 		}
901142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
902142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
903c23cc417SJani Nikula 				      connector->name, intel_encoder->hpd_pin);
904142e2398SEgbert Eich 		}
905cd569aedSEgbert Eich 	}
906cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
907cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
908cd569aedSEgbert Eich 	  * some connectors */
909ac4c16c5SEgbert Eich 	if (hpd_disabled) {
910cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
9116323751dSImre Deak 		mod_delayed_work(system_wq, &dev_priv->hotplug_reenable_work,
9126323751dSImre Deak 				 msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
913ac4c16c5SEgbert Eich 	}
914cd569aedSEgbert Eich 
9154cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
916cd569aedSEgbert Eich 
917321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
918321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
91936cd7444SDave Airlie 		if (!intel_connector->encoder)
92036cd7444SDave Airlie 			continue;
921321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
922321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
923cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
924cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
925321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
926321a1b30SEgbert Eich 				changed = true;
927321a1b30SEgbert Eich 		}
928321a1b30SEgbert Eich 	}
92940ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
93040ee3381SKeith Packard 
931321a1b30SEgbert Eich 	if (changed)
932321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
9335ca58282SJesse Barnes }
9345ca58282SJesse Barnes 
935d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
936f97108d1SJesse Barnes {
9372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
938b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
9399270388eSDaniel Vetter 	u8 new_delay;
9409270388eSDaniel Vetter 
941d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
942f97108d1SJesse Barnes 
94373edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
94473edd18fSDaniel Vetter 
94520e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
9469270388eSDaniel Vetter 
9477648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
948b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
949b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
950f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
951f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
952f97108d1SJesse Barnes 
953f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
954b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
95520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
95620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
95720e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
95820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
959b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
96020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
96120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
96220e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
96320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
964f97108d1SJesse Barnes 	}
965f97108d1SJesse Barnes 
9667648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
96720e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
968f97108d1SJesse Barnes 
969d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
9709270388eSDaniel Vetter 
971f97108d1SJesse Barnes 	return;
972f97108d1SJesse Barnes }
973f97108d1SJesse Barnes 
974549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
975a4872ba6SOscar Mateo 			struct intel_engine_cs *ring)
976549f7365SChris Wilson {
97793b0a4e0SOscar Mateo 	if (!intel_ring_initialized(ring))
978475553deSChris Wilson 		return;
979475553deSChris Wilson 
980814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
9819862e600SChris Wilson 
98284c33a64SSourab Gupta 	if (drm_core_check_feature(dev, DRIVER_MODESET))
98384c33a64SSourab Gupta 		intel_notify_mmio_flip(ring);
98484c33a64SSourab Gupta 
985549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
98610cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
987549f7365SChris Wilson }
988549f7365SChris Wilson 
98931685c25SDeepak S static u32 vlv_c0_residency(struct drm_i915_private *dev_priv,
990bf225f20SChris Wilson 			    struct intel_rps_ei *rps_ei)
99131685c25SDeepak S {
99231685c25SDeepak S 	u32 cz_ts, cz_freq_khz;
99331685c25SDeepak S 	u32 render_count, media_count;
99431685c25SDeepak S 	u32 elapsed_render, elapsed_media, elapsed_time;
99531685c25SDeepak S 	u32 residency = 0;
99631685c25SDeepak S 
99731685c25SDeepak S 	cz_ts = vlv_punit_read(dev_priv, PUNIT_REG_CZ_TIMESTAMP);
99831685c25SDeepak S 	cz_freq_khz = DIV_ROUND_CLOSEST(dev_priv->mem_freq * 1000, 4);
99931685c25SDeepak S 
100031685c25SDeepak S 	render_count = I915_READ(VLV_RENDER_C0_COUNT_REG);
100131685c25SDeepak S 	media_count = I915_READ(VLV_MEDIA_C0_COUNT_REG);
100231685c25SDeepak S 
1003bf225f20SChris Wilson 	if (rps_ei->cz_clock == 0) {
1004bf225f20SChris Wilson 		rps_ei->cz_clock = cz_ts;
1005bf225f20SChris Wilson 		rps_ei->render_c0 = render_count;
1006bf225f20SChris Wilson 		rps_ei->media_c0 = media_count;
100731685c25SDeepak S 
100831685c25SDeepak S 		return dev_priv->rps.cur_freq;
100931685c25SDeepak S 	}
101031685c25SDeepak S 
1011bf225f20SChris Wilson 	elapsed_time = cz_ts - rps_ei->cz_clock;
1012bf225f20SChris Wilson 	rps_ei->cz_clock = cz_ts;
101331685c25SDeepak S 
1014bf225f20SChris Wilson 	elapsed_render = render_count - rps_ei->render_c0;
1015bf225f20SChris Wilson 	rps_ei->render_c0 = render_count;
101631685c25SDeepak S 
1017bf225f20SChris Wilson 	elapsed_media = media_count - rps_ei->media_c0;
1018bf225f20SChris Wilson 	rps_ei->media_c0 = media_count;
101931685c25SDeepak S 
102031685c25SDeepak S 	/* Convert all the counters into common unit of milli sec */
102131685c25SDeepak S 	elapsed_time /= VLV_CZ_CLOCK_TO_MILLI_SEC;
102231685c25SDeepak S 	elapsed_render /=  cz_freq_khz;
102331685c25SDeepak S 	elapsed_media /= cz_freq_khz;
102431685c25SDeepak S 
102531685c25SDeepak S 	/*
102631685c25SDeepak S 	 * Calculate overall C0 residency percentage
102731685c25SDeepak S 	 * only if elapsed time is non zero
102831685c25SDeepak S 	 */
102931685c25SDeepak S 	if (elapsed_time) {
103031685c25SDeepak S 		residency =
103131685c25SDeepak S 			((max(elapsed_render, elapsed_media) * 100)
103231685c25SDeepak S 				/ elapsed_time);
103331685c25SDeepak S 	}
103431685c25SDeepak S 
103531685c25SDeepak S 	return residency;
103631685c25SDeepak S }
103731685c25SDeepak S 
103831685c25SDeepak S /**
103931685c25SDeepak S  * vlv_calc_delay_from_C0_counters - Increase/Decrease freq based on GPU
104031685c25SDeepak S  * busy-ness calculated from C0 counters of render & media power wells
104131685c25SDeepak S  * @dev_priv: DRM device private
104231685c25SDeepak S  *
104331685c25SDeepak S  */
10444fa79042SDamien Lespiau static int vlv_calc_delay_from_C0_counters(struct drm_i915_private *dev_priv)
104531685c25SDeepak S {
104631685c25SDeepak S 	u32 residency_C0_up = 0, residency_C0_down = 0;
10474fa79042SDamien Lespiau 	int new_delay, adj;
104831685c25SDeepak S 
104931685c25SDeepak S 	dev_priv->rps.ei_interrupt_count++;
105031685c25SDeepak S 
105131685c25SDeepak S 	WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
105231685c25SDeepak S 
105331685c25SDeepak S 
1054bf225f20SChris Wilson 	if (dev_priv->rps.up_ei.cz_clock == 0) {
1055bf225f20SChris Wilson 		vlv_c0_residency(dev_priv, &dev_priv->rps.up_ei);
1056bf225f20SChris Wilson 		vlv_c0_residency(dev_priv, &dev_priv->rps.down_ei);
105731685c25SDeepak S 		return dev_priv->rps.cur_freq;
105831685c25SDeepak S 	}
105931685c25SDeepak S 
106031685c25SDeepak S 
106131685c25SDeepak S 	/*
106231685c25SDeepak S 	 * To down throttle, C0 residency should be less than down threshold
106331685c25SDeepak S 	 * for continous EI intervals. So calculate down EI counters
106431685c25SDeepak S 	 * once in VLV_INT_COUNT_FOR_DOWN_EI
106531685c25SDeepak S 	 */
106631685c25SDeepak S 	if (dev_priv->rps.ei_interrupt_count == VLV_INT_COUNT_FOR_DOWN_EI) {
106731685c25SDeepak S 
106831685c25SDeepak S 		dev_priv->rps.ei_interrupt_count = 0;
106931685c25SDeepak S 
107031685c25SDeepak S 		residency_C0_down = vlv_c0_residency(dev_priv,
1071bf225f20SChris Wilson 						     &dev_priv->rps.down_ei);
107231685c25SDeepak S 	} else {
107331685c25SDeepak S 		residency_C0_up = vlv_c0_residency(dev_priv,
1074bf225f20SChris Wilson 						   &dev_priv->rps.up_ei);
107531685c25SDeepak S 	}
107631685c25SDeepak S 
107731685c25SDeepak S 	new_delay = dev_priv->rps.cur_freq;
107831685c25SDeepak S 
107931685c25SDeepak S 	adj = dev_priv->rps.last_adj;
108031685c25SDeepak S 	/* C0 residency is greater than UP threshold. Increase Frequency */
108131685c25SDeepak S 	if (residency_C0_up >= VLV_RP_UP_EI_THRESHOLD) {
108231685c25SDeepak S 		if (adj > 0)
108331685c25SDeepak S 			adj *= 2;
108431685c25SDeepak S 		else
108531685c25SDeepak S 			adj = 1;
108631685c25SDeepak S 
108731685c25SDeepak S 		if (dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit)
108831685c25SDeepak S 			new_delay = dev_priv->rps.cur_freq + adj;
108931685c25SDeepak S 
109031685c25SDeepak S 		/*
109131685c25SDeepak S 		 * For better performance, jump directly
109231685c25SDeepak S 		 * to RPe if we're below it.
109331685c25SDeepak S 		 */
109431685c25SDeepak S 		if (new_delay < dev_priv->rps.efficient_freq)
109531685c25SDeepak S 			new_delay = dev_priv->rps.efficient_freq;
109631685c25SDeepak S 
109731685c25SDeepak S 	} else if (!dev_priv->rps.ei_interrupt_count &&
109831685c25SDeepak S 			(residency_C0_down < VLV_RP_DOWN_EI_THRESHOLD)) {
109931685c25SDeepak S 		if (adj < 0)
110031685c25SDeepak S 			adj *= 2;
110131685c25SDeepak S 		else
110231685c25SDeepak S 			adj = -1;
110331685c25SDeepak S 		/*
110431685c25SDeepak S 		 * This means, C0 residency is less than down threshold over
110531685c25SDeepak S 		 * a period of VLV_INT_COUNT_FOR_DOWN_EI. So, reduce the freq
110631685c25SDeepak S 		 */
110731685c25SDeepak S 		if (dev_priv->rps.cur_freq > dev_priv->rps.min_freq_softlimit)
110831685c25SDeepak S 			new_delay = dev_priv->rps.cur_freq + adj;
110931685c25SDeepak S 	}
111031685c25SDeepak S 
111131685c25SDeepak S 	return new_delay;
111231685c25SDeepak S }
111331685c25SDeepak S 
11144912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
11153b8d8d91SJesse Barnes {
11162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11172d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1118edbfdb45SPaulo Zanoni 	u32 pm_iir;
1119dd75fdc8SChris Wilson 	int new_delay, adj;
11203b8d8d91SJesse Barnes 
112159cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1122c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1123c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
11246af257cdSDamien Lespiau 	if (INTEL_INFO(dev_priv->dev)->gen >= 8)
1125480c8033SDaniel Vetter 		gen8_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
11260961021aSBen Widawsky 	else {
11270961021aSBen Widawsky 		/* Make sure not to corrupt PMIMR state used by ringbuffer */
1128480c8033SDaniel Vetter 		gen6_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
11290961021aSBen Widawsky 	}
113059cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
11314912d041SBen Widawsky 
113260611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1133a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
113460611c13SPaulo Zanoni 
1135a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
11363b8d8d91SJesse Barnes 		return;
11373b8d8d91SJesse Barnes 
11384fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
11397b9e0ae6SChris Wilson 
1140dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
11417425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1142dd75fdc8SChris Wilson 		if (adj > 0)
1143dd75fdc8SChris Wilson 			adj *= 2;
114413a5660cSDeepak S 		else {
114513a5660cSDeepak S 			/* CHV needs even encode values */
114613a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? 2 : 1;
114713a5660cSDeepak S 		}
1148b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
11497425034aSVille Syrjälä 
11507425034aSVille Syrjälä 		/*
11517425034aSVille Syrjälä 		 * For better performance, jump directly
11527425034aSVille Syrjälä 		 * to RPe if we're below it.
11537425034aSVille Syrjälä 		 */
1154b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1155b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1156dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1157b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1158b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1159dd75fdc8SChris Wilson 		else
1160b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1161dd75fdc8SChris Wilson 		adj = 0;
116231685c25SDeepak S 	} else if (pm_iir & GEN6_PM_RP_UP_EI_EXPIRED) {
116331685c25SDeepak S 		new_delay = vlv_calc_delay_from_C0_counters(dev_priv);
1164dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1165dd75fdc8SChris Wilson 		if (adj < 0)
1166dd75fdc8SChris Wilson 			adj *= 2;
116713a5660cSDeepak S 		else {
116813a5660cSDeepak S 			/* CHV needs even encode values */
116913a5660cSDeepak S 			adj = IS_CHERRYVIEW(dev_priv->dev) ? -2 : -1;
117013a5660cSDeepak S 		}
1171b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1172dd75fdc8SChris Wilson 	} else { /* unknown event */
1173b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1174dd75fdc8SChris Wilson 	}
11753b8d8d91SJesse Barnes 
117679249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
117779249636SBen Widawsky 	 * interrupt
117879249636SBen Widawsky 	 */
11791272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1180b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1181b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
118227544369SDeepak S 
1183b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1184dd75fdc8SChris Wilson 
11850a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
11860a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
11870a073b84SJesse Barnes 	else
11884912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
11893b8d8d91SJesse Barnes 
11904fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
11913b8d8d91SJesse Barnes }
11923b8d8d91SJesse Barnes 
1193e3689190SBen Widawsky 
1194e3689190SBen Widawsky /**
1195e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1196e3689190SBen Widawsky  * occurred.
1197e3689190SBen Widawsky  * @work: workqueue struct
1198e3689190SBen Widawsky  *
1199e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1200e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1201e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1202e3689190SBen Widawsky  */
1203e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1204e3689190SBen Widawsky {
12052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12062d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1207e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
120835a85ac6SBen Widawsky 	char *parity_event[6];
1209e3689190SBen Widawsky 	uint32_t misccpctl;
121035a85ac6SBen Widawsky 	uint8_t slice = 0;
1211e3689190SBen Widawsky 
1212e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1213e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1214e3689190SBen Widawsky 	 * any time we access those registers.
1215e3689190SBen Widawsky 	 */
1216e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1217e3689190SBen Widawsky 
121835a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
121935a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
122035a85ac6SBen Widawsky 		goto out;
122135a85ac6SBen Widawsky 
1222e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1223e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1224e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1225e3689190SBen Widawsky 
122635a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
122735a85ac6SBen Widawsky 		u32 reg;
122835a85ac6SBen Widawsky 
122935a85ac6SBen Widawsky 		slice--;
123035a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
123135a85ac6SBen Widawsky 			break;
123235a85ac6SBen Widawsky 
123335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
123435a85ac6SBen Widawsky 
123535a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
123635a85ac6SBen Widawsky 
123735a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1238e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1239e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1240e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1241e3689190SBen Widawsky 
124235a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
124335a85ac6SBen Widawsky 		POSTING_READ(reg);
1244e3689190SBen Widawsky 
1245cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1246e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1247e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1248e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
124935a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
125035a85ac6SBen Widawsky 		parity_event[5] = NULL;
1251e3689190SBen Widawsky 
12525bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1253e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1254e3689190SBen Widawsky 
125535a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
125635a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1257e3689190SBen Widawsky 
125835a85ac6SBen Widawsky 		kfree(parity_event[4]);
1259e3689190SBen Widawsky 		kfree(parity_event[3]);
1260e3689190SBen Widawsky 		kfree(parity_event[2]);
1261e3689190SBen Widawsky 		kfree(parity_event[1]);
1262e3689190SBen Widawsky 	}
1263e3689190SBen Widawsky 
126435a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
126535a85ac6SBen Widawsky 
126635a85ac6SBen Widawsky out:
126735a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
12684cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1269480c8033SDaniel Vetter 	gen5_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
12704cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
127135a85ac6SBen Widawsky 
127235a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
127335a85ac6SBen Widawsky }
127435a85ac6SBen Widawsky 
127535a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1276e3689190SBen Widawsky {
12772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1278e3689190SBen Widawsky 
1279040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1280e3689190SBen Widawsky 		return;
1281e3689190SBen Widawsky 
1282d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1283480c8033SDaniel Vetter 	gen5_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1284d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1285e3689190SBen Widawsky 
128635a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
128735a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
128835a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
128935a85ac6SBen Widawsky 
129035a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
129135a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
129235a85ac6SBen Widawsky 
1293a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1294e3689190SBen Widawsky }
1295e3689190SBen Widawsky 
1296f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1297f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1298f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1299f1af8fc1SPaulo Zanoni {
1300f1af8fc1SPaulo Zanoni 	if (gt_iir &
1301f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1302f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1303f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1304f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1305f1af8fc1SPaulo Zanoni }
1306f1af8fc1SPaulo Zanoni 
1307e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1308e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1309e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1310e7b4c6b1SDaniel Vetter {
1311e7b4c6b1SDaniel Vetter 
1312cc609d5dSBen Widawsky 	if (gt_iir &
1313cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1314e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1315cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1316e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1317cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1318e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1319e7b4c6b1SDaniel Vetter 
1320cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1321cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1322cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
132358174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
132458174462SMika Kuoppala 				  gt_iir);
1325e7b4c6b1SDaniel Vetter 	}
1326e3689190SBen Widawsky 
132735a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
132835a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1329e7b4c6b1SDaniel Vetter }
1330e7b4c6b1SDaniel Vetter 
13310961021aSBen Widawsky static void gen8_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
13320961021aSBen Widawsky {
13330961021aSBen Widawsky 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
13340961021aSBen Widawsky 		return;
13350961021aSBen Widawsky 
13360961021aSBen Widawsky 	spin_lock(&dev_priv->irq_lock);
13370961021aSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1338480c8033SDaniel Vetter 	gen8_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
13390961021aSBen Widawsky 	spin_unlock(&dev_priv->irq_lock);
13400961021aSBen Widawsky 
13410961021aSBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->rps.work);
13420961021aSBen Widawsky }
13430961021aSBen Widawsky 
1344abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1345abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1346abd58f01SBen Widawsky 				       u32 master_ctl)
1347abd58f01SBen Widawsky {
1348e981e7b1SThomas Daniel 	struct intel_engine_cs *ring;
1349abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1350abd58f01SBen Widawsky 	uint32_t tmp = 0;
1351abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1352abd58f01SBen Widawsky 
1353abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1354abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1355abd58f01SBen Widawsky 		if (tmp) {
135638cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1357abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1358e981e7b1SThomas Daniel 
1359abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1360e981e7b1SThomas Daniel 			ring = &dev_priv->ring[RCS];
1361abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1362e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1363e981e7b1SThomas Daniel 			if (rcs & GT_CONTEXT_SWITCH_INTERRUPT)
1364e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1365e981e7b1SThomas Daniel 
1366e981e7b1SThomas Daniel 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1367e981e7b1SThomas Daniel 			ring = &dev_priv->ring[BCS];
1368abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1369e981e7b1SThomas Daniel 				notify_ring(dev, ring);
1370e981e7b1SThomas Daniel 			if (bcs & GT_CONTEXT_SWITCH_INTERRUPT)
1371e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1372abd58f01SBen Widawsky 		} else
1373abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1374abd58f01SBen Widawsky 	}
1375abd58f01SBen Widawsky 
137685f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1377abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1378abd58f01SBen Widawsky 		if (tmp) {
137938cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1380abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1381e981e7b1SThomas Daniel 
1382abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1383e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS];
1384abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1385e981e7b1SThomas Daniel 				notify_ring(dev, ring);
138673d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1387e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1388e981e7b1SThomas Daniel 
138985f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
1390e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VCS2];
139185f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
1392e981e7b1SThomas Daniel 				notify_ring(dev, ring);
139373d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1394e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1395abd58f01SBen Widawsky 		} else
1396abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1397abd58f01SBen Widawsky 	}
1398abd58f01SBen Widawsky 
13990961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
14000961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
14010961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
14020961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
14030961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
140438cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
140538cc46d7SOscar Mateo 			gen8_rps_irq_handler(dev_priv, tmp);
14060961021aSBen Widawsky 		} else
14070961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
14080961021aSBen Widawsky 	}
14090961021aSBen Widawsky 
1410abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1411abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1412abd58f01SBen Widawsky 		if (tmp) {
141338cc46d7SOscar Mateo 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1414abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1415e981e7b1SThomas Daniel 
1416abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1417e981e7b1SThomas Daniel 			ring = &dev_priv->ring[VECS];
1418abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1419e981e7b1SThomas Daniel 				notify_ring(dev, ring);
142073d477f6SOscar Mateo 			if (vcs & GT_CONTEXT_SWITCH_INTERRUPT)
1421e981e7b1SThomas Daniel 				intel_execlists_handle_ctx_events(ring);
1422abd58f01SBen Widawsky 		} else
1423abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1424abd58f01SBen Widawsky 	}
1425abd58f01SBen Widawsky 
1426abd58f01SBen Widawsky 	return ret;
1427abd58f01SBen Widawsky }
1428abd58f01SBen Widawsky 
1429b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1430b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1431b543fb04SEgbert Eich 
143207c338ceSJani Nikula static int pch_port_to_hotplug_shift(enum port port)
143313cf5504SDave Airlie {
143413cf5504SDave Airlie 	switch (port) {
143513cf5504SDave Airlie 	case PORT_A:
143613cf5504SDave Airlie 	case PORT_E:
143713cf5504SDave Airlie 	default:
143813cf5504SDave Airlie 		return -1;
143913cf5504SDave Airlie 	case PORT_B:
144013cf5504SDave Airlie 		return 0;
144113cf5504SDave Airlie 	case PORT_C:
144213cf5504SDave Airlie 		return 8;
144313cf5504SDave Airlie 	case PORT_D:
144413cf5504SDave Airlie 		return 16;
144513cf5504SDave Airlie 	}
144613cf5504SDave Airlie }
144713cf5504SDave Airlie 
144807c338ceSJani Nikula static int i915_port_to_hotplug_shift(enum port port)
144913cf5504SDave Airlie {
145013cf5504SDave Airlie 	switch (port) {
145113cf5504SDave Airlie 	case PORT_A:
145213cf5504SDave Airlie 	case PORT_E:
145313cf5504SDave Airlie 	default:
145413cf5504SDave Airlie 		return -1;
145513cf5504SDave Airlie 	case PORT_B:
145613cf5504SDave Airlie 		return 17;
145713cf5504SDave Airlie 	case PORT_C:
145813cf5504SDave Airlie 		return 19;
145913cf5504SDave Airlie 	case PORT_D:
146013cf5504SDave Airlie 		return 21;
146113cf5504SDave Airlie 	}
146213cf5504SDave Airlie }
146313cf5504SDave Airlie 
146413cf5504SDave Airlie static inline enum port get_port_from_pin(enum hpd_pin pin)
146513cf5504SDave Airlie {
146613cf5504SDave Airlie 	switch (pin) {
146713cf5504SDave Airlie 	case HPD_PORT_B:
146813cf5504SDave Airlie 		return PORT_B;
146913cf5504SDave Airlie 	case HPD_PORT_C:
147013cf5504SDave Airlie 		return PORT_C;
147113cf5504SDave Airlie 	case HPD_PORT_D:
147213cf5504SDave Airlie 		return PORT_D;
147313cf5504SDave Airlie 	default:
147413cf5504SDave Airlie 		return PORT_A; /* no hpd */
147513cf5504SDave Airlie 	}
147613cf5504SDave Airlie }
147713cf5504SDave Airlie 
147810a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1479b543fb04SEgbert Eich 					 u32 hotplug_trigger,
148013cf5504SDave Airlie 					 u32 dig_hotplug_reg,
1481b543fb04SEgbert Eich 					 const u32 *hpd)
1482b543fb04SEgbert Eich {
14832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1484b543fb04SEgbert Eich 	int i;
148513cf5504SDave Airlie 	enum port port;
148610a504deSDaniel Vetter 	bool storm_detected = false;
148713cf5504SDave Airlie 	bool queue_dig = false, queue_hp = false;
148813cf5504SDave Airlie 	u32 dig_shift;
148913cf5504SDave Airlie 	u32 dig_port_mask = 0;
1490b543fb04SEgbert Eich 
149191d131d2SDaniel Vetter 	if (!hotplug_trigger)
149291d131d2SDaniel Vetter 		return;
149391d131d2SDaniel Vetter 
149413cf5504SDave Airlie 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x, dig 0x%08x\n",
149513cf5504SDave Airlie 			 hotplug_trigger, dig_hotplug_reg);
1496cc9bd499SImre Deak 
1497b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1498b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
149913cf5504SDave Airlie 		if (!(hpd[i] & hotplug_trigger))
150013cf5504SDave Airlie 			continue;
1501821450c6SEgbert Eich 
150213cf5504SDave Airlie 		port = get_port_from_pin(i);
150313cf5504SDave Airlie 		if (port && dev_priv->hpd_irq_port[port]) {
150413cf5504SDave Airlie 			bool long_hpd;
150513cf5504SDave Airlie 
150607c338ceSJani Nikula 			if (HAS_PCH_SPLIT(dev)) {
150707c338ceSJani Nikula 				dig_shift = pch_port_to_hotplug_shift(port);
150813cf5504SDave Airlie 				long_hpd = (dig_hotplug_reg >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
150907c338ceSJani Nikula 			} else {
151007c338ceSJani Nikula 				dig_shift = i915_port_to_hotplug_shift(port);
151107c338ceSJani Nikula 				long_hpd = (hotplug_trigger >> dig_shift) & PORTB_HOTPLUG_LONG_DETECT;
151213cf5504SDave Airlie 			}
151313cf5504SDave Airlie 
151426fbb774SVille Syrjälä 			DRM_DEBUG_DRIVER("digital hpd port %c - %s\n",
151526fbb774SVille Syrjälä 					 port_name(port),
151626fbb774SVille Syrjälä 					 long_hpd ? "long" : "short");
151713cf5504SDave Airlie 			/* for long HPD pulses we want to have the digital queue happen,
151813cf5504SDave Airlie 			   but we still want HPD storm detection to function. */
151913cf5504SDave Airlie 			if (long_hpd) {
152013cf5504SDave Airlie 				dev_priv->long_hpd_port_mask |= (1 << port);
152113cf5504SDave Airlie 				dig_port_mask |= hpd[i];
152213cf5504SDave Airlie 			} else {
152313cf5504SDave Airlie 				/* for short HPD just trigger the digital queue */
152413cf5504SDave Airlie 				dev_priv->short_hpd_port_mask |= (1 << port);
152513cf5504SDave Airlie 				hotplug_trigger &= ~hpd[i];
152613cf5504SDave Airlie 			}
152713cf5504SDave Airlie 			queue_dig = true;
152813cf5504SDave Airlie 		}
152913cf5504SDave Airlie 	}
153013cf5504SDave Airlie 
153113cf5504SDave Airlie 	for (i = 1; i < HPD_NUM_PINS; i++) {
15323ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
15333ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
15343ff04a16SDaniel Vetter 			/*
15353ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
15363ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
15373ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
15383ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
15393ff04a16SDaniel Vetter 			 */
15403ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1541cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1542cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1543b8f102e8SEgbert Eich 
15443ff04a16SDaniel Vetter 			continue;
15453ff04a16SDaniel Vetter 		}
15463ff04a16SDaniel Vetter 
1547b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1548b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1549b543fb04SEgbert Eich 			continue;
1550b543fb04SEgbert Eich 
155113cf5504SDave Airlie 		if (!(dig_port_mask & hpd[i])) {
1552bc5ead8cSJani Nikula 			dev_priv->hpd_event_bits |= (1 << i);
155313cf5504SDave Airlie 			queue_hp = true;
155413cf5504SDave Airlie 		}
155513cf5504SDave Airlie 
1556b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1557b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1558b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1559b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1560b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1561b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1562b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1563b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1564142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1565b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
156610a504deSDaniel Vetter 			storm_detected = true;
1567b543fb04SEgbert Eich 		} else {
1568b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1569b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1570b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1571b543fb04SEgbert Eich 		}
1572b543fb04SEgbert Eich 	}
1573b543fb04SEgbert Eich 
157410a504deSDaniel Vetter 	if (storm_detected)
157510a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1576b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
15775876fa0dSDaniel Vetter 
1578645416f5SDaniel Vetter 	/*
1579645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1580645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1581645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1582645416f5SDaniel Vetter 	 * deadlock.
1583645416f5SDaniel Vetter 	 */
158413cf5504SDave Airlie 	if (queue_dig)
15850e32b39cSDave Airlie 		queue_work(dev_priv->dp_wq, &dev_priv->dig_port_work);
158613cf5504SDave Airlie 	if (queue_hp)
1587645416f5SDaniel Vetter 		schedule_work(&dev_priv->hotplug_work);
1588b543fb04SEgbert Eich }
1589b543fb04SEgbert Eich 
1590515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1591515ac2bbSDaniel Vetter {
15922d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
159328c70f16SDaniel Vetter 
159428c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1595515ac2bbSDaniel Vetter }
1596515ac2bbSDaniel Vetter 
1597ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1598ce99c256SDaniel Vetter {
15992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
16009ee32feaSDaniel Vetter 
16019ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1602ce99c256SDaniel Vetter }
1603ce99c256SDaniel Vetter 
16048bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1605277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1606eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1607eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
16088bc5e955SDaniel Vetter 					 uint32_t crc4)
16098bf1e9f1SShuang He {
16108bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
16118bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
16128bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1613ac2300d4SDamien Lespiau 	int head, tail;
1614b2c88f5bSDamien Lespiau 
1615d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1616d538bbdfSDamien Lespiau 
16170c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1618d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
16190c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
16200c912c79SDamien Lespiau 		return;
16210c912c79SDamien Lespiau 	}
16220c912c79SDamien Lespiau 
1623d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1624d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1625b2c88f5bSDamien Lespiau 
1626b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1627d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1628b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1629b2c88f5bSDamien Lespiau 		return;
1630b2c88f5bSDamien Lespiau 	}
1631b2c88f5bSDamien Lespiau 
1632b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
16338bf1e9f1SShuang He 
16348bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1635eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1636eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1637eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1638eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1639eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1640b2c88f5bSDamien Lespiau 
1641b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1642d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1643d538bbdfSDamien Lespiau 
1644d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
164507144428SDamien Lespiau 
164607144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
16478bf1e9f1SShuang He }
1648277de95eSDaniel Vetter #else
1649277de95eSDaniel Vetter static inline void
1650277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1651277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1652277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1653277de95eSDaniel Vetter 			     uint32_t crc4) {}
1654277de95eSDaniel Vetter #endif
1655eba94eb9SDaniel Vetter 
1656277de95eSDaniel Vetter 
1657277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16585a69b89fSDaniel Vetter {
16595a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16605a69b89fSDaniel Vetter 
1661277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16625a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16635a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16645a69b89fSDaniel Vetter }
16655a69b89fSDaniel Vetter 
1666277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1667eba94eb9SDaniel Vetter {
1668eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1669eba94eb9SDaniel Vetter 
1670277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1671eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1672eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1673eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1674eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16758bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1676eba94eb9SDaniel Vetter }
16775b3a856bSDaniel Vetter 
1678277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16795b3a856bSDaniel Vetter {
16805b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16810b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16820b5c5ed0SDaniel Vetter 
16830b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16840b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16850b5c5ed0SDaniel Vetter 	else
16860b5c5ed0SDaniel Vetter 		res1 = 0;
16870b5c5ed0SDaniel Vetter 
16880b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16890b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16900b5c5ed0SDaniel Vetter 	else
16910b5c5ed0SDaniel Vetter 		res2 = 0;
16925b3a856bSDaniel Vetter 
1693277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16940b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16950b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16960b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16970b5c5ed0SDaniel Vetter 				     res1, res2);
16985b3a856bSDaniel Vetter }
16998bf1e9f1SShuang He 
17001403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17011403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17021403c0d4SPaulo Zanoni  * the work queue. */
17031403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1704baf02a1fSBen Widawsky {
1705a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
170659cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1707a6706b45SDeepak S 		dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1708480c8033SDaniel Vetter 		gen6_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
170959cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
17102adbee62SDaniel Vetter 
17112adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
171241a05a3aSDaniel Vetter 	}
1713baf02a1fSBen Widawsky 
17141403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
171512638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
171612638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
171712638c57SBen Widawsky 
171812638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
171958174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
172058174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
172158174462SMika Kuoppala 					  pm_iir);
172212638c57SBen Widawsky 		}
172312638c57SBen Widawsky 	}
17241403c0d4SPaulo Zanoni }
1725baf02a1fSBen Widawsky 
17268d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
17278d7849dbSVille Syrjälä {
17288d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
17298d7849dbSVille Syrjälä 		return false;
17308d7849dbSVille Syrjälä 
17318d7849dbSVille Syrjälä 	return true;
17328d7849dbSVille Syrjälä }
17338d7849dbSVille Syrjälä 
1734c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
17357e231dbeSJesse Barnes {
1736c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
173791d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
17387e231dbeSJesse Barnes 	int pipe;
17397e231dbeSJesse Barnes 
174058ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
1741055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
174291d181ddSImre Deak 		int reg;
1743bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
174491d181ddSImre Deak 
1745bbb5eebfSDaniel Vetter 		/*
1746bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1747bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1748bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1749bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1750bbb5eebfSDaniel Vetter 		 * handle.
1751bbb5eebfSDaniel Vetter 		 */
1752bbb5eebfSDaniel Vetter 		mask = 0;
1753*a72e4c9fSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev_priv, pipe))
1754bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
1755bbb5eebfSDaniel Vetter 
1756bbb5eebfSDaniel Vetter 		switch (pipe) {
1757bbb5eebfSDaniel Vetter 		case PIPE_A:
1758bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1759bbb5eebfSDaniel Vetter 			break;
1760bbb5eebfSDaniel Vetter 		case PIPE_B:
1761bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1762bbb5eebfSDaniel Vetter 			break;
17633278f67fSVille Syrjälä 		case PIPE_C:
17643278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17653278f67fSVille Syrjälä 			break;
1766bbb5eebfSDaniel Vetter 		}
1767bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1768bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1769bbb5eebfSDaniel Vetter 
1770bbb5eebfSDaniel Vetter 		if (!mask)
177191d181ddSImre Deak 			continue;
177291d181ddSImre Deak 
177391d181ddSImre Deak 		reg = PIPESTAT(pipe);
1774bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1775bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17767e231dbeSJesse Barnes 
17777e231dbeSJesse Barnes 		/*
17787e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17797e231dbeSJesse Barnes 		 */
178091d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
178191d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17827e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17837e231dbeSJesse Barnes 	}
178458ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17857e231dbeSJesse Barnes 
1786055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
1787d6bbafa1SChris Wilson 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
1788d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
1789d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
179031acc7f5SJesse Barnes 
1791579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
179231acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
179331acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
179431acc7f5SJesse Barnes 		}
17954356d586SDaniel Vetter 
17964356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1797277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
17982d9d2b0bSVille Syrjälä 
17992d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
1800*a72e4c9fSDaniel Vetter 		    intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe,
1801*a72e4c9fSDaniel Vetter 							  false))
1802fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
180331acc7f5SJesse Barnes 	}
180431acc7f5SJesse Barnes 
1805c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1806c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1807c1874ed7SImre Deak }
1808c1874ed7SImre Deak 
180916c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
181016c6c56bSVille Syrjälä {
181116c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
181216c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
181316c6c56bSVille Syrjälä 
18143ff60f89SOscar Mateo 	if (hotplug_status) {
18153ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18163ff60f89SOscar Mateo 		/*
18173ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
18183ff60f89SOscar Mateo 		 * may miss hotplug events.
18193ff60f89SOscar Mateo 		 */
18203ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
18213ff60f89SOscar Mateo 
182216c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
182316c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
182416c6c56bSVille Syrjälä 
182513cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
182616c6c56bSVille Syrjälä 		} else {
182716c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
182816c6c56bSVille Syrjälä 
182913cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
183016c6c56bSVille Syrjälä 		}
183116c6c56bSVille Syrjälä 
183216c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
183316c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
183416c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
18353ff60f89SOscar Mateo 	}
183616c6c56bSVille Syrjälä }
183716c6c56bSVille Syrjälä 
1838c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1839c1874ed7SImre Deak {
184045a83f84SDaniel Vetter 	struct drm_device *dev = arg;
18412d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1842c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1843c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1844c1874ed7SImre Deak 
1845c1874ed7SImre Deak 	while (true) {
18463ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
18473ff60f89SOscar Mateo 
1848c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
18493ff60f89SOscar Mateo 		if (gt_iir)
18503ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
18513ff60f89SOscar Mateo 
1852c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18533ff60f89SOscar Mateo 		if (pm_iir)
18543ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
18553ff60f89SOscar Mateo 
18563ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
18573ff60f89SOscar Mateo 		if (iir) {
18583ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
18593ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
18603ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
18613ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
18623ff60f89SOscar Mateo 		}
1863c1874ed7SImre Deak 
1864c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1865c1874ed7SImre Deak 			goto out;
1866c1874ed7SImre Deak 
1867c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1868c1874ed7SImre Deak 
18693ff60f89SOscar Mateo 		if (gt_iir)
1870c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
187160611c13SPaulo Zanoni 		if (pm_iir)
1872d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18733ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18743ff60f89SOscar Mateo 		 * signalled in iir */
18753ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18767e231dbeSJesse Barnes 	}
18777e231dbeSJesse Barnes 
18787e231dbeSJesse Barnes out:
18797e231dbeSJesse Barnes 	return ret;
18807e231dbeSJesse Barnes }
18817e231dbeSJesse Barnes 
188243f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
188343f328d7SVille Syrjälä {
188445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
188543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
188643f328d7SVille Syrjälä 	u32 master_ctl, iir;
188743f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
188843f328d7SVille Syrjälä 
18898e5fd599SVille Syrjälä 	for (;;) {
18908e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18913278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18923278f67fSVille Syrjälä 
18933278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18948e5fd599SVille Syrjälä 			break;
189543f328d7SVille Syrjälä 
189627b6c122SOscar Mateo 		ret = IRQ_HANDLED;
189727b6c122SOscar Mateo 
189843f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
189943f328d7SVille Syrjälä 
190027b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
190127b6c122SOscar Mateo 
190227b6c122SOscar Mateo 		if (iir) {
190327b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
190427b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
190527b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
190627b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
190727b6c122SOscar Mateo 		}
190827b6c122SOscar Mateo 
19093278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
191043f328d7SVille Syrjälä 
191127b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
191227b6c122SOscar Mateo 		 * signalled in iir */
19133278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
191443f328d7SVille Syrjälä 
191543f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
191643f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
19178e5fd599SVille Syrjälä 	}
19183278f67fSVille Syrjälä 
191943f328d7SVille Syrjälä 	return ret;
192043f328d7SVille Syrjälä }
192143f328d7SVille Syrjälä 
192223e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1923776ad806SJesse Barnes {
19242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
19259db4a9c7SJesse Barnes 	int pipe;
1926b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
192713cf5504SDave Airlie 	u32 dig_hotplug_reg;
1928776ad806SJesse Barnes 
192913cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
193013cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
193113cf5504SDave Airlie 
193213cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
193391d131d2SDaniel Vetter 
1934cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1935cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1936776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1937cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1938cfc33bf7SVille Syrjälä 				 port_name(port));
1939cfc33bf7SVille Syrjälä 	}
1940776ad806SJesse Barnes 
1941ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1942ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1943ce99c256SDaniel Vetter 
1944776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1945515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1946776ad806SJesse Barnes 
1947776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1948776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1949776ad806SJesse Barnes 
1950776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1951776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1952776ad806SJesse Barnes 
1953776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1954776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1955776ad806SJesse Barnes 
19569db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1957055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19589db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19599db4a9c7SJesse Barnes 					 pipe_name(pipe),
19609db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1961776ad806SJesse Barnes 
1962776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1963776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1964776ad806SJesse Barnes 
1965776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1966776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1967776ad806SJesse Barnes 
1968776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
1969*a72e4c9fSDaniel Vetter 		if (intel_set_pch_fifo_underrun_reporting(dev_priv,
1970*a72e4c9fSDaniel Vetter 							  TRANSCODER_A,
19718664281bSPaulo Zanoni 							  false))
1972fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
19738664281bSPaulo Zanoni 
19748664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
1975*a72e4c9fSDaniel Vetter 		if (intel_set_pch_fifo_underrun_reporting(dev_priv,
1976*a72e4c9fSDaniel Vetter 							  TRANSCODER_B,
19778664281bSPaulo Zanoni 							  false))
1978fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
19798664281bSPaulo Zanoni }
19808664281bSPaulo Zanoni 
19818664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19828664281bSPaulo Zanoni {
19838664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19848664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19855a69b89fSDaniel Vetter 	enum pipe pipe;
19868664281bSPaulo Zanoni 
1987de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1988de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1989de032bf4SPaulo Zanoni 
1990055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19915a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
1992*a72e4c9fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe,
19935a69b89fSDaniel Vetter 								  false))
1994fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
19955a69b89fSDaniel Vetter 					  pipe_name(pipe));
19965a69b89fSDaniel Vetter 		}
19978664281bSPaulo Zanoni 
19985a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19995a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
2000277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
20015a69b89fSDaniel Vetter 			else
2002277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
20035a69b89fSDaniel Vetter 		}
20045a69b89fSDaniel Vetter 	}
20058bf1e9f1SShuang He 
20068664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
20078664281bSPaulo Zanoni }
20088664281bSPaulo Zanoni 
20098664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
20108664281bSPaulo Zanoni {
20118664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
20128664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
20138664281bSPaulo Zanoni 
2014de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2015de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2016de032bf4SPaulo Zanoni 
20178664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
2018*a72e4c9fSDaniel Vetter 		if (intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
20198664281bSPaulo Zanoni 							  false))
2020fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
20218664281bSPaulo Zanoni 
20228664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
2023*a72e4c9fSDaniel Vetter 		if (intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_B,
20248664281bSPaulo Zanoni 							  false))
2025fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
20268664281bSPaulo Zanoni 
20278664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
2028*a72e4c9fSDaniel Vetter 		if (intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_C,
20298664281bSPaulo Zanoni 							  false))
2030fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
20318664281bSPaulo Zanoni 
20328664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2033776ad806SJesse Barnes }
2034776ad806SJesse Barnes 
203523e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
203623e81d69SAdam Jackson {
20372d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
203823e81d69SAdam Jackson 	int pipe;
2039b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
204013cf5504SDave Airlie 	u32 dig_hotplug_reg;
204123e81d69SAdam Jackson 
204213cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
204313cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
204413cf5504SDave Airlie 
204513cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
204691d131d2SDaniel Vetter 
2047cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2048cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
204923e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2050cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2051cfc33bf7SVille Syrjälä 				 port_name(port));
2052cfc33bf7SVille Syrjälä 	}
205323e81d69SAdam Jackson 
205423e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2055ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
205623e81d69SAdam Jackson 
205723e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2058515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
205923e81d69SAdam Jackson 
206023e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
206123e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
206223e81d69SAdam Jackson 
206323e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
206423e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
206523e81d69SAdam Jackson 
206623e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2067055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
206823e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
206923e81d69SAdam Jackson 					 pipe_name(pipe),
207023e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20718664281bSPaulo Zanoni 
20728664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20738664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
207423e81d69SAdam Jackson }
207523e81d69SAdam Jackson 
2076c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2077c008bc6eSPaulo Zanoni {
2078c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
207940da17c2SDaniel Vetter 	enum pipe pipe;
2080c008bc6eSPaulo Zanoni 
2081c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2082c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2083c008bc6eSPaulo Zanoni 
2084c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2085c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2086c008bc6eSPaulo Zanoni 
2087c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2088c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2089c008bc6eSPaulo Zanoni 
2090055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2091d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2092d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2093d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2094c008bc6eSPaulo Zanoni 
209540da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
2096*a72e4c9fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev_priv,
2097*a72e4c9fSDaniel Vetter 								  pipe,
2098*a72e4c9fSDaniel Vetter 								  false))
2099fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
210040da17c2SDaniel Vetter 					  pipe_name(pipe));
2101c008bc6eSPaulo Zanoni 
210240da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
210340da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
21045b3a856bSDaniel Vetter 
210540da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
210640da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
210740da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
210840da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2109c008bc6eSPaulo Zanoni 		}
2110c008bc6eSPaulo Zanoni 	}
2111c008bc6eSPaulo Zanoni 
2112c008bc6eSPaulo Zanoni 	/* check event from PCH */
2113c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2114c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2115c008bc6eSPaulo Zanoni 
2116c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2117c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2118c008bc6eSPaulo Zanoni 		else
2119c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2120c008bc6eSPaulo Zanoni 
2121c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2122c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2123c008bc6eSPaulo Zanoni 	}
2124c008bc6eSPaulo Zanoni 
2125c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2126c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2127c008bc6eSPaulo Zanoni }
2128c008bc6eSPaulo Zanoni 
21299719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21309719fb98SPaulo Zanoni {
21319719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
213207d27e20SDamien Lespiau 	enum pipe pipe;
21339719fb98SPaulo Zanoni 
21349719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21359719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21369719fb98SPaulo Zanoni 
21379719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21389719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21399719fb98SPaulo Zanoni 
21409719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21419719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21429719fb98SPaulo Zanoni 
2143055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2144d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2145d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2146d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
214740da17c2SDaniel Vetter 
214840da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
214907d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
215007d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
215107d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21529719fb98SPaulo Zanoni 		}
21539719fb98SPaulo Zanoni 	}
21549719fb98SPaulo Zanoni 
21559719fb98SPaulo Zanoni 	/* check event from PCH */
21569719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21579719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21589719fb98SPaulo Zanoni 
21599719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21609719fb98SPaulo Zanoni 
21619719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21629719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21639719fb98SPaulo Zanoni 	}
21649719fb98SPaulo Zanoni }
21659719fb98SPaulo Zanoni 
216672c90f62SOscar Mateo /*
216772c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
216872c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
216972c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
217072c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
217172c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
217272c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
217372c90f62SOscar Mateo  */
2174f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2175b1f14ad0SJesse Barnes {
217645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21772d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2178f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21790e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2180b1f14ad0SJesse Barnes 
21818664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21828664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2183907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21848664281bSPaulo Zanoni 
2185b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2186b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2187b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
218823a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21890e43406bSChris Wilson 
219044498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
219144498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
219244498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
219344498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
219444498aeaSPaulo Zanoni 	 * due to its back queue). */
2195ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
219644498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
219744498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
219844498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2199ab5c608bSBen Widawsky 	}
220044498aeaSPaulo Zanoni 
220172c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
220272c90f62SOscar Mateo 
22030e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
22040e43406bSChris Wilson 	if (gt_iir) {
220572c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
220672c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2207d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
22080e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2209d8fc8a47SPaulo Zanoni 		else
2210d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
22110e43406bSChris Wilson 	}
2212b1f14ad0SJesse Barnes 
2213b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
22140e43406bSChris Wilson 	if (de_iir) {
221572c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
221672c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2217f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
22189719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2219f1af8fc1SPaulo Zanoni 		else
2220f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
22210e43406bSChris Wilson 	}
22220e43406bSChris Wilson 
2223f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2224f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22250e43406bSChris Wilson 		if (pm_iir) {
2226b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22270e43406bSChris Wilson 			ret = IRQ_HANDLED;
222872c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22290e43406bSChris Wilson 		}
2230f1af8fc1SPaulo Zanoni 	}
2231b1f14ad0SJesse Barnes 
2232b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2233b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2234ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
223544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
223644498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2237ab5c608bSBen Widawsky 	}
2238b1f14ad0SJesse Barnes 
2239b1f14ad0SJesse Barnes 	return ret;
2240b1f14ad0SJesse Barnes }
2241b1f14ad0SJesse Barnes 
2242abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2243abd58f01SBen Widawsky {
2244abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2245abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2246abd58f01SBen Widawsky 	u32 master_ctl;
2247abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2248abd58f01SBen Widawsky 	uint32_t tmp = 0;
2249c42664ccSDaniel Vetter 	enum pipe pipe;
2250abd58f01SBen Widawsky 
2251abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2252abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2253abd58f01SBen Widawsky 	if (!master_ctl)
2254abd58f01SBen Widawsky 		return IRQ_NONE;
2255abd58f01SBen Widawsky 
2256abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2257abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2258abd58f01SBen Widawsky 
225938cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
226038cc46d7SOscar Mateo 
2261abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2262abd58f01SBen Widawsky 
2263abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2264abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2265abd58f01SBen Widawsky 		if (tmp) {
2266abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2267abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
226838cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
226938cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
227038cc46d7SOscar Mateo 			else
227138cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2272abd58f01SBen Widawsky 		}
227338cc46d7SOscar Mateo 		else
227438cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2275abd58f01SBen Widawsky 	}
2276abd58f01SBen Widawsky 
22776d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22786d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22796d766f02SDaniel Vetter 		if (tmp) {
22806d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22816d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
228238cc46d7SOscar Mateo 			if (tmp & GEN8_AUX_CHANNEL_A)
228338cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
228438cc46d7SOscar Mateo 			else
228538cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22866d766f02SDaniel Vetter 		}
228738cc46d7SOscar Mateo 		else
228838cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22896d766f02SDaniel Vetter 	}
22906d766f02SDaniel Vetter 
2291055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2292770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2293abd58f01SBen Widawsky 
2294c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2295c42664ccSDaniel Vetter 			continue;
2296c42664ccSDaniel Vetter 
2297abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
229838cc46d7SOscar Mateo 		if (pipe_iir) {
229938cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
230038cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2301770de83dSDamien Lespiau 
2302d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2303d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2304d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2305abd58f01SBen Widawsky 
2306770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2307770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2308770de83dSDamien Lespiau 			else
2309770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2310770de83dSDamien Lespiau 
2311770de83dSDamien Lespiau 			if (flip_done) {
2312abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2313abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2314abd58f01SBen Widawsky 			}
2315abd58f01SBen Widawsky 
23160fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
23170fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
23180fbe7870SDaniel Vetter 
231938d83c96SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
2320*a72e4c9fSDaniel Vetter 				if (intel_set_cpu_fifo_underrun_reporting(dev_priv,
2321*a72e4c9fSDaniel Vetter 									  pipe,
232238d83c96SDaniel Vetter 									  false))
2323fc2c807bSVille Syrjälä 					DRM_ERROR("Pipe %c FIFO underrun\n",
232438d83c96SDaniel Vetter 						  pipe_name(pipe));
232538d83c96SDaniel Vetter 			}
232638d83c96SDaniel Vetter 
2327770de83dSDamien Lespiau 
2328770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2329770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2330770de83dSDamien Lespiau 			else
2331770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2332770de83dSDamien Lespiau 
2333770de83dSDamien Lespiau 			if (fault_errors)
233430100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
233530100f2bSDaniel Vetter 					  pipe_name(pipe),
233630100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2337c42664ccSDaniel Vetter 		} else
2338abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2339abd58f01SBen Widawsky 	}
2340abd58f01SBen Widawsky 
234192d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
234292d03a80SDaniel Vetter 		/*
234392d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
234492d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
234592d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
234692d03a80SDaniel Vetter 		 */
234792d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
234892d03a80SDaniel Vetter 		if (pch_iir) {
234992d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
235092d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
235138cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
235238cc46d7SOscar Mateo 		} else
235338cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
235438cc46d7SOscar Mateo 
235592d03a80SDaniel Vetter 	}
235692d03a80SDaniel Vetter 
2357abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2358abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2359abd58f01SBen Widawsky 
2360abd58f01SBen Widawsky 	return ret;
2361abd58f01SBen Widawsky }
2362abd58f01SBen Widawsky 
236317e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
236417e1df07SDaniel Vetter 			       bool reset_completed)
236517e1df07SDaniel Vetter {
2366a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
236717e1df07SDaniel Vetter 	int i;
236817e1df07SDaniel Vetter 
236917e1df07SDaniel Vetter 	/*
237017e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
237117e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
237217e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
237317e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
237417e1df07SDaniel Vetter 	 */
237517e1df07SDaniel Vetter 
237617e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
237717e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
237817e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
237917e1df07SDaniel Vetter 
238017e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
238117e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
238217e1df07SDaniel Vetter 
238317e1df07SDaniel Vetter 	/*
238417e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
238517e1df07SDaniel Vetter 	 * reset state is cleared.
238617e1df07SDaniel Vetter 	 */
238717e1df07SDaniel Vetter 	if (reset_completed)
238817e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
238917e1df07SDaniel Vetter }
239017e1df07SDaniel Vetter 
23918a905236SJesse Barnes /**
23928a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
23938a905236SJesse Barnes  * @work: work struct
23948a905236SJesse Barnes  *
23958a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23968a905236SJesse Barnes  * was detected.
23978a905236SJesse Barnes  */
23988a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
23998a905236SJesse Barnes {
24001f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
24011f83fee0SDaniel Vetter 						    work);
24022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
24032d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
24048a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2405cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2406cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2407cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
240817e1df07SDaniel Vetter 	int ret;
24098a905236SJesse Barnes 
24105bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
24118a905236SJesse Barnes 
24127db0ba24SDaniel Vetter 	/*
24137db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
24147db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
24157db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
24167db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
24177db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
24187db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
24197db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
24207db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
24217db0ba24SDaniel Vetter 	 */
24227db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
242344d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
24245bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
24257db0ba24SDaniel Vetter 				   reset_event);
24261f83fee0SDaniel Vetter 
242717e1df07SDaniel Vetter 		/*
2428f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2429f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2430f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2431f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2432f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2433f454c694SImre Deak 		 */
2434f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
2435f454c694SImre Deak 		/*
243617e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
243717e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
243817e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
243917e1df07SDaniel Vetter 		 * deadlocks with the reset work.
244017e1df07SDaniel Vetter 		 */
2441f69061beSDaniel Vetter 		ret = i915_reset(dev);
2442f69061beSDaniel Vetter 
244317e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
244417e1df07SDaniel Vetter 
2445f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2446f454c694SImre Deak 
2447f69061beSDaniel Vetter 		if (ret == 0) {
2448f69061beSDaniel Vetter 			/*
2449f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2450f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2451f69061beSDaniel Vetter 			 * complete.
2452f69061beSDaniel Vetter 			 *
2453f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2454f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2455f69061beSDaniel Vetter 			 * updates before
2456f69061beSDaniel Vetter 			 * the counter increment.
2457f69061beSDaniel Vetter 			 */
24584e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2459f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2460f69061beSDaniel Vetter 
24615bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2462f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24631f83fee0SDaniel Vetter 		} else {
24642ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2465f316a42cSBen Gamari 		}
24661f83fee0SDaniel Vetter 
246717e1df07SDaniel Vetter 		/*
246817e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
246917e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
247017e1df07SDaniel Vetter 		 */
247117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2472f316a42cSBen Gamari 	}
24738a905236SJesse Barnes }
24748a905236SJesse Barnes 
247535aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2476c0e09200SDave Airlie {
24778a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2478bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
247963eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2480050ee91fSBen Widawsky 	int pipe, i;
248163eeaf38SJesse Barnes 
248235aed2e6SChris Wilson 	if (!eir)
248335aed2e6SChris Wilson 		return;
248463eeaf38SJesse Barnes 
2485a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24868a905236SJesse Barnes 
2487bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2488bd9854f9SBen Widawsky 
24898a905236SJesse Barnes 	if (IS_G4X(dev)) {
24908a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24918a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24928a905236SJesse Barnes 
2493a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2494a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2495050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2496050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2497a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2498a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24998a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25003143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
25018a905236SJesse Barnes 		}
25028a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
25038a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2504a70491ccSJoe Perches 			pr_err("page table error\n");
2505a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
25068a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25073143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
25088a905236SJesse Barnes 		}
25098a905236SJesse Barnes 	}
25108a905236SJesse Barnes 
2511a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
251263eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
251363eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2514a70491ccSJoe Perches 			pr_err("page table error\n");
2515a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
251663eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
25173143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
251863eeaf38SJesse Barnes 		}
25198a905236SJesse Barnes 	}
25208a905236SJesse Barnes 
252163eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2522a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2523055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2524a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
25259db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
252663eeaf38SJesse Barnes 		/* pipestat has already been acked */
252763eeaf38SJesse Barnes 	}
252863eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2529a70491ccSJoe Perches 		pr_err("instruction error\n");
2530a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2531050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2532050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2533a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
253463eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
253563eeaf38SJesse Barnes 
2536a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2537a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2538a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
253963eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25403143a2bfSChris Wilson 			POSTING_READ(IPEIR);
254163eeaf38SJesse Barnes 		} else {
254263eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
254363eeaf38SJesse Barnes 
2544a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2545a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2546a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2547a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
254863eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25493143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
255063eeaf38SJesse Barnes 		}
255163eeaf38SJesse Barnes 	}
255263eeaf38SJesse Barnes 
255363eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25543143a2bfSChris Wilson 	POSTING_READ(EIR);
255563eeaf38SJesse Barnes 	eir = I915_READ(EIR);
255663eeaf38SJesse Barnes 	if (eir) {
255763eeaf38SJesse Barnes 		/*
255863eeaf38SJesse Barnes 		 * some errors might have become stuck,
255963eeaf38SJesse Barnes 		 * mask them.
256063eeaf38SJesse Barnes 		 */
256163eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
256263eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
256363eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
256463eeaf38SJesse Barnes 	}
256535aed2e6SChris Wilson }
256635aed2e6SChris Wilson 
256735aed2e6SChris Wilson /**
256835aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
256935aed2e6SChris Wilson  * @dev: drm device
257035aed2e6SChris Wilson  *
257135aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
257235aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
257335aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
257435aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
257535aed2e6SChris Wilson  * of a ring dump etc.).
257635aed2e6SChris Wilson  */
257758174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
257858174462SMika Kuoppala 		       const char *fmt, ...)
257935aed2e6SChris Wilson {
258035aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
258158174462SMika Kuoppala 	va_list args;
258258174462SMika Kuoppala 	char error_msg[80];
258335aed2e6SChris Wilson 
258458174462SMika Kuoppala 	va_start(args, fmt);
258558174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
258658174462SMika Kuoppala 	va_end(args);
258758174462SMika Kuoppala 
258858174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
258935aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25908a905236SJesse Barnes 
2591ba1234d1SBen Gamari 	if (wedged) {
2592f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2593f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2594ba1234d1SBen Gamari 
259511ed50ecSBen Gamari 		/*
259617e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
259717e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
259817e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
259917e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
260017e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
260117e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
260217e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
260317e1df07SDaniel Vetter 		 *
260417e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
260517e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
260617e1df07SDaniel Vetter 		 * counter atomic_t.
260711ed50ecSBen Gamari 		 */
260817e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
260911ed50ecSBen Gamari 	}
261011ed50ecSBen Gamari 
2611122f46baSDaniel Vetter 	/*
2612122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2613122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2614122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2615122f46baSDaniel Vetter 	 * code will deadlock.
2616122f46baSDaniel Vetter 	 */
2617122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
26188a905236SJesse Barnes }
26198a905236SJesse Barnes 
262042f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
262142f52ef8SKeith Packard  * we use as a pipe index
262242f52ef8SKeith Packard  */
2623f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
26240a3e67a4SJesse Barnes {
26252d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2626e9d21d7fSKeith Packard 	unsigned long irqflags;
262771e0ffa5SJesse Barnes 
26285eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
262971e0ffa5SJesse Barnes 		return -EINVAL;
26300a3e67a4SJesse Barnes 
26311ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2632f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26337c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2634755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26350a3e67a4SJesse Barnes 	else
26367c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2637755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26381ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26398692d00eSChris Wilson 
26400a3e67a4SJesse Barnes 	return 0;
26410a3e67a4SJesse Barnes }
26420a3e67a4SJesse Barnes 
2643f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2644f796cf8fSJesse Barnes {
26452d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2646f796cf8fSJesse Barnes 	unsigned long irqflags;
2647b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
264840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2649f796cf8fSJesse Barnes 
2650f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2651f796cf8fSJesse Barnes 		return -EINVAL;
2652f796cf8fSJesse Barnes 
2653f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2654b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2655b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2656b1f14ad0SJesse Barnes 
2657b1f14ad0SJesse Barnes 	return 0;
2658b1f14ad0SJesse Barnes }
2659b1f14ad0SJesse Barnes 
26607e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26617e231dbeSJesse Barnes {
26622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26637e231dbeSJesse Barnes 	unsigned long irqflags;
26647e231dbeSJesse Barnes 
26657e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
26667e231dbeSJesse Barnes 		return -EINVAL;
26677e231dbeSJesse Barnes 
26687e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
266931acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2670755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26717e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26727e231dbeSJesse Barnes 
26737e231dbeSJesse Barnes 	return 0;
26747e231dbeSJesse Barnes }
26757e231dbeSJesse Barnes 
2676abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2677abd58f01SBen Widawsky {
2678abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2679abd58f01SBen Widawsky 	unsigned long irqflags;
2680abd58f01SBen Widawsky 
2681abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2682abd58f01SBen Widawsky 		return -EINVAL;
2683abd58f01SBen Widawsky 
2684abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26857167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26867167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2687abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2688abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2689abd58f01SBen Widawsky 	return 0;
2690abd58f01SBen Widawsky }
2691abd58f01SBen Widawsky 
269242f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
269342f52ef8SKeith Packard  * we use as a pipe index
269442f52ef8SKeith Packard  */
2695f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26960a3e67a4SJesse Barnes {
26972d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2698e9d21d7fSKeith Packard 	unsigned long irqflags;
26990a3e67a4SJesse Barnes 
27001ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27017c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2702755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2703755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27041ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27050a3e67a4SJesse Barnes }
27060a3e67a4SJesse Barnes 
2707f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2708f796cf8fSJesse Barnes {
27092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2710f796cf8fSJesse Barnes 	unsigned long irqflags;
2711b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
271240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2713f796cf8fSJesse Barnes 
2714f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2715b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2716b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2717b1f14ad0SJesse Barnes }
2718b1f14ad0SJesse Barnes 
27197e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
27207e231dbeSJesse Barnes {
27212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27227e231dbeSJesse Barnes 	unsigned long irqflags;
27237e231dbeSJesse Barnes 
27247e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
272531acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2726755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27277e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27287e231dbeSJesse Barnes }
27297e231dbeSJesse Barnes 
2730abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2731abd58f01SBen Widawsky {
2732abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2733abd58f01SBen Widawsky 	unsigned long irqflags;
2734abd58f01SBen Widawsky 
2735abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2736abd58f01SBen Widawsky 		return;
2737abd58f01SBen Widawsky 
2738abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27397167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27407167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2741abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2742abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2743abd58f01SBen Widawsky }
2744abd58f01SBen Widawsky 
2745893eead0SChris Wilson static u32
2746a4872ba6SOscar Mateo ring_last_seqno(struct intel_engine_cs *ring)
2747852835f3SZou Nan hai {
2748893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2749893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2750893eead0SChris Wilson }
2751893eead0SChris Wilson 
27529107e9d2SChris Wilson static bool
2753a4872ba6SOscar Mateo ring_idle(struct intel_engine_cs *ring, u32 seqno)
2754893eead0SChris Wilson {
27559107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
27569107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2757f65d9421SBen Gamari }
2758f65d9421SBen Gamari 
2759a028c4b0SDaniel Vetter static bool
2760a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2761a028c4b0SDaniel Vetter {
2762a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2763a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2764a028c4b0SDaniel Vetter 	} else {
2765a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2766a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2767a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2768a028c4b0SDaniel Vetter 	}
2769a028c4b0SDaniel Vetter }
2770a028c4b0SDaniel Vetter 
2771a4872ba6SOscar Mateo static struct intel_engine_cs *
2772a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2773921d42eaSDaniel Vetter {
2774921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2775a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2776921d42eaSDaniel Vetter 	int i;
2777921d42eaSDaniel Vetter 
2778921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2779a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2780a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2781a6cdb93aSRodrigo Vivi 				continue;
2782a6cdb93aSRodrigo Vivi 
2783a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2784a6cdb93aSRodrigo Vivi 				return signaller;
2785a6cdb93aSRodrigo Vivi 		}
2786921d42eaSDaniel Vetter 	} else {
2787921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2788921d42eaSDaniel Vetter 
2789921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2790921d42eaSDaniel Vetter 			if(ring == signaller)
2791921d42eaSDaniel Vetter 				continue;
2792921d42eaSDaniel Vetter 
2793ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2794921d42eaSDaniel Vetter 				return signaller;
2795921d42eaSDaniel Vetter 		}
2796921d42eaSDaniel Vetter 	}
2797921d42eaSDaniel Vetter 
2798a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2799a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2800921d42eaSDaniel Vetter 
2801921d42eaSDaniel Vetter 	return NULL;
2802921d42eaSDaniel Vetter }
2803921d42eaSDaniel Vetter 
2804a4872ba6SOscar Mateo static struct intel_engine_cs *
2805a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2806a24a11e6SChris Wilson {
2807a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
280888fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2809a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2810a6cdb93aSRodrigo Vivi 	int i, backwards;
2811a24a11e6SChris Wilson 
2812a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2813a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
28146274f212SChris Wilson 		return NULL;
2815a24a11e6SChris Wilson 
281688fe429dSDaniel Vetter 	/*
281788fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
281888fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2819a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2820a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
282188fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
282288fe429dSDaniel Vetter 	 * ringbuffer itself.
2823a24a11e6SChris Wilson 	 */
282488fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2825a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
282688fe429dSDaniel Vetter 
2827a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
282888fe429dSDaniel Vetter 		/*
282988fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
283088fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
283188fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
283288fe429dSDaniel Vetter 		 */
2833ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
283488fe429dSDaniel Vetter 
283588fe429dSDaniel Vetter 		/* This here seems to blow up */
2836ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2837a24a11e6SChris Wilson 		if (cmd == ipehr)
2838a24a11e6SChris Wilson 			break;
2839a24a11e6SChris Wilson 
284088fe429dSDaniel Vetter 		head -= 4;
284188fe429dSDaniel Vetter 	}
2842a24a11e6SChris Wilson 
284388fe429dSDaniel Vetter 	if (!i)
284488fe429dSDaniel Vetter 		return NULL;
284588fe429dSDaniel Vetter 
2846ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2847a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2848a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2849a6cdb93aSRodrigo Vivi 		offset <<= 32;
2850a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2851a6cdb93aSRodrigo Vivi 	}
2852a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2853a24a11e6SChris Wilson }
2854a24a11e6SChris Wilson 
2855a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28566274f212SChris Wilson {
28576274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2858a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2859a0d036b0SChris Wilson 	u32 seqno;
28606274f212SChris Wilson 
28614be17381SChris Wilson 	ring->hangcheck.deadlock++;
28626274f212SChris Wilson 
28636274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28644be17381SChris Wilson 	if (signaller == NULL)
28654be17381SChris Wilson 		return -1;
28664be17381SChris Wilson 
28674be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28684be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28696274f212SChris Wilson 		return -1;
28706274f212SChris Wilson 
28714be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28724be17381SChris Wilson 		return 1;
28734be17381SChris Wilson 
2874a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2875a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2876a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28774be17381SChris Wilson 		return -1;
28784be17381SChris Wilson 
28794be17381SChris Wilson 	return 0;
28806274f212SChris Wilson }
28816274f212SChris Wilson 
28826274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28836274f212SChris Wilson {
2884a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28856274f212SChris Wilson 	int i;
28866274f212SChris Wilson 
28876274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28884be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28896274f212SChris Wilson }
28906274f212SChris Wilson 
2891ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2892a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28931ec14ad3SChris Wilson {
28941ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28951ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28969107e9d2SChris Wilson 	u32 tmp;
28979107e9d2SChris Wilson 
2898f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2899f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2900f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2901f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2902f260fe7bSMika Kuoppala 		}
2903f260fe7bSMika Kuoppala 
2904f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2905f260fe7bSMika Kuoppala 	}
29066274f212SChris Wilson 
29079107e9d2SChris Wilson 	if (IS_GEN2(dev))
2908f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
29099107e9d2SChris Wilson 
29109107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
29119107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
29129107e9d2SChris Wilson 	 * and break the hang. This should work on
29139107e9d2SChris Wilson 	 * all but the second generation chipsets.
29149107e9d2SChris Wilson 	 */
29159107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
29161ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
291758174462SMika Kuoppala 		i915_handle_error(dev, false,
291858174462SMika Kuoppala 				  "Kicking stuck wait on %s",
29191ec14ad3SChris Wilson 				  ring->name);
29201ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2921f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
29221ec14ad3SChris Wilson 	}
2923a24a11e6SChris Wilson 
29246274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
29256274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29266274f212SChris Wilson 		default:
2927f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29286274f212SChris Wilson 		case 1:
292958174462SMika Kuoppala 			i915_handle_error(dev, false,
293058174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2931a24a11e6SChris Wilson 					  ring->name);
2932a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2933f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29346274f212SChris Wilson 		case 0:
2935f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29366274f212SChris Wilson 		}
29379107e9d2SChris Wilson 	}
29389107e9d2SChris Wilson 
2939f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2940a24a11e6SChris Wilson }
2941d1e61e7fSChris Wilson 
2942f65d9421SBen Gamari /**
2943f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
294405407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
294505407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
294605407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
294705407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
294805407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2949f65d9421SBen Gamari  */
2950a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2951f65d9421SBen Gamari {
2952f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
29532d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2954a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2955b4519513SChris Wilson 	int i;
295605407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29579107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29589107e9d2SChris Wilson #define BUSY 1
29599107e9d2SChris Wilson #define KICK 5
29609107e9d2SChris Wilson #define HUNG 20
2961893eead0SChris Wilson 
2962d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29633e0dc6b0SBen Widawsky 		return;
29643e0dc6b0SBen Widawsky 
2965b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
296650877445SChris Wilson 		u64 acthd;
296750877445SChris Wilson 		u32 seqno;
29689107e9d2SChris Wilson 		bool busy = true;
2969b4519513SChris Wilson 
29706274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29716274f212SChris Wilson 
297205407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
297305407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
297405407ff8SMika Kuoppala 
297505407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
29769107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2977da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2978da661464SMika Kuoppala 
29799107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29809107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2981094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2982f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29839107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29849107e9d2SChris Wilson 								  ring->name);
2985f4adcd24SDaniel Vetter 						else
2986f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2987f4adcd24SDaniel Vetter 								 ring->name);
29889107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2989094f9a54SChris Wilson 					}
2990094f9a54SChris Wilson 					/* Safeguard against driver failure */
2991094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29929107e9d2SChris Wilson 				} else
29939107e9d2SChris Wilson 					busy = false;
299405407ff8SMika Kuoppala 			} else {
29956274f212SChris Wilson 				/* We always increment the hangcheck score
29966274f212SChris Wilson 				 * if the ring is busy and still processing
29976274f212SChris Wilson 				 * the same request, so that no single request
29986274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29996274f212SChris Wilson 				 * batches). The only time we do not increment
30006274f212SChris Wilson 				 * the hangcheck score on this ring, if this
30016274f212SChris Wilson 				 * ring is in a legitimate wait for another
30026274f212SChris Wilson 				 * ring. In that case the waiting ring is a
30036274f212SChris Wilson 				 * victim and we want to be sure we catch the
30046274f212SChris Wilson 				 * right culprit. Then every time we do kick
30056274f212SChris Wilson 				 * the ring, add a small increment to the
30066274f212SChris Wilson 				 * score so that we can catch a batch that is
30076274f212SChris Wilson 				 * being repeatedly kicked and so responsible
30086274f212SChris Wilson 				 * for stalling the machine.
30099107e9d2SChris Wilson 				 */
3010ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3011ad8beaeaSMika Kuoppala 								    acthd);
3012ad8beaeaSMika Kuoppala 
3013ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3014da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3015f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
3016f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
3017f260fe7bSMika Kuoppala 					break;
3018f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
3019ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
30206274f212SChris Wilson 					break;
3021f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3022ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
30236274f212SChris Wilson 					break;
3024f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3025ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30266274f212SChris Wilson 					stuck[i] = true;
30276274f212SChris Wilson 					break;
30286274f212SChris Wilson 				}
302905407ff8SMika Kuoppala 			}
30309107e9d2SChris Wilson 		} else {
3031da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3032da661464SMika Kuoppala 
30339107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30349107e9d2SChris Wilson 			 * attempts across multiple batches.
30359107e9d2SChris Wilson 			 */
30369107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30379107e9d2SChris Wilson 				ring->hangcheck.score--;
3038f260fe7bSMika Kuoppala 
3039f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3040cbb465e7SChris Wilson 		}
3041f65d9421SBen Gamari 
304205407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
304305407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30449107e9d2SChris Wilson 		busy_count += busy;
304505407ff8SMika Kuoppala 	}
304605407ff8SMika Kuoppala 
304705407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3048b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3049b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
305005407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3051a43adf07SChris Wilson 				 ring->name);
3052a43adf07SChris Wilson 			rings_hung++;
305305407ff8SMika Kuoppala 		}
305405407ff8SMika Kuoppala 	}
305505407ff8SMika Kuoppala 
305605407ff8SMika Kuoppala 	if (rings_hung)
305758174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
305805407ff8SMika Kuoppala 
305905407ff8SMika Kuoppala 	if (busy_count)
306005407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
306105407ff8SMika Kuoppala 		 * being added */
306210cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
306310cd45b6SMika Kuoppala }
306410cd45b6SMika Kuoppala 
306510cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
306610cd45b6SMika Kuoppala {
306710cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
3068d330a953SJani Nikula 	if (!i915.enable_hangcheck)
306910cd45b6SMika Kuoppala 		return;
307010cd45b6SMika Kuoppala 
307199584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
307210cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3073f65d9421SBen Gamari }
3074f65d9421SBen Gamari 
30751c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
307691738a95SPaulo Zanoni {
307791738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
307891738a95SPaulo Zanoni 
307991738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
308091738a95SPaulo Zanoni 		return;
308191738a95SPaulo Zanoni 
3082f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3083105b122eSPaulo Zanoni 
3084105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3085105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3086622364b6SPaulo Zanoni }
3087105b122eSPaulo Zanoni 
308891738a95SPaulo Zanoni /*
3089622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3090622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3091622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3092622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3093622364b6SPaulo Zanoni  *
3094622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
309591738a95SPaulo Zanoni  */
3096622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3097622364b6SPaulo Zanoni {
3098622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3099622364b6SPaulo Zanoni 
3100622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3101622364b6SPaulo Zanoni 		return;
3102622364b6SPaulo Zanoni 
3103622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
310491738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
310591738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
310691738a95SPaulo Zanoni }
310791738a95SPaulo Zanoni 
31087c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3109d18ea1b5SDaniel Vetter {
3110d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3111d18ea1b5SDaniel Vetter 
3112f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3113a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3114f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3115d18ea1b5SDaniel Vetter }
3116d18ea1b5SDaniel Vetter 
3117c0e09200SDave Airlie /* drm_dma.h hooks
3118c0e09200SDave Airlie */
3119be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3120036a4a7dSZhenyu Wang {
31212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3122036a4a7dSZhenyu Wang 
31230c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3124bdfcdb63SDaniel Vetter 
3125f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3126c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3127c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3128036a4a7dSZhenyu Wang 
31297c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3130c650156aSZhenyu Wang 
31311c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31327d99163dSBen Widawsky }
31337d99163dSBen Widawsky 
31347e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31357e231dbeSJesse Barnes {
31362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31377e231dbeSJesse Barnes 	int pipe;
31387e231dbeSJesse Barnes 
31397e231dbeSJesse Barnes 	/* VLV magic */
31407e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31417e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31427e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31437e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31447e231dbeSJesse Barnes 
31457e231dbeSJesse Barnes 	/* and GT */
31467e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
31477e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3148d18ea1b5SDaniel Vetter 
31497c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31507e231dbeSJesse Barnes 
31517e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
31527e231dbeSJesse Barnes 
31537e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
31547e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3155055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
31567e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
31577e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31587e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
31597e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
31607e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31617e231dbeSJesse Barnes }
31627e231dbeSJesse Barnes 
3163d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3164d6e3cca3SDaniel Vetter {
3165d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3166d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3167d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3168d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3169d6e3cca3SDaniel Vetter }
3170d6e3cca3SDaniel Vetter 
3171823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3172abd58f01SBen Widawsky {
3173abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3174abd58f01SBen Widawsky 	int pipe;
3175abd58f01SBen Widawsky 
3176abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3177abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3178abd58f01SBen Widawsky 
3179d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3180abd58f01SBen Widawsky 
3181055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3182f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3183813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3184f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3185abd58f01SBen Widawsky 
3186f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3187f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3188f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3189abd58f01SBen Widawsky 
31901c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3191abd58f01SBen Widawsky }
3192abd58f01SBen Widawsky 
3193d49bdb0eSPaulo Zanoni void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
3194d49bdb0eSPaulo Zanoni {
31951180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3196d49bdb0eSPaulo Zanoni 
319713321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3198d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
31991180e206SPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
3200d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
32011180e206SPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
320213321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3203d49bdb0eSPaulo Zanoni }
3204d49bdb0eSPaulo Zanoni 
320543f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
320643f328d7SVille Syrjälä {
320743f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
320843f328d7SVille Syrjälä 	int pipe;
320943f328d7SVille Syrjälä 
321043f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
321143f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
321243f328d7SVille Syrjälä 
3213d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
321443f328d7SVille Syrjälä 
321543f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
321643f328d7SVille Syrjälä 
321743f328d7SVille Syrjälä 	POSTING_READ(GEN8_PCU_IIR);
321843f328d7SVille Syrjälä 
321943f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
322043f328d7SVille Syrjälä 
322143f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
322243f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
322343f328d7SVille Syrjälä 
3224055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
322543f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
322643f328d7SVille Syrjälä 
322743f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
322843f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
322943f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
323043f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
323143f328d7SVille Syrjälä }
323243f328d7SVille Syrjälä 
323382a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
323482a28bcfSDaniel Vetter {
32352d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
323682a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3237fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
323882a28bcfSDaniel Vetter 
323982a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3240fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3241b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3242cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3243fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
324482a28bcfSDaniel Vetter 	} else {
3245fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3246b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3247cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3248fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
324982a28bcfSDaniel Vetter 	}
325082a28bcfSDaniel Vetter 
3251fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
325282a28bcfSDaniel Vetter 
32537fe0b973SKeith Packard 	/*
32547fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32557fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
32567fe0b973SKeith Packard 	 *
32577fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
32587fe0b973SKeith Packard 	 */
32597fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32607fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32617fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32627fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32637fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32647fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32657fe0b973SKeith Packard }
32667fe0b973SKeith Packard 
3267d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3268d46da437SPaulo Zanoni {
32692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
327082a28bcfSDaniel Vetter 	u32 mask;
3271d46da437SPaulo Zanoni 
3272692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3273692a04cfSDaniel Vetter 		return;
3274692a04cfSDaniel Vetter 
3275105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32765c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3277105b122eSPaulo Zanoni 	else
32785c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32798664281bSPaulo Zanoni 
3280337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3281d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3282d46da437SPaulo Zanoni }
3283d46da437SPaulo Zanoni 
32840a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32850a9a8c91SDaniel Vetter {
32860a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32870a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32880a9a8c91SDaniel Vetter 
32890a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32900a9a8c91SDaniel Vetter 
32910a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3292040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32930a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
329435a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
329535a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32960a9a8c91SDaniel Vetter 	}
32970a9a8c91SDaniel Vetter 
32980a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32990a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
33000a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
33010a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
33020a9a8c91SDaniel Vetter 	} else {
33030a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
33040a9a8c91SDaniel Vetter 	}
33050a9a8c91SDaniel Vetter 
330635079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
33070a9a8c91SDaniel Vetter 
33080a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3309a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
33100a9a8c91SDaniel Vetter 
33110a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
33120a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
33130a9a8c91SDaniel Vetter 
3314605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
331535079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
33160a9a8c91SDaniel Vetter 	}
33170a9a8c91SDaniel Vetter }
33180a9a8c91SDaniel Vetter 
3319f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3320036a4a7dSZhenyu Wang {
33212d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33228e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33238e76f8dcSPaulo Zanoni 
33248e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33258e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33268e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33278e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33285c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33298e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
33305c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
33318e76f8dcSPaulo Zanoni 	} else {
33328e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3333ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33345b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33355b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33365b3a856bSDaniel Vetter 				DE_POISON);
33375c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
33385c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
33398e76f8dcSPaulo Zanoni 	}
3340036a4a7dSZhenyu Wang 
33411ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3342036a4a7dSZhenyu Wang 
33430c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33440c841212SPaulo Zanoni 
3345622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3346622364b6SPaulo Zanoni 
334735079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3348036a4a7dSZhenyu Wang 
33490a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3350036a4a7dSZhenyu Wang 
3351d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33527fe0b973SKeith Packard 
3353f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33546005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33556005ce42SDaniel Vetter 		 *
33566005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33574bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33584bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3359d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3360f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3361d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3362f97108d1SJesse Barnes 	}
3363f97108d1SJesse Barnes 
3364036a4a7dSZhenyu Wang 	return 0;
3365036a4a7dSZhenyu Wang }
3366036a4a7dSZhenyu Wang 
3367f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3368f8b79e58SImre Deak {
3369f8b79e58SImre Deak 	u32 pipestat_mask;
3370f8b79e58SImre Deak 	u32 iir_mask;
3371f8b79e58SImre Deak 
3372f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3373f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3374f8b79e58SImre Deak 
3375f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3376f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3377f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3378f8b79e58SImre Deak 
3379f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3380f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3381f8b79e58SImre Deak 
3382f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3383f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3384f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3385f8b79e58SImre Deak 
3386f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3387f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3388f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3389f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3390f8b79e58SImre Deak 
3391f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3392f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3393f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3394f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3395f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3396f8b79e58SImre Deak }
3397f8b79e58SImre Deak 
3398f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3399f8b79e58SImre Deak {
3400f8b79e58SImre Deak 	u32 pipestat_mask;
3401f8b79e58SImre Deak 	u32 iir_mask;
3402f8b79e58SImre Deak 
3403f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3404f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
34056c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3406f8b79e58SImre Deak 
3407f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3408f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3409f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3410f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3411f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3412f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3413f8b79e58SImre Deak 
3414f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3415f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3416f8b79e58SImre Deak 
3417f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3418f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3419f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3420f8b79e58SImre Deak 
3421f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3422f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3423f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3424f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3425f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3426f8b79e58SImre Deak }
3427f8b79e58SImre Deak 
3428f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3429f8b79e58SImre Deak {
3430f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3431f8b79e58SImre Deak 
3432f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3433f8b79e58SImre Deak 		return;
3434f8b79e58SImre Deak 
3435f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3436f8b79e58SImre Deak 
3437950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3438f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3439f8b79e58SImre Deak }
3440f8b79e58SImre Deak 
3441f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3442f8b79e58SImre Deak {
3443f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3444f8b79e58SImre Deak 
3445f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3446f8b79e58SImre Deak 		return;
3447f8b79e58SImre Deak 
3448f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3449f8b79e58SImre Deak 
3450950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3451f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3452f8b79e58SImre Deak }
3453f8b79e58SImre Deak 
34547e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
34557e231dbeSJesse Barnes {
34562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34577e231dbeSJesse Barnes 
3458f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34597e231dbeSJesse Barnes 
346020afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
346120afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
346220afbda2SDaniel Vetter 
34637e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3464f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
34657e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34667e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
34677e231dbeSJesse Barnes 
3468b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3469b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3470d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3471f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3472f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3473d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
347431acc7f5SJesse Barnes 
34757e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34767e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34777e231dbeSJesse Barnes 
34780a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34797e231dbeSJesse Barnes 
34807e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34817e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34827e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34837e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34847e231dbeSJesse Barnes #endif
34857e231dbeSJesse Barnes 
34867e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
348720afbda2SDaniel Vetter 
348820afbda2SDaniel Vetter 	return 0;
348920afbda2SDaniel Vetter }
349020afbda2SDaniel Vetter 
3491abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3492abd58f01SBen Widawsky {
3493abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3494abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3495abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
349673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3497abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
349873d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
349973d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3500abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
350173d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
350273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
350373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3504abd58f01SBen Widawsky 		0,
350573d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
350673d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3507abd58f01SBen Widawsky 		};
3508abd58f01SBen Widawsky 
35090961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
35109a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
35119a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
35129a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, dev_priv->pm_rps_events);
35139a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3514abd58f01SBen Widawsky }
3515abd58f01SBen Widawsky 
3516abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3517abd58f01SBen Widawsky {
3518770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3519770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3520abd58f01SBen Widawsky 	int pipe;
3521770de83dSDamien Lespiau 
3522770de83dSDamien Lespiau 	if (IS_GEN9(dev_priv))
3523770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3524770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
3525770de83dSDamien Lespiau 	else
3526770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3527770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3528770de83dSDamien Lespiau 
3529770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3530770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3531770de83dSDamien Lespiau 
353213b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
353313b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
353413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3535abd58f01SBen Widawsky 
3536055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3537f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3538813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3539813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3540813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
354135079899SPaulo Zanoni 					  de_pipe_enables);
3542abd58f01SBen Widawsky 
354335079899SPaulo Zanoni 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
3544abd58f01SBen Widawsky }
3545abd58f01SBen Widawsky 
3546abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3547abd58f01SBen Widawsky {
3548abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3549abd58f01SBen Widawsky 
3550622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3551622364b6SPaulo Zanoni 
3552abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3553abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3554abd58f01SBen Widawsky 
3555abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3556abd58f01SBen Widawsky 
3557abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3558abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3559abd58f01SBen Widawsky 
3560abd58f01SBen Widawsky 	return 0;
3561abd58f01SBen Widawsky }
3562abd58f01SBen Widawsky 
356343f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
356443f328d7SVille Syrjälä {
356543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
356643f328d7SVille Syrjälä 	u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
356743f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
356843f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
35693278f67fSVille Syrjälä 		I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
35703278f67fSVille Syrjälä 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_STATUS_VLV |
35713278f67fSVille Syrjälä 		PIPE_CRC_DONE_INTERRUPT_STATUS;
357243f328d7SVille Syrjälä 	int pipe;
357343f328d7SVille Syrjälä 
357443f328d7SVille Syrjälä 	/*
357543f328d7SVille Syrjälä 	 * Leave vblank interrupts masked initially.  enable/disable will
357643f328d7SVille Syrjälä 	 * toggle them based on usage.
357743f328d7SVille Syrjälä 	 */
35783278f67fSVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
357943f328d7SVille Syrjälä 
3580055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
358143f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
358243f328d7SVille Syrjälä 
3583d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
35843278f67fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3585055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
358643f328d7SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
3587d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
358843f328d7SVille Syrjälä 
358943f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
359043f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
359143f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, enable_mask);
359243f328d7SVille Syrjälä 
359343f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
359443f328d7SVille Syrjälä 
359543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
359643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
359743f328d7SVille Syrjälä 
359843f328d7SVille Syrjälä 	return 0;
359943f328d7SVille Syrjälä }
360043f328d7SVille Syrjälä 
3601abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3602abd58f01SBen Widawsky {
3603abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3604abd58f01SBen Widawsky 
3605abd58f01SBen Widawsky 	if (!dev_priv)
3606abd58f01SBen Widawsky 		return;
3607abd58f01SBen Widawsky 
3608823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3609abd58f01SBen Widawsky }
3610abd58f01SBen Widawsky 
36117e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
36127e231dbeSJesse Barnes {
36132d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36147e231dbeSJesse Barnes 	int pipe;
36157e231dbeSJesse Barnes 
36167e231dbeSJesse Barnes 	if (!dev_priv)
36177e231dbeSJesse Barnes 		return;
36187e231dbeSJesse Barnes 
3619843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3620843d0e7dSImre Deak 
3621055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
36227e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
36237e231dbeSJesse Barnes 
36247e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
36257e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
36267e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3627f8b79e58SImre Deak 
3628d6207435SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3629d6207435SDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3630d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3631f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3632f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3633d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3634f8b79e58SImre Deak 
3635f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3636f8b79e58SImre Deak 
36377e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
36387e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
36397e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
36407e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
36417e231dbeSJesse Barnes }
36427e231dbeSJesse Barnes 
364343f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
364443f328d7SVille Syrjälä {
364543f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
364643f328d7SVille Syrjälä 	int pipe;
364743f328d7SVille Syrjälä 
364843f328d7SVille Syrjälä 	if (!dev_priv)
364943f328d7SVille Syrjälä 		return;
365043f328d7SVille Syrjälä 
365143f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
365243f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
365343f328d7SVille Syrjälä 
365443f328d7SVille Syrjälä #define GEN8_IRQ_FINI_NDX(type, which)				\
365543f328d7SVille Syrjälä do {								\
365643f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff);	\
365743f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER(which), 0);		\
365843f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
365943f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR(which));			\
366043f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
366143f328d7SVille Syrjälä } while (0)
366243f328d7SVille Syrjälä 
366343f328d7SVille Syrjälä #define GEN8_IRQ_FINI(type)				\
366443f328d7SVille Syrjälä do {							\
366543f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR, 0xffffffff);	\
366643f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER, 0);		\
366743f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
366843f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR);		\
366943f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
367043f328d7SVille Syrjälä } while (0)
367143f328d7SVille Syrjälä 
367243f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 0);
367343f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 1);
367443f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 2);
367543f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 3);
367643f328d7SVille Syrjälä 
367743f328d7SVille Syrjälä 	GEN8_IRQ_FINI(PCU);
367843f328d7SVille Syrjälä 
367943f328d7SVille Syrjälä #undef GEN8_IRQ_FINI
368043f328d7SVille Syrjälä #undef GEN8_IRQ_FINI_NDX
368143f328d7SVille Syrjälä 
368243f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
368343f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
368443f328d7SVille Syrjälä 
3685055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
368643f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
368743f328d7SVille Syrjälä 
368843f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
368943f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
369043f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
369143f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
369243f328d7SVille Syrjälä }
369343f328d7SVille Syrjälä 
3694f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3695036a4a7dSZhenyu Wang {
36962d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36974697995bSJesse Barnes 
36984697995bSJesse Barnes 	if (!dev_priv)
36994697995bSJesse Barnes 		return;
37004697995bSJesse Barnes 
3701be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3702036a4a7dSZhenyu Wang }
3703036a4a7dSZhenyu Wang 
3704c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3705c2798b19SChris Wilson {
37062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3707c2798b19SChris Wilson 	int pipe;
3708c2798b19SChris Wilson 
3709055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3710c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3711c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3712c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3713c2798b19SChris Wilson 	POSTING_READ16(IER);
3714c2798b19SChris Wilson }
3715c2798b19SChris Wilson 
3716c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3717c2798b19SChris Wilson {
37182d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3719c2798b19SChris Wilson 
3720c2798b19SChris Wilson 	I915_WRITE16(EMR,
3721c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3722c2798b19SChris Wilson 
3723c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3724c2798b19SChris Wilson 	dev_priv->irq_mask =
3725c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3726c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3727c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3728c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3729c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3730c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3731c2798b19SChris Wilson 
3732c2798b19SChris Wilson 	I915_WRITE16(IER,
3733c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3734c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3735c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3736c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3737c2798b19SChris Wilson 	POSTING_READ16(IER);
3738c2798b19SChris Wilson 
3739379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3740379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3741d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3742755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3743755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3744d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3745379ef82dSDaniel Vetter 
3746c2798b19SChris Wilson 	return 0;
3747c2798b19SChris Wilson }
3748c2798b19SChris Wilson 
374990a72f87SVille Syrjälä /*
375090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
375190a72f87SVille Syrjälä  */
375290a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37531f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
375490a72f87SVille Syrjälä {
37552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37561f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
375790a72f87SVille Syrjälä 
37588d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
375990a72f87SVille Syrjälä 		return false;
376090a72f87SVille Syrjälä 
376190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3762d6bbafa1SChris Wilson 		goto check_page_flip;
376390a72f87SVille Syrjälä 
37641f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
376590a72f87SVille Syrjälä 
376690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
376790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
376890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
376990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
377090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
377190a72f87SVille Syrjälä 	 */
377290a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3773d6bbafa1SChris Wilson 		goto check_page_flip;
377490a72f87SVille Syrjälä 
377590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
377690a72f87SVille Syrjälä 	return true;
3777d6bbafa1SChris Wilson 
3778d6bbafa1SChris Wilson check_page_flip:
3779d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3780d6bbafa1SChris Wilson 	return false;
378190a72f87SVille Syrjälä }
378290a72f87SVille Syrjälä 
3783ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3784c2798b19SChris Wilson {
378545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37862d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3787c2798b19SChris Wilson 	u16 iir, new_iir;
3788c2798b19SChris Wilson 	u32 pipe_stats[2];
3789c2798b19SChris Wilson 	int pipe;
3790c2798b19SChris Wilson 	u16 flip_mask =
3791c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3792c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3793c2798b19SChris Wilson 
3794c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3795c2798b19SChris Wilson 	if (iir == 0)
3796c2798b19SChris Wilson 		return IRQ_NONE;
3797c2798b19SChris Wilson 
3798c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3799c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3800c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3801c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3802c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3803c2798b19SChris Wilson 		 */
3804222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3805c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
380658174462SMika Kuoppala 			i915_handle_error(dev, false,
380758174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
380858174462SMika Kuoppala 					  iir);
3809c2798b19SChris Wilson 
3810055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3811c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3812c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3813c2798b19SChris Wilson 
3814c2798b19SChris Wilson 			/*
3815c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3816c2798b19SChris Wilson 			 */
38172d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3818c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3819c2798b19SChris Wilson 		}
3820222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3821c2798b19SChris Wilson 
3822c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3823c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3824c2798b19SChris Wilson 
3825d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3826c2798b19SChris Wilson 
3827c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3828c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3829c2798b19SChris Wilson 
3830055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
38311f1c2e24SVille Syrjälä 			int plane = pipe;
38323a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38331f1c2e24SVille Syrjälä 				plane = !plane;
38341f1c2e24SVille Syrjälä 
38354356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38361f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38371f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3838c2798b19SChris Wilson 
38394356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3840277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38412d9d2b0bSVille Syrjälä 
38422d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
3843*a72e4c9fSDaniel Vetter 			    intel_set_cpu_fifo_underrun_reporting(dev_priv,
3844*a72e4c9fSDaniel Vetter 								  pipe, false))
3845fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
38464356d586SDaniel Vetter 		}
3847c2798b19SChris Wilson 
3848c2798b19SChris Wilson 		iir = new_iir;
3849c2798b19SChris Wilson 	}
3850c2798b19SChris Wilson 
3851c2798b19SChris Wilson 	return IRQ_HANDLED;
3852c2798b19SChris Wilson }
3853c2798b19SChris Wilson 
3854c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3855c2798b19SChris Wilson {
38562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3857c2798b19SChris Wilson 	int pipe;
3858c2798b19SChris Wilson 
3859055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3860c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3861c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3862c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3863c2798b19SChris Wilson 	}
3864c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3865c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3866c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3867c2798b19SChris Wilson }
3868c2798b19SChris Wilson 
3869a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3870a266c7d5SChris Wilson {
38712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3872a266c7d5SChris Wilson 	int pipe;
3873a266c7d5SChris Wilson 
3874a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3875a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3876a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3877a266c7d5SChris Wilson 	}
3878a266c7d5SChris Wilson 
387900d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3880055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3881a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3882a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3883a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3884a266c7d5SChris Wilson 	POSTING_READ(IER);
3885a266c7d5SChris Wilson }
3886a266c7d5SChris Wilson 
3887a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3888a266c7d5SChris Wilson {
38892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
389038bde180SChris Wilson 	u32 enable_mask;
3891a266c7d5SChris Wilson 
389238bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
389338bde180SChris Wilson 
389438bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
389538bde180SChris Wilson 	dev_priv->irq_mask =
389638bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
389738bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
389838bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
389938bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
390038bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
390138bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
390238bde180SChris Wilson 
390338bde180SChris Wilson 	enable_mask =
390438bde180SChris Wilson 		I915_ASLE_INTERRUPT |
390538bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
390638bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
390738bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
390838bde180SChris Wilson 		I915_USER_INTERRUPT;
390938bde180SChris Wilson 
3910a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
391120afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
391220afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
391320afbda2SDaniel Vetter 
3914a266c7d5SChris Wilson 		/* Enable in IER... */
3915a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3916a266c7d5SChris Wilson 		/* and unmask in IMR */
3917a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3918a266c7d5SChris Wilson 	}
3919a266c7d5SChris Wilson 
3920a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3921a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3922a266c7d5SChris Wilson 	POSTING_READ(IER);
3923a266c7d5SChris Wilson 
3924f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
392520afbda2SDaniel Vetter 
3926379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3927379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3928d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3929755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3930755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3931d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3932379ef82dSDaniel Vetter 
393320afbda2SDaniel Vetter 	return 0;
393420afbda2SDaniel Vetter }
393520afbda2SDaniel Vetter 
393690a72f87SVille Syrjälä /*
393790a72f87SVille Syrjälä  * Returns true when a page flip has completed.
393890a72f87SVille Syrjälä  */
393990a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
394090a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
394190a72f87SVille Syrjälä {
39422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
394390a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
394490a72f87SVille Syrjälä 
39458d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
394690a72f87SVille Syrjälä 		return false;
394790a72f87SVille Syrjälä 
394890a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3949d6bbafa1SChris Wilson 		goto check_page_flip;
395090a72f87SVille Syrjälä 
395190a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
395290a72f87SVille Syrjälä 
395390a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
395490a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
395590a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
395690a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
395790a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
395890a72f87SVille Syrjälä 	 */
395990a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3960d6bbafa1SChris Wilson 		goto check_page_flip;
396190a72f87SVille Syrjälä 
396290a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
396390a72f87SVille Syrjälä 	return true;
3964d6bbafa1SChris Wilson 
3965d6bbafa1SChris Wilson check_page_flip:
3966d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3967d6bbafa1SChris Wilson 	return false;
396890a72f87SVille Syrjälä }
396990a72f87SVille Syrjälä 
3970ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3971a266c7d5SChris Wilson {
397245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39732d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39748291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
397538bde180SChris Wilson 	u32 flip_mask =
397638bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
397738bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
397838bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3979a266c7d5SChris Wilson 
3980a266c7d5SChris Wilson 	iir = I915_READ(IIR);
398138bde180SChris Wilson 	do {
398238bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39838291ee90SChris Wilson 		bool blc_event = false;
3984a266c7d5SChris Wilson 
3985a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3986a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3987a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3988a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3989a266c7d5SChris Wilson 		 */
3990222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3991a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
399258174462SMika Kuoppala 			i915_handle_error(dev, false,
399358174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
399458174462SMika Kuoppala 					  iir);
3995a266c7d5SChris Wilson 
3996055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3997a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3998a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3999a266c7d5SChris Wilson 
400038bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
4001a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4002a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
400338bde180SChris Wilson 				irq_received = true;
4004a266c7d5SChris Wilson 			}
4005a266c7d5SChris Wilson 		}
4006222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4007a266c7d5SChris Wilson 
4008a266c7d5SChris Wilson 		if (!irq_received)
4009a266c7d5SChris Wilson 			break;
4010a266c7d5SChris Wilson 
4011a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
401216c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
401316c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
401416c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4015a266c7d5SChris Wilson 
401638bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
4017a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4018a266c7d5SChris Wilson 
4019a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4020a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4021a266c7d5SChris Wilson 
4022055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
402338bde180SChris Wilson 			int plane = pipe;
40243a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
402538bde180SChris Wilson 				plane = !plane;
40265e2032d4SVille Syrjälä 
402790a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
402890a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
402990a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4030a266c7d5SChris Wilson 
4031a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4032a266c7d5SChris Wilson 				blc_event = true;
40334356d586SDaniel Vetter 
40344356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4035277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40362d9d2b0bSVille Syrjälä 
40372d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
4038*a72e4c9fSDaniel Vetter 			    intel_set_cpu_fifo_underrun_reporting(dev_priv,
4039*a72e4c9fSDaniel Vetter 								  pipe, false))
4040fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
4041a266c7d5SChris Wilson 		}
4042a266c7d5SChris Wilson 
4043a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4044a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4045a266c7d5SChris Wilson 
4046a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4047a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4048a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4049a266c7d5SChris Wilson 		 * we would never get another interrupt.
4050a266c7d5SChris Wilson 		 *
4051a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4052a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4053a266c7d5SChris Wilson 		 * another one.
4054a266c7d5SChris Wilson 		 *
4055a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4056a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4057a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4058a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4059a266c7d5SChris Wilson 		 * stray interrupts.
4060a266c7d5SChris Wilson 		 */
406138bde180SChris Wilson 		ret = IRQ_HANDLED;
4062a266c7d5SChris Wilson 		iir = new_iir;
406338bde180SChris Wilson 	} while (iir & ~flip_mask);
4064a266c7d5SChris Wilson 
4065d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
40668291ee90SChris Wilson 
4067a266c7d5SChris Wilson 	return ret;
4068a266c7d5SChris Wilson }
4069a266c7d5SChris Wilson 
4070a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4071a266c7d5SChris Wilson {
40722d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4073a266c7d5SChris Wilson 	int pipe;
4074a266c7d5SChris Wilson 
4075a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4076a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4077a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4078a266c7d5SChris Wilson 	}
4079a266c7d5SChris Wilson 
408000d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4081055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
408255b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4083a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
408455b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
408555b39755SChris Wilson 	}
4086a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4087a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4088a266c7d5SChris Wilson 
4089a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4090a266c7d5SChris Wilson }
4091a266c7d5SChris Wilson 
4092a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4093a266c7d5SChris Wilson {
40942d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4095a266c7d5SChris Wilson 	int pipe;
4096a266c7d5SChris Wilson 
4097a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4098a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4099a266c7d5SChris Wilson 
4100a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4101055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4102a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4103a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4104a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4105a266c7d5SChris Wilson 	POSTING_READ(IER);
4106a266c7d5SChris Wilson }
4107a266c7d5SChris Wilson 
4108a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4109a266c7d5SChris Wilson {
41102d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4111bbba0a97SChris Wilson 	u32 enable_mask;
4112a266c7d5SChris Wilson 	u32 error_mask;
4113a266c7d5SChris Wilson 
4114a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4115bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4116adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4117bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4118bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4119bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4120bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4121bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4122bbba0a97SChris Wilson 
4123bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
412421ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
412521ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4126bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4127bbba0a97SChris Wilson 
4128bbba0a97SChris Wilson 	if (IS_G4X(dev))
4129bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4130a266c7d5SChris Wilson 
4131b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4132b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4133d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4134755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4135755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4136755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4137d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4138a266c7d5SChris Wilson 
4139a266c7d5SChris Wilson 	/*
4140a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4141a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4142a266c7d5SChris Wilson 	 */
4143a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4144a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4145a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4146a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4147a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4148a266c7d5SChris Wilson 	} else {
4149a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4150a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4151a266c7d5SChris Wilson 	}
4152a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4153a266c7d5SChris Wilson 
4154a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4155a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4156a266c7d5SChris Wilson 	POSTING_READ(IER);
4157a266c7d5SChris Wilson 
415820afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
415920afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
416020afbda2SDaniel Vetter 
4161f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
416220afbda2SDaniel Vetter 
416320afbda2SDaniel Vetter 	return 0;
416420afbda2SDaniel Vetter }
416520afbda2SDaniel Vetter 
4166bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
416720afbda2SDaniel Vetter {
41682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4169cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
417020afbda2SDaniel Vetter 	u32 hotplug_en;
417120afbda2SDaniel Vetter 
4172b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4173b5ea2d56SDaniel Vetter 
4174bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
4175bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4176bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4177adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
4178e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
4179b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
4180cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4181cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4182a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
4183a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
4184a266c7d5SChris Wilson 		   seconds later.  So just do it once.
4185a266c7d5SChris Wilson 		*/
4186a266c7d5SChris Wilson 		if (IS_G4X(dev))
4187a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
418885fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4189a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4190a266c7d5SChris Wilson 
4191a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
4192a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4193a266c7d5SChris Wilson 	}
4194bac56d5bSEgbert Eich }
4195a266c7d5SChris Wilson 
4196ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4197a266c7d5SChris Wilson {
419845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4200a266c7d5SChris Wilson 	u32 iir, new_iir;
4201a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4202a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
420321ad8330SVille Syrjälä 	u32 flip_mask =
420421ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
420521ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4206a266c7d5SChris Wilson 
4207a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4208a266c7d5SChris Wilson 
4209a266c7d5SChris Wilson 	for (;;) {
4210501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
42112c8ba29fSChris Wilson 		bool blc_event = false;
42122c8ba29fSChris Wilson 
4213a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4214a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4215a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4216a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4217a266c7d5SChris Wilson 		 */
4218222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4219a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
422058174462SMika Kuoppala 			i915_handle_error(dev, false,
422158174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
422258174462SMika Kuoppala 					  iir);
4223a266c7d5SChris Wilson 
4224055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4225a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4226a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4227a266c7d5SChris Wilson 
4228a266c7d5SChris Wilson 			/*
4229a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4230a266c7d5SChris Wilson 			 */
4231a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4232a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4233501e01d7SVille Syrjälä 				irq_received = true;
4234a266c7d5SChris Wilson 			}
4235a266c7d5SChris Wilson 		}
4236222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4237a266c7d5SChris Wilson 
4238a266c7d5SChris Wilson 		if (!irq_received)
4239a266c7d5SChris Wilson 			break;
4240a266c7d5SChris Wilson 
4241a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4242a266c7d5SChris Wilson 
4243a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
424416c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
424516c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4246a266c7d5SChris Wilson 
424721ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4248a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4249a266c7d5SChris Wilson 
4250a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4251a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4252a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4253a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4254a266c7d5SChris Wilson 
4255055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42562c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
425790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
425890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4259a266c7d5SChris Wilson 
4260a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4261a266c7d5SChris Wilson 				blc_event = true;
42624356d586SDaniel Vetter 
42634356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4264277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4265a266c7d5SChris Wilson 
42662d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
4267*a72e4c9fSDaniel Vetter 			    intel_set_cpu_fifo_underrun_reporting(dev_priv,
4268*a72e4c9fSDaniel Vetter 								  pipe, false))
4269fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
42702d9d2b0bSVille Syrjälä 		}
4271a266c7d5SChris Wilson 
4272a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4273a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4274a266c7d5SChris Wilson 
4275515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4276515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4277515ac2bbSDaniel Vetter 
4278a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4279a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4280a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4281a266c7d5SChris Wilson 		 * we would never get another interrupt.
4282a266c7d5SChris Wilson 		 *
4283a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4284a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4285a266c7d5SChris Wilson 		 * another one.
4286a266c7d5SChris Wilson 		 *
4287a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4288a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4289a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4290a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4291a266c7d5SChris Wilson 		 * stray interrupts.
4292a266c7d5SChris Wilson 		 */
4293a266c7d5SChris Wilson 		iir = new_iir;
4294a266c7d5SChris Wilson 	}
4295a266c7d5SChris Wilson 
4296d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
42972c8ba29fSChris Wilson 
4298a266c7d5SChris Wilson 	return ret;
4299a266c7d5SChris Wilson }
4300a266c7d5SChris Wilson 
4301a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4302a266c7d5SChris Wilson {
43032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4304a266c7d5SChris Wilson 	int pipe;
4305a266c7d5SChris Wilson 
4306a266c7d5SChris Wilson 	if (!dev_priv)
4307a266c7d5SChris Wilson 		return;
4308a266c7d5SChris Wilson 
4309a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4310a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4311a266c7d5SChris Wilson 
4312a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4313055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4314a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4315a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4316a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4317a266c7d5SChris Wilson 
4318055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4319a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4320a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4321a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4322a266c7d5SChris Wilson }
4323a266c7d5SChris Wilson 
43244cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4325ac4c16c5SEgbert Eich {
43266323751dSImre Deak 	struct drm_i915_private *dev_priv =
43276323751dSImre Deak 		container_of(work, typeof(*dev_priv),
43286323751dSImre Deak 			     hotplug_reenable_work.work);
4329ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4330ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4331ac4c16c5SEgbert Eich 	int i;
4332ac4c16c5SEgbert Eich 
43336323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
43346323751dSImre Deak 
43354cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4336ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4337ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4338ac4c16c5SEgbert Eich 
4339ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4340ac4c16c5SEgbert Eich 			continue;
4341ac4c16c5SEgbert Eich 
4342ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4343ac4c16c5SEgbert Eich 
4344ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4345ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4346ac4c16c5SEgbert Eich 
4347ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4348ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4349ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4350c23cc417SJani Nikula 							 connector->name);
4351ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4352ac4c16c5SEgbert Eich 				if (!connector->polled)
4353ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4354ac4c16c5SEgbert Eich 			}
4355ac4c16c5SEgbert Eich 		}
4356ac4c16c5SEgbert Eich 	}
4357ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4358ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
43594cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
43606323751dSImre Deak 
43616323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4362ac4c16c5SEgbert Eich }
4363ac4c16c5SEgbert Eich 
4364fca52a55SDaniel Vetter /**
4365fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4366fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4367fca52a55SDaniel Vetter  *
4368fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4369fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4370fca52a55SDaniel Vetter  */
4371b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4372f71d4af4SJesse Barnes {
4373b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43748b2e326dSChris Wilson 
43758b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
437613cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
437799584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4378c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4379a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43808b2e326dSChris Wilson 
4381a6706b45SDeepak S 	/* Let's track the enabled rps events */
4382b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43836c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
438431685c25SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
438531685c25SDeepak S 	else
4386a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4387a6706b45SDeepak S 
438899584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
438999584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
439061bac78eSDaniel Vetter 		    (unsigned long) dev);
43916323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
43924cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
439361bac78eSDaniel Vetter 
439497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43959ee32feaSDaniel Vetter 
4396b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43974cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43984cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4399b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4400f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4401f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4402391f75e2SVille Syrjälä 	} else {
4403391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4404391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4405f71d4af4SJesse Barnes 	}
4406f71d4af4SJesse Barnes 
440721da2700SVille Syrjälä 	/*
440821da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
440921da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
441021da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
441121da2700SVille Syrjälä 	 */
4412b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
441321da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
441421da2700SVille Syrjälä 
4415c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4416f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4417f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4418c2baf4b7SVille Syrjälä 	}
4419f71d4af4SJesse Barnes 
4420b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
442143f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
442243f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
442343f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
442443f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
442543f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
442643f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
442743f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4428b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
44297e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
44307e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
44317e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
44327e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
44337e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
44347e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4435fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4436b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4437abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4438723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4439abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4440abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4441abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4442abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4443abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4444f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4445f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4446723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4447f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4448f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4449f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4450f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
445182a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4452f71d4af4SJesse Barnes 	} else {
4453b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4454c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4455c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4456c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4457c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4458b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4459a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4460a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4461a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4462a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
446320afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4464c2798b19SChris Wilson 		} else {
4465a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4466a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4467a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4468a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4469bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4470c2798b19SChris Wilson 		}
4471f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4472f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4473f71d4af4SJesse Barnes 	}
4474f71d4af4SJesse Barnes }
447520afbda2SDaniel Vetter 
4476fca52a55SDaniel Vetter /**
4477fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4478fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4479fca52a55SDaniel Vetter  *
4480fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4481fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4482fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4483fca52a55SDaniel Vetter  * obeyed.
4484fca52a55SDaniel Vetter  *
4485fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4486fca52a55SDaniel Vetter  * in the driver load and resume code.
4487fca52a55SDaniel Vetter  */
4488b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
448920afbda2SDaniel Vetter {
4490b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4491821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4492821450c6SEgbert Eich 	struct drm_connector *connector;
4493821450c6SEgbert Eich 	int i;
449420afbda2SDaniel Vetter 
4495821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4496821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4497821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4498821450c6SEgbert Eich 	}
4499821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4500821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4501821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
45020e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
45030e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
45040e32b39cSDave Airlie 		if (intel_connector->mst_port)
4505821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4506821450c6SEgbert Eich 	}
4507b5ea2d56SDaniel Vetter 
4508b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4509b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4510d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
451120afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
451220afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4513d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
451420afbda2SDaniel Vetter }
4515c67a470bSPaulo Zanoni 
4516fca52a55SDaniel Vetter /**
4517fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4518fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4519fca52a55SDaniel Vetter  *
4520fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4521fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4522fca52a55SDaniel Vetter  *
4523fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4524fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4525fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4526fca52a55SDaniel Vetter  */
45272aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
45282aeb7d3aSDaniel Vetter {
45292aeb7d3aSDaniel Vetter 	/*
45302aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
45312aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
45322aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
45332aeb7d3aSDaniel Vetter 	 */
45342aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
45352aeb7d3aSDaniel Vetter 
45362aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
45372aeb7d3aSDaniel Vetter }
45382aeb7d3aSDaniel Vetter 
4539fca52a55SDaniel Vetter /**
4540fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4541fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4542fca52a55SDaniel Vetter  *
4543fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4544fca52a55SDaniel Vetter  * resources acquired in the init functions.
4545fca52a55SDaniel Vetter  */
45462aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
45472aeb7d3aSDaniel Vetter {
45482aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
45492aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
45502aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
45512aeb7d3aSDaniel Vetter }
45522aeb7d3aSDaniel Vetter 
4553fca52a55SDaniel Vetter /**
4554fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4555fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4556fca52a55SDaniel Vetter  *
4557fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4558fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4559fca52a55SDaniel Vetter  */
4560b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4561c67a470bSPaulo Zanoni {
4562b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
45632aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
4564c67a470bSPaulo Zanoni }
4565c67a470bSPaulo Zanoni 
4566fca52a55SDaniel Vetter /**
4567fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4568fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4569fca52a55SDaniel Vetter  *
4570fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4571fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4572fca52a55SDaniel Vetter  */
4573b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4574c67a470bSPaulo Zanoni {
45752aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4576b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4577b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4578c67a470bSPaulo Zanoni }
4579