xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision a2c30fbafceb937eb3c3535a21810d4458628e62)
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##_IER(which), (ier_val)); \
1307d1bd539SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
1317d1bd539SVille Syrjälä 	POSTING_READ(GEN8_##type##_IMR(which)); \
13235079899SPaulo Zanoni } while (0)
13335079899SPaulo Zanoni 
13435079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
135337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
13635079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
1377d1bd539SVille Syrjälä 	I915_WRITE(type##IMR, (imr_val)); \
1387d1bd539SVille Syrjälä 	POSTING_READ(type##IMR); \
13935079899SPaulo Zanoni } while (0)
14035079899SPaulo Zanoni 
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 		 */
17520f239f4cSDaniel Vetter 
17530f239f4cSDaniel Vetter 		/* fifo underruns are filterered in the underrun handler. */
17540f239f4cSDaniel 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ä 
17991f7247c0SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
18001f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
180131acc7f5SJesse Barnes 	}
180231acc7f5SJesse Barnes 
1803c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1804c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1805c1874ed7SImre Deak }
1806c1874ed7SImre Deak 
180716c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
180816c6c56bSVille Syrjälä {
180916c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
181016c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
181116c6c56bSVille Syrjälä 
18123ff60f89SOscar Mateo 	if (hotplug_status) {
18133ff60f89SOscar Mateo 		I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
18143ff60f89SOscar Mateo 		/*
18153ff60f89SOscar Mateo 		 * Make sure hotplug status is cleared before we clear IIR, or else we
18163ff60f89SOscar Mateo 		 * may miss hotplug events.
18173ff60f89SOscar Mateo 		 */
18183ff60f89SOscar Mateo 		POSTING_READ(PORT_HOTPLUG_STAT);
18193ff60f89SOscar Mateo 
182016c6c56bSVille Syrjälä 		if (IS_G4X(dev)) {
182116c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
182216c6c56bSVille Syrjälä 
182313cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_g4x);
182416c6c56bSVille Syrjälä 		} else {
182516c6c56bSVille Syrjälä 			u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
182616c6c56bSVille Syrjälä 
182713cf5504SDave Airlie 			intel_hpd_irq_handler(dev, hotplug_trigger, 0, hpd_status_i915);
182816c6c56bSVille Syrjälä 		}
182916c6c56bSVille Syrjälä 
183016c6c56bSVille Syrjälä 		if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
183116c6c56bSVille Syrjälä 		    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
183216c6c56bSVille Syrjälä 			dp_aux_irq_handler(dev);
18333ff60f89SOscar Mateo 	}
183416c6c56bSVille Syrjälä }
183516c6c56bSVille Syrjälä 
1836c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1837c1874ed7SImre Deak {
183845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
18392d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1840c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1841c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1842c1874ed7SImre Deak 
1843c1874ed7SImre Deak 	while (true) {
18443ff60f89SOscar Mateo 		/* Find, clear, then process each source of interrupt */
18453ff60f89SOscar Mateo 
1846c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
18473ff60f89SOscar Mateo 		if (gt_iir)
18483ff60f89SOscar Mateo 			I915_WRITE(GTIIR, gt_iir);
18493ff60f89SOscar Mateo 
1850c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
18513ff60f89SOscar Mateo 		if (pm_iir)
18523ff60f89SOscar Mateo 			I915_WRITE(GEN6_PMIIR, pm_iir);
18533ff60f89SOscar Mateo 
18543ff60f89SOscar Mateo 		iir = I915_READ(VLV_IIR);
18553ff60f89SOscar Mateo 		if (iir) {
18563ff60f89SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
18573ff60f89SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
18583ff60f89SOscar Mateo 				i9xx_hpd_irq_handler(dev);
18593ff60f89SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
18603ff60f89SOscar Mateo 		}
1861c1874ed7SImre Deak 
1862c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1863c1874ed7SImre Deak 			goto out;
1864c1874ed7SImre Deak 
1865c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1866c1874ed7SImre Deak 
18673ff60f89SOscar Mateo 		if (gt_iir)
1868c1874ed7SImre Deak 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
186960611c13SPaulo Zanoni 		if (pm_iir)
1870d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18713ff60f89SOscar Mateo 		/* Call regardless, as some status bits might not be
18723ff60f89SOscar Mateo 		 * signalled in iir */
18733ff60f89SOscar Mateo 		valleyview_pipestat_irq_handler(dev, iir);
18747e231dbeSJesse Barnes 	}
18757e231dbeSJesse Barnes 
18767e231dbeSJesse Barnes out:
18777e231dbeSJesse Barnes 	return ret;
18787e231dbeSJesse Barnes }
18797e231dbeSJesse Barnes 
188043f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
188143f328d7SVille Syrjälä {
188245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
188343f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
188443f328d7SVille Syrjälä 	u32 master_ctl, iir;
188543f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
188643f328d7SVille Syrjälä 
18878e5fd599SVille Syrjälä 	for (;;) {
18888e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18893278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18903278f67fSVille Syrjälä 
18913278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18928e5fd599SVille Syrjälä 			break;
189343f328d7SVille Syrjälä 
189427b6c122SOscar Mateo 		ret = IRQ_HANDLED;
189527b6c122SOscar Mateo 
189643f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
189743f328d7SVille Syrjälä 
189827b6c122SOscar Mateo 		/* Find, clear, then process each source of interrupt */
189927b6c122SOscar Mateo 
190027b6c122SOscar Mateo 		if (iir) {
190127b6c122SOscar Mateo 			/* Consume port before clearing IIR or we'll miss events */
190227b6c122SOscar Mateo 			if (iir & I915_DISPLAY_PORT_INTERRUPT)
190327b6c122SOscar Mateo 				i9xx_hpd_irq_handler(dev);
190427b6c122SOscar Mateo 			I915_WRITE(VLV_IIR, iir);
190527b6c122SOscar Mateo 		}
190627b6c122SOscar Mateo 
19073278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
190843f328d7SVille Syrjälä 
190927b6c122SOscar Mateo 		/* Call regardless, as some status bits might not be
191027b6c122SOscar Mateo 		 * signalled in iir */
19113278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
191243f328d7SVille Syrjälä 
191343f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
191443f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
19158e5fd599SVille Syrjälä 	}
19163278f67fSVille Syrjälä 
191743f328d7SVille Syrjälä 	return ret;
191843f328d7SVille Syrjälä }
191943f328d7SVille Syrjälä 
192023e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1921776ad806SJesse Barnes {
19222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
19239db4a9c7SJesse Barnes 	int pipe;
1924b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
192513cf5504SDave Airlie 	u32 dig_hotplug_reg;
1926776ad806SJesse Barnes 
192713cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
192813cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
192913cf5504SDave Airlie 
193013cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_ibx);
193191d131d2SDaniel Vetter 
1932cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1933cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1934776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1935cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1936cfc33bf7SVille Syrjälä 				 port_name(port));
1937cfc33bf7SVille Syrjälä 	}
1938776ad806SJesse Barnes 
1939ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1940ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1941ce99c256SDaniel Vetter 
1942776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1943515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1944776ad806SJesse Barnes 
1945776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1946776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1947776ad806SJesse Barnes 
1948776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1949776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1950776ad806SJesse Barnes 
1951776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1952776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1953776ad806SJesse Barnes 
19549db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
1955055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
19569db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19579db4a9c7SJesse Barnes 					 pipe_name(pipe),
19589db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1959776ad806SJesse Barnes 
1960776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1961776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1962776ad806SJesse Barnes 
1963776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1964776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1965776ad806SJesse Barnes 
1966776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19671f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
19688664281bSPaulo Zanoni 
19698664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19701f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
19718664281bSPaulo Zanoni }
19728664281bSPaulo Zanoni 
19738664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19748664281bSPaulo Zanoni {
19758664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19768664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19775a69b89fSDaniel Vetter 	enum pipe pipe;
19788664281bSPaulo Zanoni 
1979de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1980de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1981de032bf4SPaulo Zanoni 
1982055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
19831f7247c0SDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe))
19841f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
19858664281bSPaulo Zanoni 
19865a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19875a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1988277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19895a69b89fSDaniel Vetter 			else
1990277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19915a69b89fSDaniel Vetter 		}
19925a69b89fSDaniel Vetter 	}
19938bf1e9f1SShuang He 
19948664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19958664281bSPaulo Zanoni }
19968664281bSPaulo Zanoni 
19978664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19988664281bSPaulo Zanoni {
19998664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
20008664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
20018664281bSPaulo Zanoni 
2002de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
2003de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
2004de032bf4SPaulo Zanoni 
20058664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
20061f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_A);
20078664281bSPaulo Zanoni 
20088664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
20091f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_B);
20108664281bSPaulo Zanoni 
20118664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
20121f7247c0SDaniel Vetter 		intel_pch_fifo_underrun_irq_handler(dev_priv, TRANSCODER_C);
20138664281bSPaulo Zanoni 
20148664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2015776ad806SJesse Barnes }
2016776ad806SJesse Barnes 
201723e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
201823e81d69SAdam Jackson {
20192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
202023e81d69SAdam Jackson 	int pipe;
2021b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
202213cf5504SDave Airlie 	u32 dig_hotplug_reg;
202323e81d69SAdam Jackson 
202413cf5504SDave Airlie 	dig_hotplug_reg = I915_READ(PCH_PORT_HOTPLUG);
202513cf5504SDave Airlie 	I915_WRITE(PCH_PORT_HOTPLUG, dig_hotplug_reg);
202613cf5504SDave Airlie 
202713cf5504SDave Airlie 	intel_hpd_irq_handler(dev, hotplug_trigger, dig_hotplug_reg, hpd_cpt);
202891d131d2SDaniel Vetter 
2029cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2030cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
203123e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2032cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2033cfc33bf7SVille Syrjälä 				 port_name(port));
2034cfc33bf7SVille Syrjälä 	}
203523e81d69SAdam Jackson 
203623e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2037ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
203823e81d69SAdam Jackson 
203923e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2040515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
204123e81d69SAdam Jackson 
204223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
204323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
204423e81d69SAdam Jackson 
204523e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
204623e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
204723e81d69SAdam Jackson 
204823e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
2049055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
205023e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
205123e81d69SAdam Jackson 					 pipe_name(pipe),
205223e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20538664281bSPaulo Zanoni 
20548664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20558664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
205623e81d69SAdam Jackson }
205723e81d69SAdam Jackson 
2058c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2059c008bc6eSPaulo Zanoni {
2060c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
206140da17c2SDaniel Vetter 	enum pipe pipe;
2062c008bc6eSPaulo Zanoni 
2063c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2064c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2065c008bc6eSPaulo Zanoni 
2066c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2067c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2068c008bc6eSPaulo Zanoni 
2069c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2070c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2071c008bc6eSPaulo Zanoni 
2072055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2073d6bbafa1SChris Wilson 		if (de_iir & DE_PIPE_VBLANK(pipe) &&
2074d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2075d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
2076c008bc6eSPaulo Zanoni 
207740da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
20781f7247c0SDaniel Vetter 			intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
2079c008bc6eSPaulo Zanoni 
208040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
208140da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20825b3a856bSDaniel Vetter 
208340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
208440da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
208540da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
208640da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2087c008bc6eSPaulo Zanoni 		}
2088c008bc6eSPaulo Zanoni 	}
2089c008bc6eSPaulo Zanoni 
2090c008bc6eSPaulo Zanoni 	/* check event from PCH */
2091c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2092c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2093c008bc6eSPaulo Zanoni 
2094c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2095c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2096c008bc6eSPaulo Zanoni 		else
2097c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2098c008bc6eSPaulo Zanoni 
2099c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2100c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2101c008bc6eSPaulo Zanoni 	}
2102c008bc6eSPaulo Zanoni 
2103c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2104c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2105c008bc6eSPaulo Zanoni }
2106c008bc6eSPaulo Zanoni 
21079719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21089719fb98SPaulo Zanoni {
21099719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
211007d27e20SDamien Lespiau 	enum pipe pipe;
21119719fb98SPaulo Zanoni 
21129719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21139719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21149719fb98SPaulo Zanoni 
21159719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21169719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21179719fb98SPaulo Zanoni 
21189719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21199719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21209719fb98SPaulo Zanoni 
2121055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2122d6bbafa1SChris Wilson 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)) &&
2123d6bbafa1SChris Wilson 		    intel_pipe_handle_vblank(dev, pipe))
2124d6bbafa1SChris Wilson 			intel_check_page_flip(dev, pipe);
212540da17c2SDaniel Vetter 
212640da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
212707d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
212807d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
212907d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21309719fb98SPaulo Zanoni 		}
21319719fb98SPaulo Zanoni 	}
21329719fb98SPaulo Zanoni 
21339719fb98SPaulo Zanoni 	/* check event from PCH */
21349719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21359719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21369719fb98SPaulo Zanoni 
21379719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21389719fb98SPaulo Zanoni 
21399719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21409719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21419719fb98SPaulo Zanoni 	}
21429719fb98SPaulo Zanoni }
21439719fb98SPaulo Zanoni 
214472c90f62SOscar Mateo /*
214572c90f62SOscar Mateo  * To handle irqs with the minimum potential races with fresh interrupts, we:
214672c90f62SOscar Mateo  * 1 - Disable Master Interrupt Control.
214772c90f62SOscar Mateo  * 2 - Find the source(s) of the interrupt.
214872c90f62SOscar Mateo  * 3 - Clear the Interrupt Identity bits (IIR).
214972c90f62SOscar Mateo  * 4 - Process the interrupt(s) that had bits set in the IIRs.
215072c90f62SOscar Mateo  * 5 - Re-enable Master Interrupt Control.
215172c90f62SOscar Mateo  */
2152f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2153b1f14ad0SJesse Barnes {
215445a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2156f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21570e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2158b1f14ad0SJesse Barnes 
21598664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21608664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2161907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21628664281bSPaulo Zanoni 
2163b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2164b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2165b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
216623a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21670e43406bSChris Wilson 
216844498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
216944498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
217044498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
217144498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
217244498aeaSPaulo Zanoni 	 * due to its back queue). */
2173ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
217444498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
217544498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
217644498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2177ab5c608bSBen Widawsky 	}
217844498aeaSPaulo Zanoni 
217972c90f62SOscar Mateo 	/* Find, clear, then process each source of interrupt */
218072c90f62SOscar Mateo 
21810e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21820e43406bSChris Wilson 	if (gt_iir) {
218372c90f62SOscar Mateo 		I915_WRITE(GTIIR, gt_iir);
218472c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2185d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21860e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2187d8fc8a47SPaulo Zanoni 		else
2188d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21890e43406bSChris Wilson 	}
2190b1f14ad0SJesse Barnes 
2191b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21920e43406bSChris Wilson 	if (de_iir) {
219372c90f62SOscar Mateo 		I915_WRITE(DEIIR, de_iir);
219472c90f62SOscar Mateo 		ret = IRQ_HANDLED;
2195f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21969719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2197f1af8fc1SPaulo Zanoni 		else
2198f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21990e43406bSChris Wilson 	}
22000e43406bSChris Wilson 
2201f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2202f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
22030e43406bSChris Wilson 		if (pm_iir) {
2204b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
22050e43406bSChris Wilson 			ret = IRQ_HANDLED;
220672c90f62SOscar Mateo 			gen6_rps_irq_handler(dev_priv, pm_iir);
22070e43406bSChris Wilson 		}
2208f1af8fc1SPaulo Zanoni 	}
2209b1f14ad0SJesse Barnes 
2210b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2211b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2212ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
221344498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
221444498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2215ab5c608bSBen Widawsky 	}
2216b1f14ad0SJesse Barnes 
2217b1f14ad0SJesse Barnes 	return ret;
2218b1f14ad0SJesse Barnes }
2219b1f14ad0SJesse Barnes 
2220abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2221abd58f01SBen Widawsky {
2222abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2223abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2224abd58f01SBen Widawsky 	u32 master_ctl;
2225abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2226abd58f01SBen Widawsky 	uint32_t tmp = 0;
2227c42664ccSDaniel Vetter 	enum pipe pipe;
2228abd58f01SBen Widawsky 
2229abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2230abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2231abd58f01SBen Widawsky 	if (!master_ctl)
2232abd58f01SBen Widawsky 		return IRQ_NONE;
2233abd58f01SBen Widawsky 
2234abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2235abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2236abd58f01SBen Widawsky 
223738cc46d7SOscar Mateo 	/* Find, clear, then process each source of interrupt */
223838cc46d7SOscar Mateo 
2239abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2240abd58f01SBen Widawsky 
2241abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2242abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2243abd58f01SBen Widawsky 		if (tmp) {
2244abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2245abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
224638cc46d7SOscar Mateo 			if (tmp & GEN8_DE_MISC_GSE)
224738cc46d7SOscar Mateo 				intel_opregion_asle_intr(dev);
224838cc46d7SOscar Mateo 			else
224938cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Misc interrupt\n");
2250abd58f01SBen Widawsky 		}
225138cc46d7SOscar Mateo 		else
225238cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2253abd58f01SBen Widawsky 	}
2254abd58f01SBen Widawsky 
22556d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22566d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22576d766f02SDaniel Vetter 		if (tmp) {
22586d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22596d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
226038cc46d7SOscar Mateo 			if (tmp & GEN8_AUX_CHANNEL_A)
226138cc46d7SOscar Mateo 				dp_aux_irq_handler(dev);
226238cc46d7SOscar Mateo 			else
226338cc46d7SOscar Mateo 				DRM_ERROR("Unexpected DE Port interrupt\n");
22646d766f02SDaniel Vetter 		}
226538cc46d7SOscar Mateo 		else
226638cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22676d766f02SDaniel Vetter 	}
22686d766f02SDaniel Vetter 
2269055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
2270770de83dSDamien Lespiau 		uint32_t pipe_iir, flip_done = 0, fault_errors = 0;
2271abd58f01SBen Widawsky 
2272c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2273c42664ccSDaniel Vetter 			continue;
2274c42664ccSDaniel Vetter 
2275abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
227638cc46d7SOscar Mateo 		if (pipe_iir) {
227738cc46d7SOscar Mateo 			ret = IRQ_HANDLED;
227838cc46d7SOscar Mateo 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2279770de83dSDamien Lespiau 
2280d6bbafa1SChris Wilson 			if (pipe_iir & GEN8_PIPE_VBLANK &&
2281d6bbafa1SChris Wilson 			    intel_pipe_handle_vblank(dev, pipe))
2282d6bbafa1SChris Wilson 				intel_check_page_flip(dev, pipe);
2283abd58f01SBen Widawsky 
2284770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2285770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN9_PIPE_PLANE1_FLIP_DONE;
2286770de83dSDamien Lespiau 			else
2287770de83dSDamien Lespiau 				flip_done = pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE;
2288770de83dSDamien Lespiau 
2289770de83dSDamien Lespiau 			if (flip_done) {
2290abd58f01SBen Widawsky 				intel_prepare_page_flip(dev, pipe);
2291abd58f01SBen Widawsky 				intel_finish_page_flip_plane(dev, pipe);
2292abd58f01SBen Widawsky 			}
2293abd58f01SBen Widawsky 
22940fbe7870SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22950fbe7870SDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
22960fbe7870SDaniel Vetter 
22971f7247c0SDaniel Vetter 			if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN)
22981f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
22991f7247c0SDaniel Vetter 								    pipe);
230038d83c96SDaniel Vetter 
2301770de83dSDamien Lespiau 
2302770de83dSDamien Lespiau 			if (IS_GEN9(dev))
2303770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
2304770de83dSDamien Lespiau 			else
2305770de83dSDamien Lespiau 				fault_errors = pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
2306770de83dSDamien Lespiau 
2307770de83dSDamien Lespiau 			if (fault_errors)
230830100f2bSDaniel Vetter 				DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
230930100f2bSDaniel Vetter 					  pipe_name(pipe),
231030100f2bSDaniel Vetter 					  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
2311c42664ccSDaniel Vetter 		} else
2312abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2313abd58f01SBen Widawsky 	}
2314abd58f01SBen Widawsky 
231592d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
231692d03a80SDaniel Vetter 		/*
231792d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
231892d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
231992d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
232092d03a80SDaniel Vetter 		 */
232192d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
232292d03a80SDaniel Vetter 		if (pch_iir) {
232392d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
232492d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
232538cc46d7SOscar Mateo 			cpt_irq_handler(dev, pch_iir);
232638cc46d7SOscar Mateo 		} else
232738cc46d7SOscar Mateo 			DRM_ERROR("The master control interrupt lied (SDE)!\n");
232838cc46d7SOscar Mateo 
232992d03a80SDaniel Vetter 	}
233092d03a80SDaniel Vetter 
2331abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2332abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2333abd58f01SBen Widawsky 
2334abd58f01SBen Widawsky 	return ret;
2335abd58f01SBen Widawsky }
2336abd58f01SBen Widawsky 
233717e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
233817e1df07SDaniel Vetter 			       bool reset_completed)
233917e1df07SDaniel Vetter {
2340a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
234117e1df07SDaniel Vetter 	int i;
234217e1df07SDaniel Vetter 
234317e1df07SDaniel Vetter 	/*
234417e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
234517e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
234617e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
234717e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
234817e1df07SDaniel Vetter 	 */
234917e1df07SDaniel Vetter 
235017e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
235117e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
235217e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
235317e1df07SDaniel Vetter 
235417e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
235517e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
235617e1df07SDaniel Vetter 
235717e1df07SDaniel Vetter 	/*
235817e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
235917e1df07SDaniel Vetter 	 * reset state is cleared.
236017e1df07SDaniel Vetter 	 */
236117e1df07SDaniel Vetter 	if (reset_completed)
236217e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
236317e1df07SDaniel Vetter }
236417e1df07SDaniel Vetter 
23658a905236SJesse Barnes /**
23668a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
23678a905236SJesse Barnes  * @work: work struct
23688a905236SJesse Barnes  *
23698a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23708a905236SJesse Barnes  * was detected.
23718a905236SJesse Barnes  */
23728a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
23738a905236SJesse Barnes {
23741f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
23751f83fee0SDaniel Vetter 						    work);
23762d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
23772d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
23788a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2379cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2380cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2381cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
238217e1df07SDaniel Vetter 	int ret;
23838a905236SJesse Barnes 
23845bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23858a905236SJesse Barnes 
23867db0ba24SDaniel Vetter 	/*
23877db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23887db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23897db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23907db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23917db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23927db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23937db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23947db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23957db0ba24SDaniel Vetter 	 */
23967db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
239744d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23985bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23997db0ba24SDaniel Vetter 				   reset_event);
24001f83fee0SDaniel Vetter 
240117e1df07SDaniel Vetter 		/*
2402f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2403f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2404f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2405f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2406f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2407f454c694SImre Deak 		 */
2408f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
2409f454c694SImre Deak 		/*
241017e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
241117e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
241217e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
241317e1df07SDaniel Vetter 		 * deadlocks with the reset work.
241417e1df07SDaniel Vetter 		 */
2415f69061beSDaniel Vetter 		ret = i915_reset(dev);
2416f69061beSDaniel Vetter 
241717e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
241817e1df07SDaniel Vetter 
2419f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2420f454c694SImre Deak 
2421f69061beSDaniel Vetter 		if (ret == 0) {
2422f69061beSDaniel Vetter 			/*
2423f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2424f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2425f69061beSDaniel Vetter 			 * complete.
2426f69061beSDaniel Vetter 			 *
2427f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2428f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2429f69061beSDaniel Vetter 			 * updates before
2430f69061beSDaniel Vetter 			 * the counter increment.
2431f69061beSDaniel Vetter 			 */
24324e857c58SPeter Zijlstra 			smp_mb__before_atomic();
2433f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2434f69061beSDaniel Vetter 
24355bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2436f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24371f83fee0SDaniel Vetter 		} else {
24382ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2439f316a42cSBen Gamari 		}
24401f83fee0SDaniel Vetter 
244117e1df07SDaniel Vetter 		/*
244217e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
244317e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
244417e1df07SDaniel Vetter 		 */
244517e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2446f316a42cSBen Gamari 	}
24478a905236SJesse Barnes }
24488a905236SJesse Barnes 
244935aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2450c0e09200SDave Airlie {
24518a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2452bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
245363eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2454050ee91fSBen Widawsky 	int pipe, i;
245563eeaf38SJesse Barnes 
245635aed2e6SChris Wilson 	if (!eir)
245735aed2e6SChris Wilson 		return;
245863eeaf38SJesse Barnes 
2459a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24608a905236SJesse Barnes 
2461bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2462bd9854f9SBen Widawsky 
24638a905236SJesse Barnes 	if (IS_G4X(dev)) {
24648a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24658a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24668a905236SJesse Barnes 
2467a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2468a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2469050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2470050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2471a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2472a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24738a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24743143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24758a905236SJesse Barnes 		}
24768a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24778a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2478a70491ccSJoe Perches 			pr_err("page table error\n");
2479a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24808a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24813143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24828a905236SJesse Barnes 		}
24838a905236SJesse Barnes 	}
24848a905236SJesse Barnes 
2485a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
248663eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
248763eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2488a70491ccSJoe Perches 			pr_err("page table error\n");
2489a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
249063eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24913143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
249263eeaf38SJesse Barnes 		}
24938a905236SJesse Barnes 	}
24948a905236SJesse Barnes 
249563eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2496a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
2497055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe)
2498a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24999db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
250063eeaf38SJesse Barnes 		/* pipestat has already been acked */
250163eeaf38SJesse Barnes 	}
250263eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2503a70491ccSJoe Perches 		pr_err("instruction error\n");
2504a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2505050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2506050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2507a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
250863eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
250963eeaf38SJesse Barnes 
2510a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2511a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2512a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
251363eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
25143143a2bfSChris Wilson 			POSTING_READ(IPEIR);
251563eeaf38SJesse Barnes 		} else {
251663eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
251763eeaf38SJesse Barnes 
2518a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2519a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2520a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2521a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
252263eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25233143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
252463eeaf38SJesse Barnes 		}
252563eeaf38SJesse Barnes 	}
252663eeaf38SJesse Barnes 
252763eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25283143a2bfSChris Wilson 	POSTING_READ(EIR);
252963eeaf38SJesse Barnes 	eir = I915_READ(EIR);
253063eeaf38SJesse Barnes 	if (eir) {
253163eeaf38SJesse Barnes 		/*
253263eeaf38SJesse Barnes 		 * some errors might have become stuck,
253363eeaf38SJesse Barnes 		 * mask them.
253463eeaf38SJesse Barnes 		 */
253563eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
253663eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
253763eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
253863eeaf38SJesse Barnes 	}
253935aed2e6SChris Wilson }
254035aed2e6SChris Wilson 
254135aed2e6SChris Wilson /**
254235aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
254335aed2e6SChris Wilson  * @dev: drm device
254435aed2e6SChris Wilson  *
254535aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
254635aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
254735aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
254835aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
254935aed2e6SChris Wilson  * of a ring dump etc.).
255035aed2e6SChris Wilson  */
255158174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
255258174462SMika Kuoppala 		       const char *fmt, ...)
255335aed2e6SChris Wilson {
255435aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
255558174462SMika Kuoppala 	va_list args;
255658174462SMika Kuoppala 	char error_msg[80];
255735aed2e6SChris Wilson 
255858174462SMika Kuoppala 	va_start(args, fmt);
255958174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
256058174462SMika Kuoppala 	va_end(args);
256158174462SMika Kuoppala 
256258174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
256335aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25648a905236SJesse Barnes 
2565ba1234d1SBen Gamari 	if (wedged) {
2566f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2567f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2568ba1234d1SBen Gamari 
256911ed50ecSBen Gamari 		/*
257017e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
257117e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
257217e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
257317e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
257417e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
257517e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
257617e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
257717e1df07SDaniel Vetter 		 *
257817e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
257917e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
258017e1df07SDaniel Vetter 		 * counter atomic_t.
258111ed50ecSBen Gamari 		 */
258217e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
258311ed50ecSBen Gamari 	}
258411ed50ecSBen Gamari 
2585122f46baSDaniel Vetter 	/*
2586122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2587122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2588122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2589122f46baSDaniel Vetter 	 * code will deadlock.
2590122f46baSDaniel Vetter 	 */
2591122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
25928a905236SJesse Barnes }
25938a905236SJesse Barnes 
259442f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
259542f52ef8SKeith Packard  * we use as a pipe index
259642f52ef8SKeith Packard  */
2597f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
25980a3e67a4SJesse Barnes {
25992d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2600e9d21d7fSKeith Packard 	unsigned long irqflags;
260171e0ffa5SJesse Barnes 
26025eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
260371e0ffa5SJesse Barnes 		return -EINVAL;
26040a3e67a4SJesse Barnes 
26051ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2606f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26077c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2608755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26090a3e67a4SJesse Barnes 	else
26107c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2611755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26121ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26138692d00eSChris Wilson 
26140a3e67a4SJesse Barnes 	return 0;
26150a3e67a4SJesse Barnes }
26160a3e67a4SJesse Barnes 
2617f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2618f796cf8fSJesse Barnes {
26192d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2620f796cf8fSJesse Barnes 	unsigned long irqflags;
2621b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
262240da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2623f796cf8fSJesse Barnes 
2624f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2625f796cf8fSJesse Barnes 		return -EINVAL;
2626f796cf8fSJesse Barnes 
2627f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2628b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2629b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2630b1f14ad0SJesse Barnes 
2631b1f14ad0SJesse Barnes 	return 0;
2632b1f14ad0SJesse Barnes }
2633b1f14ad0SJesse Barnes 
26347e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26357e231dbeSJesse Barnes {
26362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26377e231dbeSJesse Barnes 	unsigned long irqflags;
26387e231dbeSJesse Barnes 
26397e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
26407e231dbeSJesse Barnes 		return -EINVAL;
26417e231dbeSJesse Barnes 
26427e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
264331acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2644755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26457e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26467e231dbeSJesse Barnes 
26477e231dbeSJesse Barnes 	return 0;
26487e231dbeSJesse Barnes }
26497e231dbeSJesse Barnes 
2650abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2651abd58f01SBen Widawsky {
2652abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2653abd58f01SBen Widawsky 	unsigned long irqflags;
2654abd58f01SBen Widawsky 
2655abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2656abd58f01SBen Widawsky 		return -EINVAL;
2657abd58f01SBen Widawsky 
2658abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26597167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26607167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2661abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2662abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2663abd58f01SBen Widawsky 	return 0;
2664abd58f01SBen Widawsky }
2665abd58f01SBen Widawsky 
266642f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
266742f52ef8SKeith Packard  * we use as a pipe index
266842f52ef8SKeith Packard  */
2669f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26700a3e67a4SJesse Barnes {
26712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2672e9d21d7fSKeith Packard 	unsigned long irqflags;
26730a3e67a4SJesse Barnes 
26741ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26757c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2676755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2677755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
26781ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26790a3e67a4SJesse Barnes }
26800a3e67a4SJesse Barnes 
2681f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2682f796cf8fSJesse Barnes {
26832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2684f796cf8fSJesse Barnes 	unsigned long irqflags;
2685b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
268640da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2687f796cf8fSJesse Barnes 
2688f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2689b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2690b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2691b1f14ad0SJesse Barnes }
2692b1f14ad0SJesse Barnes 
26937e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
26947e231dbeSJesse Barnes {
26952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26967e231dbeSJesse Barnes 	unsigned long irqflags;
26977e231dbeSJesse Barnes 
26987e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
269931acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2700755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27017e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27027e231dbeSJesse Barnes }
27037e231dbeSJesse Barnes 
2704abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2705abd58f01SBen Widawsky {
2706abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2707abd58f01SBen Widawsky 	unsigned long irqflags;
2708abd58f01SBen Widawsky 
2709abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2710abd58f01SBen Widawsky 		return;
2711abd58f01SBen Widawsky 
2712abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27137167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27147167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2715abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2716abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2717abd58f01SBen Widawsky }
2718abd58f01SBen Widawsky 
2719893eead0SChris Wilson static u32
2720a4872ba6SOscar Mateo ring_last_seqno(struct intel_engine_cs *ring)
2721852835f3SZou Nan hai {
2722893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2723893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2724893eead0SChris Wilson }
2725893eead0SChris Wilson 
27269107e9d2SChris Wilson static bool
2727a4872ba6SOscar Mateo ring_idle(struct intel_engine_cs *ring, u32 seqno)
2728893eead0SChris Wilson {
27299107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
27309107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2731f65d9421SBen Gamari }
2732f65d9421SBen Gamari 
2733a028c4b0SDaniel Vetter static bool
2734a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2735a028c4b0SDaniel Vetter {
2736a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2737a6cdb93aSRodrigo Vivi 		return (ipehr >> 23) == 0x1c;
2738a028c4b0SDaniel Vetter 	} else {
2739a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2740a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2741a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2742a028c4b0SDaniel Vetter 	}
2743a028c4b0SDaniel Vetter }
2744a028c4b0SDaniel Vetter 
2745a4872ba6SOscar Mateo static struct intel_engine_cs *
2746a6cdb93aSRodrigo Vivi semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr, u64 offset)
2747921d42eaSDaniel Vetter {
2748921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2749a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2750921d42eaSDaniel Vetter 	int i;
2751921d42eaSDaniel Vetter 
2752921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2753a6cdb93aSRodrigo Vivi 		for_each_ring(signaller, dev_priv, i) {
2754a6cdb93aSRodrigo Vivi 			if (ring == signaller)
2755a6cdb93aSRodrigo Vivi 				continue;
2756a6cdb93aSRodrigo Vivi 
2757a6cdb93aSRodrigo Vivi 			if (offset == signaller->semaphore.signal_ggtt[ring->id])
2758a6cdb93aSRodrigo Vivi 				return signaller;
2759a6cdb93aSRodrigo Vivi 		}
2760921d42eaSDaniel Vetter 	} else {
2761921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2762921d42eaSDaniel Vetter 
2763921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2764921d42eaSDaniel Vetter 			if(ring == signaller)
2765921d42eaSDaniel Vetter 				continue;
2766921d42eaSDaniel Vetter 
2767ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2768921d42eaSDaniel Vetter 				return signaller;
2769921d42eaSDaniel Vetter 		}
2770921d42eaSDaniel Vetter 	}
2771921d42eaSDaniel Vetter 
2772a6cdb93aSRodrigo Vivi 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x, offset 0x%016llx\n",
2773a6cdb93aSRodrigo Vivi 		  ring->id, ipehr, offset);
2774921d42eaSDaniel Vetter 
2775921d42eaSDaniel Vetter 	return NULL;
2776921d42eaSDaniel Vetter }
2777921d42eaSDaniel Vetter 
2778a4872ba6SOscar Mateo static struct intel_engine_cs *
2779a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2780a24a11e6SChris Wilson {
2781a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
278288fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
2783a6cdb93aSRodrigo Vivi 	u64 offset = 0;
2784a6cdb93aSRodrigo Vivi 	int i, backwards;
2785a24a11e6SChris Wilson 
2786a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2787a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
27886274f212SChris Wilson 		return NULL;
2789a24a11e6SChris Wilson 
279088fe429dSDaniel Vetter 	/*
279188fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
279288fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
2793a6cdb93aSRodrigo Vivi 	 * or 4 dwords depending on the semaphore wait command size.
2794a6cdb93aSRodrigo Vivi 	 * Note that we don't care about ACTHD here since that might
279588fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
279688fe429dSDaniel Vetter 	 * ringbuffer itself.
2797a24a11e6SChris Wilson 	 */
279888fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
2799a6cdb93aSRodrigo Vivi 	backwards = (INTEL_INFO(ring->dev)->gen >= 8) ? 5 : 4;
280088fe429dSDaniel Vetter 
2801a6cdb93aSRodrigo Vivi 	for (i = backwards; i; --i) {
280288fe429dSDaniel Vetter 		/*
280388fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
280488fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
280588fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
280688fe429dSDaniel Vetter 		 */
2807ee1b1e5eSOscar Mateo 		head &= ring->buffer->size - 1;
280888fe429dSDaniel Vetter 
280988fe429dSDaniel Vetter 		/* This here seems to blow up */
2810ee1b1e5eSOscar Mateo 		cmd = ioread32(ring->buffer->virtual_start + head);
2811a24a11e6SChris Wilson 		if (cmd == ipehr)
2812a24a11e6SChris Wilson 			break;
2813a24a11e6SChris Wilson 
281488fe429dSDaniel Vetter 		head -= 4;
281588fe429dSDaniel Vetter 	}
2816a24a11e6SChris Wilson 
281788fe429dSDaniel Vetter 	if (!i)
281888fe429dSDaniel Vetter 		return NULL;
281988fe429dSDaniel Vetter 
2820ee1b1e5eSOscar Mateo 	*seqno = ioread32(ring->buffer->virtual_start + head + 4) + 1;
2821a6cdb93aSRodrigo Vivi 	if (INTEL_INFO(ring->dev)->gen >= 8) {
2822a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 12);
2823a6cdb93aSRodrigo Vivi 		offset <<= 32;
2824a6cdb93aSRodrigo Vivi 		offset = ioread32(ring->buffer->virtual_start + head + 8);
2825a6cdb93aSRodrigo Vivi 	}
2826a6cdb93aSRodrigo Vivi 	return semaphore_wait_to_signaller_ring(ring, ipehr, offset);
2827a24a11e6SChris Wilson }
2828a24a11e6SChris Wilson 
2829a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28306274f212SChris Wilson {
28316274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2832a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2833a0d036b0SChris Wilson 	u32 seqno;
28346274f212SChris Wilson 
28354be17381SChris Wilson 	ring->hangcheck.deadlock++;
28366274f212SChris Wilson 
28376274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28384be17381SChris Wilson 	if (signaller == NULL)
28394be17381SChris Wilson 		return -1;
28404be17381SChris Wilson 
28414be17381SChris Wilson 	/* Prevent pathological recursion due to driver bugs */
28424be17381SChris Wilson 	if (signaller->hangcheck.deadlock >= I915_NUM_RINGS)
28436274f212SChris Wilson 		return -1;
28446274f212SChris Wilson 
28454be17381SChris Wilson 	if (i915_seqno_passed(signaller->get_seqno(signaller, false), seqno))
28464be17381SChris Wilson 		return 1;
28474be17381SChris Wilson 
2848a0d036b0SChris Wilson 	/* cursory check for an unkickable deadlock */
2849a0d036b0SChris Wilson 	if (I915_READ_CTL(signaller) & RING_WAIT_SEMAPHORE &&
2850a0d036b0SChris Wilson 	    semaphore_passed(signaller) < 0)
28514be17381SChris Wilson 		return -1;
28524be17381SChris Wilson 
28534be17381SChris Wilson 	return 0;
28546274f212SChris Wilson }
28556274f212SChris Wilson 
28566274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28576274f212SChris Wilson {
2858a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28596274f212SChris Wilson 	int i;
28606274f212SChris Wilson 
28616274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28624be17381SChris Wilson 		ring->hangcheck.deadlock = 0;
28636274f212SChris Wilson }
28646274f212SChris Wilson 
2865ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2866a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28671ec14ad3SChris Wilson {
28681ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28691ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28709107e9d2SChris Wilson 	u32 tmp;
28719107e9d2SChris Wilson 
2872f260fe7bSMika Kuoppala 	if (acthd != ring->hangcheck.acthd) {
2873f260fe7bSMika Kuoppala 		if (acthd > ring->hangcheck.max_acthd) {
2874f260fe7bSMika Kuoppala 			ring->hangcheck.max_acthd = acthd;
2875f2f4d82fSJani Nikula 			return HANGCHECK_ACTIVE;
2876f260fe7bSMika Kuoppala 		}
2877f260fe7bSMika Kuoppala 
2878f260fe7bSMika Kuoppala 		return HANGCHECK_ACTIVE_LOOP;
2879f260fe7bSMika Kuoppala 	}
28806274f212SChris Wilson 
28819107e9d2SChris Wilson 	if (IS_GEN2(dev))
2882f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28839107e9d2SChris Wilson 
28849107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
28859107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
28869107e9d2SChris Wilson 	 * and break the hang. This should work on
28879107e9d2SChris Wilson 	 * all but the second generation chipsets.
28889107e9d2SChris Wilson 	 */
28899107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
28901ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
289158174462SMika Kuoppala 		i915_handle_error(dev, false,
289258174462SMika Kuoppala 				  "Kicking stuck wait on %s",
28931ec14ad3SChris Wilson 				  ring->name);
28941ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2895f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
28961ec14ad3SChris Wilson 	}
2897a24a11e6SChris Wilson 
28986274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
28996274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29006274f212SChris Wilson 		default:
2901f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29026274f212SChris Wilson 		case 1:
290358174462SMika Kuoppala 			i915_handle_error(dev, false,
290458174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2905a24a11e6SChris Wilson 					  ring->name);
2906a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2907f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29086274f212SChris Wilson 		case 0:
2909f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29106274f212SChris Wilson 		}
29119107e9d2SChris Wilson 	}
29129107e9d2SChris Wilson 
2913f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2914a24a11e6SChris Wilson }
2915d1e61e7fSChris Wilson 
2916f65d9421SBen Gamari /**
2917f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
291805407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
291905407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
292005407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
292105407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
292205407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2923f65d9421SBen Gamari  */
2924a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2925f65d9421SBen Gamari {
2926f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
29272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2928a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2929b4519513SChris Wilson 	int i;
293005407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29319107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29329107e9d2SChris Wilson #define BUSY 1
29339107e9d2SChris Wilson #define KICK 5
29349107e9d2SChris Wilson #define HUNG 20
2935893eead0SChris Wilson 
2936d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29373e0dc6b0SBen Widawsky 		return;
29383e0dc6b0SBen Widawsky 
2939b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
294050877445SChris Wilson 		u64 acthd;
294150877445SChris Wilson 		u32 seqno;
29429107e9d2SChris Wilson 		bool busy = true;
2943b4519513SChris Wilson 
29446274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29456274f212SChris Wilson 
294605407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
294705407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
294805407ff8SMika Kuoppala 
294905407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
29509107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2951da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2952da661464SMika Kuoppala 
29539107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29549107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2955094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2956f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29579107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29589107e9d2SChris Wilson 								  ring->name);
2959f4adcd24SDaniel Vetter 						else
2960f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2961f4adcd24SDaniel Vetter 								 ring->name);
29629107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2963094f9a54SChris Wilson 					}
2964094f9a54SChris Wilson 					/* Safeguard against driver failure */
2965094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29669107e9d2SChris Wilson 				} else
29679107e9d2SChris Wilson 					busy = false;
296805407ff8SMika Kuoppala 			} else {
29696274f212SChris Wilson 				/* We always increment the hangcheck score
29706274f212SChris Wilson 				 * if the ring is busy and still processing
29716274f212SChris Wilson 				 * the same request, so that no single request
29726274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29736274f212SChris Wilson 				 * batches). The only time we do not increment
29746274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29756274f212SChris Wilson 				 * ring is in a legitimate wait for another
29766274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29776274f212SChris Wilson 				 * victim and we want to be sure we catch the
29786274f212SChris Wilson 				 * right culprit. Then every time we do kick
29796274f212SChris Wilson 				 * the ring, add a small increment to the
29806274f212SChris Wilson 				 * score so that we can catch a batch that is
29816274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29826274f212SChris Wilson 				 * for stalling the machine.
29839107e9d2SChris Wilson 				 */
2984ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
2985ad8beaeaSMika Kuoppala 								    acthd);
2986ad8beaeaSMika Kuoppala 
2987ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
2988da661464SMika Kuoppala 				case HANGCHECK_IDLE:
2989f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
2990f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
2991f260fe7bSMika Kuoppala 					break;
2992f260fe7bSMika Kuoppala 				case HANGCHECK_ACTIVE_LOOP:
2993ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
29946274f212SChris Wilson 					break;
2995f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
2996ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
29976274f212SChris Wilson 					break;
2998f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
2999ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30006274f212SChris Wilson 					stuck[i] = true;
30016274f212SChris Wilson 					break;
30026274f212SChris Wilson 				}
300305407ff8SMika Kuoppala 			}
30049107e9d2SChris Wilson 		} else {
3005da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3006da661464SMika Kuoppala 
30079107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30089107e9d2SChris Wilson 			 * attempts across multiple batches.
30099107e9d2SChris Wilson 			 */
30109107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30119107e9d2SChris Wilson 				ring->hangcheck.score--;
3012f260fe7bSMika Kuoppala 
3013f260fe7bSMika Kuoppala 			ring->hangcheck.acthd = ring->hangcheck.max_acthd = 0;
3014cbb465e7SChris Wilson 		}
3015f65d9421SBen Gamari 
301605407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
301705407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30189107e9d2SChris Wilson 		busy_count += busy;
301905407ff8SMika Kuoppala 	}
302005407ff8SMika Kuoppala 
302105407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3022b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3023b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
302405407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3025a43adf07SChris Wilson 				 ring->name);
3026a43adf07SChris Wilson 			rings_hung++;
302705407ff8SMika Kuoppala 		}
302805407ff8SMika Kuoppala 	}
302905407ff8SMika Kuoppala 
303005407ff8SMika Kuoppala 	if (rings_hung)
303158174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
303205407ff8SMika Kuoppala 
303305407ff8SMika Kuoppala 	if (busy_count)
303405407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
303505407ff8SMika Kuoppala 		 * being added */
303610cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
303710cd45b6SMika Kuoppala }
303810cd45b6SMika Kuoppala 
303910cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
304010cd45b6SMika Kuoppala {
304110cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
3042d330a953SJani Nikula 	if (!i915.enable_hangcheck)
304310cd45b6SMika Kuoppala 		return;
304410cd45b6SMika Kuoppala 
304599584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
304610cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3047f65d9421SBen Gamari }
3048f65d9421SBen Gamari 
30491c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
305091738a95SPaulo Zanoni {
305191738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
305291738a95SPaulo Zanoni 
305391738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
305491738a95SPaulo Zanoni 		return;
305591738a95SPaulo Zanoni 
3056f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3057105b122eSPaulo Zanoni 
3058105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3059105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3060622364b6SPaulo Zanoni }
3061105b122eSPaulo Zanoni 
306291738a95SPaulo Zanoni /*
3063622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3064622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3065622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3066622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3067622364b6SPaulo Zanoni  *
3068622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
306991738a95SPaulo Zanoni  */
3070622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3071622364b6SPaulo Zanoni {
3072622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3073622364b6SPaulo Zanoni 
3074622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3075622364b6SPaulo Zanoni 		return;
3076622364b6SPaulo Zanoni 
3077622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
307891738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
307991738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
308091738a95SPaulo Zanoni }
308191738a95SPaulo Zanoni 
30827c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3083d18ea1b5SDaniel Vetter {
3084d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3085d18ea1b5SDaniel Vetter 
3086f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3087a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3088f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3089d18ea1b5SDaniel Vetter }
3090d18ea1b5SDaniel Vetter 
3091c0e09200SDave Airlie /* drm_dma.h hooks
3092c0e09200SDave Airlie */
3093be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3094036a4a7dSZhenyu Wang {
30952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3096036a4a7dSZhenyu Wang 
30970c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3098bdfcdb63SDaniel Vetter 
3099f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3100c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3101c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3102036a4a7dSZhenyu Wang 
31037c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3104c650156aSZhenyu Wang 
31051c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31067d99163dSBen Widawsky }
31077d99163dSBen Widawsky 
31087e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31097e231dbeSJesse Barnes {
31102d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31117e231dbeSJesse Barnes 	int pipe;
31127e231dbeSJesse Barnes 
31137e231dbeSJesse Barnes 	/* VLV magic */
31147e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31157e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31167e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31177e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31187e231dbeSJesse Barnes 
31197e231dbeSJesse Barnes 	/* and GT */
31207e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
31217e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3122d18ea1b5SDaniel Vetter 
31237c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31247e231dbeSJesse Barnes 
31257c4cde39SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
31267e231dbeSJesse Barnes 
31277e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
31287e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3129055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
31307e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
31317e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31327e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
31337e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
31347e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31357e231dbeSJesse Barnes }
31367e231dbeSJesse Barnes 
3137d6e3cca3SDaniel Vetter static void gen8_gt_irq_reset(struct drm_i915_private *dev_priv)
3138d6e3cca3SDaniel Vetter {
3139d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 0);
3140d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 1);
3141d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 2);
3142d6e3cca3SDaniel Vetter 	GEN8_IRQ_RESET_NDX(GT, 3);
3143d6e3cca3SDaniel Vetter }
3144d6e3cca3SDaniel Vetter 
3145823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3146abd58f01SBen Widawsky {
3147abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3148abd58f01SBen Widawsky 	int pipe;
3149abd58f01SBen Widawsky 
3150abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3151abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3152abd58f01SBen Widawsky 
3153d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
3154abd58f01SBen Widawsky 
3155055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3156f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3157813bde43SPaulo Zanoni 						   POWER_DOMAIN_PIPE(pipe)))
3158f86f3fb0SPaulo Zanoni 			GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3159abd58f01SBen Widawsky 
3160f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3161f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3162f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3163abd58f01SBen Widawsky 
31641c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3165abd58f01SBen Widawsky }
3166abd58f01SBen Widawsky 
3167d49bdb0eSPaulo Zanoni void gen8_irq_power_well_post_enable(struct drm_i915_private *dev_priv)
3168d49bdb0eSPaulo Zanoni {
31691180e206SPaulo Zanoni 	uint32_t extra_ier = GEN8_PIPE_VBLANK | GEN8_PIPE_FIFO_UNDERRUN;
3170d49bdb0eSPaulo Zanoni 
317113321786SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3172d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_B, dev_priv->de_irq_mask[PIPE_B],
31731180e206SPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_B] | extra_ier);
3174d49bdb0eSPaulo Zanoni 	GEN8_IRQ_INIT_NDX(DE_PIPE, PIPE_C, dev_priv->de_irq_mask[PIPE_C],
31751180e206SPaulo Zanoni 			  ~dev_priv->de_irq_mask[PIPE_C] | extra_ier);
317613321786SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3177d49bdb0eSPaulo Zanoni }
3178d49bdb0eSPaulo Zanoni 
317943f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
318043f328d7SVille Syrjälä {
318143f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
318243f328d7SVille Syrjälä 	int pipe;
318343f328d7SVille Syrjälä 
318443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
318543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
318643f328d7SVille Syrjälä 
3187d6e3cca3SDaniel Vetter 	gen8_gt_irq_reset(dev_priv);
318843f328d7SVille Syrjälä 
318943f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
319043f328d7SVille Syrjälä 
319143f328d7SVille Syrjälä 	POSTING_READ(GEN8_PCU_IIR);
319243f328d7SVille Syrjälä 
319343f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
319443f328d7SVille Syrjälä 
319543f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
319643f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
319743f328d7SVille Syrjälä 
3198055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
319943f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
320043f328d7SVille Syrjälä 
320143f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
320243f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
320343f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
320443f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
320543f328d7SVille Syrjälä }
320643f328d7SVille Syrjälä 
320782a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
320882a28bcfSDaniel Vetter {
32092d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
321082a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3211fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
321282a28bcfSDaniel Vetter 
321382a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3214fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
3215b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3216cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3217fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
321882a28bcfSDaniel Vetter 	} else {
3219fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
3220b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
3221cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3222fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
322382a28bcfSDaniel Vetter 	}
322482a28bcfSDaniel Vetter 
3225fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
322682a28bcfSDaniel Vetter 
32277fe0b973SKeith Packard 	/*
32287fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32297fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
32307fe0b973SKeith Packard 	 *
32317fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
32327fe0b973SKeith Packard 	 */
32337fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32347fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32357fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32367fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32377fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32387fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32397fe0b973SKeith Packard }
32407fe0b973SKeith Packard 
3241d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3242d46da437SPaulo Zanoni {
32432d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
324482a28bcfSDaniel Vetter 	u32 mask;
3245d46da437SPaulo Zanoni 
3246692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3247692a04cfSDaniel Vetter 		return;
3248692a04cfSDaniel Vetter 
3249105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32505c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3251105b122eSPaulo Zanoni 	else
32525c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32538664281bSPaulo Zanoni 
3254337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3255d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3256d46da437SPaulo Zanoni }
3257d46da437SPaulo Zanoni 
32580a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32590a9a8c91SDaniel Vetter {
32600a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32610a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32620a9a8c91SDaniel Vetter 
32630a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32640a9a8c91SDaniel Vetter 
32650a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3266040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32670a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
326835a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
326935a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32700a9a8c91SDaniel Vetter 	}
32710a9a8c91SDaniel Vetter 
32720a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32730a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32740a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32750a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32760a9a8c91SDaniel Vetter 	} else {
32770a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32780a9a8c91SDaniel Vetter 	}
32790a9a8c91SDaniel Vetter 
328035079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32810a9a8c91SDaniel Vetter 
32820a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3283a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
32840a9a8c91SDaniel Vetter 
32850a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32860a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32870a9a8c91SDaniel Vetter 
3288605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
328935079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32900a9a8c91SDaniel Vetter 	}
32910a9a8c91SDaniel Vetter }
32920a9a8c91SDaniel Vetter 
3293f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3294036a4a7dSZhenyu Wang {
32952d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
32968e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
32978e76f8dcSPaulo Zanoni 
32988e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
32998e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33008e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33018e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33025c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33038e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
33045c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
33058e76f8dcSPaulo Zanoni 	} else {
33068e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3307ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33085b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33095b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33105b3a856bSDaniel Vetter 				DE_POISON);
33115c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
33125c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
33138e76f8dcSPaulo Zanoni 	}
3314036a4a7dSZhenyu Wang 
33151ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3316036a4a7dSZhenyu Wang 
33170c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33180c841212SPaulo Zanoni 
3319622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3320622364b6SPaulo Zanoni 
332135079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3322036a4a7dSZhenyu Wang 
33230a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3324036a4a7dSZhenyu Wang 
3325d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33267fe0b973SKeith Packard 
3327f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33286005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33296005ce42SDaniel Vetter 		 *
33306005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33314bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33324bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
3333d6207435SDaniel Vetter 		spin_lock_irq(&dev_priv->irq_lock);
3334f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
3335d6207435SDaniel Vetter 		spin_unlock_irq(&dev_priv->irq_lock);
3336f97108d1SJesse Barnes 	}
3337f97108d1SJesse Barnes 
3338036a4a7dSZhenyu Wang 	return 0;
3339036a4a7dSZhenyu Wang }
3340036a4a7dSZhenyu Wang 
3341f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3342f8b79e58SImre Deak {
3343f8b79e58SImre Deak 	u32 pipestat_mask;
3344f8b79e58SImre Deak 	u32 iir_mask;
3345f8b79e58SImre Deak 
3346f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3347f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3348f8b79e58SImre Deak 
3349f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3350f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3351f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3352f8b79e58SImre Deak 
3353f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3354f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3355f8b79e58SImre Deak 
3356f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3357f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3358f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3359f8b79e58SImre Deak 
3360f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3361f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3362f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3363f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3364f8b79e58SImre Deak 
3365f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3366f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3367f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3368f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3369f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3370f8b79e58SImre Deak }
3371f8b79e58SImre Deak 
3372f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3373f8b79e58SImre Deak {
3374f8b79e58SImre Deak 	u32 pipestat_mask;
3375f8b79e58SImre Deak 	u32 iir_mask;
3376f8b79e58SImre Deak 
3377f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3378f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33796c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3380f8b79e58SImre Deak 
3381f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3382f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3383f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3384f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3385f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3386f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3387f8b79e58SImre Deak 
3388f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3389f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3390f8b79e58SImre Deak 
3391f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3392f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3393f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3394f8b79e58SImre Deak 
3395f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3396f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3397f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3398f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3399f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3400f8b79e58SImre Deak }
3401f8b79e58SImre Deak 
3402f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3403f8b79e58SImre Deak {
3404f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3405f8b79e58SImre Deak 
3406f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3407f8b79e58SImre Deak 		return;
3408f8b79e58SImre Deak 
3409f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3410f8b79e58SImre Deak 
3411950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3412f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3413f8b79e58SImre Deak }
3414f8b79e58SImre Deak 
3415f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3416f8b79e58SImre Deak {
3417f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3418f8b79e58SImre Deak 
3419f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3420f8b79e58SImre Deak 		return;
3421f8b79e58SImre Deak 
3422f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3423f8b79e58SImre Deak 
3424950eabafSImre Deak 	if (intel_irqs_enabled(dev_priv))
3425f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3426f8b79e58SImre Deak }
3427f8b79e58SImre Deak 
34287e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
34297e231dbeSJesse Barnes {
34302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
34317e231dbeSJesse Barnes 
3432f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34337e231dbeSJesse Barnes 
343420afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
343520afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
343620afbda2SDaniel Vetter 
34377e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3438f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
34397e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34407e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
34417e231dbeSJesse Barnes 
3442b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3443b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3444d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3445f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3446f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3447d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
344831acc7f5SJesse Barnes 
34497e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34507e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34517e231dbeSJesse Barnes 
34520a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34537e231dbeSJesse Barnes 
34547e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34557e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34567e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34577e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34587e231dbeSJesse Barnes #endif
34597e231dbeSJesse Barnes 
34607e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
346120afbda2SDaniel Vetter 
346220afbda2SDaniel Vetter 	return 0;
346320afbda2SDaniel Vetter }
346420afbda2SDaniel Vetter 
3465abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3466abd58f01SBen Widawsky {
3467abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3468abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3469abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
347073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3471abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
347273d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT |
347373d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3474abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
347573d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
347673d477f6SOscar Mateo 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT |
347773d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3478abd58f01SBen Widawsky 		0,
347973d477f6SOscar Mateo 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT |
348073d477f6SOscar Mateo 			GT_CONTEXT_SWITCH_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3481abd58f01SBen Widawsky 		};
3482abd58f01SBen Widawsky 
34830961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
34849a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 0, ~gt_interrupts[0], gt_interrupts[0]);
34859a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 1, ~gt_interrupts[1], gt_interrupts[1]);
34869a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 2, dev_priv->pm_irq_mask, dev_priv->pm_rps_events);
34879a2d2d87SDeepak S 	GEN8_IRQ_INIT_NDX(GT, 3, ~gt_interrupts[3], gt_interrupts[3]);
3488abd58f01SBen Widawsky }
3489abd58f01SBen Widawsky 
3490abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3491abd58f01SBen Widawsky {
3492770de83dSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_CDCLK_CRC_DONE;
3493770de83dSDamien Lespiau 	uint32_t de_pipe_enables;
3494abd58f01SBen Widawsky 	int pipe;
3495770de83dSDamien Lespiau 
3496770de83dSDamien Lespiau 	if (IS_GEN9(dev_priv))
3497770de83dSDamien Lespiau 		de_pipe_masked |= GEN9_PIPE_PLANE1_FLIP_DONE |
3498770de83dSDamien Lespiau 				  GEN9_DE_PIPE_IRQ_FAULT_ERRORS;
3499770de83dSDamien Lespiau 	else
3500770de83dSDamien Lespiau 		de_pipe_masked |= GEN8_PIPE_PRIMARY_FLIP_DONE |
3501770de83dSDamien Lespiau 				  GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
3502770de83dSDamien Lespiau 
3503770de83dSDamien Lespiau 	de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
3504770de83dSDamien Lespiau 					   GEN8_PIPE_FIFO_UNDERRUN;
3505770de83dSDamien Lespiau 
350613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
350713b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
350813b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3509abd58f01SBen Widawsky 
3510055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3511f458ebbcSDaniel Vetter 		if (intel_display_power_is_enabled(dev_priv,
3512813bde43SPaulo Zanoni 				POWER_DOMAIN_PIPE(pipe)))
3513813bde43SPaulo Zanoni 			GEN8_IRQ_INIT_NDX(DE_PIPE, pipe,
3514813bde43SPaulo Zanoni 					  dev_priv->de_irq_mask[pipe],
351535079899SPaulo Zanoni 					  de_pipe_enables);
3516abd58f01SBen Widawsky 
351735079899SPaulo Zanoni 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
3518abd58f01SBen Widawsky }
3519abd58f01SBen Widawsky 
3520abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3521abd58f01SBen Widawsky {
3522abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3523abd58f01SBen Widawsky 
3524622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3525622364b6SPaulo Zanoni 
3526abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3527abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3528abd58f01SBen Widawsky 
3529abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3530abd58f01SBen Widawsky 
3531abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3532abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3533abd58f01SBen Widawsky 
3534abd58f01SBen Widawsky 	return 0;
3535abd58f01SBen Widawsky }
3536abd58f01SBen Widawsky 
353743f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
353843f328d7SVille Syrjälä {
353943f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
354043f328d7SVille Syrjälä 	u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
354143f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
354243f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
35433278f67fSVille Syrjälä 		I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
35443278f67fSVille Syrjälä 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_STATUS_VLV |
35453278f67fSVille Syrjälä 		PIPE_CRC_DONE_INTERRUPT_STATUS;
354643f328d7SVille Syrjälä 	int pipe;
354743f328d7SVille Syrjälä 
354843f328d7SVille Syrjälä 	/*
354943f328d7SVille Syrjälä 	 * Leave vblank interrupts masked initially.  enable/disable will
355043f328d7SVille Syrjälä 	 * toggle them based on usage.
355143f328d7SVille Syrjälä 	 */
35523278f67fSVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
355343f328d7SVille Syrjälä 
3554055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
355543f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
355643f328d7SVille Syrjälä 
3557d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
35583278f67fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
3559055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
356043f328d7SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
3561d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
356243f328d7SVille Syrjälä 
356343f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
356443f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
356543f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, enable_mask);
356643f328d7SVille Syrjälä 
356743f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
356843f328d7SVille Syrjälä 
356943f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
357043f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
357143f328d7SVille Syrjälä 
357243f328d7SVille Syrjälä 	return 0;
357343f328d7SVille Syrjälä }
357443f328d7SVille Syrjälä 
3575abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3576abd58f01SBen Widawsky {
3577abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3578abd58f01SBen Widawsky 
3579abd58f01SBen Widawsky 	if (!dev_priv)
3580abd58f01SBen Widawsky 		return;
3581abd58f01SBen Widawsky 
3582823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3583abd58f01SBen Widawsky }
3584abd58f01SBen Widawsky 
35857e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35867e231dbeSJesse Barnes {
35872d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
35887e231dbeSJesse Barnes 	int pipe;
35897e231dbeSJesse Barnes 
35907e231dbeSJesse Barnes 	if (!dev_priv)
35917e231dbeSJesse Barnes 		return;
35927e231dbeSJesse Barnes 
3593843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3594843d0e7dSImre Deak 
3595055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
35967e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
35977e231dbeSJesse Barnes 
35987e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
35997e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
36007e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3601f8b79e58SImre Deak 
3602d6207435SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3603d6207435SDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3604d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3605f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3606f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3607d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3608f8b79e58SImre Deak 
3609f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3610f8b79e58SImre Deak 
36117e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
36127e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
36137e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
36147e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
36157e231dbeSJesse Barnes }
36167e231dbeSJesse Barnes 
361743f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
361843f328d7SVille Syrjälä {
361943f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
362043f328d7SVille Syrjälä 	int pipe;
362143f328d7SVille Syrjälä 
362243f328d7SVille Syrjälä 	if (!dev_priv)
362343f328d7SVille Syrjälä 		return;
362443f328d7SVille Syrjälä 
362543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
362643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
362743f328d7SVille Syrjälä 
3628*a2c30fbaSVille Syrjälä 	gen8_gt_irq_reset(dev_priv);
362943f328d7SVille Syrjälä 
3630*a2c30fbaSVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
363143f328d7SVille Syrjälä 
363243f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
363343f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
363443f328d7SVille Syrjälä 
3635055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
363643f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
363743f328d7SVille Syrjälä 
363843f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
363943f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
364043f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
364143f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
364243f328d7SVille Syrjälä }
364343f328d7SVille Syrjälä 
3644f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3645036a4a7dSZhenyu Wang {
36462d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36474697995bSJesse Barnes 
36484697995bSJesse Barnes 	if (!dev_priv)
36494697995bSJesse Barnes 		return;
36504697995bSJesse Barnes 
3651be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3652036a4a7dSZhenyu Wang }
3653036a4a7dSZhenyu Wang 
3654c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3655c2798b19SChris Wilson {
36562d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3657c2798b19SChris Wilson 	int pipe;
3658c2798b19SChris Wilson 
3659055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3660c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3661c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3662c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3663c2798b19SChris Wilson 	POSTING_READ16(IER);
3664c2798b19SChris Wilson }
3665c2798b19SChris Wilson 
3666c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3667c2798b19SChris Wilson {
36682d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3669c2798b19SChris Wilson 
3670c2798b19SChris Wilson 	I915_WRITE16(EMR,
3671c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3672c2798b19SChris Wilson 
3673c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3674c2798b19SChris Wilson 	dev_priv->irq_mask =
3675c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3676c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3677c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3678c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3679c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3680c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3681c2798b19SChris Wilson 
3682c2798b19SChris Wilson 	I915_WRITE16(IER,
3683c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3684c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3685c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3686c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3687c2798b19SChris Wilson 	POSTING_READ16(IER);
3688c2798b19SChris Wilson 
3689379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3690379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3691d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3692755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3693755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3694d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3695379ef82dSDaniel Vetter 
3696c2798b19SChris Wilson 	return 0;
3697c2798b19SChris Wilson }
3698c2798b19SChris Wilson 
369990a72f87SVille Syrjälä /*
370090a72f87SVille Syrjälä  * Returns true when a page flip has completed.
370190a72f87SVille Syrjälä  */
370290a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37031f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
370490a72f87SVille Syrjälä {
37052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37061f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
370790a72f87SVille Syrjälä 
37088d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
370990a72f87SVille Syrjälä 		return false;
371090a72f87SVille Syrjälä 
371190a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3712d6bbafa1SChris Wilson 		goto check_page_flip;
371390a72f87SVille Syrjälä 
37141f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
371590a72f87SVille Syrjälä 
371690a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
371790a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
371890a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
371990a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
372090a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
372190a72f87SVille Syrjälä 	 */
372290a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
3723d6bbafa1SChris Wilson 		goto check_page_flip;
372490a72f87SVille Syrjälä 
372590a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
372690a72f87SVille Syrjälä 	return true;
3727d6bbafa1SChris Wilson 
3728d6bbafa1SChris Wilson check_page_flip:
3729d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3730d6bbafa1SChris Wilson 	return false;
373190a72f87SVille Syrjälä }
373290a72f87SVille Syrjälä 
3733ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3734c2798b19SChris Wilson {
373545a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37362d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3737c2798b19SChris Wilson 	u16 iir, new_iir;
3738c2798b19SChris Wilson 	u32 pipe_stats[2];
3739c2798b19SChris Wilson 	int pipe;
3740c2798b19SChris Wilson 	u16 flip_mask =
3741c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3742c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3743c2798b19SChris Wilson 
3744c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3745c2798b19SChris Wilson 	if (iir == 0)
3746c2798b19SChris Wilson 		return IRQ_NONE;
3747c2798b19SChris Wilson 
3748c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3749c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3750c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3751c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3752c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3753c2798b19SChris Wilson 		 */
3754222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3755c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
375658174462SMika Kuoppala 			i915_handle_error(dev, false,
375758174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
375858174462SMika Kuoppala 					  iir);
3759c2798b19SChris Wilson 
3760055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3761c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3762c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3763c2798b19SChris Wilson 
3764c2798b19SChris Wilson 			/*
3765c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3766c2798b19SChris Wilson 			 */
37672d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3768c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3769c2798b19SChris Wilson 		}
3770222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3771c2798b19SChris Wilson 
3772c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3773c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3774c2798b19SChris Wilson 
3775d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3776c2798b19SChris Wilson 
3777c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3778c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3779c2798b19SChris Wilson 
3780055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
37811f1c2e24SVille Syrjälä 			int plane = pipe;
37823a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
37831f1c2e24SVille Syrjälä 				plane = !plane;
37841f1c2e24SVille Syrjälä 
37854356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
37861f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
37871f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3788c2798b19SChris Wilson 
37894356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3790277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
37912d9d2b0bSVille Syrjälä 
37921f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
37931f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
37941f7247c0SDaniel Vetter 								    pipe);
37954356d586SDaniel Vetter 		}
3796c2798b19SChris Wilson 
3797c2798b19SChris Wilson 		iir = new_iir;
3798c2798b19SChris Wilson 	}
3799c2798b19SChris Wilson 
3800c2798b19SChris Wilson 	return IRQ_HANDLED;
3801c2798b19SChris Wilson }
3802c2798b19SChris Wilson 
3803c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3804c2798b19SChris Wilson {
38052d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3806c2798b19SChris Wilson 	int pipe;
3807c2798b19SChris Wilson 
3808055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
3809c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3810c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3811c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3812c2798b19SChris Wilson 	}
3813c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3814c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3815c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3816c2798b19SChris Wilson }
3817c2798b19SChris Wilson 
3818a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3819a266c7d5SChris Wilson {
38202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3821a266c7d5SChris Wilson 	int pipe;
3822a266c7d5SChris Wilson 
3823a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3824a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3825a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3826a266c7d5SChris Wilson 	}
3827a266c7d5SChris Wilson 
382800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3829055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
3830a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3831a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3832a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3833a266c7d5SChris Wilson 	POSTING_READ(IER);
3834a266c7d5SChris Wilson }
3835a266c7d5SChris Wilson 
3836a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3837a266c7d5SChris Wilson {
38382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
383938bde180SChris Wilson 	u32 enable_mask;
3840a266c7d5SChris Wilson 
384138bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
384238bde180SChris Wilson 
384338bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
384438bde180SChris Wilson 	dev_priv->irq_mask =
384538bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
384638bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
384738bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
384838bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
384938bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
385038bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
385138bde180SChris Wilson 
385238bde180SChris Wilson 	enable_mask =
385338bde180SChris Wilson 		I915_ASLE_INTERRUPT |
385438bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
385538bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
385638bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
385738bde180SChris Wilson 		I915_USER_INTERRUPT;
385838bde180SChris Wilson 
3859a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
386020afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
386120afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
386220afbda2SDaniel Vetter 
3863a266c7d5SChris Wilson 		/* Enable in IER... */
3864a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3865a266c7d5SChris Wilson 		/* and unmask in IMR */
3866a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3867a266c7d5SChris Wilson 	}
3868a266c7d5SChris Wilson 
3869a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3870a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3871a266c7d5SChris Wilson 	POSTING_READ(IER);
3872a266c7d5SChris Wilson 
3873f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
387420afbda2SDaniel Vetter 
3875379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3876379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3877d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
3878755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3879755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3880d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
3881379ef82dSDaniel Vetter 
388220afbda2SDaniel Vetter 	return 0;
388320afbda2SDaniel Vetter }
388420afbda2SDaniel Vetter 
388590a72f87SVille Syrjälä /*
388690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
388790a72f87SVille Syrjälä  */
388890a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
388990a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
389090a72f87SVille Syrjälä {
38912d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
389290a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
389390a72f87SVille Syrjälä 
38948d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
389590a72f87SVille Syrjälä 		return false;
389690a72f87SVille Syrjälä 
389790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
3898d6bbafa1SChris Wilson 		goto check_page_flip;
389990a72f87SVille Syrjälä 
390090a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
390190a72f87SVille Syrjälä 
390290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
390390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
390490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
390590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
390690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
390790a72f87SVille Syrjälä 	 */
390890a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
3909d6bbafa1SChris Wilson 		goto check_page_flip;
391090a72f87SVille Syrjälä 
391190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
391290a72f87SVille Syrjälä 	return true;
3913d6bbafa1SChris Wilson 
3914d6bbafa1SChris Wilson check_page_flip:
3915d6bbafa1SChris Wilson 	intel_check_page_flip(dev, pipe);
3916d6bbafa1SChris Wilson 	return false;
391790a72f87SVille Syrjälä }
391890a72f87SVille Syrjälä 
3919ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3920a266c7d5SChris Wilson {
392145a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39222d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39238291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
392438bde180SChris Wilson 	u32 flip_mask =
392538bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
392638bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
392738bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3928a266c7d5SChris Wilson 
3929a266c7d5SChris Wilson 	iir = I915_READ(IIR);
393038bde180SChris Wilson 	do {
393138bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39328291ee90SChris Wilson 		bool blc_event = false;
3933a266c7d5SChris Wilson 
3934a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3935a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3936a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3937a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3938a266c7d5SChris Wilson 		 */
3939222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
3940a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
394158174462SMika Kuoppala 			i915_handle_error(dev, false,
394258174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
394358174462SMika Kuoppala 					  iir);
3944a266c7d5SChris Wilson 
3945055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
3946a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3947a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3948a266c7d5SChris Wilson 
394938bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3950a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3951a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
395238bde180SChris Wilson 				irq_received = true;
3953a266c7d5SChris Wilson 			}
3954a266c7d5SChris Wilson 		}
3955222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
3956a266c7d5SChris Wilson 
3957a266c7d5SChris Wilson 		if (!irq_received)
3958a266c7d5SChris Wilson 			break;
3959a266c7d5SChris Wilson 
3960a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
396116c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
396216c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
396316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3964a266c7d5SChris Wilson 
396538bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3966a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3967a266c7d5SChris Wilson 
3968a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3969a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3970a266c7d5SChris Wilson 
3971055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
397238bde180SChris Wilson 			int plane = pipe;
39733a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
397438bde180SChris Wilson 				plane = !plane;
39755e2032d4SVille Syrjälä 
397690a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
397790a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
397890a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3979a266c7d5SChris Wilson 
3980a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
3981a266c7d5SChris Wilson 				blc_event = true;
39824356d586SDaniel Vetter 
39834356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3984277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
39852d9d2b0bSVille Syrjälä 
39861f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
39871f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv,
39881f7247c0SDaniel Vetter 								    pipe);
3989a266c7d5SChris Wilson 		}
3990a266c7d5SChris Wilson 
3991a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
3992a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
3993a266c7d5SChris Wilson 
3994a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
3995a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
3996a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
3997a266c7d5SChris Wilson 		 * we would never get another interrupt.
3998a266c7d5SChris Wilson 		 *
3999a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4000a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4001a266c7d5SChris Wilson 		 * another one.
4002a266c7d5SChris Wilson 		 *
4003a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4004a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4005a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4006a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4007a266c7d5SChris Wilson 		 * stray interrupts.
4008a266c7d5SChris Wilson 		 */
400938bde180SChris Wilson 		ret = IRQ_HANDLED;
4010a266c7d5SChris Wilson 		iir = new_iir;
401138bde180SChris Wilson 	} while (iir & ~flip_mask);
4012a266c7d5SChris Wilson 
4013d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
40148291ee90SChris Wilson 
4015a266c7d5SChris Wilson 	return ret;
4016a266c7d5SChris Wilson }
4017a266c7d5SChris Wilson 
4018a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4019a266c7d5SChris Wilson {
40202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4021a266c7d5SChris Wilson 	int pipe;
4022a266c7d5SChris Wilson 
4023a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4024a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4025a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4026a266c7d5SChris Wilson 	}
4027a266c7d5SChris Wilson 
402800d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
4029055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe) {
403055b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4031a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
403255b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
403355b39755SChris Wilson 	}
4034a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4035a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4036a266c7d5SChris Wilson 
4037a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4038a266c7d5SChris Wilson }
4039a266c7d5SChris Wilson 
4040a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4041a266c7d5SChris Wilson {
40422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4043a266c7d5SChris Wilson 	int pipe;
4044a266c7d5SChris Wilson 
4045a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4046a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4047a266c7d5SChris Wilson 
4048a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4049055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4050a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4051a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4052a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4053a266c7d5SChris Wilson 	POSTING_READ(IER);
4054a266c7d5SChris Wilson }
4055a266c7d5SChris Wilson 
4056a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4057a266c7d5SChris Wilson {
40582d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4059bbba0a97SChris Wilson 	u32 enable_mask;
4060a266c7d5SChris Wilson 	u32 error_mask;
4061a266c7d5SChris Wilson 
4062a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4063bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4064adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4065bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4066bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4067bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4068bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4069bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4070bbba0a97SChris Wilson 
4071bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
407221ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
407321ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4074bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4075bbba0a97SChris Wilson 
4076bbba0a97SChris Wilson 	if (IS_G4X(dev))
4077bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4078a266c7d5SChris Wilson 
4079b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4080b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4081d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4082755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4083755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4084755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4085d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
4086a266c7d5SChris Wilson 
4087a266c7d5SChris Wilson 	/*
4088a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4089a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4090a266c7d5SChris Wilson 	 */
4091a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4092a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4093a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4094a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4095a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4096a266c7d5SChris Wilson 	} else {
4097a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4098a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4099a266c7d5SChris Wilson 	}
4100a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4101a266c7d5SChris Wilson 
4102a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4103a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4104a266c7d5SChris Wilson 	POSTING_READ(IER);
4105a266c7d5SChris Wilson 
410620afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
410720afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
410820afbda2SDaniel Vetter 
4109f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
411020afbda2SDaniel Vetter 
411120afbda2SDaniel Vetter 	return 0;
411220afbda2SDaniel Vetter }
411320afbda2SDaniel Vetter 
4114bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
411520afbda2SDaniel Vetter {
41162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4117cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
411820afbda2SDaniel Vetter 	u32 hotplug_en;
411920afbda2SDaniel Vetter 
4120b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4121b5ea2d56SDaniel Vetter 
4122bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
4123bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4124bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4125adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
4126e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
4127b2784e15SDamien Lespiau 		for_each_intel_encoder(dev, intel_encoder)
4128cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4129cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4130a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
4131a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
4132a266c7d5SChris Wilson 		   seconds later.  So just do it once.
4133a266c7d5SChris Wilson 		*/
4134a266c7d5SChris Wilson 		if (IS_G4X(dev))
4135a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
413685fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4137a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4138a266c7d5SChris Wilson 
4139a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
4140a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4141a266c7d5SChris Wilson 	}
4142bac56d5bSEgbert Eich }
4143a266c7d5SChris Wilson 
4144ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4145a266c7d5SChris Wilson {
414645a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41472d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4148a266c7d5SChris Wilson 	u32 iir, new_iir;
4149a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4150a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
415121ad8330SVille Syrjälä 	u32 flip_mask =
415221ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
415321ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4154a266c7d5SChris Wilson 
4155a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4156a266c7d5SChris Wilson 
4157a266c7d5SChris Wilson 	for (;;) {
4158501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41592c8ba29fSChris Wilson 		bool blc_event = false;
41602c8ba29fSChris Wilson 
4161a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4162a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4163a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4164a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4165a266c7d5SChris Wilson 		 */
4166222c7f51SDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
4167a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
416858174462SMika Kuoppala 			i915_handle_error(dev, false,
416958174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
417058174462SMika Kuoppala 					  iir);
4171a266c7d5SChris Wilson 
4172055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
4173a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4174a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4175a266c7d5SChris Wilson 
4176a266c7d5SChris Wilson 			/*
4177a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4178a266c7d5SChris Wilson 			 */
4179a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4180a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4181501e01d7SVille Syrjälä 				irq_received = true;
4182a266c7d5SChris Wilson 			}
4183a266c7d5SChris Wilson 		}
4184222c7f51SDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
4185a266c7d5SChris Wilson 
4186a266c7d5SChris Wilson 		if (!irq_received)
4187a266c7d5SChris Wilson 			break;
4188a266c7d5SChris Wilson 
4189a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4190a266c7d5SChris Wilson 
4191a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
419216c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
419316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4194a266c7d5SChris Wilson 
419521ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4196a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4197a266c7d5SChris Wilson 
4198a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4199a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4200a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4201a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4202a266c7d5SChris Wilson 
4203055e393fSDamien Lespiau 		for_each_pipe(dev_priv, pipe) {
42042c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
420590a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
420690a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4207a266c7d5SChris Wilson 
4208a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4209a266c7d5SChris Wilson 				blc_event = true;
42104356d586SDaniel Vetter 
42114356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4212277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4213a266c7d5SChris Wilson 
42141f7247c0SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS)
42151f7247c0SDaniel Vetter 				intel_cpu_fifo_underrun_irq_handler(dev_priv, pipe);
42162d9d2b0bSVille Syrjälä 		}
4217a266c7d5SChris Wilson 
4218a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4219a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4220a266c7d5SChris Wilson 
4221515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4222515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4223515ac2bbSDaniel Vetter 
4224a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4225a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4226a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4227a266c7d5SChris Wilson 		 * we would never get another interrupt.
4228a266c7d5SChris Wilson 		 *
4229a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4230a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4231a266c7d5SChris Wilson 		 * another one.
4232a266c7d5SChris Wilson 		 *
4233a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4234a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4235a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4236a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4237a266c7d5SChris Wilson 		 * stray interrupts.
4238a266c7d5SChris Wilson 		 */
4239a266c7d5SChris Wilson 		iir = new_iir;
4240a266c7d5SChris Wilson 	}
4241a266c7d5SChris Wilson 
4242d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
42432c8ba29fSChris Wilson 
4244a266c7d5SChris Wilson 	return ret;
4245a266c7d5SChris Wilson }
4246a266c7d5SChris Wilson 
4247a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4248a266c7d5SChris Wilson {
42492d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4250a266c7d5SChris Wilson 	int pipe;
4251a266c7d5SChris Wilson 
4252a266c7d5SChris Wilson 	if (!dev_priv)
4253a266c7d5SChris Wilson 		return;
4254a266c7d5SChris Wilson 
4255a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4256a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4257a266c7d5SChris Wilson 
4258a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4259055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4260a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4261a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4262a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4263a266c7d5SChris Wilson 
4264055e393fSDamien Lespiau 	for_each_pipe(dev_priv, pipe)
4265a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4266a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4267a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4268a266c7d5SChris Wilson }
4269a266c7d5SChris Wilson 
42704cb21832SDaniel Vetter static void intel_hpd_irq_reenable_work(struct work_struct *work)
4271ac4c16c5SEgbert Eich {
42726323751dSImre Deak 	struct drm_i915_private *dev_priv =
42736323751dSImre Deak 		container_of(work, typeof(*dev_priv),
42746323751dSImre Deak 			     hotplug_reenable_work.work);
4275ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4276ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4277ac4c16c5SEgbert Eich 	int i;
4278ac4c16c5SEgbert Eich 
42796323751dSImre Deak 	intel_runtime_pm_get(dev_priv);
42806323751dSImre Deak 
42814cb21832SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
4282ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4283ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4284ac4c16c5SEgbert Eich 
4285ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4286ac4c16c5SEgbert Eich 			continue;
4287ac4c16c5SEgbert Eich 
4288ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4289ac4c16c5SEgbert Eich 
4290ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4291ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4292ac4c16c5SEgbert Eich 
4293ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4294ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4295ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4296c23cc417SJani Nikula 							 connector->name);
4297ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4298ac4c16c5SEgbert Eich 				if (!connector->polled)
4299ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4300ac4c16c5SEgbert Eich 			}
4301ac4c16c5SEgbert Eich 		}
4302ac4c16c5SEgbert Eich 	}
4303ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4304ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
43054cb21832SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
43066323751dSImre Deak 
43076323751dSImre Deak 	intel_runtime_pm_put(dev_priv);
4308ac4c16c5SEgbert Eich }
4309ac4c16c5SEgbert Eich 
4310fca52a55SDaniel Vetter /**
4311fca52a55SDaniel Vetter  * intel_irq_init - initializes irq support
4312fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4313fca52a55SDaniel Vetter  *
4314fca52a55SDaniel Vetter  * This function initializes all the irq support including work items, timers
4315fca52a55SDaniel Vetter  * and all the vtables. It does not setup the interrupt itself though.
4316fca52a55SDaniel Vetter  */
4317b963291cSDaniel Vetter void intel_irq_init(struct drm_i915_private *dev_priv)
4318f71d4af4SJesse Barnes {
4319b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
43208b2e326dSChris Wilson 
43218b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
432213cf5504SDave Airlie 	INIT_WORK(&dev_priv->dig_port_work, i915_digport_work_func);
432399584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4324c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4325a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43268b2e326dSChris Wilson 
4327a6706b45SDeepak S 	/* Let's track the enabled rps events */
4328b963291cSDaniel Vetter 	if (IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
43296c65a587SVille Syrjälä 		/* WaGsvRC0ResidencyMethod:vlv */
433031685c25SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RP_UP_EI_EXPIRED;
433131685c25SDeepak S 	else
4332a6706b45SDeepak S 		dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4333a6706b45SDeepak S 
433499584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
433599584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
433661bac78eSDaniel Vetter 		    (unsigned long) dev);
43376323751dSImre Deak 	INIT_DELAYED_WORK(&dev_priv->hotplug_reenable_work,
43384cb21832SDaniel Vetter 			  intel_hpd_irq_reenable_work);
433961bac78eSDaniel Vetter 
434097a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43419ee32feaSDaniel Vetter 
4342b963291cSDaniel Vetter 	if (IS_GEN2(dev_priv)) {
43434cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43444cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
4345b963291cSDaniel Vetter 	} else if (IS_G4X(dev_priv) || INTEL_INFO(dev_priv)->gen >= 5) {
4346f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4347f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4348391f75e2SVille Syrjälä 	} else {
4349391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4350391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4351f71d4af4SJesse Barnes 	}
4352f71d4af4SJesse Barnes 
435321da2700SVille Syrjälä 	/*
435421da2700SVille Syrjälä 	 * Opt out of the vblank disable timer on everything except gen2.
435521da2700SVille Syrjälä 	 * Gen2 doesn't have a hardware frame counter and so depends on
435621da2700SVille Syrjälä 	 * vblank interrupts to produce sane vblank seuquence numbers.
435721da2700SVille Syrjälä 	 */
4358b963291cSDaniel Vetter 	if (!IS_GEN2(dev_priv))
435921da2700SVille Syrjälä 		dev->vblank_disable_immediate = true;
436021da2700SVille Syrjälä 
4361c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4362f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4363f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4364c2baf4b7SVille Syrjälä 	}
4365f71d4af4SJesse Barnes 
4366b963291cSDaniel Vetter 	if (IS_CHERRYVIEW(dev_priv)) {
436743f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
436843f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
436943f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
437043f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
437143f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
437243f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
437343f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4374b963291cSDaniel Vetter 	} else if (IS_VALLEYVIEW(dev_priv)) {
43757e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43767e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43777e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43787e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43797e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43807e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4381fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4382b963291cSDaniel Vetter 	} else if (INTEL_INFO(dev_priv)->gen >= 8) {
4383abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4384723761b8SDaniel Vetter 		dev->driver->irq_preinstall = gen8_irq_reset;
4385abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4386abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4387abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4388abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4389abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4390f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4391f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4392723761b8SDaniel Vetter 		dev->driver->irq_preinstall = ironlake_irq_reset;
4393f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4394f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4395f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4396f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
439782a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4398f71d4af4SJesse Barnes 	} else {
4399b963291cSDaniel Vetter 		if (INTEL_INFO(dev_priv)->gen == 2) {
4400c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4401c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4402c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4403c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4404b963291cSDaniel Vetter 		} else if (INTEL_INFO(dev_priv)->gen == 3) {
4405a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4406a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4407a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4408a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
440920afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4410c2798b19SChris Wilson 		} else {
4411a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4412a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4413a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4414a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4415bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4416c2798b19SChris Wilson 		}
4417f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4418f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4419f71d4af4SJesse Barnes 	}
4420f71d4af4SJesse Barnes }
442120afbda2SDaniel Vetter 
4422fca52a55SDaniel Vetter /**
4423fca52a55SDaniel Vetter  * intel_hpd_init - initializes and enables hpd support
4424fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4425fca52a55SDaniel Vetter  *
4426fca52a55SDaniel Vetter  * This function enables the hotplug support. It requires that interrupts have
4427fca52a55SDaniel Vetter  * already been enabled with intel_irq_init_hw(). From this point on hotplug and
4428fca52a55SDaniel Vetter  * poll request can run concurrently to other code, so locking rules must be
4429fca52a55SDaniel Vetter  * obeyed.
4430fca52a55SDaniel Vetter  *
4431fca52a55SDaniel Vetter  * This is a separate step from interrupt enabling to simplify the locking rules
4432fca52a55SDaniel Vetter  * in the driver load and resume code.
4433fca52a55SDaniel Vetter  */
4434b963291cSDaniel Vetter void intel_hpd_init(struct drm_i915_private *dev_priv)
443520afbda2SDaniel Vetter {
4436b963291cSDaniel Vetter 	struct drm_device *dev = dev_priv->dev;
4437821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4438821450c6SEgbert Eich 	struct drm_connector *connector;
4439821450c6SEgbert Eich 	int i;
444020afbda2SDaniel Vetter 
4441821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4442821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4443821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4444821450c6SEgbert Eich 	}
4445821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4446821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4447821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
44480e32b39cSDave Airlie 		if (connector->encoder && !connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
44490e32b39cSDave Airlie 			connector->polled = DRM_CONNECTOR_POLL_HPD;
44500e32b39cSDave Airlie 		if (intel_connector->mst_port)
4451821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4452821450c6SEgbert Eich 	}
4453b5ea2d56SDaniel Vetter 
4454b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4455b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4456d6207435SDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
445720afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
445820afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4459d6207435SDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
446020afbda2SDaniel Vetter }
4461c67a470bSPaulo Zanoni 
4462fca52a55SDaniel Vetter /**
4463fca52a55SDaniel Vetter  * intel_irq_install - enables the hardware interrupt
4464fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4465fca52a55SDaniel Vetter  *
4466fca52a55SDaniel Vetter  * This function enables the hardware interrupt handling, but leaves the hotplug
4467fca52a55SDaniel Vetter  * handling still disabled. It is called after intel_irq_init().
4468fca52a55SDaniel Vetter  *
4469fca52a55SDaniel Vetter  * In the driver load and resume code we need working interrupts in a few places
4470fca52a55SDaniel Vetter  * but don't want to deal with the hassle of concurrent probe and hotplug
4471fca52a55SDaniel Vetter  * workers. Hence the split into this two-stage approach.
4472fca52a55SDaniel Vetter  */
44732aeb7d3aSDaniel Vetter int intel_irq_install(struct drm_i915_private *dev_priv)
44742aeb7d3aSDaniel Vetter {
44752aeb7d3aSDaniel Vetter 	/*
44762aeb7d3aSDaniel Vetter 	 * We enable some interrupt sources in our postinstall hooks, so mark
44772aeb7d3aSDaniel Vetter 	 * interrupts as enabled _before_ actually enabling them to avoid
44782aeb7d3aSDaniel Vetter 	 * special cases in our ordering checks.
44792aeb7d3aSDaniel Vetter 	 */
44802aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
44812aeb7d3aSDaniel Vetter 
44822aeb7d3aSDaniel Vetter 	return drm_irq_install(dev_priv->dev, dev_priv->dev->pdev->irq);
44832aeb7d3aSDaniel Vetter }
44842aeb7d3aSDaniel Vetter 
4485fca52a55SDaniel Vetter /**
4486fca52a55SDaniel Vetter  * intel_irq_uninstall - finilizes all irq handling
4487fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4488fca52a55SDaniel Vetter  *
4489fca52a55SDaniel Vetter  * This stops interrupt and hotplug handling and unregisters and frees all
4490fca52a55SDaniel Vetter  * resources acquired in the init functions.
4491fca52a55SDaniel Vetter  */
44922aeb7d3aSDaniel Vetter void intel_irq_uninstall(struct drm_i915_private *dev_priv)
44932aeb7d3aSDaniel Vetter {
44942aeb7d3aSDaniel Vetter 	drm_irq_uninstall(dev_priv->dev);
44952aeb7d3aSDaniel Vetter 	intel_hpd_cancel_work(dev_priv);
44962aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
44972aeb7d3aSDaniel Vetter }
44982aeb7d3aSDaniel Vetter 
4499fca52a55SDaniel Vetter /**
4500fca52a55SDaniel Vetter  * intel_runtime_pm_disable_interrupts - runtime interrupt disabling
4501fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4502fca52a55SDaniel Vetter  *
4503fca52a55SDaniel Vetter  * This function is used to disable interrupts at runtime, both in the runtime
4504fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4505fca52a55SDaniel Vetter  */
4506b963291cSDaniel Vetter void intel_runtime_pm_disable_interrupts(struct drm_i915_private *dev_priv)
4507c67a470bSPaulo Zanoni {
4508b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_uninstall(dev_priv->dev);
45092aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = false;
4510c67a470bSPaulo Zanoni }
4511c67a470bSPaulo Zanoni 
4512fca52a55SDaniel Vetter /**
4513fca52a55SDaniel Vetter  * intel_runtime_pm_enable_interrupts - runtime interrupt enabling
4514fca52a55SDaniel Vetter  * @dev_priv: i915 device instance
4515fca52a55SDaniel Vetter  *
4516fca52a55SDaniel Vetter  * This function is used to enable interrupts at runtime, both in the runtime
4517fca52a55SDaniel Vetter  * pm and the system suspend/resume code.
4518fca52a55SDaniel Vetter  */
4519b963291cSDaniel Vetter void intel_runtime_pm_enable_interrupts(struct drm_i915_private *dev_priv)
4520c67a470bSPaulo Zanoni {
45212aeb7d3aSDaniel Vetter 	dev_priv->pm.irqs_enabled = true;
4522b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_preinstall(dev_priv->dev);
4523b963291cSDaniel Vetter 	dev_priv->dev->driver->irq_postinstall(dev_priv->dev);
4524c67a470bSPaulo Zanoni }
4525