xref: /openbmc/linux/drivers/gpu/drm/i915/i915_irq.c (revision a4872ba6d01454dfeb251d96f623ab5d1b0666a4)
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 
40e5868a31SEgbert Eich static const u32 hpd_ibx[] = {
41e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG,
42e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG,
43e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG,
44e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG,
45e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG
46e5868a31SEgbert Eich };
47e5868a31SEgbert Eich 
48e5868a31SEgbert Eich static const u32 hpd_cpt[] = {
49e5868a31SEgbert Eich 	[HPD_CRT] = SDE_CRT_HOTPLUG_CPT,
5073c352a2SDaniel Vetter 	[HPD_SDVO_B] = SDE_SDVOB_HOTPLUG_CPT,
51e5868a31SEgbert Eich 	[HPD_PORT_B] = SDE_PORTB_HOTPLUG_CPT,
52e5868a31SEgbert Eich 	[HPD_PORT_C] = SDE_PORTC_HOTPLUG_CPT,
53e5868a31SEgbert Eich 	[HPD_PORT_D] = SDE_PORTD_HOTPLUG_CPT
54e5868a31SEgbert Eich };
55e5868a31SEgbert Eich 
56e5868a31SEgbert Eich static const u32 hpd_mask_i915[] = {
57e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_EN,
58e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_EN,
59e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_EN,
60e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_EN,
61e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_EN,
62e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_EN
63e5868a31SEgbert Eich };
64e5868a31SEgbert Eich 
65704cfb87SDaniel Vetter static const u32 hpd_status_g4x[] = {
66e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
67e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_G4X,
68e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_G4X,
69e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
70e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
71e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
72e5868a31SEgbert Eich };
73e5868a31SEgbert Eich 
74e5868a31SEgbert Eich static const u32 hpd_status_i915[] = { /* i915 and valleyview are the same */
75e5868a31SEgbert Eich 	[HPD_CRT] = CRT_HOTPLUG_INT_STATUS,
76e5868a31SEgbert Eich 	[HPD_SDVO_B] = SDVOB_HOTPLUG_INT_STATUS_I915,
77e5868a31SEgbert Eich 	[HPD_SDVO_C] = SDVOC_HOTPLUG_INT_STATUS_I915,
78e5868a31SEgbert Eich 	[HPD_PORT_B] = PORTB_HOTPLUG_INT_STATUS,
79e5868a31SEgbert Eich 	[HPD_PORT_C] = PORTC_HOTPLUG_INT_STATUS,
80e5868a31SEgbert Eich 	[HPD_PORT_D] = PORTD_HOTPLUG_INT_STATUS
81e5868a31SEgbert Eich };
82e5868a31SEgbert Eich 
835c502442SPaulo Zanoni /* IIR can theoretically queue up two events. Be paranoid. */
84f86f3fb0SPaulo Zanoni #define GEN8_IRQ_RESET_NDX(type, which) do { \
855c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff); \
865c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IMR(which)); \
875c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), 0); \
885c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
895c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
905c502442SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff); \
915c502442SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IIR(which)); \
925c502442SPaulo Zanoni } while (0)
935c502442SPaulo Zanoni 
94f86f3fb0SPaulo Zanoni #define GEN5_IRQ_RESET(type) do { \
95a9d356a6SPaulo Zanoni 	I915_WRITE(type##IMR, 0xffffffff); \
965c502442SPaulo Zanoni 	POSTING_READ(type##IMR); \
97a9d356a6SPaulo Zanoni 	I915_WRITE(type##IER, 0); \
985c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
995c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
1005c502442SPaulo Zanoni 	I915_WRITE(type##IIR, 0xffffffff); \
1015c502442SPaulo Zanoni 	POSTING_READ(type##IIR); \
102a9d356a6SPaulo Zanoni } while (0)
103a9d356a6SPaulo Zanoni 
104337ba017SPaulo Zanoni /*
105337ba017SPaulo Zanoni  * We should clear IMR at preinstall/uninstall, and just check at postinstall.
106337ba017SPaulo Zanoni  */
107337ba017SPaulo Zanoni #define GEN5_ASSERT_IIR_IS_ZERO(reg) do { \
108337ba017SPaulo Zanoni 	u32 val = I915_READ(reg); \
109337ba017SPaulo Zanoni 	if (val) { \
110337ba017SPaulo Zanoni 		WARN(1, "Interrupt register 0x%x is not zero: 0x%08x\n", \
111337ba017SPaulo Zanoni 		     (reg), val); \
112337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
113337ba017SPaulo Zanoni 		POSTING_READ(reg); \
114337ba017SPaulo Zanoni 		I915_WRITE((reg), 0xffffffff); \
115337ba017SPaulo Zanoni 		POSTING_READ(reg); \
116337ba017SPaulo Zanoni 	} \
117337ba017SPaulo Zanoni } while (0)
118337ba017SPaulo Zanoni 
11935079899SPaulo Zanoni #define GEN8_IRQ_INIT_NDX(type, which, imr_val, ier_val) do { \
120337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(GEN8_##type##_IIR(which)); \
12135079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IMR(which), (imr_val)); \
12235079899SPaulo Zanoni 	I915_WRITE(GEN8_##type##_IER(which), (ier_val)); \
12335079899SPaulo Zanoni 	POSTING_READ(GEN8_##type##_IER(which)); \
12435079899SPaulo Zanoni } while (0)
12535079899SPaulo Zanoni 
12635079899SPaulo Zanoni #define GEN5_IRQ_INIT(type, imr_val, ier_val) do { \
127337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(type##IIR); \
12835079899SPaulo Zanoni 	I915_WRITE(type##IMR, (imr_val)); \
12935079899SPaulo Zanoni 	I915_WRITE(type##IER, (ier_val)); \
13035079899SPaulo Zanoni 	POSTING_READ(type##IER); \
13135079899SPaulo Zanoni } while (0)
13235079899SPaulo Zanoni 
133036a4a7dSZhenyu Wang /* For display hotplug interrupt */
134995b6762SChris Wilson static void
1352d1013ddSJani Nikula ironlake_enable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
136036a4a7dSZhenyu Wang {
1374bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1384bc9d430SDaniel Vetter 
139730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
140c67a470bSPaulo Zanoni 		return;
141c67a470bSPaulo Zanoni 
1421ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != 0) {
1431ec14ad3SChris Wilson 		dev_priv->irq_mask &= ~mask;
1441ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1453143a2bfSChris Wilson 		POSTING_READ(DEIMR);
146036a4a7dSZhenyu Wang 	}
147036a4a7dSZhenyu Wang }
148036a4a7dSZhenyu Wang 
1490ff9800aSPaulo Zanoni static void
1502d1013ddSJani Nikula ironlake_disable_display_irq(struct drm_i915_private *dev_priv, u32 mask)
151036a4a7dSZhenyu Wang {
1524bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
1534bc9d430SDaniel Vetter 
154730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
155c67a470bSPaulo Zanoni 		return;
156c67a470bSPaulo Zanoni 
1571ec14ad3SChris Wilson 	if ((dev_priv->irq_mask & mask) != mask) {
1581ec14ad3SChris Wilson 		dev_priv->irq_mask |= mask;
1591ec14ad3SChris Wilson 		I915_WRITE(DEIMR, dev_priv->irq_mask);
1603143a2bfSChris Wilson 		POSTING_READ(DEIMR);
161036a4a7dSZhenyu Wang 	}
162036a4a7dSZhenyu Wang }
163036a4a7dSZhenyu Wang 
16443eaea13SPaulo Zanoni /**
16543eaea13SPaulo Zanoni  * ilk_update_gt_irq - update GTIMR
16643eaea13SPaulo Zanoni  * @dev_priv: driver private
16743eaea13SPaulo Zanoni  * @interrupt_mask: mask of interrupt bits to update
16843eaea13SPaulo Zanoni  * @enabled_irq_mask: mask of interrupt bits to enable
16943eaea13SPaulo Zanoni  */
17043eaea13SPaulo Zanoni static void ilk_update_gt_irq(struct drm_i915_private *dev_priv,
17143eaea13SPaulo Zanoni 			      uint32_t interrupt_mask,
17243eaea13SPaulo Zanoni 			      uint32_t enabled_irq_mask)
17343eaea13SPaulo Zanoni {
17443eaea13SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
17543eaea13SPaulo Zanoni 
176730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
177c67a470bSPaulo Zanoni 		return;
178c67a470bSPaulo Zanoni 
17943eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask &= ~interrupt_mask;
18043eaea13SPaulo Zanoni 	dev_priv->gt_irq_mask |= (~enabled_irq_mask & interrupt_mask);
18143eaea13SPaulo Zanoni 	I915_WRITE(GTIMR, dev_priv->gt_irq_mask);
18243eaea13SPaulo Zanoni 	POSTING_READ(GTIMR);
18343eaea13SPaulo Zanoni }
18443eaea13SPaulo Zanoni 
18543eaea13SPaulo Zanoni void ilk_enable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
18643eaea13SPaulo Zanoni {
18743eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, mask);
18843eaea13SPaulo Zanoni }
18943eaea13SPaulo Zanoni 
19043eaea13SPaulo Zanoni void ilk_disable_gt_irq(struct drm_i915_private *dev_priv, uint32_t mask)
19143eaea13SPaulo Zanoni {
19243eaea13SPaulo Zanoni 	ilk_update_gt_irq(dev_priv, mask, 0);
19343eaea13SPaulo Zanoni }
19443eaea13SPaulo Zanoni 
195edbfdb45SPaulo Zanoni /**
196edbfdb45SPaulo Zanoni   * snb_update_pm_irq - update GEN6_PMIMR
197edbfdb45SPaulo Zanoni   * @dev_priv: driver private
198edbfdb45SPaulo Zanoni   * @interrupt_mask: mask of interrupt bits to update
199edbfdb45SPaulo Zanoni   * @enabled_irq_mask: mask of interrupt bits to enable
200edbfdb45SPaulo Zanoni   */
201edbfdb45SPaulo Zanoni static void snb_update_pm_irq(struct drm_i915_private *dev_priv,
202edbfdb45SPaulo Zanoni 			      uint32_t interrupt_mask,
203edbfdb45SPaulo Zanoni 			      uint32_t enabled_irq_mask)
204edbfdb45SPaulo Zanoni {
205605cd25bSPaulo Zanoni 	uint32_t new_val;
206edbfdb45SPaulo Zanoni 
207edbfdb45SPaulo Zanoni 	assert_spin_locked(&dev_priv->irq_lock);
208edbfdb45SPaulo Zanoni 
209730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
210c67a470bSPaulo Zanoni 		return;
211c67a470bSPaulo Zanoni 
212605cd25bSPaulo Zanoni 	new_val = dev_priv->pm_irq_mask;
213f52ecbcfSPaulo Zanoni 	new_val &= ~interrupt_mask;
214f52ecbcfSPaulo Zanoni 	new_val |= (~enabled_irq_mask & interrupt_mask);
215f52ecbcfSPaulo Zanoni 
216605cd25bSPaulo Zanoni 	if (new_val != dev_priv->pm_irq_mask) {
217605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = new_val;
218605cd25bSPaulo Zanoni 		I915_WRITE(GEN6_PMIMR, dev_priv->pm_irq_mask);
219edbfdb45SPaulo Zanoni 		POSTING_READ(GEN6_PMIMR);
220edbfdb45SPaulo Zanoni 	}
221f52ecbcfSPaulo Zanoni }
222edbfdb45SPaulo Zanoni 
223edbfdb45SPaulo Zanoni void snb_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
224edbfdb45SPaulo Zanoni {
225edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, mask);
226edbfdb45SPaulo Zanoni }
227edbfdb45SPaulo Zanoni 
228edbfdb45SPaulo Zanoni void snb_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
229edbfdb45SPaulo Zanoni {
230edbfdb45SPaulo Zanoni 	snb_update_pm_irq(dev_priv, mask, 0);
231edbfdb45SPaulo Zanoni }
232edbfdb45SPaulo Zanoni 
2338664281bSPaulo Zanoni static bool ivb_can_enable_err_int(struct drm_device *dev)
2348664281bSPaulo Zanoni {
2358664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2368664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2378664281bSPaulo Zanoni 	enum pipe pipe;
2388664281bSPaulo Zanoni 
2394bc9d430SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
2404bc9d430SDaniel Vetter 
2418664281bSPaulo Zanoni 	for_each_pipe(pipe) {
2428664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
2438664281bSPaulo Zanoni 
2448664281bSPaulo Zanoni 		if (crtc->cpu_fifo_underrun_disabled)
2458664281bSPaulo Zanoni 			return false;
2468664281bSPaulo Zanoni 	}
2478664281bSPaulo Zanoni 
2488664281bSPaulo Zanoni 	return true;
2498664281bSPaulo Zanoni }
2508664281bSPaulo Zanoni 
2510961021aSBen Widawsky /**
2520961021aSBen Widawsky   * bdw_update_pm_irq - update GT interrupt 2
2530961021aSBen Widawsky   * @dev_priv: driver private
2540961021aSBen Widawsky   * @interrupt_mask: mask of interrupt bits to update
2550961021aSBen Widawsky   * @enabled_irq_mask: mask of interrupt bits to enable
2560961021aSBen Widawsky   *
2570961021aSBen Widawsky   * Copied from the snb function, updated with relevant register offsets
2580961021aSBen Widawsky   */
2590961021aSBen Widawsky static void bdw_update_pm_irq(struct drm_i915_private *dev_priv,
2600961021aSBen Widawsky 			      uint32_t interrupt_mask,
2610961021aSBen Widawsky 			      uint32_t enabled_irq_mask)
2620961021aSBen Widawsky {
2630961021aSBen Widawsky 	uint32_t new_val;
2640961021aSBen Widawsky 
2650961021aSBen Widawsky 	assert_spin_locked(&dev_priv->irq_lock);
2660961021aSBen Widawsky 
2670961021aSBen Widawsky 	if (WARN_ON(dev_priv->pm.irqs_disabled))
2680961021aSBen Widawsky 		return;
2690961021aSBen Widawsky 
2700961021aSBen Widawsky 	new_val = dev_priv->pm_irq_mask;
2710961021aSBen Widawsky 	new_val &= ~interrupt_mask;
2720961021aSBen Widawsky 	new_val |= (~enabled_irq_mask & interrupt_mask);
2730961021aSBen Widawsky 
2740961021aSBen Widawsky 	if (new_val != dev_priv->pm_irq_mask) {
2750961021aSBen Widawsky 		dev_priv->pm_irq_mask = new_val;
2760961021aSBen Widawsky 		I915_WRITE(GEN8_GT_IMR(2), dev_priv->pm_irq_mask);
2770961021aSBen Widawsky 		POSTING_READ(GEN8_GT_IMR(2));
2780961021aSBen Widawsky 	}
2790961021aSBen Widawsky }
2800961021aSBen Widawsky 
2810961021aSBen Widawsky void bdw_enable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
2820961021aSBen Widawsky {
2830961021aSBen Widawsky 	bdw_update_pm_irq(dev_priv, mask, mask);
2840961021aSBen Widawsky }
2850961021aSBen Widawsky 
2860961021aSBen Widawsky void bdw_disable_pm_irq(struct drm_i915_private *dev_priv, uint32_t mask)
2870961021aSBen Widawsky {
2880961021aSBen Widawsky 	bdw_update_pm_irq(dev_priv, mask, 0);
2890961021aSBen Widawsky }
2900961021aSBen Widawsky 
2918664281bSPaulo Zanoni static bool cpt_can_enable_serr_int(struct drm_device *dev)
2928664281bSPaulo Zanoni {
2938664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
2948664281bSPaulo Zanoni 	enum pipe pipe;
2958664281bSPaulo Zanoni 	struct intel_crtc *crtc;
2968664281bSPaulo Zanoni 
297fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
298fee884edSDaniel Vetter 
2998664281bSPaulo Zanoni 	for_each_pipe(pipe) {
3008664281bSPaulo Zanoni 		crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
3018664281bSPaulo Zanoni 
3028664281bSPaulo Zanoni 		if (crtc->pch_fifo_underrun_disabled)
3038664281bSPaulo Zanoni 			return false;
3048664281bSPaulo Zanoni 	}
3058664281bSPaulo Zanoni 
3068664281bSPaulo Zanoni 	return true;
3078664281bSPaulo Zanoni }
3088664281bSPaulo Zanoni 
30956b80e1fSVille Syrjälä void i9xx_check_fifo_underruns(struct drm_device *dev)
31056b80e1fSVille Syrjälä {
31156b80e1fSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
31256b80e1fSVille Syrjälä 	struct intel_crtc *crtc;
31356b80e1fSVille Syrjälä 	unsigned long flags;
31456b80e1fSVille Syrjälä 
31556b80e1fSVille Syrjälä 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
31656b80e1fSVille Syrjälä 
31756b80e1fSVille Syrjälä 	for_each_intel_crtc(dev, crtc) {
31856b80e1fSVille Syrjälä 		u32 reg = PIPESTAT(crtc->pipe);
31956b80e1fSVille Syrjälä 		u32 pipestat;
32056b80e1fSVille Syrjälä 
32156b80e1fSVille Syrjälä 		if (crtc->cpu_fifo_underrun_disabled)
32256b80e1fSVille Syrjälä 			continue;
32356b80e1fSVille Syrjälä 
32456b80e1fSVille Syrjälä 		pipestat = I915_READ(reg) & 0xffff0000;
32556b80e1fSVille Syrjälä 		if ((pipestat & PIPE_FIFO_UNDERRUN_STATUS) == 0)
32656b80e1fSVille Syrjälä 			continue;
32756b80e1fSVille Syrjälä 
32856b80e1fSVille Syrjälä 		I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
32956b80e1fSVille Syrjälä 		POSTING_READ(reg);
33056b80e1fSVille Syrjälä 
33156b80e1fSVille Syrjälä 		DRM_ERROR("pipe %c underrun\n", pipe_name(crtc->pipe));
33256b80e1fSVille Syrjälä 	}
33356b80e1fSVille Syrjälä 
33456b80e1fSVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
33556b80e1fSVille Syrjälä }
33656b80e1fSVille Syrjälä 
337e69abff0SVille Syrjälä static void i9xx_set_fifo_underrun_reporting(struct drm_device *dev,
338e69abff0SVille Syrjälä 					     enum pipe pipe, bool enable)
3392d9d2b0bSVille Syrjälä {
3402d9d2b0bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
3412d9d2b0bSVille Syrjälä 	u32 reg = PIPESTAT(pipe);
342e69abff0SVille Syrjälä 	u32 pipestat = I915_READ(reg) & 0xffff0000;
3432d9d2b0bSVille Syrjälä 
3442d9d2b0bSVille Syrjälä 	assert_spin_locked(&dev_priv->irq_lock);
3452d9d2b0bSVille Syrjälä 
346e69abff0SVille Syrjälä 	if (enable) {
3472d9d2b0bSVille Syrjälä 		I915_WRITE(reg, pipestat | PIPE_FIFO_UNDERRUN_STATUS);
3482d9d2b0bSVille Syrjälä 		POSTING_READ(reg);
349e69abff0SVille Syrjälä 	} else {
350e69abff0SVille Syrjälä 		if (pipestat & PIPE_FIFO_UNDERRUN_STATUS)
351e69abff0SVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
352e69abff0SVille Syrjälä 	}
3532d9d2b0bSVille Syrjälä }
3542d9d2b0bSVille Syrjälä 
3558664281bSPaulo Zanoni static void ironlake_set_fifo_underrun_reporting(struct drm_device *dev,
3568664281bSPaulo Zanoni 						 enum pipe pipe, bool enable)
3578664281bSPaulo Zanoni {
3588664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3598664281bSPaulo Zanoni 	uint32_t bit = (pipe == PIPE_A) ? DE_PIPEA_FIFO_UNDERRUN :
3608664281bSPaulo Zanoni 					  DE_PIPEB_FIFO_UNDERRUN;
3618664281bSPaulo Zanoni 
3628664281bSPaulo Zanoni 	if (enable)
3638664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, bit);
3648664281bSPaulo Zanoni 	else
3658664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, bit);
3668664281bSPaulo Zanoni }
3678664281bSPaulo Zanoni 
3688664281bSPaulo Zanoni static void ivybridge_set_fifo_underrun_reporting(struct drm_device *dev,
3697336df65SDaniel Vetter 						  enum pipe pipe, bool enable)
3708664281bSPaulo Zanoni {
3718664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3728664281bSPaulo Zanoni 	if (enable) {
3737336df65SDaniel Vetter 		I915_WRITE(GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
3747336df65SDaniel Vetter 
3758664281bSPaulo Zanoni 		if (!ivb_can_enable_err_int(dev))
3768664281bSPaulo Zanoni 			return;
3778664281bSPaulo Zanoni 
3788664281bSPaulo Zanoni 		ironlake_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
3798664281bSPaulo Zanoni 	} else {
3808664281bSPaulo Zanoni 		ironlake_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
3817336df65SDaniel Vetter 
38229c6b0c5SVille Syrjälä 		if (I915_READ(GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe)) {
383823c6909SVille Syrjälä 			DRM_ERROR("uncleared fifo underrun on pipe %c\n",
3847336df65SDaniel Vetter 				  pipe_name(pipe));
3857336df65SDaniel Vetter 		}
3868664281bSPaulo Zanoni 	}
3878664281bSPaulo Zanoni }
3888664281bSPaulo Zanoni 
38938d83c96SDaniel Vetter static void broadwell_set_fifo_underrun_reporting(struct drm_device *dev,
39038d83c96SDaniel Vetter 						  enum pipe pipe, bool enable)
39138d83c96SDaniel Vetter {
39238d83c96SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
39338d83c96SDaniel Vetter 
39438d83c96SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
39538d83c96SDaniel Vetter 
39638d83c96SDaniel Vetter 	if (enable)
39738d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_FIFO_UNDERRUN;
39838d83c96SDaniel Vetter 	else
39938d83c96SDaniel Vetter 		dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_FIFO_UNDERRUN;
40038d83c96SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
40138d83c96SDaniel Vetter 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
40238d83c96SDaniel Vetter }
40338d83c96SDaniel Vetter 
404fee884edSDaniel Vetter /**
405fee884edSDaniel Vetter  * ibx_display_interrupt_update - update SDEIMR
406fee884edSDaniel Vetter  * @dev_priv: driver private
407fee884edSDaniel Vetter  * @interrupt_mask: mask of interrupt bits to update
408fee884edSDaniel Vetter  * @enabled_irq_mask: mask of interrupt bits to enable
409fee884edSDaniel Vetter  */
410fee884edSDaniel Vetter static void ibx_display_interrupt_update(struct drm_i915_private *dev_priv,
411fee884edSDaniel Vetter 					 uint32_t interrupt_mask,
412fee884edSDaniel Vetter 					 uint32_t enabled_irq_mask)
413fee884edSDaniel Vetter {
414fee884edSDaniel Vetter 	uint32_t sdeimr = I915_READ(SDEIMR);
415fee884edSDaniel Vetter 	sdeimr &= ~interrupt_mask;
416fee884edSDaniel Vetter 	sdeimr |= (~enabled_irq_mask & interrupt_mask);
417fee884edSDaniel Vetter 
418fee884edSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
419fee884edSDaniel Vetter 
420730488b2SPaulo Zanoni 	if (WARN_ON(dev_priv->pm.irqs_disabled))
421c67a470bSPaulo Zanoni 		return;
422c67a470bSPaulo Zanoni 
423fee884edSDaniel Vetter 	I915_WRITE(SDEIMR, sdeimr);
424fee884edSDaniel Vetter 	POSTING_READ(SDEIMR);
425fee884edSDaniel Vetter }
426fee884edSDaniel Vetter #define ibx_enable_display_interrupt(dev_priv, bits) \
427fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), (bits))
428fee884edSDaniel Vetter #define ibx_disable_display_interrupt(dev_priv, bits) \
429fee884edSDaniel Vetter 	ibx_display_interrupt_update((dev_priv), (bits), 0)
430fee884edSDaniel Vetter 
431de28075dSDaniel Vetter static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
432de28075dSDaniel Vetter 					    enum transcoder pch_transcoder,
4338664281bSPaulo Zanoni 					    bool enable)
4348664281bSPaulo Zanoni {
4358664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
436de28075dSDaniel Vetter 	uint32_t bit = (pch_transcoder == TRANSCODER_A) ?
437de28075dSDaniel Vetter 		       SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
4388664281bSPaulo Zanoni 
4398664281bSPaulo Zanoni 	if (enable)
440fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, bit);
4418664281bSPaulo Zanoni 	else
442fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, bit);
4438664281bSPaulo Zanoni }
4448664281bSPaulo Zanoni 
4458664281bSPaulo Zanoni static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
4468664281bSPaulo Zanoni 					    enum transcoder pch_transcoder,
4478664281bSPaulo Zanoni 					    bool enable)
4488664281bSPaulo Zanoni {
4498664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4508664281bSPaulo Zanoni 
4518664281bSPaulo Zanoni 	if (enable) {
4521dd246fbSDaniel Vetter 		I915_WRITE(SERR_INT,
4531dd246fbSDaniel Vetter 			   SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
4541dd246fbSDaniel Vetter 
4558664281bSPaulo Zanoni 		if (!cpt_can_enable_serr_int(dev))
4568664281bSPaulo Zanoni 			return;
4578664281bSPaulo Zanoni 
458fee884edSDaniel Vetter 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4598664281bSPaulo Zanoni 	} else {
460fee884edSDaniel Vetter 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
4611dd246fbSDaniel Vetter 
46229c6b0c5SVille Syrjälä 		if (I915_READ(SERR_INT) & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder)) {
463823c6909SVille Syrjälä 			DRM_ERROR("uncleared pch fifo underrun on pch transcoder %c\n",
4641dd246fbSDaniel Vetter 				  transcoder_name(pch_transcoder));
4651dd246fbSDaniel Vetter 		}
4668664281bSPaulo Zanoni 	}
4678664281bSPaulo Zanoni }
4688664281bSPaulo Zanoni 
4698664281bSPaulo Zanoni /**
4708664281bSPaulo Zanoni  * intel_set_cpu_fifo_underrun_reporting - enable/disable FIFO underrun messages
4718664281bSPaulo Zanoni  * @dev: drm device
4728664281bSPaulo Zanoni  * @pipe: pipe
4738664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
4748664281bSPaulo Zanoni  *
4758664281bSPaulo Zanoni  * This function makes us disable or enable CPU fifo underruns for a specific
4768664281bSPaulo Zanoni  * pipe. Notice that on some Gens (e.g. IVB, HSW), disabling FIFO underrun
4778664281bSPaulo Zanoni  * reporting for one pipe may also disable all the other CPU error interruts for
4788664281bSPaulo Zanoni  * the other pipes, due to the fact that there's just one interrupt mask/enable
4798664281bSPaulo Zanoni  * bit for all the pipes.
4808664281bSPaulo Zanoni  *
4818664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
4828664281bSPaulo Zanoni  */
483c5ab3bc0SDaniel Vetter static bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
4848664281bSPaulo Zanoni 						    enum pipe pipe, bool enable)
4858664281bSPaulo Zanoni {
4868664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4878664281bSPaulo Zanoni 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
4888664281bSPaulo Zanoni 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4898664281bSPaulo Zanoni 	bool ret;
4908664281bSPaulo Zanoni 
49177961eb9SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
49277961eb9SImre Deak 
4938664281bSPaulo Zanoni 	ret = !intel_crtc->cpu_fifo_underrun_disabled;
4948664281bSPaulo Zanoni 
4958664281bSPaulo Zanoni 	if (enable == ret)
4968664281bSPaulo Zanoni 		goto done;
4978664281bSPaulo Zanoni 
4988664281bSPaulo Zanoni 	intel_crtc->cpu_fifo_underrun_disabled = !enable;
4998664281bSPaulo Zanoni 
500e69abff0SVille Syrjälä 	if (INTEL_INFO(dev)->gen < 5 || IS_VALLEYVIEW(dev))
501e69abff0SVille Syrjälä 		i9xx_set_fifo_underrun_reporting(dev, pipe, enable);
5022d9d2b0bSVille Syrjälä 	else if (IS_GEN5(dev) || IS_GEN6(dev))
5038664281bSPaulo Zanoni 		ironlake_set_fifo_underrun_reporting(dev, pipe, enable);
5048664281bSPaulo Zanoni 	else if (IS_GEN7(dev))
5057336df65SDaniel Vetter 		ivybridge_set_fifo_underrun_reporting(dev, pipe, enable);
50638d83c96SDaniel Vetter 	else if (IS_GEN8(dev))
50738d83c96SDaniel Vetter 		broadwell_set_fifo_underrun_reporting(dev, pipe, enable);
5088664281bSPaulo Zanoni 
5098664281bSPaulo Zanoni done:
510f88d42f1SImre Deak 	return ret;
511f88d42f1SImre Deak }
512f88d42f1SImre Deak 
513f88d42f1SImre Deak bool intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
514f88d42f1SImre Deak 					   enum pipe pipe, bool enable)
515f88d42f1SImre Deak {
516f88d42f1SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
517f88d42f1SImre Deak 	unsigned long flags;
518f88d42f1SImre Deak 	bool ret;
519f88d42f1SImre Deak 
520f88d42f1SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
521f88d42f1SImre Deak 	ret = __intel_set_cpu_fifo_underrun_reporting(dev, pipe, enable);
5228664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
523f88d42f1SImre Deak 
5248664281bSPaulo Zanoni 	return ret;
5258664281bSPaulo Zanoni }
5268664281bSPaulo Zanoni 
52791d181ddSImre Deak static bool __cpu_fifo_underrun_reporting_enabled(struct drm_device *dev,
52891d181ddSImre Deak 						  enum pipe pipe)
52991d181ddSImre Deak {
53091d181ddSImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
53191d181ddSImre Deak 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
53291d181ddSImre Deak 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
53391d181ddSImre Deak 
53491d181ddSImre Deak 	return !intel_crtc->cpu_fifo_underrun_disabled;
53591d181ddSImre Deak }
53691d181ddSImre Deak 
5378664281bSPaulo Zanoni /**
5388664281bSPaulo Zanoni  * intel_set_pch_fifo_underrun_reporting - enable/disable FIFO underrun messages
5398664281bSPaulo Zanoni  * @dev: drm device
5408664281bSPaulo Zanoni  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
5418664281bSPaulo Zanoni  * @enable: true if we want to report FIFO underrun errors, false otherwise
5428664281bSPaulo Zanoni  *
5438664281bSPaulo Zanoni  * This function makes us disable or enable PCH fifo underruns for a specific
5448664281bSPaulo Zanoni  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
5458664281bSPaulo Zanoni  * underrun reporting for one transcoder may also disable all the other PCH
5468664281bSPaulo Zanoni  * error interruts for the other transcoders, due to the fact that there's just
5478664281bSPaulo Zanoni  * one interrupt mask/enable bit for all the transcoders.
5488664281bSPaulo Zanoni  *
5498664281bSPaulo Zanoni  * Returns the previous state of underrun reporting.
5508664281bSPaulo Zanoni  */
5518664281bSPaulo Zanoni bool intel_set_pch_fifo_underrun_reporting(struct drm_device *dev,
5528664281bSPaulo Zanoni 					   enum transcoder pch_transcoder,
5538664281bSPaulo Zanoni 					   bool enable)
5548664281bSPaulo Zanoni {
5558664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
556de28075dSDaniel Vetter 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pch_transcoder];
557de28075dSDaniel Vetter 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5588664281bSPaulo Zanoni 	unsigned long flags;
5598664281bSPaulo Zanoni 	bool ret;
5608664281bSPaulo Zanoni 
561de28075dSDaniel Vetter 	/*
562de28075dSDaniel Vetter 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
563de28075dSDaniel Vetter 	 * has only one pch transcoder A that all pipes can use. To avoid racy
564de28075dSDaniel Vetter 	 * pch transcoder -> pipe lookups from interrupt code simply store the
565de28075dSDaniel Vetter 	 * underrun statistics in crtc A. Since we never expose this anywhere
566de28075dSDaniel Vetter 	 * nor use it outside of the fifo underrun code here using the "wrong"
567de28075dSDaniel Vetter 	 * crtc on LPT won't cause issues.
568de28075dSDaniel Vetter 	 */
5698664281bSPaulo Zanoni 
5708664281bSPaulo Zanoni 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
5718664281bSPaulo Zanoni 
5728664281bSPaulo Zanoni 	ret = !intel_crtc->pch_fifo_underrun_disabled;
5738664281bSPaulo Zanoni 
5748664281bSPaulo Zanoni 	if (enable == ret)
5758664281bSPaulo Zanoni 		goto done;
5768664281bSPaulo Zanoni 
5778664281bSPaulo Zanoni 	intel_crtc->pch_fifo_underrun_disabled = !enable;
5788664281bSPaulo Zanoni 
5798664281bSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
580de28075dSDaniel Vetter 		ibx_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5818664281bSPaulo Zanoni 	else
5828664281bSPaulo Zanoni 		cpt_set_fifo_underrun_reporting(dev, pch_transcoder, enable);
5838664281bSPaulo Zanoni 
5848664281bSPaulo Zanoni done:
5858664281bSPaulo Zanoni 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
5868664281bSPaulo Zanoni 	return ret;
5878664281bSPaulo Zanoni }
5888664281bSPaulo Zanoni 
5898664281bSPaulo Zanoni 
590b5ea642aSDaniel Vetter static void
591755e9019SImre Deak __i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
592755e9019SImre Deak 		       u32 enable_mask, u32 status_mask)
5937c463586SKeith Packard {
5949db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
595755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
5967c463586SKeith Packard 
597b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
598b79480baSDaniel Vetter 
59904feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
60004feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
60104feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
60204feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
603755e9019SImre Deak 		return;
604755e9019SImre Deak 
605755e9019SImre Deak 	if ((pipestat & enable_mask) == enable_mask)
60646c06a30SVille Syrjälä 		return;
60746c06a30SVille Syrjälä 
60891d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] |= status_mask;
60991d181ddSImre Deak 
6107c463586SKeith Packard 	/* Enable the interrupt, clear any pending status */
611755e9019SImre Deak 	pipestat |= enable_mask | status_mask;
61246c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
6133143a2bfSChris Wilson 	POSTING_READ(reg);
6147c463586SKeith Packard }
6157c463586SKeith Packard 
616b5ea642aSDaniel Vetter static void
617755e9019SImre Deak __i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
618755e9019SImre Deak 		        u32 enable_mask, u32 status_mask)
6197c463586SKeith Packard {
6209db4a9c7SJesse Barnes 	u32 reg = PIPESTAT(pipe);
621755e9019SImre Deak 	u32 pipestat = I915_READ(reg) & PIPESTAT_INT_ENABLE_MASK;
6227c463586SKeith Packard 
623b79480baSDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
624b79480baSDaniel Vetter 
62504feced9SVille Syrjälä 	if (WARN_ONCE(enable_mask & ~PIPESTAT_INT_ENABLE_MASK ||
62604feced9SVille Syrjälä 		      status_mask & ~PIPESTAT_INT_STATUS_MASK,
62704feced9SVille Syrjälä 		      "pipe %c: enable_mask=0x%x, status_mask=0x%x\n",
62804feced9SVille Syrjälä 		      pipe_name(pipe), enable_mask, status_mask))
62946c06a30SVille Syrjälä 		return;
63046c06a30SVille Syrjälä 
631755e9019SImre Deak 	if ((pipestat & enable_mask) == 0)
632755e9019SImre Deak 		return;
633755e9019SImre Deak 
63491d181ddSImre Deak 	dev_priv->pipestat_irq_mask[pipe] &= ~status_mask;
63591d181ddSImre Deak 
636755e9019SImre Deak 	pipestat &= ~enable_mask;
63746c06a30SVille Syrjälä 	I915_WRITE(reg, pipestat);
6383143a2bfSChris Wilson 	POSTING_READ(reg);
6397c463586SKeith Packard }
6407c463586SKeith Packard 
64110c59c51SImre Deak static u32 vlv_get_pipestat_enable_mask(struct drm_device *dev, u32 status_mask)
64210c59c51SImre Deak {
64310c59c51SImre Deak 	u32 enable_mask = status_mask << 16;
64410c59c51SImre Deak 
64510c59c51SImre Deak 	/*
646724a6905SVille Syrjälä 	 * On pipe A we don't support the PSR interrupt yet,
647724a6905SVille Syrjälä 	 * on pipe B and C the same bit MBZ.
64810c59c51SImre Deak 	 */
64910c59c51SImre Deak 	if (WARN_ON_ONCE(status_mask & PIPE_A_PSR_STATUS_VLV))
65010c59c51SImre Deak 		return 0;
651724a6905SVille Syrjälä 	/*
652724a6905SVille Syrjälä 	 * On pipe B and C we don't support the PSR interrupt yet, on pipe
653724a6905SVille Syrjälä 	 * A the same bit is for perf counters which we don't use either.
654724a6905SVille Syrjälä 	 */
655724a6905SVille Syrjälä 	if (WARN_ON_ONCE(status_mask & PIPE_B_PSR_STATUS_VLV))
656724a6905SVille Syrjälä 		return 0;
65710c59c51SImre Deak 
65810c59c51SImre Deak 	enable_mask &= ~(PIPE_FIFO_UNDERRUN_STATUS |
65910c59c51SImre Deak 			 SPRITE0_FLIP_DONE_INT_EN_VLV |
66010c59c51SImre Deak 			 SPRITE1_FLIP_DONE_INT_EN_VLV);
66110c59c51SImre Deak 	if (status_mask & SPRITE0_FLIP_DONE_INT_STATUS_VLV)
66210c59c51SImre Deak 		enable_mask |= SPRITE0_FLIP_DONE_INT_EN_VLV;
66310c59c51SImre Deak 	if (status_mask & SPRITE1_FLIP_DONE_INT_STATUS_VLV)
66410c59c51SImre Deak 		enable_mask |= SPRITE1_FLIP_DONE_INT_EN_VLV;
66510c59c51SImre Deak 
66610c59c51SImre Deak 	return enable_mask;
66710c59c51SImre Deak }
66810c59c51SImre Deak 
669755e9019SImre Deak void
670755e9019SImre Deak i915_enable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
671755e9019SImre Deak 		     u32 status_mask)
672755e9019SImre Deak {
673755e9019SImre Deak 	u32 enable_mask;
674755e9019SImre Deak 
67510c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
67610c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
67710c59c51SImre Deak 							   status_mask);
67810c59c51SImre Deak 	else
679755e9019SImre Deak 		enable_mask = status_mask << 16;
680755e9019SImre Deak 	__i915_enable_pipestat(dev_priv, pipe, enable_mask, status_mask);
681755e9019SImre Deak }
682755e9019SImre Deak 
683755e9019SImre Deak void
684755e9019SImre Deak i915_disable_pipestat(struct drm_i915_private *dev_priv, enum pipe pipe,
685755e9019SImre Deak 		      u32 status_mask)
686755e9019SImre Deak {
687755e9019SImre Deak 	u32 enable_mask;
688755e9019SImre Deak 
68910c59c51SImre Deak 	if (IS_VALLEYVIEW(dev_priv->dev))
69010c59c51SImre Deak 		enable_mask = vlv_get_pipestat_enable_mask(dev_priv->dev,
69110c59c51SImre Deak 							   status_mask);
69210c59c51SImre Deak 	else
693755e9019SImre Deak 		enable_mask = status_mask << 16;
694755e9019SImre Deak 	__i915_disable_pipestat(dev_priv, pipe, enable_mask, status_mask);
695755e9019SImre Deak }
696755e9019SImre Deak 
697c0e09200SDave Airlie /**
698f49e38ddSJani Nikula  * i915_enable_asle_pipestat - enable ASLE pipestat for OpRegion
69901c66889SZhao Yakui  */
700f49e38ddSJani Nikula static void i915_enable_asle_pipestat(struct drm_device *dev)
70101c66889SZhao Yakui {
7022d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
7031ec14ad3SChris Wilson 	unsigned long irqflags;
7041ec14ad3SChris Wilson 
705f49e38ddSJani Nikula 	if (!dev_priv->opregion.asle || !IS_MOBILE(dev))
706f49e38ddSJani Nikula 		return;
707f49e38ddSJani Nikula 
7081ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
70901c66889SZhao Yakui 
710755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_LEGACY_BLC_EVENT_STATUS);
711a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4)
7123b6c42e8SDaniel Vetter 		i915_enable_pipestat(dev_priv, PIPE_A,
713755e9019SImre Deak 				     PIPE_LEGACY_BLC_EVENT_STATUS);
7141ec14ad3SChris Wilson 
7151ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
71601c66889SZhao Yakui }
71701c66889SZhao Yakui 
71801c66889SZhao Yakui /**
7190a3e67a4SJesse Barnes  * i915_pipe_enabled - check if a pipe is enabled
7200a3e67a4SJesse Barnes  * @dev: DRM device
7210a3e67a4SJesse Barnes  * @pipe: pipe to check
7220a3e67a4SJesse Barnes  *
7230a3e67a4SJesse Barnes  * Reading certain registers when the pipe is disabled can hang the chip.
7240a3e67a4SJesse Barnes  * Use this routine to make sure the PLL is running and the pipe is active
7250a3e67a4SJesse Barnes  * before reading such registers if unsure.
7260a3e67a4SJesse Barnes  */
7270a3e67a4SJesse Barnes static int
7280a3e67a4SJesse Barnes i915_pipe_enabled(struct drm_device *dev, int pipe)
7290a3e67a4SJesse Barnes {
7302d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
731702e7a56SPaulo Zanoni 
732a01025afSDaniel Vetter 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
733a01025afSDaniel Vetter 		/* Locking is horribly broken here, but whatever. */
734a01025afSDaniel Vetter 		struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
735a01025afSDaniel Vetter 		struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
73671f8ba6bSPaulo Zanoni 
737a01025afSDaniel Vetter 		return intel_crtc->active;
738a01025afSDaniel Vetter 	} else {
739a01025afSDaniel Vetter 		return I915_READ(PIPECONF(pipe)) & PIPECONF_ENABLE;
740a01025afSDaniel Vetter 	}
7410a3e67a4SJesse Barnes }
7420a3e67a4SJesse Barnes 
743f75f3746SVille Syrjälä /*
744f75f3746SVille Syrjälä  * This timing diagram depicts the video signal in and
745f75f3746SVille Syrjälä  * around the vertical blanking period.
746f75f3746SVille Syrjälä  *
747f75f3746SVille Syrjälä  * Assumptions about the fictitious mode used in this example:
748f75f3746SVille Syrjälä  *  vblank_start >= 3
749f75f3746SVille Syrjälä  *  vsync_start = vblank_start + 1
750f75f3746SVille Syrjälä  *  vsync_end = vblank_start + 2
751f75f3746SVille Syrjälä  *  vtotal = vblank_start + 3
752f75f3746SVille Syrjälä  *
753f75f3746SVille Syrjälä  *           start of vblank:
754f75f3746SVille Syrjälä  *           latch double buffered registers
755f75f3746SVille Syrjälä  *           increment frame counter (ctg+)
756f75f3746SVille Syrjälä  *           generate start of vblank interrupt (gen4+)
757f75f3746SVille Syrjälä  *           |
758f75f3746SVille Syrjälä  *           |          frame start:
759f75f3746SVille Syrjälä  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
760f75f3746SVille Syrjälä  *           |          may be shifted forward 1-3 extra lines via PIPECONF
761f75f3746SVille Syrjälä  *           |          |
762f75f3746SVille Syrjälä  *           |          |  start of vsync:
763f75f3746SVille Syrjälä  *           |          |  generate vsync interrupt
764f75f3746SVille Syrjälä  *           |          |  |
765f75f3746SVille Syrjälä  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
766f75f3746SVille Syrjälä  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
767f75f3746SVille Syrjälä  * ----va---> <-----------------vb--------------------> <--------va-------------
768f75f3746SVille Syrjälä  *       |          |       <----vs----->                     |
769f75f3746SVille Syrjälä  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
770f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
771f75f3746SVille Syrjälä  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
772f75f3746SVille Syrjälä  *       |          |                                         |
773f75f3746SVille Syrjälä  *       last visible pixel                                   first visible pixel
774f75f3746SVille Syrjälä  *                  |                                         increment frame counter (gen3/4)
775f75f3746SVille Syrjälä  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
776f75f3746SVille Syrjälä  *
777f75f3746SVille Syrjälä  * x  = horizontal active
778f75f3746SVille Syrjälä  * _  = horizontal blanking
779f75f3746SVille Syrjälä  * hs = horizontal sync
780f75f3746SVille Syrjälä  * va = vertical active
781f75f3746SVille Syrjälä  * vb = vertical blanking
782f75f3746SVille Syrjälä  * vs = vertical sync
783f75f3746SVille Syrjälä  * vbs = vblank_start (number)
784f75f3746SVille Syrjälä  *
785f75f3746SVille Syrjälä  * Summary:
786f75f3746SVille Syrjälä  * - most events happen at the start of horizontal sync
787f75f3746SVille Syrjälä  * - frame start happens at the start of horizontal blank, 1-4 lines
788f75f3746SVille Syrjälä  *   (depending on PIPECONF settings) after the start of vblank
789f75f3746SVille Syrjälä  * - gen3/4 pixel and frame counter are synchronized with the start
790f75f3746SVille Syrjälä  *   of horizontal active on the first line of vertical active
791f75f3746SVille Syrjälä  */
792f75f3746SVille Syrjälä 
7934cdb83ecSVille Syrjälä static u32 i8xx_get_vblank_counter(struct drm_device *dev, int pipe)
7944cdb83ecSVille Syrjälä {
7954cdb83ecSVille Syrjälä 	/* Gen2 doesn't have a hardware frame counter */
7964cdb83ecSVille Syrjälä 	return 0;
7974cdb83ecSVille Syrjälä }
7984cdb83ecSVille Syrjälä 
79942f52ef8SKeith Packard /* Called from drm generic code, passed a 'crtc', which
80042f52ef8SKeith Packard  * we use as a pipe index
80142f52ef8SKeith Packard  */
802f71d4af4SJesse Barnes static u32 i915_get_vblank_counter(struct drm_device *dev, int pipe)
8030a3e67a4SJesse Barnes {
8042d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
8050a3e67a4SJesse Barnes 	unsigned long high_frame;
8060a3e67a4SJesse Barnes 	unsigned long low_frame;
8070b2a8e09SVille Syrjälä 	u32 high1, high2, low, pixel, vbl_start, hsync_start, htotal;
8080a3e67a4SJesse Barnes 
8090a3e67a4SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
81044d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
8119db4a9c7SJesse Barnes 				"pipe %c\n", pipe_name(pipe));
8120a3e67a4SJesse Barnes 		return 0;
8130a3e67a4SJesse Barnes 	}
8140a3e67a4SJesse Barnes 
815391f75e2SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
816391f75e2SVille Syrjälä 		struct intel_crtc *intel_crtc =
817391f75e2SVille Syrjälä 			to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
818391f75e2SVille Syrjälä 		const struct drm_display_mode *mode =
819391f75e2SVille Syrjälä 			&intel_crtc->config.adjusted_mode;
820391f75e2SVille Syrjälä 
8210b2a8e09SVille Syrjälä 		htotal = mode->crtc_htotal;
8220b2a8e09SVille Syrjälä 		hsync_start = mode->crtc_hsync_start;
8230b2a8e09SVille Syrjälä 		vbl_start = mode->crtc_vblank_start;
8240b2a8e09SVille Syrjälä 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
8250b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
826391f75e2SVille Syrjälä 	} else {
827a2d213ddSDaniel Vetter 		enum transcoder cpu_transcoder = (enum transcoder) pipe;
828391f75e2SVille Syrjälä 
829391f75e2SVille Syrjälä 		htotal = ((I915_READ(HTOTAL(cpu_transcoder)) >> 16) & 0x1fff) + 1;
8300b2a8e09SVille Syrjälä 		hsync_start = (I915_READ(HSYNC(cpu_transcoder))  & 0x1fff) + 1;
831391f75e2SVille Syrjälä 		vbl_start = (I915_READ(VBLANK(cpu_transcoder)) & 0x1fff) + 1;
8320b2a8e09SVille Syrjälä 		if ((I915_READ(PIPECONF(cpu_transcoder)) &
8330b2a8e09SVille Syrjälä 		     PIPECONF_INTERLACE_MASK) != PIPECONF_PROGRESSIVE)
8340b2a8e09SVille Syrjälä 			vbl_start = DIV_ROUND_UP(vbl_start, 2);
835391f75e2SVille Syrjälä 	}
836391f75e2SVille Syrjälä 
8370b2a8e09SVille Syrjälä 	/* Convert to pixel count */
8380b2a8e09SVille Syrjälä 	vbl_start *= htotal;
8390b2a8e09SVille Syrjälä 
8400b2a8e09SVille Syrjälä 	/* Start of vblank event occurs at start of hsync */
8410b2a8e09SVille Syrjälä 	vbl_start -= htotal - hsync_start;
8420b2a8e09SVille Syrjälä 
8439db4a9c7SJesse Barnes 	high_frame = PIPEFRAME(pipe);
8449db4a9c7SJesse Barnes 	low_frame = PIPEFRAMEPIXEL(pipe);
8455eddb70bSChris Wilson 
8460a3e67a4SJesse Barnes 	/*
8470a3e67a4SJesse Barnes 	 * High & low register fields aren't synchronized, so make sure
8480a3e67a4SJesse Barnes 	 * we get a low value that's stable across two reads of the high
8490a3e67a4SJesse Barnes 	 * register.
8500a3e67a4SJesse Barnes 	 */
8510a3e67a4SJesse Barnes 	do {
8525eddb70bSChris Wilson 		high1 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
853391f75e2SVille Syrjälä 		low   = I915_READ(low_frame);
8545eddb70bSChris Wilson 		high2 = I915_READ(high_frame) & PIPE_FRAME_HIGH_MASK;
8550a3e67a4SJesse Barnes 	} while (high1 != high2);
8560a3e67a4SJesse Barnes 
8575eddb70bSChris Wilson 	high1 >>= PIPE_FRAME_HIGH_SHIFT;
858391f75e2SVille Syrjälä 	pixel = low & PIPE_PIXEL_MASK;
8595eddb70bSChris Wilson 	low >>= PIPE_FRAME_LOW_SHIFT;
860391f75e2SVille Syrjälä 
861391f75e2SVille Syrjälä 	/*
862391f75e2SVille Syrjälä 	 * The frame counter increments at beginning of active.
863391f75e2SVille Syrjälä 	 * Cook up a vblank counter by also checking the pixel
864391f75e2SVille Syrjälä 	 * counter against vblank start.
865391f75e2SVille Syrjälä 	 */
866edc08d0aSVille Syrjälä 	return (((high1 << 8) | low) + (pixel >= vbl_start)) & 0xffffff;
8670a3e67a4SJesse Barnes }
8680a3e67a4SJesse Barnes 
869f71d4af4SJesse Barnes static u32 gm45_get_vblank_counter(struct drm_device *dev, int pipe)
8709880b7a5SJesse Barnes {
8712d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
8729db4a9c7SJesse Barnes 	int reg = PIPE_FRMCOUNT_GM45(pipe);
8739880b7a5SJesse Barnes 
8749880b7a5SJesse Barnes 	if (!i915_pipe_enabled(dev, pipe)) {
87544d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("trying to get vblank count for disabled "
8769db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
8779880b7a5SJesse Barnes 		return 0;
8789880b7a5SJesse Barnes 	}
8799880b7a5SJesse Barnes 
8809880b7a5SJesse Barnes 	return I915_READ(reg);
8819880b7a5SJesse Barnes }
8829880b7a5SJesse Barnes 
883ad3543edSMario Kleiner /* raw reads, only for fast reads of display block, no need for forcewake etc. */
884ad3543edSMario Kleiner #define __raw_i915_read32(dev_priv__, reg__) readl((dev_priv__)->regs + (reg__))
885ad3543edSMario Kleiner 
886a225f079SVille Syrjälä static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
887a225f079SVille Syrjälä {
888a225f079SVille Syrjälä 	struct drm_device *dev = crtc->base.dev;
889a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
890a225f079SVille Syrjälä 	const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
891a225f079SVille Syrjälä 	enum pipe pipe = crtc->pipe;
89280715b2fSVille Syrjälä 	int position, vtotal;
893a225f079SVille Syrjälä 
89480715b2fSVille Syrjälä 	vtotal = mode->crtc_vtotal;
895a225f079SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
896a225f079SVille Syrjälä 		vtotal /= 2;
897a225f079SVille Syrjälä 
898a225f079SVille Syrjälä 	if (IS_GEN2(dev))
899a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN2;
900a225f079SVille Syrjälä 	else
901a225f079SVille Syrjälä 		position = __raw_i915_read32(dev_priv, PIPEDSL(pipe)) & DSL_LINEMASK_GEN3;
902a225f079SVille Syrjälä 
903a225f079SVille Syrjälä 	/*
90480715b2fSVille Syrjälä 	 * See update_scanline_offset() for the details on the
90580715b2fSVille Syrjälä 	 * scanline_offset adjustment.
906a225f079SVille Syrjälä 	 */
90780715b2fSVille Syrjälä 	return (position + crtc->scanline_offset) % vtotal;
908a225f079SVille Syrjälä }
909a225f079SVille Syrjälä 
910f71d4af4SJesse Barnes static int i915_get_crtc_scanoutpos(struct drm_device *dev, int pipe,
911abca9e45SVille Syrjälä 				    unsigned int flags, int *vpos, int *hpos,
912abca9e45SVille Syrjälä 				    ktime_t *stime, ktime_t *etime)
9130af7e4dfSMario Kleiner {
914c2baf4b7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
915c2baf4b7SVille Syrjälä 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
916c2baf4b7SVille Syrjälä 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
917c2baf4b7SVille Syrjälä 	const struct drm_display_mode *mode = &intel_crtc->config.adjusted_mode;
9183aa18df8SVille Syrjälä 	int position;
91978e8fc6bSVille Syrjälä 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
9200af7e4dfSMario Kleiner 	bool in_vbl = true;
9210af7e4dfSMario Kleiner 	int ret = 0;
922ad3543edSMario Kleiner 	unsigned long irqflags;
9230af7e4dfSMario Kleiner 
924c2baf4b7SVille Syrjälä 	if (!intel_crtc->active) {
9250af7e4dfSMario Kleiner 		DRM_DEBUG_DRIVER("trying to get scanoutpos for disabled "
9269db4a9c7SJesse Barnes 				 "pipe %c\n", pipe_name(pipe));
9270af7e4dfSMario Kleiner 		return 0;
9280af7e4dfSMario Kleiner 	}
9290af7e4dfSMario Kleiner 
930c2baf4b7SVille Syrjälä 	htotal = mode->crtc_htotal;
93178e8fc6bSVille Syrjälä 	hsync_start = mode->crtc_hsync_start;
932c2baf4b7SVille Syrjälä 	vtotal = mode->crtc_vtotal;
933c2baf4b7SVille Syrjälä 	vbl_start = mode->crtc_vblank_start;
934c2baf4b7SVille Syrjälä 	vbl_end = mode->crtc_vblank_end;
9350af7e4dfSMario Kleiner 
936d31faf65SVille Syrjälä 	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
937d31faf65SVille Syrjälä 		vbl_start = DIV_ROUND_UP(vbl_start, 2);
938d31faf65SVille Syrjälä 		vbl_end /= 2;
939d31faf65SVille Syrjälä 		vtotal /= 2;
940d31faf65SVille Syrjälä 	}
941d31faf65SVille Syrjälä 
942c2baf4b7SVille Syrjälä 	ret |= DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE;
943c2baf4b7SVille Syrjälä 
944ad3543edSMario Kleiner 	/*
945ad3543edSMario Kleiner 	 * Lock uncore.lock, as we will do multiple timing critical raw
946ad3543edSMario Kleiner 	 * register reads, potentially with preemption disabled, so the
947ad3543edSMario Kleiner 	 * following code must not block on uncore.lock.
948ad3543edSMario Kleiner 	 */
949ad3543edSMario Kleiner 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
950ad3543edSMario Kleiner 
951ad3543edSMario Kleiner 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
952ad3543edSMario Kleiner 
953ad3543edSMario Kleiner 	/* Get optional system timestamp before query. */
954ad3543edSMario Kleiner 	if (stime)
955ad3543edSMario Kleiner 		*stime = ktime_get();
956ad3543edSMario Kleiner 
9577c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
9580af7e4dfSMario Kleiner 		/* No obvious pixelcount register. Only query vertical
9590af7e4dfSMario Kleiner 		 * scanout position from Display scan line register.
9600af7e4dfSMario Kleiner 		 */
961a225f079SVille Syrjälä 		position = __intel_get_crtc_scanline(intel_crtc);
9620af7e4dfSMario Kleiner 	} else {
9630af7e4dfSMario Kleiner 		/* Have access to pixelcount since start of frame.
9640af7e4dfSMario Kleiner 		 * We can split this into vertical and horizontal
9650af7e4dfSMario Kleiner 		 * scanout position.
9660af7e4dfSMario Kleiner 		 */
967ad3543edSMario Kleiner 		position = (__raw_i915_read32(dev_priv, PIPEFRAMEPIXEL(pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
9680af7e4dfSMario Kleiner 
9693aa18df8SVille Syrjälä 		/* convert to pixel counts */
9703aa18df8SVille Syrjälä 		vbl_start *= htotal;
9713aa18df8SVille Syrjälä 		vbl_end *= htotal;
9723aa18df8SVille Syrjälä 		vtotal *= htotal;
97378e8fc6bSVille Syrjälä 
97478e8fc6bSVille Syrjälä 		/*
9757e78f1cbSVille Syrjälä 		 * In interlaced modes, the pixel counter counts all pixels,
9767e78f1cbSVille Syrjälä 		 * so one field will have htotal more pixels. In order to avoid
9777e78f1cbSVille Syrjälä 		 * the reported position from jumping backwards when the pixel
9787e78f1cbSVille Syrjälä 		 * counter is beyond the length of the shorter field, just
9797e78f1cbSVille Syrjälä 		 * clamp the position the length of the shorter field. This
9807e78f1cbSVille Syrjälä 		 * matches how the scanline counter based position works since
9817e78f1cbSVille Syrjälä 		 * the scanline counter doesn't count the two half lines.
9827e78f1cbSVille Syrjälä 		 */
9837e78f1cbSVille Syrjälä 		if (position >= vtotal)
9847e78f1cbSVille Syrjälä 			position = vtotal - 1;
9857e78f1cbSVille Syrjälä 
9867e78f1cbSVille Syrjälä 		/*
98778e8fc6bSVille Syrjälä 		 * Start of vblank interrupt is triggered at start of hsync,
98878e8fc6bSVille Syrjälä 		 * just prior to the first active line of vblank. However we
98978e8fc6bSVille Syrjälä 		 * consider lines to start at the leading edge of horizontal
99078e8fc6bSVille Syrjälä 		 * active. So, should we get here before we've crossed into
99178e8fc6bSVille Syrjälä 		 * the horizontal active of the first line in vblank, we would
99278e8fc6bSVille Syrjälä 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
99378e8fc6bSVille Syrjälä 		 * always add htotal-hsync_start to the current pixel position.
99478e8fc6bSVille Syrjälä 		 */
99578e8fc6bSVille Syrjälä 		position = (position + htotal - hsync_start) % vtotal;
9963aa18df8SVille Syrjälä 	}
9973aa18df8SVille Syrjälä 
998ad3543edSMario Kleiner 	/* Get optional system timestamp after query. */
999ad3543edSMario Kleiner 	if (etime)
1000ad3543edSMario Kleiner 		*etime = ktime_get();
1001ad3543edSMario Kleiner 
1002ad3543edSMario Kleiner 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
1003ad3543edSMario Kleiner 
1004ad3543edSMario Kleiner 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1005ad3543edSMario Kleiner 
10063aa18df8SVille Syrjälä 	in_vbl = position >= vbl_start && position < vbl_end;
10073aa18df8SVille Syrjälä 
10083aa18df8SVille Syrjälä 	/*
10093aa18df8SVille Syrjälä 	 * While in vblank, position will be negative
10103aa18df8SVille Syrjälä 	 * counting up towards 0 at vbl_end. And outside
10113aa18df8SVille Syrjälä 	 * vblank, position will be positive counting
10123aa18df8SVille Syrjälä 	 * up since vbl_end.
10133aa18df8SVille Syrjälä 	 */
10143aa18df8SVille Syrjälä 	if (position >= vbl_start)
10153aa18df8SVille Syrjälä 		position -= vbl_end;
10163aa18df8SVille Syrjälä 	else
10173aa18df8SVille Syrjälä 		position += vtotal - vbl_end;
10183aa18df8SVille Syrjälä 
10197c06b08aSVille Syrjälä 	if (IS_GEN2(dev) || IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
10203aa18df8SVille Syrjälä 		*vpos = position;
10213aa18df8SVille Syrjälä 		*hpos = 0;
10223aa18df8SVille Syrjälä 	} else {
10230af7e4dfSMario Kleiner 		*vpos = position / htotal;
10240af7e4dfSMario Kleiner 		*hpos = position - (*vpos * htotal);
10250af7e4dfSMario Kleiner 	}
10260af7e4dfSMario Kleiner 
10270af7e4dfSMario Kleiner 	/* In vblank? */
10280af7e4dfSMario Kleiner 	if (in_vbl)
10290af7e4dfSMario Kleiner 		ret |= DRM_SCANOUTPOS_INVBL;
10300af7e4dfSMario Kleiner 
10310af7e4dfSMario Kleiner 	return ret;
10320af7e4dfSMario Kleiner }
10330af7e4dfSMario Kleiner 
1034a225f079SVille Syrjälä int intel_get_crtc_scanline(struct intel_crtc *crtc)
1035a225f079SVille Syrjälä {
1036a225f079SVille Syrjälä 	struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1037a225f079SVille Syrjälä 	unsigned long irqflags;
1038a225f079SVille Syrjälä 	int position;
1039a225f079SVille Syrjälä 
1040a225f079SVille Syrjälä 	spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
1041a225f079SVille Syrjälä 	position = __intel_get_crtc_scanline(crtc);
1042a225f079SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
1043a225f079SVille Syrjälä 
1044a225f079SVille Syrjälä 	return position;
1045a225f079SVille Syrjälä }
1046a225f079SVille Syrjälä 
1047f71d4af4SJesse Barnes static int i915_get_vblank_timestamp(struct drm_device *dev, int pipe,
10480af7e4dfSMario Kleiner 			      int *max_error,
10490af7e4dfSMario Kleiner 			      struct timeval *vblank_time,
10500af7e4dfSMario Kleiner 			      unsigned flags)
10510af7e4dfSMario Kleiner {
10524041b853SChris Wilson 	struct drm_crtc *crtc;
10530af7e4dfSMario Kleiner 
10547eb552aeSBen Widawsky 	if (pipe < 0 || pipe >= INTEL_INFO(dev)->num_pipes) {
10554041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
10560af7e4dfSMario Kleiner 		return -EINVAL;
10570af7e4dfSMario Kleiner 	}
10580af7e4dfSMario Kleiner 
10590af7e4dfSMario Kleiner 	/* Get drm_crtc to timestamp: */
10604041b853SChris Wilson 	crtc = intel_get_crtc_for_pipe(dev, pipe);
10614041b853SChris Wilson 	if (crtc == NULL) {
10624041b853SChris Wilson 		DRM_ERROR("Invalid crtc %d\n", pipe);
10634041b853SChris Wilson 		return -EINVAL;
10644041b853SChris Wilson 	}
10654041b853SChris Wilson 
10664041b853SChris Wilson 	if (!crtc->enabled) {
10674041b853SChris Wilson 		DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
10684041b853SChris Wilson 		return -EBUSY;
10694041b853SChris Wilson 	}
10700af7e4dfSMario Kleiner 
10710af7e4dfSMario Kleiner 	/* Helper routine in DRM core does all the work: */
10724041b853SChris Wilson 	return drm_calc_vbltimestamp_from_scanoutpos(dev, pipe, max_error,
10734041b853SChris Wilson 						     vblank_time, flags,
10747da903efSVille Syrjälä 						     crtc,
10757da903efSVille Syrjälä 						     &to_intel_crtc(crtc)->config.adjusted_mode);
10760af7e4dfSMario Kleiner }
10770af7e4dfSMario Kleiner 
107867c347ffSJani Nikula static bool intel_hpd_irq_event(struct drm_device *dev,
107967c347ffSJani Nikula 				struct drm_connector *connector)
1080321a1b30SEgbert Eich {
1081321a1b30SEgbert Eich 	enum drm_connector_status old_status;
1082321a1b30SEgbert Eich 
1083321a1b30SEgbert Eich 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
1084321a1b30SEgbert Eich 	old_status = connector->status;
1085321a1b30SEgbert Eich 
1086321a1b30SEgbert Eich 	connector->status = connector->funcs->detect(connector, false);
108767c347ffSJani Nikula 	if (old_status == connector->status)
108867c347ffSJani Nikula 		return false;
108967c347ffSJani Nikula 
109067c347ffSJani Nikula 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s] status updated from %s to %s\n",
1091321a1b30SEgbert Eich 		      connector->base.id,
1092321a1b30SEgbert Eich 		      drm_get_connector_name(connector),
109367c347ffSJani Nikula 		      drm_get_connector_status_name(old_status),
109467c347ffSJani Nikula 		      drm_get_connector_status_name(connector->status));
109567c347ffSJani Nikula 
109667c347ffSJani Nikula 	return true;
1097321a1b30SEgbert Eich }
1098321a1b30SEgbert Eich 
10995ca58282SJesse Barnes /*
11005ca58282SJesse Barnes  * Handle hotplug events outside the interrupt handler proper.
11015ca58282SJesse Barnes  */
1102ac4c16c5SEgbert Eich #define I915_REENABLE_HOTPLUG_DELAY (2*60*1000)
1103ac4c16c5SEgbert Eich 
11045ca58282SJesse Barnes static void i915_hotplug_work_func(struct work_struct *work)
11055ca58282SJesse Barnes {
11062d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
11072d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, hotplug_work);
11085ca58282SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
1109c31c4ba3SKeith Packard 	struct drm_mode_config *mode_config = &dev->mode_config;
1110cd569aedSEgbert Eich 	struct intel_connector *intel_connector;
1111cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
1112cd569aedSEgbert Eich 	struct drm_connector *connector;
1113cd569aedSEgbert Eich 	unsigned long irqflags;
1114cd569aedSEgbert Eich 	bool hpd_disabled = false;
1115321a1b30SEgbert Eich 	bool changed = false;
1116142e2398SEgbert Eich 	u32 hpd_event_bits;
11175ca58282SJesse Barnes 
111852d7ecedSDaniel Vetter 	/* HPD irq before everything is fully set up. */
111952d7ecedSDaniel Vetter 	if (!dev_priv->enable_hotplug_processing)
112052d7ecedSDaniel Vetter 		return;
112152d7ecedSDaniel Vetter 
1122a65e34c7SKeith Packard 	mutex_lock(&mode_config->mutex);
1123e67189abSJesse Barnes 	DRM_DEBUG_KMS("running encoder hotplug functions\n");
1124e67189abSJesse Barnes 
1125cd569aedSEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
1126142e2398SEgbert Eich 
1127142e2398SEgbert Eich 	hpd_event_bits = dev_priv->hpd_event_bits;
1128142e2398SEgbert Eich 	dev_priv->hpd_event_bits = 0;
1129cd569aedSEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1130cd569aedSEgbert Eich 		intel_connector = to_intel_connector(connector);
1131cd569aedSEgbert Eich 		intel_encoder = intel_connector->encoder;
1132cd569aedSEgbert Eich 		if (intel_encoder->hpd_pin > HPD_NONE &&
1133cd569aedSEgbert Eich 		    dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_MARK_DISABLED &&
1134cd569aedSEgbert Eich 		    connector->polled == DRM_CONNECTOR_POLL_HPD) {
1135cd569aedSEgbert Eich 			DRM_INFO("HPD interrupt storm detected on connector %s: "
1136cd569aedSEgbert Eich 				 "switching from hotplug detection to polling\n",
1137cd569aedSEgbert Eich 				drm_get_connector_name(connector));
1138cd569aedSEgbert Eich 			dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark = HPD_DISABLED;
1139cd569aedSEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_CONNECT
1140cd569aedSEgbert Eich 				| DRM_CONNECTOR_POLL_DISCONNECT;
1141cd569aedSEgbert Eich 			hpd_disabled = true;
1142cd569aedSEgbert Eich 		}
1143142e2398SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1144142e2398SEgbert Eich 			DRM_DEBUG_KMS("Connector %s (pin %i) received hotplug event.\n",
1145142e2398SEgbert Eich 				      drm_get_connector_name(connector), intel_encoder->hpd_pin);
1146142e2398SEgbert Eich 		}
1147cd569aedSEgbert Eich 	}
1148cd569aedSEgbert Eich 	 /* if there were no outputs to poll, poll was disabled,
1149cd569aedSEgbert Eich 	  * therefore make sure it's enabled when disabling HPD on
1150cd569aedSEgbert Eich 	  * some connectors */
1151ac4c16c5SEgbert Eich 	if (hpd_disabled) {
1152cd569aedSEgbert Eich 		drm_kms_helper_poll_enable(dev);
1153ac4c16c5SEgbert Eich 		mod_timer(&dev_priv->hotplug_reenable_timer,
1154ac4c16c5SEgbert Eich 			  jiffies + msecs_to_jiffies(I915_REENABLE_HOTPLUG_DELAY));
1155ac4c16c5SEgbert Eich 	}
1156cd569aedSEgbert Eich 
1157cd569aedSEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
1158cd569aedSEgbert Eich 
1159321a1b30SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
1160321a1b30SEgbert Eich 		intel_connector = to_intel_connector(connector);
1161321a1b30SEgbert Eich 		intel_encoder = intel_connector->encoder;
1162321a1b30SEgbert Eich 		if (hpd_event_bits & (1 << intel_encoder->hpd_pin)) {
1163cd569aedSEgbert Eich 			if (intel_encoder->hot_plug)
1164cd569aedSEgbert Eich 				intel_encoder->hot_plug(intel_encoder);
1165321a1b30SEgbert Eich 			if (intel_hpd_irq_event(dev, connector))
1166321a1b30SEgbert Eich 				changed = true;
1167321a1b30SEgbert Eich 		}
1168321a1b30SEgbert Eich 	}
116940ee3381SKeith Packard 	mutex_unlock(&mode_config->mutex);
117040ee3381SKeith Packard 
1171321a1b30SEgbert Eich 	if (changed)
1172321a1b30SEgbert Eich 		drm_kms_helper_hotplug_event(dev);
11735ca58282SJesse Barnes }
11745ca58282SJesse Barnes 
11753ca1ccedSVille Syrjälä static void intel_hpd_irq_uninstall(struct drm_i915_private *dev_priv)
11763ca1ccedSVille Syrjälä {
11773ca1ccedSVille Syrjälä 	del_timer_sync(&dev_priv->hotplug_reenable_timer);
11783ca1ccedSVille Syrjälä }
11793ca1ccedSVille Syrjälä 
1180d0ecd7e2SDaniel Vetter static void ironlake_rps_change_irq_handler(struct drm_device *dev)
1181f97108d1SJesse Barnes {
11822d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1183b5b72e89SMatthew Garrett 	u32 busy_up, busy_down, max_avg, min_avg;
11849270388eSDaniel Vetter 	u8 new_delay;
11859270388eSDaniel Vetter 
1186d0ecd7e2SDaniel Vetter 	spin_lock(&mchdev_lock);
1187f97108d1SJesse Barnes 
118873edd18fSDaniel Vetter 	I915_WRITE16(MEMINTRSTS, I915_READ(MEMINTRSTS));
118973edd18fSDaniel Vetter 
119020e4d407SDaniel Vetter 	new_delay = dev_priv->ips.cur_delay;
11919270388eSDaniel Vetter 
11927648fa99SJesse Barnes 	I915_WRITE16(MEMINTRSTS, MEMINT_EVAL_CHG);
1193b5b72e89SMatthew Garrett 	busy_up = I915_READ(RCPREVBSYTUPAVG);
1194b5b72e89SMatthew Garrett 	busy_down = I915_READ(RCPREVBSYTDNAVG);
1195f97108d1SJesse Barnes 	max_avg = I915_READ(RCBMAXAVG);
1196f97108d1SJesse Barnes 	min_avg = I915_READ(RCBMINAVG);
1197f97108d1SJesse Barnes 
1198f97108d1SJesse Barnes 	/* Handle RCS change request from hw */
1199b5b72e89SMatthew Garrett 	if (busy_up > max_avg) {
120020e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.max_delay)
120120e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay - 1;
120220e4d407SDaniel Vetter 		if (new_delay < dev_priv->ips.max_delay)
120320e4d407SDaniel Vetter 			new_delay = dev_priv->ips.max_delay;
1204b5b72e89SMatthew Garrett 	} else if (busy_down < min_avg) {
120520e4d407SDaniel Vetter 		if (dev_priv->ips.cur_delay != dev_priv->ips.min_delay)
120620e4d407SDaniel Vetter 			new_delay = dev_priv->ips.cur_delay + 1;
120720e4d407SDaniel Vetter 		if (new_delay > dev_priv->ips.min_delay)
120820e4d407SDaniel Vetter 			new_delay = dev_priv->ips.min_delay;
1209f97108d1SJesse Barnes 	}
1210f97108d1SJesse Barnes 
12117648fa99SJesse Barnes 	if (ironlake_set_drps(dev, new_delay))
121220e4d407SDaniel Vetter 		dev_priv->ips.cur_delay = new_delay;
1213f97108d1SJesse Barnes 
1214d0ecd7e2SDaniel Vetter 	spin_unlock(&mchdev_lock);
12159270388eSDaniel Vetter 
1216f97108d1SJesse Barnes 	return;
1217f97108d1SJesse Barnes }
1218f97108d1SJesse Barnes 
1219549f7365SChris Wilson static void notify_ring(struct drm_device *dev,
1220*a4872ba6SOscar Mateo 			struct intel_engine_cs *ring)
1221549f7365SChris Wilson {
1222475553deSChris Wilson 	if (ring->obj == NULL)
1223475553deSChris Wilson 		return;
1224475553deSChris Wilson 
1225814e9b57SChris Wilson 	trace_i915_gem_request_complete(ring);
12269862e600SChris Wilson 
1227549f7365SChris Wilson 	wake_up_all(&ring->irq_queue);
122810cd45b6SMika Kuoppala 	i915_queue_hangcheck(dev);
1229549f7365SChris Wilson }
1230549f7365SChris Wilson 
12314912d041SBen Widawsky static void gen6_pm_rps_work(struct work_struct *work)
12323b8d8d91SJesse Barnes {
12332d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
12342d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, rps.work);
1235edbfdb45SPaulo Zanoni 	u32 pm_iir;
1236dd75fdc8SChris Wilson 	int new_delay, adj;
12373b8d8d91SJesse Barnes 
123859cdb63dSDaniel Vetter 	spin_lock_irq(&dev_priv->irq_lock);
1239c6a828d3SDaniel Vetter 	pm_iir = dev_priv->rps.pm_iir;
1240c6a828d3SDaniel Vetter 	dev_priv->rps.pm_iir = 0;
12410961021aSBen Widawsky 	if (IS_BROADWELL(dev_priv->dev))
12420961021aSBen Widawsky 		bdw_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
12430961021aSBen Widawsky 	else {
12440961021aSBen Widawsky 		/* Make sure not to corrupt PMIMR state used by ringbuffer */
1245a6706b45SDeepak S 		snb_enable_pm_irq(dev_priv, dev_priv->pm_rps_events);
12460961021aSBen Widawsky 	}
124759cdb63dSDaniel Vetter 	spin_unlock_irq(&dev_priv->irq_lock);
12484912d041SBen Widawsky 
124960611c13SPaulo Zanoni 	/* Make sure we didn't queue anything we're not going to process. */
1250a6706b45SDeepak S 	WARN_ON(pm_iir & ~dev_priv->pm_rps_events);
125160611c13SPaulo Zanoni 
1252a6706b45SDeepak S 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
12533b8d8d91SJesse Barnes 		return;
12543b8d8d91SJesse Barnes 
12554fc688ceSJesse Barnes 	mutex_lock(&dev_priv->rps.hw_lock);
12567b9e0ae6SChris Wilson 
1257dd75fdc8SChris Wilson 	adj = dev_priv->rps.last_adj;
12587425034aSVille Syrjälä 	if (pm_iir & GEN6_PM_RP_UP_THRESHOLD) {
1259dd75fdc8SChris Wilson 		if (adj > 0)
1260dd75fdc8SChris Wilson 			adj *= 2;
1261dd75fdc8SChris Wilson 		else
1262dd75fdc8SChris Wilson 			adj = 1;
1263b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
12647425034aSVille Syrjälä 
12657425034aSVille Syrjälä 		/*
12667425034aSVille Syrjälä 		 * For better performance, jump directly
12677425034aSVille Syrjälä 		 * to RPe if we're below it.
12687425034aSVille Syrjälä 		 */
1269b39fb297SBen Widawsky 		if (new_delay < dev_priv->rps.efficient_freq)
1270b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1271dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_TIMEOUT) {
1272b39fb297SBen Widawsky 		if (dev_priv->rps.cur_freq > dev_priv->rps.efficient_freq)
1273b39fb297SBen Widawsky 			new_delay = dev_priv->rps.efficient_freq;
1274dd75fdc8SChris Wilson 		else
1275b39fb297SBen Widawsky 			new_delay = dev_priv->rps.min_freq_softlimit;
1276dd75fdc8SChris Wilson 		adj = 0;
1277dd75fdc8SChris Wilson 	} else if (pm_iir & GEN6_PM_RP_DOWN_THRESHOLD) {
1278dd75fdc8SChris Wilson 		if (adj < 0)
1279dd75fdc8SChris Wilson 			adj *= 2;
1280dd75fdc8SChris Wilson 		else
1281dd75fdc8SChris Wilson 			adj = -1;
1282b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq + adj;
1283dd75fdc8SChris Wilson 	} else { /* unknown event */
1284b39fb297SBen Widawsky 		new_delay = dev_priv->rps.cur_freq;
1285dd75fdc8SChris Wilson 	}
12863b8d8d91SJesse Barnes 
128779249636SBen Widawsky 	/* sysfs frequency interfaces may have snuck in while servicing the
128879249636SBen Widawsky 	 * interrupt
128979249636SBen Widawsky 	 */
12901272e7b8SVille Syrjälä 	new_delay = clamp_t(int, new_delay,
1291b39fb297SBen Widawsky 			    dev_priv->rps.min_freq_softlimit,
1292b39fb297SBen Widawsky 			    dev_priv->rps.max_freq_softlimit);
129327544369SDeepak S 
1294b39fb297SBen Widawsky 	dev_priv->rps.last_adj = new_delay - dev_priv->rps.cur_freq;
1295dd75fdc8SChris Wilson 
12960a073b84SJesse Barnes 	if (IS_VALLEYVIEW(dev_priv->dev))
12970a073b84SJesse Barnes 		valleyview_set_rps(dev_priv->dev, new_delay);
12980a073b84SJesse Barnes 	else
12994912d041SBen Widawsky 		gen6_set_rps(dev_priv->dev, new_delay);
13003b8d8d91SJesse Barnes 
13014fc688ceSJesse Barnes 	mutex_unlock(&dev_priv->rps.hw_lock);
13023b8d8d91SJesse Barnes }
13033b8d8d91SJesse Barnes 
1304e3689190SBen Widawsky 
1305e3689190SBen Widawsky /**
1306e3689190SBen Widawsky  * ivybridge_parity_work - Workqueue called when a parity error interrupt
1307e3689190SBen Widawsky  * occurred.
1308e3689190SBen Widawsky  * @work: workqueue struct
1309e3689190SBen Widawsky  *
1310e3689190SBen Widawsky  * Doesn't actually do anything except notify userspace. As a consequence of
1311e3689190SBen Widawsky  * this event, userspace should try to remap the bad rows since statistically
1312e3689190SBen Widawsky  * it is likely the same row is more likely to go bad again.
1313e3689190SBen Widawsky  */
1314e3689190SBen Widawsky static void ivybridge_parity_work(struct work_struct *work)
1315e3689190SBen Widawsky {
13162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
13172d1013ddSJani Nikula 		container_of(work, struct drm_i915_private, l3_parity.error_work);
1318e3689190SBen Widawsky 	u32 error_status, row, bank, subbank;
131935a85ac6SBen Widawsky 	char *parity_event[6];
1320e3689190SBen Widawsky 	uint32_t misccpctl;
1321e3689190SBen Widawsky 	unsigned long flags;
132235a85ac6SBen Widawsky 	uint8_t slice = 0;
1323e3689190SBen Widawsky 
1324e3689190SBen Widawsky 	/* We must turn off DOP level clock gating to access the L3 registers.
1325e3689190SBen Widawsky 	 * In order to prevent a get/put style interface, acquire struct mutex
1326e3689190SBen Widawsky 	 * any time we access those registers.
1327e3689190SBen Widawsky 	 */
1328e3689190SBen Widawsky 	mutex_lock(&dev_priv->dev->struct_mutex);
1329e3689190SBen Widawsky 
133035a85ac6SBen Widawsky 	/* If we've screwed up tracking, just let the interrupt fire again */
133135a85ac6SBen Widawsky 	if (WARN_ON(!dev_priv->l3_parity.which_slice))
133235a85ac6SBen Widawsky 		goto out;
133335a85ac6SBen Widawsky 
1334e3689190SBen Widawsky 	misccpctl = I915_READ(GEN7_MISCCPCTL);
1335e3689190SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
1336e3689190SBen Widawsky 	POSTING_READ(GEN7_MISCCPCTL);
1337e3689190SBen Widawsky 
133835a85ac6SBen Widawsky 	while ((slice = ffs(dev_priv->l3_parity.which_slice)) != 0) {
133935a85ac6SBen Widawsky 		u32 reg;
134035a85ac6SBen Widawsky 
134135a85ac6SBen Widawsky 		slice--;
134235a85ac6SBen Widawsky 		if (WARN_ON_ONCE(slice >= NUM_L3_SLICES(dev_priv->dev)))
134335a85ac6SBen Widawsky 			break;
134435a85ac6SBen Widawsky 
134535a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice &= ~(1<<slice);
134635a85ac6SBen Widawsky 
134735a85ac6SBen Widawsky 		reg = GEN7_L3CDERRST1 + (slice * 0x200);
134835a85ac6SBen Widawsky 
134935a85ac6SBen Widawsky 		error_status = I915_READ(reg);
1350e3689190SBen Widawsky 		row = GEN7_PARITY_ERROR_ROW(error_status);
1351e3689190SBen Widawsky 		bank = GEN7_PARITY_ERROR_BANK(error_status);
1352e3689190SBen Widawsky 		subbank = GEN7_PARITY_ERROR_SUBBANK(error_status);
1353e3689190SBen Widawsky 
135435a85ac6SBen Widawsky 		I915_WRITE(reg, GEN7_PARITY_ERROR_VALID | GEN7_L3CDERRST1_ENABLE);
135535a85ac6SBen Widawsky 		POSTING_READ(reg);
1356e3689190SBen Widawsky 
1357cce723edSBen Widawsky 		parity_event[0] = I915_L3_PARITY_UEVENT "=1";
1358e3689190SBen Widawsky 		parity_event[1] = kasprintf(GFP_KERNEL, "ROW=%d", row);
1359e3689190SBen Widawsky 		parity_event[2] = kasprintf(GFP_KERNEL, "BANK=%d", bank);
1360e3689190SBen Widawsky 		parity_event[3] = kasprintf(GFP_KERNEL, "SUBBANK=%d", subbank);
136135a85ac6SBen Widawsky 		parity_event[4] = kasprintf(GFP_KERNEL, "SLICE=%d", slice);
136235a85ac6SBen Widawsky 		parity_event[5] = NULL;
1363e3689190SBen Widawsky 
13645bdebb18SDave Airlie 		kobject_uevent_env(&dev_priv->dev->primary->kdev->kobj,
1365e3689190SBen Widawsky 				   KOBJ_CHANGE, parity_event);
1366e3689190SBen Widawsky 
136735a85ac6SBen Widawsky 		DRM_DEBUG("Parity error: Slice = %d, Row = %d, Bank = %d, Sub bank = %d.\n",
136835a85ac6SBen Widawsky 			  slice, row, bank, subbank);
1369e3689190SBen Widawsky 
137035a85ac6SBen Widawsky 		kfree(parity_event[4]);
1371e3689190SBen Widawsky 		kfree(parity_event[3]);
1372e3689190SBen Widawsky 		kfree(parity_event[2]);
1373e3689190SBen Widawsky 		kfree(parity_event[1]);
1374e3689190SBen Widawsky 	}
1375e3689190SBen Widawsky 
137635a85ac6SBen Widawsky 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
137735a85ac6SBen Widawsky 
137835a85ac6SBen Widawsky out:
137935a85ac6SBen Widawsky 	WARN_ON(dev_priv->l3_parity.which_slice);
138035a85ac6SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
138135a85ac6SBen Widawsky 	ilk_enable_gt_irq(dev_priv, GT_PARITY_ERROR(dev_priv->dev));
138235a85ac6SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
138335a85ac6SBen Widawsky 
138435a85ac6SBen Widawsky 	mutex_unlock(&dev_priv->dev->struct_mutex);
138535a85ac6SBen Widawsky }
138635a85ac6SBen Widawsky 
138735a85ac6SBen Widawsky static void ivybridge_parity_error_irq_handler(struct drm_device *dev, u32 iir)
1388e3689190SBen Widawsky {
13892d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1390e3689190SBen Widawsky 
1391040d2baaSBen Widawsky 	if (!HAS_L3_DPF(dev))
1392e3689190SBen Widawsky 		return;
1393e3689190SBen Widawsky 
1394d0ecd7e2SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
139535a85ac6SBen Widawsky 	ilk_disable_gt_irq(dev_priv, GT_PARITY_ERROR(dev));
1396d0ecd7e2SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
1397e3689190SBen Widawsky 
139835a85ac6SBen Widawsky 	iir &= GT_PARITY_ERROR(dev);
139935a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT_S1)
140035a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 1;
140135a85ac6SBen Widawsky 
140235a85ac6SBen Widawsky 	if (iir & GT_RENDER_L3_PARITY_ERROR_INTERRUPT)
140335a85ac6SBen Widawsky 		dev_priv->l3_parity.which_slice |= 1 << 0;
140435a85ac6SBen Widawsky 
1405a4da4fa4SDaniel Vetter 	queue_work(dev_priv->wq, &dev_priv->l3_parity.error_work);
1406e3689190SBen Widawsky }
1407e3689190SBen Widawsky 
1408f1af8fc1SPaulo Zanoni static void ilk_gt_irq_handler(struct drm_device *dev,
1409f1af8fc1SPaulo Zanoni 			       struct drm_i915_private *dev_priv,
1410f1af8fc1SPaulo Zanoni 			       u32 gt_iir)
1411f1af8fc1SPaulo Zanoni {
1412f1af8fc1SPaulo Zanoni 	if (gt_iir &
1413f1af8fc1SPaulo Zanoni 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1414f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[RCS]);
1415f1af8fc1SPaulo Zanoni 	if (gt_iir & ILK_BSD_USER_INTERRUPT)
1416f1af8fc1SPaulo Zanoni 		notify_ring(dev, &dev_priv->ring[VCS]);
1417f1af8fc1SPaulo Zanoni }
1418f1af8fc1SPaulo Zanoni 
1419e7b4c6b1SDaniel Vetter static void snb_gt_irq_handler(struct drm_device *dev,
1420e7b4c6b1SDaniel Vetter 			       struct drm_i915_private *dev_priv,
1421e7b4c6b1SDaniel Vetter 			       u32 gt_iir)
1422e7b4c6b1SDaniel Vetter {
1423e7b4c6b1SDaniel Vetter 
1424cc609d5dSBen Widawsky 	if (gt_iir &
1425cc609d5dSBen Widawsky 	    (GT_RENDER_USER_INTERRUPT | GT_RENDER_PIPECTL_NOTIFY_INTERRUPT))
1426e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[RCS]);
1427cc609d5dSBen Widawsky 	if (gt_iir & GT_BSD_USER_INTERRUPT)
1428e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[VCS]);
1429cc609d5dSBen Widawsky 	if (gt_iir & GT_BLT_USER_INTERRUPT)
1430e7b4c6b1SDaniel Vetter 		notify_ring(dev, &dev_priv->ring[BCS]);
1431e7b4c6b1SDaniel Vetter 
1432cc609d5dSBen Widawsky 	if (gt_iir & (GT_BLT_CS_ERROR_INTERRUPT |
1433cc609d5dSBen Widawsky 		      GT_BSD_CS_ERROR_INTERRUPT |
1434cc609d5dSBen Widawsky 		      GT_RENDER_CS_MASTER_ERROR_INTERRUPT)) {
143558174462SMika Kuoppala 		i915_handle_error(dev, false, "GT error interrupt 0x%08x",
143658174462SMika Kuoppala 				  gt_iir);
1437e7b4c6b1SDaniel Vetter 	}
1438e3689190SBen Widawsky 
143935a85ac6SBen Widawsky 	if (gt_iir & GT_PARITY_ERROR(dev))
144035a85ac6SBen Widawsky 		ivybridge_parity_error_irq_handler(dev, gt_iir);
1441e7b4c6b1SDaniel Vetter }
1442e7b4c6b1SDaniel Vetter 
14430961021aSBen Widawsky static void gen8_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
14440961021aSBen Widawsky {
14450961021aSBen Widawsky 	if ((pm_iir & dev_priv->pm_rps_events) == 0)
14460961021aSBen Widawsky 		return;
14470961021aSBen Widawsky 
14480961021aSBen Widawsky 	spin_lock(&dev_priv->irq_lock);
14490961021aSBen Widawsky 	dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
14500961021aSBen Widawsky 	bdw_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
14510961021aSBen Widawsky 	spin_unlock(&dev_priv->irq_lock);
14520961021aSBen Widawsky 
14530961021aSBen Widawsky 	queue_work(dev_priv->wq, &dev_priv->rps.work);
14540961021aSBen Widawsky }
14550961021aSBen Widawsky 
1456abd58f01SBen Widawsky static irqreturn_t gen8_gt_irq_handler(struct drm_device *dev,
1457abd58f01SBen Widawsky 				       struct drm_i915_private *dev_priv,
1458abd58f01SBen Widawsky 				       u32 master_ctl)
1459abd58f01SBen Widawsky {
1460abd58f01SBen Widawsky 	u32 rcs, bcs, vcs;
1461abd58f01SBen Widawsky 	uint32_t tmp = 0;
1462abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
1463abd58f01SBen Widawsky 
1464abd58f01SBen Widawsky 	if (master_ctl & (GEN8_GT_RCS_IRQ | GEN8_GT_BCS_IRQ)) {
1465abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(0));
1466abd58f01SBen Widawsky 		if (tmp) {
1467abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1468abd58f01SBen Widawsky 			rcs = tmp >> GEN8_RCS_IRQ_SHIFT;
1469abd58f01SBen Widawsky 			bcs = tmp >> GEN8_BCS_IRQ_SHIFT;
1470abd58f01SBen Widawsky 			if (rcs & GT_RENDER_USER_INTERRUPT)
1471abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[RCS]);
1472abd58f01SBen Widawsky 			if (bcs & GT_RENDER_USER_INTERRUPT)
1473abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[BCS]);
1474abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(0), tmp);
1475abd58f01SBen Widawsky 		} else
1476abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT0)!\n");
1477abd58f01SBen Widawsky 	}
1478abd58f01SBen Widawsky 
147985f9b5f9SZhao Yakui 	if (master_ctl & (GEN8_GT_VCS1_IRQ | GEN8_GT_VCS2_IRQ)) {
1480abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(1));
1481abd58f01SBen Widawsky 		if (tmp) {
1482abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1483abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VCS1_IRQ_SHIFT;
1484abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1485abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VCS]);
148685f9b5f9SZhao Yakui 			vcs = tmp >> GEN8_VCS2_IRQ_SHIFT;
148785f9b5f9SZhao Yakui 			if (vcs & GT_RENDER_USER_INTERRUPT)
148885f9b5f9SZhao Yakui 				notify_ring(dev, &dev_priv->ring[VCS2]);
1489abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(1), tmp);
1490abd58f01SBen Widawsky 		} else
1491abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT1)!\n");
1492abd58f01SBen Widawsky 	}
1493abd58f01SBen Widawsky 
14940961021aSBen Widawsky 	if (master_ctl & GEN8_GT_PM_IRQ) {
14950961021aSBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(2));
14960961021aSBen Widawsky 		if (tmp & dev_priv->pm_rps_events) {
14970961021aSBen Widawsky 			ret = IRQ_HANDLED;
14980961021aSBen Widawsky 			gen8_rps_irq_handler(dev_priv, tmp);
14990961021aSBen Widawsky 			I915_WRITE(GEN8_GT_IIR(2),
15000961021aSBen Widawsky 				   tmp & dev_priv->pm_rps_events);
15010961021aSBen Widawsky 		} else
15020961021aSBen Widawsky 			DRM_ERROR("The master control interrupt lied (PM)!\n");
15030961021aSBen Widawsky 	}
15040961021aSBen Widawsky 
1505abd58f01SBen Widawsky 	if (master_ctl & GEN8_GT_VECS_IRQ) {
1506abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_GT_IIR(3));
1507abd58f01SBen Widawsky 		if (tmp) {
1508abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
1509abd58f01SBen Widawsky 			vcs = tmp >> GEN8_VECS_IRQ_SHIFT;
1510abd58f01SBen Widawsky 			if (vcs & GT_RENDER_USER_INTERRUPT)
1511abd58f01SBen Widawsky 				notify_ring(dev, &dev_priv->ring[VECS]);
1512abd58f01SBen Widawsky 			I915_WRITE(GEN8_GT_IIR(3), tmp);
1513abd58f01SBen Widawsky 		} else
1514abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (GT3)!\n");
1515abd58f01SBen Widawsky 	}
1516abd58f01SBen Widawsky 
1517abd58f01SBen Widawsky 	return ret;
1518abd58f01SBen Widawsky }
1519abd58f01SBen Widawsky 
1520b543fb04SEgbert Eich #define HPD_STORM_DETECT_PERIOD 1000
1521b543fb04SEgbert Eich #define HPD_STORM_THRESHOLD 5
1522b543fb04SEgbert Eich 
152310a504deSDaniel Vetter static inline void intel_hpd_irq_handler(struct drm_device *dev,
1524b543fb04SEgbert Eich 					 u32 hotplug_trigger,
1525b543fb04SEgbert Eich 					 const u32 *hpd)
1526b543fb04SEgbert Eich {
15272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1528b543fb04SEgbert Eich 	int i;
152910a504deSDaniel Vetter 	bool storm_detected = false;
1530b543fb04SEgbert Eich 
153191d131d2SDaniel Vetter 	if (!hotplug_trigger)
153291d131d2SDaniel Vetter 		return;
153391d131d2SDaniel Vetter 
1534cc9bd499SImre Deak 	DRM_DEBUG_DRIVER("hotplug event received, stat 0x%08x\n",
1535cc9bd499SImre Deak 			  hotplug_trigger);
1536cc9bd499SImre Deak 
1537b5ea2d56SDaniel Vetter 	spin_lock(&dev_priv->irq_lock);
1538b543fb04SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
1539821450c6SEgbert Eich 
15403ff04a16SDaniel Vetter 		if (hpd[i] & hotplug_trigger &&
15413ff04a16SDaniel Vetter 		    dev_priv->hpd_stats[i].hpd_mark == HPD_DISABLED) {
15423ff04a16SDaniel Vetter 			/*
15433ff04a16SDaniel Vetter 			 * On GMCH platforms the interrupt mask bits only
15443ff04a16SDaniel Vetter 			 * prevent irq generation, not the setting of the
15453ff04a16SDaniel Vetter 			 * hotplug bits itself. So only WARN about unexpected
15463ff04a16SDaniel Vetter 			 * interrupts on saner platforms.
15473ff04a16SDaniel Vetter 			 */
15483ff04a16SDaniel Vetter 			WARN_ONCE(INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev),
1549cba1c073SChris Wilson 				  "Received HPD interrupt (0x%08x) on pin %d (0x%08x) although disabled\n",
1550cba1c073SChris Wilson 				  hotplug_trigger, i, hpd[i]);
1551b8f102e8SEgbert Eich 
15523ff04a16SDaniel Vetter 			continue;
15533ff04a16SDaniel Vetter 		}
15543ff04a16SDaniel Vetter 
1555b543fb04SEgbert Eich 		if (!(hpd[i] & hotplug_trigger) ||
1556b543fb04SEgbert Eich 		    dev_priv->hpd_stats[i].hpd_mark != HPD_ENABLED)
1557b543fb04SEgbert Eich 			continue;
1558b543fb04SEgbert Eich 
1559bc5ead8cSJani Nikula 		dev_priv->hpd_event_bits |= (1 << i);
1560b543fb04SEgbert Eich 		if (!time_in_range(jiffies, dev_priv->hpd_stats[i].hpd_last_jiffies,
1561b543fb04SEgbert Eich 				   dev_priv->hpd_stats[i].hpd_last_jiffies
1562b543fb04SEgbert Eich 				   + msecs_to_jiffies(HPD_STORM_DETECT_PERIOD))) {
1563b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_last_jiffies = jiffies;
1564b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt = 0;
1565b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: 0\n", i);
1566b543fb04SEgbert Eich 		} else if (dev_priv->hpd_stats[i].hpd_cnt > HPD_STORM_THRESHOLD) {
1567b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_mark = HPD_MARK_DISABLED;
1568142e2398SEgbert Eich 			dev_priv->hpd_event_bits &= ~(1 << i);
1569b543fb04SEgbert Eich 			DRM_DEBUG_KMS("HPD interrupt storm detected on PIN %d\n", i);
157010a504deSDaniel Vetter 			storm_detected = true;
1571b543fb04SEgbert Eich 		} else {
1572b543fb04SEgbert Eich 			dev_priv->hpd_stats[i].hpd_cnt++;
1573b8f102e8SEgbert Eich 			DRM_DEBUG_KMS("Received HPD interrupt on PIN %d - cnt: %d\n", i,
1574b8f102e8SEgbert Eich 				      dev_priv->hpd_stats[i].hpd_cnt);
1575b543fb04SEgbert Eich 		}
1576b543fb04SEgbert Eich 	}
1577b543fb04SEgbert Eich 
157810a504deSDaniel Vetter 	if (storm_detected)
157910a504deSDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
1580b5ea2d56SDaniel Vetter 	spin_unlock(&dev_priv->irq_lock);
15815876fa0dSDaniel Vetter 
1582645416f5SDaniel Vetter 	/*
1583645416f5SDaniel Vetter 	 * Our hotplug handler can grab modeset locks (by calling down into the
1584645416f5SDaniel Vetter 	 * fb helpers). Hence it must not be run on our own dev-priv->wq work
1585645416f5SDaniel Vetter 	 * queue for otherwise the flush_work in the pageflip code will
1586645416f5SDaniel Vetter 	 * deadlock.
1587645416f5SDaniel Vetter 	 */
1588645416f5SDaniel Vetter 	schedule_work(&dev_priv->hotplug_work);
1589b543fb04SEgbert Eich }
1590b543fb04SEgbert Eich 
1591515ac2bbSDaniel Vetter static void gmbus_irq_handler(struct drm_device *dev)
1592515ac2bbSDaniel Vetter {
15932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
159428c70f16SDaniel Vetter 
159528c70f16SDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1596515ac2bbSDaniel Vetter }
1597515ac2bbSDaniel Vetter 
1598ce99c256SDaniel Vetter static void dp_aux_irq_handler(struct drm_device *dev)
1599ce99c256SDaniel Vetter {
16002d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
16019ee32feaSDaniel Vetter 
16029ee32feaSDaniel Vetter 	wake_up_all(&dev_priv->gmbus_wait_queue);
1603ce99c256SDaniel Vetter }
1604ce99c256SDaniel Vetter 
16058bf1e9f1SShuang He #if defined(CONFIG_DEBUG_FS)
1606277de95eSDaniel Vetter static void display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1607eba94eb9SDaniel Vetter 					 uint32_t crc0, uint32_t crc1,
1608eba94eb9SDaniel Vetter 					 uint32_t crc2, uint32_t crc3,
16098bc5e955SDaniel Vetter 					 uint32_t crc4)
16108bf1e9f1SShuang He {
16118bf1e9f1SShuang He 	struct drm_i915_private *dev_priv = dev->dev_private;
16128bf1e9f1SShuang He 	struct intel_pipe_crc *pipe_crc = &dev_priv->pipe_crc[pipe];
16138bf1e9f1SShuang He 	struct intel_pipe_crc_entry *entry;
1614ac2300d4SDamien Lespiau 	int head, tail;
1615b2c88f5bSDamien Lespiau 
1616d538bbdfSDamien Lespiau 	spin_lock(&pipe_crc->lock);
1617d538bbdfSDamien Lespiau 
16180c912c79SDamien Lespiau 	if (!pipe_crc->entries) {
1619d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
16200c912c79SDamien Lespiau 		DRM_ERROR("spurious interrupt\n");
16210c912c79SDamien Lespiau 		return;
16220c912c79SDamien Lespiau 	}
16230c912c79SDamien Lespiau 
1624d538bbdfSDamien Lespiau 	head = pipe_crc->head;
1625d538bbdfSDamien Lespiau 	tail = pipe_crc->tail;
1626b2c88f5bSDamien Lespiau 
1627b2c88f5bSDamien Lespiau 	if (CIRC_SPACE(head, tail, INTEL_PIPE_CRC_ENTRIES_NR) < 1) {
1628d538bbdfSDamien Lespiau 		spin_unlock(&pipe_crc->lock);
1629b2c88f5bSDamien Lespiau 		DRM_ERROR("CRC buffer overflowing\n");
1630b2c88f5bSDamien Lespiau 		return;
1631b2c88f5bSDamien Lespiau 	}
1632b2c88f5bSDamien Lespiau 
1633b2c88f5bSDamien Lespiau 	entry = &pipe_crc->entries[head];
16348bf1e9f1SShuang He 
16358bc5e955SDaniel Vetter 	entry->frame = dev->driver->get_vblank_counter(dev, pipe);
1636eba94eb9SDaniel Vetter 	entry->crc[0] = crc0;
1637eba94eb9SDaniel Vetter 	entry->crc[1] = crc1;
1638eba94eb9SDaniel Vetter 	entry->crc[2] = crc2;
1639eba94eb9SDaniel Vetter 	entry->crc[3] = crc3;
1640eba94eb9SDaniel Vetter 	entry->crc[4] = crc4;
1641b2c88f5bSDamien Lespiau 
1642b2c88f5bSDamien Lespiau 	head = (head + 1) & (INTEL_PIPE_CRC_ENTRIES_NR - 1);
1643d538bbdfSDamien Lespiau 	pipe_crc->head = head;
1644d538bbdfSDamien Lespiau 
1645d538bbdfSDamien Lespiau 	spin_unlock(&pipe_crc->lock);
164607144428SDamien Lespiau 
164707144428SDamien Lespiau 	wake_up_interruptible(&pipe_crc->wq);
16488bf1e9f1SShuang He }
1649277de95eSDaniel Vetter #else
1650277de95eSDaniel Vetter static inline void
1651277de95eSDaniel Vetter display_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe,
1652277de95eSDaniel Vetter 			     uint32_t crc0, uint32_t crc1,
1653277de95eSDaniel Vetter 			     uint32_t crc2, uint32_t crc3,
1654277de95eSDaniel Vetter 			     uint32_t crc4) {}
1655277de95eSDaniel Vetter #endif
1656eba94eb9SDaniel Vetter 
1657277de95eSDaniel Vetter 
1658277de95eSDaniel Vetter static void hsw_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16595a69b89fSDaniel Vetter {
16605a69b89fSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16615a69b89fSDaniel Vetter 
1662277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16635a69b89fSDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
16645a69b89fSDaniel Vetter 				     0, 0, 0, 0);
16655a69b89fSDaniel Vetter }
16665a69b89fSDaniel Vetter 
1667277de95eSDaniel Vetter static void ivb_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
1668eba94eb9SDaniel Vetter {
1669eba94eb9SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
1670eba94eb9SDaniel Vetter 
1671277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
1672eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_1_IVB(pipe)),
1673eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_2_IVB(pipe)),
1674eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_3_IVB(pipe)),
1675eba94eb9SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_4_IVB(pipe)),
16768bc5e955SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_5_IVB(pipe)));
1677eba94eb9SDaniel Vetter }
16785b3a856bSDaniel Vetter 
1679277de95eSDaniel Vetter static void i9xx_pipe_crc_irq_handler(struct drm_device *dev, enum pipe pipe)
16805b3a856bSDaniel Vetter {
16815b3a856bSDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
16820b5c5ed0SDaniel Vetter 	uint32_t res1, res2;
16830b5c5ed0SDaniel Vetter 
16840b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 3)
16850b5c5ed0SDaniel Vetter 		res1 = I915_READ(PIPE_CRC_RES_RES1_I915(pipe));
16860b5c5ed0SDaniel Vetter 	else
16870b5c5ed0SDaniel Vetter 		res1 = 0;
16880b5c5ed0SDaniel Vetter 
16890b5c5ed0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
16900b5c5ed0SDaniel Vetter 		res2 = I915_READ(PIPE_CRC_RES_RES2_G4X(pipe));
16910b5c5ed0SDaniel Vetter 	else
16920b5c5ed0SDaniel Vetter 		res2 = 0;
16935b3a856bSDaniel Vetter 
1694277de95eSDaniel Vetter 	display_pipe_crc_irq_handler(dev, pipe,
16950b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_RED(pipe)),
16960b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_GREEN(pipe)),
16970b5c5ed0SDaniel Vetter 				     I915_READ(PIPE_CRC_RES_BLUE(pipe)),
16980b5c5ed0SDaniel Vetter 				     res1, res2);
16995b3a856bSDaniel Vetter }
17008bf1e9f1SShuang He 
17011403c0d4SPaulo Zanoni /* The RPS events need forcewake, so we add them to a work queue and mask their
17021403c0d4SPaulo Zanoni  * IMR bits until the work is done. Other interrupts can be processed without
17031403c0d4SPaulo Zanoni  * the work queue. */
17041403c0d4SPaulo Zanoni static void gen6_rps_irq_handler(struct drm_i915_private *dev_priv, u32 pm_iir)
1705baf02a1fSBen Widawsky {
1706a6706b45SDeepak S 	if (pm_iir & dev_priv->pm_rps_events) {
170759cdb63dSDaniel Vetter 		spin_lock(&dev_priv->irq_lock);
1708a6706b45SDeepak S 		dev_priv->rps.pm_iir |= pm_iir & dev_priv->pm_rps_events;
1709a6706b45SDeepak S 		snb_disable_pm_irq(dev_priv, pm_iir & dev_priv->pm_rps_events);
171059cdb63dSDaniel Vetter 		spin_unlock(&dev_priv->irq_lock);
17112adbee62SDaniel Vetter 
17122adbee62SDaniel Vetter 		queue_work(dev_priv->wq, &dev_priv->rps.work);
171341a05a3aSDaniel Vetter 	}
1714baf02a1fSBen Widawsky 
17151403c0d4SPaulo Zanoni 	if (HAS_VEBOX(dev_priv->dev)) {
171612638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_USER_INTERRUPT)
171712638c57SBen Widawsky 			notify_ring(dev_priv->dev, &dev_priv->ring[VECS]);
171812638c57SBen Widawsky 
171912638c57SBen Widawsky 		if (pm_iir & PM_VEBOX_CS_ERROR_INTERRUPT) {
172058174462SMika Kuoppala 			i915_handle_error(dev_priv->dev, false,
172158174462SMika Kuoppala 					  "VEBOX CS error interrupt 0x%08x",
172258174462SMika Kuoppala 					  pm_iir);
172312638c57SBen Widawsky 		}
172412638c57SBen Widawsky 	}
17251403c0d4SPaulo Zanoni }
1726baf02a1fSBen Widawsky 
17278d7849dbSVille Syrjälä static bool intel_pipe_handle_vblank(struct drm_device *dev, enum pipe pipe)
17288d7849dbSVille Syrjälä {
17298d7849dbSVille Syrjälä 	struct intel_crtc *crtc;
17308d7849dbSVille Syrjälä 
17318d7849dbSVille Syrjälä 	if (!drm_handle_vblank(dev, pipe))
17328d7849dbSVille Syrjälä 		return false;
17338d7849dbSVille Syrjälä 
17348d7849dbSVille Syrjälä 	crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
17358d7849dbSVille Syrjälä 	wake_up(&crtc->vbl_wait);
17368d7849dbSVille Syrjälä 
17378d7849dbSVille Syrjälä 	return true;
17388d7849dbSVille Syrjälä }
17398d7849dbSVille Syrjälä 
1740c1874ed7SImre Deak static void valleyview_pipestat_irq_handler(struct drm_device *dev, u32 iir)
17417e231dbeSJesse Barnes {
1742c1874ed7SImre Deak 	struct drm_i915_private *dev_priv = dev->dev_private;
174391d181ddSImre Deak 	u32 pipe_stats[I915_MAX_PIPES] = { };
17447e231dbeSJesse Barnes 	int pipe;
17457e231dbeSJesse Barnes 
174658ead0d7SImre Deak 	spin_lock(&dev_priv->irq_lock);
17477e231dbeSJesse Barnes 	for_each_pipe(pipe) {
174891d181ddSImre Deak 		int reg;
1749bbb5eebfSDaniel Vetter 		u32 mask, iir_bit = 0;
175091d181ddSImre Deak 
1751bbb5eebfSDaniel Vetter 		/*
1752bbb5eebfSDaniel Vetter 		 * PIPESTAT bits get signalled even when the interrupt is
1753bbb5eebfSDaniel Vetter 		 * disabled with the mask bits, and some of the status bits do
1754bbb5eebfSDaniel Vetter 		 * not generate interrupts at all (like the underrun bit). Hence
1755bbb5eebfSDaniel Vetter 		 * we need to be careful that we only handle what we want to
1756bbb5eebfSDaniel Vetter 		 * handle.
1757bbb5eebfSDaniel Vetter 		 */
1758bbb5eebfSDaniel Vetter 		mask = 0;
1759bbb5eebfSDaniel Vetter 		if (__cpu_fifo_underrun_reporting_enabled(dev, pipe))
1760bbb5eebfSDaniel Vetter 			mask |= PIPE_FIFO_UNDERRUN_STATUS;
1761bbb5eebfSDaniel Vetter 
1762bbb5eebfSDaniel Vetter 		switch (pipe) {
1763bbb5eebfSDaniel Vetter 		case PIPE_A:
1764bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_A_EVENT_INTERRUPT;
1765bbb5eebfSDaniel Vetter 			break;
1766bbb5eebfSDaniel Vetter 		case PIPE_B:
1767bbb5eebfSDaniel Vetter 			iir_bit = I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
1768bbb5eebfSDaniel Vetter 			break;
17693278f67fSVille Syrjälä 		case PIPE_C:
17703278f67fSVille Syrjälä 			iir_bit = I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
17713278f67fSVille Syrjälä 			break;
1772bbb5eebfSDaniel Vetter 		}
1773bbb5eebfSDaniel Vetter 		if (iir & iir_bit)
1774bbb5eebfSDaniel Vetter 			mask |= dev_priv->pipestat_irq_mask[pipe];
1775bbb5eebfSDaniel Vetter 
1776bbb5eebfSDaniel Vetter 		if (!mask)
177791d181ddSImre Deak 			continue;
177891d181ddSImre Deak 
177991d181ddSImre Deak 		reg = PIPESTAT(pipe);
1780bbb5eebfSDaniel Vetter 		mask |= PIPESTAT_INT_ENABLE_MASK;
1781bbb5eebfSDaniel Vetter 		pipe_stats[pipe] = I915_READ(reg) & mask;
17827e231dbeSJesse Barnes 
17837e231dbeSJesse Barnes 		/*
17847e231dbeSJesse Barnes 		 * Clear the PIPE*STAT regs before the IIR
17857e231dbeSJesse Barnes 		 */
178691d181ddSImre Deak 		if (pipe_stats[pipe] & (PIPE_FIFO_UNDERRUN_STATUS |
178791d181ddSImre Deak 					PIPESTAT_INT_STATUS_MASK))
17887e231dbeSJesse Barnes 			I915_WRITE(reg, pipe_stats[pipe]);
17897e231dbeSJesse Barnes 	}
179058ead0d7SImre Deak 	spin_unlock(&dev_priv->irq_lock);
17917e231dbeSJesse Barnes 
179231acc7f5SJesse Barnes 	for_each_pipe(pipe) {
17937b5562d4SJesse Barnes 		if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS)
17948d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
179531acc7f5SJesse Barnes 
1796579a9b0eSImre Deak 		if (pipe_stats[pipe] & PLANE_FLIP_DONE_INT_STATUS_VLV) {
179731acc7f5SJesse Barnes 			intel_prepare_page_flip(dev, pipe);
179831acc7f5SJesse Barnes 			intel_finish_page_flip(dev, pipe);
179931acc7f5SJesse Barnes 		}
18004356d586SDaniel Vetter 
18014356d586SDaniel Vetter 		if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
1802277de95eSDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
18032d9d2b0bSVille Syrjälä 
18042d9d2b0bSVille Syrjälä 		if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
18052d9d2b0bSVille Syrjälä 		    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
1806fc2c807bSVille Syrjälä 			DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
180731acc7f5SJesse Barnes 	}
180831acc7f5SJesse Barnes 
1809c1874ed7SImre Deak 	if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
1810c1874ed7SImre Deak 		gmbus_irq_handler(dev);
1811c1874ed7SImre Deak }
1812c1874ed7SImre Deak 
181316c6c56bSVille Syrjälä static void i9xx_hpd_irq_handler(struct drm_device *dev)
181416c6c56bSVille Syrjälä {
181516c6c56bSVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
181616c6c56bSVille Syrjälä 	u32 hotplug_status = I915_READ(PORT_HOTPLUG_STAT);
181716c6c56bSVille Syrjälä 
181816c6c56bSVille Syrjälä 	if (IS_G4X(dev)) {
181916c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_G4X;
182016c6c56bSVille Syrjälä 
182116c6c56bSVille Syrjälä 		intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_g4x);
182216c6c56bSVille Syrjälä 	} else {
182316c6c56bSVille Syrjälä 		u32 hotplug_trigger = hotplug_status & HOTPLUG_INT_STATUS_I915;
182416c6c56bSVille Syrjälä 
182516c6c56bSVille Syrjälä 		intel_hpd_irq_handler(dev, hotplug_trigger, hpd_status_i915);
182616c6c56bSVille Syrjälä 	}
182716c6c56bSVille Syrjälä 
182816c6c56bSVille Syrjälä 	if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) &&
182916c6c56bSVille Syrjälä 	    hotplug_status & DP_AUX_CHANNEL_MASK_INT_STATUS_G4X)
183016c6c56bSVille Syrjälä 		dp_aux_irq_handler(dev);
183116c6c56bSVille Syrjälä 
183216c6c56bSVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, hotplug_status);
183316c6c56bSVille Syrjälä 	/*
183416c6c56bSVille Syrjälä 	 * Make sure hotplug status is cleared before we clear IIR, or else we
183516c6c56bSVille Syrjälä 	 * may miss hotplug events.
183616c6c56bSVille Syrjälä 	 */
183716c6c56bSVille Syrjälä 	POSTING_READ(PORT_HOTPLUG_STAT);
183816c6c56bSVille Syrjälä }
183916c6c56bSVille Syrjälä 
1840c1874ed7SImre Deak static irqreturn_t valleyview_irq_handler(int irq, void *arg)
1841c1874ed7SImre Deak {
184245a83f84SDaniel Vetter 	struct drm_device *dev = arg;
18432d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
1844c1874ed7SImre Deak 	u32 iir, gt_iir, pm_iir;
1845c1874ed7SImre Deak 	irqreturn_t ret = IRQ_NONE;
1846c1874ed7SImre Deak 
1847c1874ed7SImre Deak 	while (true) {
1848c1874ed7SImre Deak 		iir = I915_READ(VLV_IIR);
1849c1874ed7SImre Deak 		gt_iir = I915_READ(GTIIR);
1850c1874ed7SImre Deak 		pm_iir = I915_READ(GEN6_PMIIR);
1851c1874ed7SImre Deak 
1852c1874ed7SImre Deak 		if (gt_iir == 0 && pm_iir == 0 && iir == 0)
1853c1874ed7SImre Deak 			goto out;
1854c1874ed7SImre Deak 
1855c1874ed7SImre Deak 		ret = IRQ_HANDLED;
1856c1874ed7SImre Deak 
1857c1874ed7SImre Deak 		snb_gt_irq_handler(dev, dev_priv, gt_iir);
1858c1874ed7SImre Deak 
1859c1874ed7SImre Deak 		valleyview_pipestat_irq_handler(dev, iir);
1860c1874ed7SImre Deak 
18617e231dbeSJesse Barnes 		/* Consume port.  Then clear IIR or we'll miss events */
186216c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
186316c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
18647e231dbeSJesse Barnes 
186560611c13SPaulo Zanoni 		if (pm_iir)
1866d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
18677e231dbeSJesse Barnes 
18687e231dbeSJesse Barnes 		I915_WRITE(GTIIR, gt_iir);
18697e231dbeSJesse Barnes 		I915_WRITE(GEN6_PMIIR, pm_iir);
18707e231dbeSJesse Barnes 		I915_WRITE(VLV_IIR, iir);
18717e231dbeSJesse Barnes 	}
18727e231dbeSJesse Barnes 
18737e231dbeSJesse Barnes out:
18747e231dbeSJesse Barnes 	return ret;
18757e231dbeSJesse Barnes }
18767e231dbeSJesse Barnes 
187743f328d7SVille Syrjälä static irqreturn_t cherryview_irq_handler(int irq, void *arg)
187843f328d7SVille Syrjälä {
187945a83f84SDaniel Vetter 	struct drm_device *dev = arg;
188043f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
188143f328d7SVille Syrjälä 	u32 master_ctl, iir;
188243f328d7SVille Syrjälä 	irqreturn_t ret = IRQ_NONE;
188343f328d7SVille Syrjälä 
18848e5fd599SVille Syrjälä 	for (;;) {
18858e5fd599SVille Syrjälä 		master_ctl = I915_READ(GEN8_MASTER_IRQ) & ~GEN8_MASTER_IRQ_CONTROL;
18863278f67fSVille Syrjälä 		iir = I915_READ(VLV_IIR);
18873278f67fSVille Syrjälä 
18883278f67fSVille Syrjälä 		if (master_ctl == 0 && iir == 0)
18898e5fd599SVille Syrjälä 			break;
189043f328d7SVille Syrjälä 
189143f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, 0);
189243f328d7SVille Syrjälä 
18933278f67fSVille Syrjälä 		gen8_gt_irq_handler(dev, dev_priv, master_ctl);
189443f328d7SVille Syrjälä 
18953278f67fSVille Syrjälä 		valleyview_pipestat_irq_handler(dev, iir);
189643f328d7SVille Syrjälä 
189743f328d7SVille Syrjälä 		/* Consume port.  Then clear IIR or we'll miss events */
18983278f67fSVille Syrjälä 		i9xx_hpd_irq_handler(dev);
189943f328d7SVille Syrjälä 
190043f328d7SVille Syrjälä 		I915_WRITE(VLV_IIR, iir);
190143f328d7SVille Syrjälä 
190243f328d7SVille Syrjälä 		I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
190343f328d7SVille Syrjälä 		POSTING_READ(GEN8_MASTER_IRQ);
190443f328d7SVille Syrjälä 
19053278f67fSVille Syrjälä 		ret = IRQ_HANDLED;
19068e5fd599SVille Syrjälä 	}
19073278f67fSVille Syrjälä 
190843f328d7SVille Syrjälä 	return ret;
190943f328d7SVille Syrjälä }
191043f328d7SVille Syrjälä 
191123e81d69SAdam Jackson static void ibx_irq_handler(struct drm_device *dev, u32 pch_iir)
1912776ad806SJesse Barnes {
19132d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
19149db4a9c7SJesse Barnes 	int pipe;
1915b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK;
1916776ad806SJesse Barnes 
191710a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_ibx);
191891d131d2SDaniel Vetter 
1919cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK) {
1920cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK) >>
1921776ad806SJesse Barnes 			       SDE_AUDIO_POWER_SHIFT);
1922cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %d\n",
1923cfc33bf7SVille Syrjälä 				 port_name(port));
1924cfc33bf7SVille Syrjälä 	}
1925776ad806SJesse Barnes 
1926ce99c256SDaniel Vetter 	if (pch_iir & SDE_AUX_MASK)
1927ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
1928ce99c256SDaniel Vetter 
1929776ad806SJesse Barnes 	if (pch_iir & SDE_GMBUS)
1930515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
1931776ad806SJesse Barnes 
1932776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_HDCP_MASK)
1933776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH HDCP audio interrupt\n");
1934776ad806SJesse Barnes 
1935776ad806SJesse Barnes 	if (pch_iir & SDE_AUDIO_TRANS_MASK)
1936776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder audio interrupt\n");
1937776ad806SJesse Barnes 
1938776ad806SJesse Barnes 	if (pch_iir & SDE_POISON)
1939776ad806SJesse Barnes 		DRM_ERROR("PCH poison interrupt\n");
1940776ad806SJesse Barnes 
19419db4a9c7SJesse Barnes 	if (pch_iir & SDE_FDI_MASK)
19429db4a9c7SJesse Barnes 		for_each_pipe(pipe)
19439db4a9c7SJesse Barnes 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
19449db4a9c7SJesse Barnes 					 pipe_name(pipe),
19459db4a9c7SJesse Barnes 					 I915_READ(FDI_RX_IIR(pipe)));
1946776ad806SJesse Barnes 
1947776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_DONE | SDE_TRANSA_CRC_DONE))
1948776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC done interrupt\n");
1949776ad806SJesse Barnes 
1950776ad806SJesse Barnes 	if (pch_iir & (SDE_TRANSB_CRC_ERR | SDE_TRANSA_CRC_ERR))
1951776ad806SJesse Barnes 		DRM_DEBUG_DRIVER("PCH transcoder CRC error interrupt\n");
1952776ad806SJesse Barnes 
1953776ad806SJesse Barnes 	if (pch_iir & SDE_TRANSA_FIFO_UNDER)
19548664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
19558664281bSPaulo Zanoni 							  false))
1956fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
19578664281bSPaulo Zanoni 
19588664281bSPaulo Zanoni 	if (pch_iir & SDE_TRANSB_FIFO_UNDER)
19598664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
19608664281bSPaulo Zanoni 							  false))
1961fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
19628664281bSPaulo Zanoni }
19638664281bSPaulo Zanoni 
19648664281bSPaulo Zanoni static void ivb_err_int_handler(struct drm_device *dev)
19658664281bSPaulo Zanoni {
19668664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19678664281bSPaulo Zanoni 	u32 err_int = I915_READ(GEN7_ERR_INT);
19685a69b89fSDaniel Vetter 	enum pipe pipe;
19698664281bSPaulo Zanoni 
1970de032bf4SPaulo Zanoni 	if (err_int & ERR_INT_POISON)
1971de032bf4SPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
1972de032bf4SPaulo Zanoni 
19735a69b89fSDaniel Vetter 	for_each_pipe(pipe) {
19745a69b89fSDaniel Vetter 		if (err_int & ERR_INT_FIFO_UNDERRUN(pipe)) {
19755a69b89fSDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
19765a69b89fSDaniel Vetter 								  false))
1977fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
19785a69b89fSDaniel Vetter 					  pipe_name(pipe));
19795a69b89fSDaniel Vetter 		}
19808664281bSPaulo Zanoni 
19815a69b89fSDaniel Vetter 		if (err_int & ERR_INT_PIPE_CRC_DONE(pipe)) {
19825a69b89fSDaniel Vetter 			if (IS_IVYBRIDGE(dev))
1983277de95eSDaniel Vetter 				ivb_pipe_crc_irq_handler(dev, pipe);
19845a69b89fSDaniel Vetter 			else
1985277de95eSDaniel Vetter 				hsw_pipe_crc_irq_handler(dev, pipe);
19865a69b89fSDaniel Vetter 		}
19875a69b89fSDaniel Vetter 	}
19888bf1e9f1SShuang He 
19898664281bSPaulo Zanoni 	I915_WRITE(GEN7_ERR_INT, err_int);
19908664281bSPaulo Zanoni }
19918664281bSPaulo Zanoni 
19928664281bSPaulo Zanoni static void cpt_serr_int_handler(struct drm_device *dev)
19938664281bSPaulo Zanoni {
19948664281bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
19958664281bSPaulo Zanoni 	u32 serr_int = I915_READ(SERR_INT);
19968664281bSPaulo Zanoni 
1997de032bf4SPaulo Zanoni 	if (serr_int & SERR_INT_POISON)
1998de032bf4SPaulo Zanoni 		DRM_ERROR("PCH poison interrupt\n");
1999de032bf4SPaulo Zanoni 
20008664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_A_FIFO_UNDERRUN)
20018664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A,
20028664281bSPaulo Zanoni 							  false))
2003fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder A FIFO underrun\n");
20048664281bSPaulo Zanoni 
20058664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_B_FIFO_UNDERRUN)
20068664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_B,
20078664281bSPaulo Zanoni 							  false))
2008fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder B FIFO underrun\n");
20098664281bSPaulo Zanoni 
20108664281bSPaulo Zanoni 	if (serr_int & SERR_INT_TRANS_C_FIFO_UNDERRUN)
20118664281bSPaulo Zanoni 		if (intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_C,
20128664281bSPaulo Zanoni 							  false))
2013fc2c807bSVille Syrjälä 			DRM_ERROR("PCH transcoder C FIFO underrun\n");
20148664281bSPaulo Zanoni 
20158664281bSPaulo Zanoni 	I915_WRITE(SERR_INT, serr_int);
2016776ad806SJesse Barnes }
2017776ad806SJesse Barnes 
201823e81d69SAdam Jackson static void cpt_irq_handler(struct drm_device *dev, u32 pch_iir)
201923e81d69SAdam Jackson {
20202d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
202123e81d69SAdam Jackson 	int pipe;
2022b543fb04SEgbert Eich 	u32 hotplug_trigger = pch_iir & SDE_HOTPLUG_MASK_CPT;
202323e81d69SAdam Jackson 
202410a504deSDaniel Vetter 	intel_hpd_irq_handler(dev, hotplug_trigger, hpd_cpt);
202591d131d2SDaniel Vetter 
2026cfc33bf7SVille Syrjälä 	if (pch_iir & SDE_AUDIO_POWER_MASK_CPT) {
2027cfc33bf7SVille Syrjälä 		int port = ffs((pch_iir & SDE_AUDIO_POWER_MASK_CPT) >>
202823e81d69SAdam Jackson 			       SDE_AUDIO_POWER_SHIFT_CPT);
2029cfc33bf7SVille Syrjälä 		DRM_DEBUG_DRIVER("PCH audio power change on port %c\n",
2030cfc33bf7SVille Syrjälä 				 port_name(port));
2031cfc33bf7SVille Syrjälä 	}
203223e81d69SAdam Jackson 
203323e81d69SAdam Jackson 	if (pch_iir & SDE_AUX_MASK_CPT)
2034ce99c256SDaniel Vetter 		dp_aux_irq_handler(dev);
203523e81d69SAdam Jackson 
203623e81d69SAdam Jackson 	if (pch_iir & SDE_GMBUS_CPT)
2037515ac2bbSDaniel Vetter 		gmbus_irq_handler(dev);
203823e81d69SAdam Jackson 
203923e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_REQ_CPT)
204023e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP request interrupt\n");
204123e81d69SAdam Jackson 
204223e81d69SAdam Jackson 	if (pch_iir & SDE_AUDIO_CP_CHG_CPT)
204323e81d69SAdam Jackson 		DRM_DEBUG_DRIVER("Audio CP change interrupt\n");
204423e81d69SAdam Jackson 
204523e81d69SAdam Jackson 	if (pch_iir & SDE_FDI_MASK_CPT)
204623e81d69SAdam Jackson 		for_each_pipe(pipe)
204723e81d69SAdam Jackson 			DRM_DEBUG_DRIVER("  pipe %c FDI IIR: 0x%08x\n",
204823e81d69SAdam Jackson 					 pipe_name(pipe),
204923e81d69SAdam Jackson 					 I915_READ(FDI_RX_IIR(pipe)));
20508664281bSPaulo Zanoni 
20518664281bSPaulo Zanoni 	if (pch_iir & SDE_ERROR_CPT)
20528664281bSPaulo Zanoni 		cpt_serr_int_handler(dev);
205323e81d69SAdam Jackson }
205423e81d69SAdam Jackson 
2055c008bc6eSPaulo Zanoni static void ilk_display_irq_handler(struct drm_device *dev, u32 de_iir)
2056c008bc6eSPaulo Zanoni {
2057c008bc6eSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
205840da17c2SDaniel Vetter 	enum pipe pipe;
2059c008bc6eSPaulo Zanoni 
2060c008bc6eSPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A)
2061c008bc6eSPaulo Zanoni 		dp_aux_irq_handler(dev);
2062c008bc6eSPaulo Zanoni 
2063c008bc6eSPaulo Zanoni 	if (de_iir & DE_GSE)
2064c008bc6eSPaulo Zanoni 		intel_opregion_asle_intr(dev);
2065c008bc6eSPaulo Zanoni 
2066c008bc6eSPaulo Zanoni 	if (de_iir & DE_POISON)
2067c008bc6eSPaulo Zanoni 		DRM_ERROR("Poison interrupt\n");
2068c008bc6eSPaulo Zanoni 
206940da17c2SDaniel Vetter 	for_each_pipe(pipe) {
207040da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_VBLANK(pipe))
20718d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
2072c008bc6eSPaulo Zanoni 
207340da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_FIFO_UNDERRUN(pipe))
207440da17c2SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
2075fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
207640da17c2SDaniel Vetter 					  pipe_name(pipe));
2077c008bc6eSPaulo Zanoni 
207840da17c2SDaniel Vetter 		if (de_iir & DE_PIPE_CRC_DONE(pipe))
207940da17c2SDaniel Vetter 			i9xx_pipe_crc_irq_handler(dev, pipe);
20805b3a856bSDaniel Vetter 
208140da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
208240da17c2SDaniel Vetter 		if (de_iir & DE_PLANE_FLIP_DONE(pipe)) {
208340da17c2SDaniel Vetter 			intel_prepare_page_flip(dev, pipe);
208440da17c2SDaniel Vetter 			intel_finish_page_flip_plane(dev, pipe);
2085c008bc6eSPaulo Zanoni 		}
2086c008bc6eSPaulo Zanoni 	}
2087c008bc6eSPaulo Zanoni 
2088c008bc6eSPaulo Zanoni 	/* check event from PCH */
2089c008bc6eSPaulo Zanoni 	if (de_iir & DE_PCH_EVENT) {
2090c008bc6eSPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
2091c008bc6eSPaulo Zanoni 
2092c008bc6eSPaulo Zanoni 		if (HAS_PCH_CPT(dev))
2093c008bc6eSPaulo Zanoni 			cpt_irq_handler(dev, pch_iir);
2094c008bc6eSPaulo Zanoni 		else
2095c008bc6eSPaulo Zanoni 			ibx_irq_handler(dev, pch_iir);
2096c008bc6eSPaulo Zanoni 
2097c008bc6eSPaulo Zanoni 		/* should clear PCH hotplug event before clear CPU irq */
2098c008bc6eSPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
2099c008bc6eSPaulo Zanoni 	}
2100c008bc6eSPaulo Zanoni 
2101c008bc6eSPaulo Zanoni 	if (IS_GEN5(dev) && de_iir & DE_PCU_EVENT)
2102c008bc6eSPaulo Zanoni 		ironlake_rps_change_irq_handler(dev);
2103c008bc6eSPaulo Zanoni }
2104c008bc6eSPaulo Zanoni 
21059719fb98SPaulo Zanoni static void ivb_display_irq_handler(struct drm_device *dev, u32 de_iir)
21069719fb98SPaulo Zanoni {
21079719fb98SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
210807d27e20SDamien Lespiau 	enum pipe pipe;
21099719fb98SPaulo Zanoni 
21109719fb98SPaulo Zanoni 	if (de_iir & DE_ERR_INT_IVB)
21119719fb98SPaulo Zanoni 		ivb_err_int_handler(dev);
21129719fb98SPaulo Zanoni 
21139719fb98SPaulo Zanoni 	if (de_iir & DE_AUX_CHANNEL_A_IVB)
21149719fb98SPaulo Zanoni 		dp_aux_irq_handler(dev);
21159719fb98SPaulo Zanoni 
21169719fb98SPaulo Zanoni 	if (de_iir & DE_GSE_IVB)
21179719fb98SPaulo Zanoni 		intel_opregion_asle_intr(dev);
21189719fb98SPaulo Zanoni 
211907d27e20SDamien Lespiau 	for_each_pipe(pipe) {
212007d27e20SDamien Lespiau 		if (de_iir & (DE_PIPE_VBLANK_IVB(pipe)))
21218d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
212240da17c2SDaniel Vetter 
212340da17c2SDaniel Vetter 		/* plane/pipes map 1:1 on ilk+ */
212407d27e20SDamien Lespiau 		if (de_iir & DE_PLANE_FLIP_DONE_IVB(pipe)) {
212507d27e20SDamien Lespiau 			intel_prepare_page_flip(dev, pipe);
212607d27e20SDamien Lespiau 			intel_finish_page_flip_plane(dev, pipe);
21279719fb98SPaulo Zanoni 		}
21289719fb98SPaulo Zanoni 	}
21299719fb98SPaulo Zanoni 
21309719fb98SPaulo Zanoni 	/* check event from PCH */
21319719fb98SPaulo Zanoni 	if (!HAS_PCH_NOP(dev) && (de_iir & DE_PCH_EVENT_IVB)) {
21329719fb98SPaulo Zanoni 		u32 pch_iir = I915_READ(SDEIIR);
21339719fb98SPaulo Zanoni 
21349719fb98SPaulo Zanoni 		cpt_irq_handler(dev, pch_iir);
21359719fb98SPaulo Zanoni 
21369719fb98SPaulo Zanoni 		/* clear PCH hotplug event before clear CPU irq */
21379719fb98SPaulo Zanoni 		I915_WRITE(SDEIIR, pch_iir);
21389719fb98SPaulo Zanoni 	}
21399719fb98SPaulo Zanoni }
21409719fb98SPaulo Zanoni 
2141f1af8fc1SPaulo Zanoni static irqreturn_t ironlake_irq_handler(int irq, void *arg)
2142b1f14ad0SJesse Barnes {
214345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
21442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2145f1af8fc1SPaulo Zanoni 	u32 de_iir, gt_iir, de_ier, sde_ier = 0;
21460e43406bSChris Wilson 	irqreturn_t ret = IRQ_NONE;
2147b1f14ad0SJesse Barnes 
21488664281bSPaulo Zanoni 	/* We get interrupts on unclaimed registers, so check for this before we
21498664281bSPaulo Zanoni 	 * do any I915_{READ,WRITE}. */
2150907b28c5SChris Wilson 	intel_uncore_check_errors(dev);
21518664281bSPaulo Zanoni 
2152b1f14ad0SJesse Barnes 	/* disable master interrupt before clearing iir  */
2153b1f14ad0SJesse Barnes 	de_ier = I915_READ(DEIER);
2154b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier & ~DE_MASTER_IRQ_CONTROL);
215523a78516SPaulo Zanoni 	POSTING_READ(DEIER);
21560e43406bSChris Wilson 
215744498aeaSPaulo Zanoni 	/* Disable south interrupts. We'll only write to SDEIIR once, so further
215844498aeaSPaulo Zanoni 	 * interrupts will will be stored on its back queue, and then we'll be
215944498aeaSPaulo Zanoni 	 * able to process them after we restore SDEIER (as soon as we restore
216044498aeaSPaulo Zanoni 	 * it, we'll get an interrupt if SDEIIR still has something to process
216144498aeaSPaulo Zanoni 	 * due to its back queue). */
2162ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
216344498aeaSPaulo Zanoni 		sde_ier = I915_READ(SDEIER);
216444498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, 0);
216544498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2166ab5c608bSBen Widawsky 	}
216744498aeaSPaulo Zanoni 
21680e43406bSChris Wilson 	gt_iir = I915_READ(GTIIR);
21690e43406bSChris Wilson 	if (gt_iir) {
2170d8fc8a47SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 6)
21710e43406bSChris Wilson 			snb_gt_irq_handler(dev, dev_priv, gt_iir);
2172d8fc8a47SPaulo Zanoni 		else
2173d8fc8a47SPaulo Zanoni 			ilk_gt_irq_handler(dev, dev_priv, gt_iir);
21740e43406bSChris Wilson 		I915_WRITE(GTIIR, gt_iir);
21750e43406bSChris Wilson 		ret = IRQ_HANDLED;
21760e43406bSChris Wilson 	}
2177b1f14ad0SJesse Barnes 
2178b1f14ad0SJesse Barnes 	de_iir = I915_READ(DEIIR);
21790e43406bSChris Wilson 	if (de_iir) {
2180f1af8fc1SPaulo Zanoni 		if (INTEL_INFO(dev)->gen >= 7)
21819719fb98SPaulo Zanoni 			ivb_display_irq_handler(dev, de_iir);
2182f1af8fc1SPaulo Zanoni 		else
2183f1af8fc1SPaulo Zanoni 			ilk_display_irq_handler(dev, de_iir);
21840e43406bSChris Wilson 		I915_WRITE(DEIIR, de_iir);
21850e43406bSChris Wilson 		ret = IRQ_HANDLED;
21860e43406bSChris Wilson 	}
21870e43406bSChris Wilson 
2188f1af8fc1SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6) {
2189f1af8fc1SPaulo Zanoni 		u32 pm_iir = I915_READ(GEN6_PMIIR);
21900e43406bSChris Wilson 		if (pm_iir) {
2191d0ecd7e2SDaniel Vetter 			gen6_rps_irq_handler(dev_priv, pm_iir);
2192b1f14ad0SJesse Barnes 			I915_WRITE(GEN6_PMIIR, pm_iir);
21930e43406bSChris Wilson 			ret = IRQ_HANDLED;
21940e43406bSChris Wilson 		}
2195f1af8fc1SPaulo Zanoni 	}
2196b1f14ad0SJesse Barnes 
2197b1f14ad0SJesse Barnes 	I915_WRITE(DEIER, de_ier);
2198b1f14ad0SJesse Barnes 	POSTING_READ(DEIER);
2199ab5c608bSBen Widawsky 	if (!HAS_PCH_NOP(dev)) {
220044498aeaSPaulo Zanoni 		I915_WRITE(SDEIER, sde_ier);
220144498aeaSPaulo Zanoni 		POSTING_READ(SDEIER);
2202ab5c608bSBen Widawsky 	}
2203b1f14ad0SJesse Barnes 
2204b1f14ad0SJesse Barnes 	return ret;
2205b1f14ad0SJesse Barnes }
2206b1f14ad0SJesse Barnes 
2207abd58f01SBen Widawsky static irqreturn_t gen8_irq_handler(int irq, void *arg)
2208abd58f01SBen Widawsky {
2209abd58f01SBen Widawsky 	struct drm_device *dev = arg;
2210abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2211abd58f01SBen Widawsky 	u32 master_ctl;
2212abd58f01SBen Widawsky 	irqreturn_t ret = IRQ_NONE;
2213abd58f01SBen Widawsky 	uint32_t tmp = 0;
2214c42664ccSDaniel Vetter 	enum pipe pipe;
2215abd58f01SBen Widawsky 
2216abd58f01SBen Widawsky 	master_ctl = I915_READ(GEN8_MASTER_IRQ);
2217abd58f01SBen Widawsky 	master_ctl &= ~GEN8_MASTER_IRQ_CONTROL;
2218abd58f01SBen Widawsky 	if (!master_ctl)
2219abd58f01SBen Widawsky 		return IRQ_NONE;
2220abd58f01SBen Widawsky 
2221abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
2222abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2223abd58f01SBen Widawsky 
2224abd58f01SBen Widawsky 	ret = gen8_gt_irq_handler(dev, dev_priv, master_ctl);
2225abd58f01SBen Widawsky 
2226abd58f01SBen Widawsky 	if (master_ctl & GEN8_DE_MISC_IRQ) {
2227abd58f01SBen Widawsky 		tmp = I915_READ(GEN8_DE_MISC_IIR);
2228abd58f01SBen Widawsky 		if (tmp & GEN8_DE_MISC_GSE)
2229abd58f01SBen Widawsky 			intel_opregion_asle_intr(dev);
2230abd58f01SBen Widawsky 		else if (tmp)
2231abd58f01SBen Widawsky 			DRM_ERROR("Unexpected DE Misc interrupt\n");
2232abd58f01SBen Widawsky 		else
2233abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE MISC)!\n");
2234abd58f01SBen Widawsky 
2235abd58f01SBen Widawsky 		if (tmp) {
2236abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_MISC_IIR, tmp);
2237abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2238abd58f01SBen Widawsky 		}
2239abd58f01SBen Widawsky 	}
2240abd58f01SBen Widawsky 
22416d766f02SDaniel Vetter 	if (master_ctl & GEN8_DE_PORT_IRQ) {
22426d766f02SDaniel Vetter 		tmp = I915_READ(GEN8_DE_PORT_IIR);
22436d766f02SDaniel Vetter 		if (tmp & GEN8_AUX_CHANNEL_A)
22446d766f02SDaniel Vetter 			dp_aux_irq_handler(dev);
22456d766f02SDaniel Vetter 		else if (tmp)
22466d766f02SDaniel Vetter 			DRM_ERROR("Unexpected DE Port interrupt\n");
22476d766f02SDaniel Vetter 		else
22486d766f02SDaniel Vetter 			DRM_ERROR("The master control interrupt lied (DE PORT)!\n");
22496d766f02SDaniel Vetter 
22506d766f02SDaniel Vetter 		if (tmp) {
22516d766f02SDaniel Vetter 			I915_WRITE(GEN8_DE_PORT_IIR, tmp);
22526d766f02SDaniel Vetter 			ret = IRQ_HANDLED;
22536d766f02SDaniel Vetter 		}
22546d766f02SDaniel Vetter 	}
22556d766f02SDaniel Vetter 
2256abd58f01SBen Widawsky 	for_each_pipe(pipe) {
2257abd58f01SBen Widawsky 		uint32_t pipe_iir;
2258abd58f01SBen Widawsky 
2259c42664ccSDaniel Vetter 		if (!(master_ctl & GEN8_DE_PIPE_IRQ(pipe)))
2260c42664ccSDaniel Vetter 			continue;
2261c42664ccSDaniel Vetter 
2262abd58f01SBen Widawsky 		pipe_iir = I915_READ(GEN8_DE_PIPE_IIR(pipe));
2263abd58f01SBen Widawsky 		if (pipe_iir & GEN8_PIPE_VBLANK)
22648d7849dbSVille Syrjälä 			intel_pipe_handle_vblank(dev, pipe);
2265abd58f01SBen Widawsky 
2266d0e1f1cbSDamien Lespiau 		if (pipe_iir & GEN8_PIPE_PRIMARY_FLIP_DONE) {
2267abd58f01SBen Widawsky 			intel_prepare_page_flip(dev, pipe);
2268abd58f01SBen Widawsky 			intel_finish_page_flip_plane(dev, pipe);
2269abd58f01SBen Widawsky 		}
2270abd58f01SBen Widawsky 
22710fbe7870SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_CDCLK_CRC_DONE)
22720fbe7870SDaniel Vetter 			hsw_pipe_crc_irq_handler(dev, pipe);
22730fbe7870SDaniel Vetter 
227438d83c96SDaniel Vetter 		if (pipe_iir & GEN8_PIPE_FIFO_UNDERRUN) {
227538d83c96SDaniel Vetter 			if (intel_set_cpu_fifo_underrun_reporting(dev, pipe,
227638d83c96SDaniel Vetter 								  false))
2277fc2c807bSVille Syrjälä 				DRM_ERROR("Pipe %c FIFO underrun\n",
227838d83c96SDaniel Vetter 					  pipe_name(pipe));
227938d83c96SDaniel Vetter 		}
228038d83c96SDaniel Vetter 
228130100f2bSDaniel Vetter 		if (pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS) {
228230100f2bSDaniel Vetter 			DRM_ERROR("Fault errors on pipe %c\n: 0x%08x",
228330100f2bSDaniel Vetter 				  pipe_name(pipe),
228430100f2bSDaniel Vetter 				  pipe_iir & GEN8_DE_PIPE_IRQ_FAULT_ERRORS);
228530100f2bSDaniel Vetter 		}
2286abd58f01SBen Widawsky 
2287abd58f01SBen Widawsky 		if (pipe_iir) {
2288abd58f01SBen Widawsky 			ret = IRQ_HANDLED;
2289abd58f01SBen Widawsky 			I915_WRITE(GEN8_DE_PIPE_IIR(pipe), pipe_iir);
2290c42664ccSDaniel Vetter 		} else
2291abd58f01SBen Widawsky 			DRM_ERROR("The master control interrupt lied (DE PIPE)!\n");
2292abd58f01SBen Widawsky 	}
2293abd58f01SBen Widawsky 
229492d03a80SDaniel Vetter 	if (!HAS_PCH_NOP(dev) && master_ctl & GEN8_DE_PCH_IRQ) {
229592d03a80SDaniel Vetter 		/*
229692d03a80SDaniel Vetter 		 * FIXME(BDW): Assume for now that the new interrupt handling
229792d03a80SDaniel Vetter 		 * scheme also closed the SDE interrupt handling race we've seen
229892d03a80SDaniel Vetter 		 * on older pch-split platforms. But this needs testing.
229992d03a80SDaniel Vetter 		 */
230092d03a80SDaniel Vetter 		u32 pch_iir = I915_READ(SDEIIR);
230192d03a80SDaniel Vetter 
230292d03a80SDaniel Vetter 		cpt_irq_handler(dev, pch_iir);
230392d03a80SDaniel Vetter 
230492d03a80SDaniel Vetter 		if (pch_iir) {
230592d03a80SDaniel Vetter 			I915_WRITE(SDEIIR, pch_iir);
230692d03a80SDaniel Vetter 			ret = IRQ_HANDLED;
230792d03a80SDaniel Vetter 		}
230892d03a80SDaniel Vetter 	}
230992d03a80SDaniel Vetter 
2310abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, GEN8_MASTER_IRQ_CONTROL);
2311abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
2312abd58f01SBen Widawsky 
2313abd58f01SBen Widawsky 	return ret;
2314abd58f01SBen Widawsky }
2315abd58f01SBen Widawsky 
231617e1df07SDaniel Vetter static void i915_error_wake_up(struct drm_i915_private *dev_priv,
231717e1df07SDaniel Vetter 			       bool reset_completed)
231817e1df07SDaniel Vetter {
2319*a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
232017e1df07SDaniel Vetter 	int i;
232117e1df07SDaniel Vetter 
232217e1df07SDaniel Vetter 	/*
232317e1df07SDaniel Vetter 	 * Notify all waiters for GPU completion events that reset state has
232417e1df07SDaniel Vetter 	 * been changed, and that they need to restart their wait after
232517e1df07SDaniel Vetter 	 * checking for potential errors (and bail out to drop locks if there is
232617e1df07SDaniel Vetter 	 * a gpu reset pending so that i915_error_work_func can acquire them).
232717e1df07SDaniel Vetter 	 */
232817e1df07SDaniel Vetter 
232917e1df07SDaniel Vetter 	/* Wake up __wait_seqno, potentially holding dev->struct_mutex. */
233017e1df07SDaniel Vetter 	for_each_ring(ring, dev_priv, i)
233117e1df07SDaniel Vetter 		wake_up_all(&ring->irq_queue);
233217e1df07SDaniel Vetter 
233317e1df07SDaniel Vetter 	/* Wake up intel_crtc_wait_for_pending_flips, holding crtc->mutex. */
233417e1df07SDaniel Vetter 	wake_up_all(&dev_priv->pending_flip_queue);
233517e1df07SDaniel Vetter 
233617e1df07SDaniel Vetter 	/*
233717e1df07SDaniel Vetter 	 * Signal tasks blocked in i915_gem_wait_for_error that the pending
233817e1df07SDaniel Vetter 	 * reset state is cleared.
233917e1df07SDaniel Vetter 	 */
234017e1df07SDaniel Vetter 	if (reset_completed)
234117e1df07SDaniel Vetter 		wake_up_all(&dev_priv->gpu_error.reset_queue);
234217e1df07SDaniel Vetter }
234317e1df07SDaniel Vetter 
23448a905236SJesse Barnes /**
23458a905236SJesse Barnes  * i915_error_work_func - do process context error handling work
23468a905236SJesse Barnes  * @work: work struct
23478a905236SJesse Barnes  *
23488a905236SJesse Barnes  * Fire an error uevent so userspace can see that a hang or error
23498a905236SJesse Barnes  * was detected.
23508a905236SJesse Barnes  */
23518a905236SJesse Barnes static void i915_error_work_func(struct work_struct *work)
23528a905236SJesse Barnes {
23531f83fee0SDaniel Vetter 	struct i915_gpu_error *error = container_of(work, struct i915_gpu_error,
23541f83fee0SDaniel Vetter 						    work);
23552d1013ddSJani Nikula 	struct drm_i915_private *dev_priv =
23562d1013ddSJani Nikula 		container_of(error, struct drm_i915_private, gpu_error);
23578a905236SJesse Barnes 	struct drm_device *dev = dev_priv->dev;
2358cce723edSBen Widawsky 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
2359cce723edSBen Widawsky 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
2360cce723edSBen Widawsky 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
236117e1df07SDaniel Vetter 	int ret;
23628a905236SJesse Barnes 
23635bdebb18SDave Airlie 	kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, error_event);
23648a905236SJesse Barnes 
23657db0ba24SDaniel Vetter 	/*
23667db0ba24SDaniel Vetter 	 * Note that there's only one work item which does gpu resets, so we
23677db0ba24SDaniel Vetter 	 * need not worry about concurrent gpu resets potentially incrementing
23687db0ba24SDaniel Vetter 	 * error->reset_counter twice. We only need to take care of another
23697db0ba24SDaniel Vetter 	 * racing irq/hangcheck declaring the gpu dead for a second time. A
23707db0ba24SDaniel Vetter 	 * quick check for that is good enough: schedule_work ensures the
23717db0ba24SDaniel Vetter 	 * correct ordering between hang detection and this work item, and since
23727db0ba24SDaniel Vetter 	 * the reset in-progress bit is only ever set by code outside of this
23737db0ba24SDaniel Vetter 	 * work we don't need to worry about any other races.
23747db0ba24SDaniel Vetter 	 */
23757db0ba24SDaniel Vetter 	if (i915_reset_in_progress(error) && !i915_terminally_wedged(error)) {
237644d98a61SZhao Yakui 		DRM_DEBUG_DRIVER("resetting chip\n");
23775bdebb18SDave Airlie 		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE,
23787db0ba24SDaniel Vetter 				   reset_event);
23791f83fee0SDaniel Vetter 
238017e1df07SDaniel Vetter 		/*
2381f454c694SImre Deak 		 * In most cases it's guaranteed that we get here with an RPM
2382f454c694SImre Deak 		 * reference held, for example because there is a pending GPU
2383f454c694SImre Deak 		 * request that won't finish until the reset is done. This
2384f454c694SImre Deak 		 * isn't the case at least when we get here by doing a
2385f454c694SImre Deak 		 * simulated reset via debugs, so get an RPM reference.
2386f454c694SImre Deak 		 */
2387f454c694SImre Deak 		intel_runtime_pm_get(dev_priv);
2388f454c694SImre Deak 		/*
238917e1df07SDaniel Vetter 		 * All state reset _must_ be completed before we update the
239017e1df07SDaniel Vetter 		 * reset counter, for otherwise waiters might miss the reset
239117e1df07SDaniel Vetter 		 * pending state and not properly drop locks, resulting in
239217e1df07SDaniel Vetter 		 * deadlocks with the reset work.
239317e1df07SDaniel Vetter 		 */
2394f69061beSDaniel Vetter 		ret = i915_reset(dev);
2395f69061beSDaniel Vetter 
239617e1df07SDaniel Vetter 		intel_display_handle_reset(dev);
239717e1df07SDaniel Vetter 
2398f454c694SImre Deak 		intel_runtime_pm_put(dev_priv);
2399f454c694SImre Deak 
2400f69061beSDaniel Vetter 		if (ret == 0) {
2401f69061beSDaniel Vetter 			/*
2402f69061beSDaniel Vetter 			 * After all the gem state is reset, increment the reset
2403f69061beSDaniel Vetter 			 * counter and wake up everyone waiting for the reset to
2404f69061beSDaniel Vetter 			 * complete.
2405f69061beSDaniel Vetter 			 *
2406f69061beSDaniel Vetter 			 * Since unlock operations are a one-sided barrier only,
2407f69061beSDaniel Vetter 			 * we need to insert a barrier here to order any seqno
2408f69061beSDaniel Vetter 			 * updates before
2409f69061beSDaniel Vetter 			 * the counter increment.
2410f69061beSDaniel Vetter 			 */
2411f69061beSDaniel Vetter 			smp_mb__before_atomic_inc();
2412f69061beSDaniel Vetter 			atomic_inc(&dev_priv->gpu_error.reset_counter);
2413f69061beSDaniel Vetter 
24145bdebb18SDave Airlie 			kobject_uevent_env(&dev->primary->kdev->kobj,
2415f69061beSDaniel Vetter 					   KOBJ_CHANGE, reset_done_event);
24161f83fee0SDaniel Vetter 		} else {
24172ac0f450SMika Kuoppala 			atomic_set_mask(I915_WEDGED, &error->reset_counter);
2418f316a42cSBen Gamari 		}
24191f83fee0SDaniel Vetter 
242017e1df07SDaniel Vetter 		/*
242117e1df07SDaniel Vetter 		 * Note: The wake_up also serves as a memory barrier so that
242217e1df07SDaniel Vetter 		 * waiters see the update value of the reset counter atomic_t.
242317e1df07SDaniel Vetter 		 */
242417e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, true);
2425f316a42cSBen Gamari 	}
24268a905236SJesse Barnes }
24278a905236SJesse Barnes 
242835aed2e6SChris Wilson static void i915_report_and_clear_eir(struct drm_device *dev)
2429c0e09200SDave Airlie {
24308a905236SJesse Barnes 	struct drm_i915_private *dev_priv = dev->dev_private;
2431bd9854f9SBen Widawsky 	uint32_t instdone[I915_NUM_INSTDONE_REG];
243263eeaf38SJesse Barnes 	u32 eir = I915_READ(EIR);
2433050ee91fSBen Widawsky 	int pipe, i;
243463eeaf38SJesse Barnes 
243535aed2e6SChris Wilson 	if (!eir)
243635aed2e6SChris Wilson 		return;
243763eeaf38SJesse Barnes 
2438a70491ccSJoe Perches 	pr_err("render error detected, EIR: 0x%08x\n", eir);
24398a905236SJesse Barnes 
2440bd9854f9SBen Widawsky 	i915_get_extra_instdone(dev, instdone);
2441bd9854f9SBen Widawsky 
24428a905236SJesse Barnes 	if (IS_G4X(dev)) {
24438a905236SJesse Barnes 		if (eir & (GM45_ERROR_MEM_PRIV | GM45_ERROR_CP_PRIV)) {
24448a905236SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
24458a905236SJesse Barnes 
2446a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2447a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2448050ee91fSBen Widawsky 			for (i = 0; i < ARRAY_SIZE(instdone); i++)
2449050ee91fSBen Widawsky 				pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2450a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2451a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
24528a905236SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
24533143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
24548a905236SJesse Barnes 		}
24558a905236SJesse Barnes 		if (eir & GM45_ERROR_PAGE_TABLE) {
24568a905236SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2457a70491ccSJoe Perches 			pr_err("page table error\n");
2458a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
24598a905236SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24603143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
24618a905236SJesse Barnes 		}
24628a905236SJesse Barnes 	}
24638a905236SJesse Barnes 
2464a6c45cf0SChris Wilson 	if (!IS_GEN2(dev)) {
246563eeaf38SJesse Barnes 		if (eir & I915_ERROR_PAGE_TABLE) {
246663eeaf38SJesse Barnes 			u32 pgtbl_err = I915_READ(PGTBL_ER);
2467a70491ccSJoe Perches 			pr_err("page table error\n");
2468a70491ccSJoe Perches 			pr_err("  PGTBL_ER: 0x%08x\n", pgtbl_err);
246963eeaf38SJesse Barnes 			I915_WRITE(PGTBL_ER, pgtbl_err);
24703143a2bfSChris Wilson 			POSTING_READ(PGTBL_ER);
247163eeaf38SJesse Barnes 		}
24728a905236SJesse Barnes 	}
24738a905236SJesse Barnes 
247463eeaf38SJesse Barnes 	if (eir & I915_ERROR_MEMORY_REFRESH) {
2475a70491ccSJoe Perches 		pr_err("memory refresh error:\n");
24769db4a9c7SJesse Barnes 		for_each_pipe(pipe)
2477a70491ccSJoe Perches 			pr_err("pipe %c stat: 0x%08x\n",
24789db4a9c7SJesse Barnes 			       pipe_name(pipe), I915_READ(PIPESTAT(pipe)));
247963eeaf38SJesse Barnes 		/* pipestat has already been acked */
248063eeaf38SJesse Barnes 	}
248163eeaf38SJesse Barnes 	if (eir & I915_ERROR_INSTRUCTION) {
2482a70491ccSJoe Perches 		pr_err("instruction error\n");
2483a70491ccSJoe Perches 		pr_err("  INSTPM: 0x%08x\n", I915_READ(INSTPM));
2484050ee91fSBen Widawsky 		for (i = 0; i < ARRAY_SIZE(instdone); i++)
2485050ee91fSBen Widawsky 			pr_err("  INSTDONE_%d: 0x%08x\n", i, instdone[i]);
2486a6c45cf0SChris Wilson 		if (INTEL_INFO(dev)->gen < 4) {
248763eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR);
248863eeaf38SJesse Barnes 
2489a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR));
2490a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR));
2491a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD));
249263eeaf38SJesse Barnes 			I915_WRITE(IPEIR, ipeir);
24933143a2bfSChris Wilson 			POSTING_READ(IPEIR);
249463eeaf38SJesse Barnes 		} else {
249563eeaf38SJesse Barnes 			u32 ipeir = I915_READ(IPEIR_I965);
249663eeaf38SJesse Barnes 
2497a70491ccSJoe Perches 			pr_err("  IPEIR: 0x%08x\n", I915_READ(IPEIR_I965));
2498a70491ccSJoe Perches 			pr_err("  IPEHR: 0x%08x\n", I915_READ(IPEHR_I965));
2499a70491ccSJoe Perches 			pr_err("  INSTPS: 0x%08x\n", I915_READ(INSTPS));
2500a70491ccSJoe Perches 			pr_err("  ACTHD: 0x%08x\n", I915_READ(ACTHD_I965));
250163eeaf38SJesse Barnes 			I915_WRITE(IPEIR_I965, ipeir);
25023143a2bfSChris Wilson 			POSTING_READ(IPEIR_I965);
250363eeaf38SJesse Barnes 		}
250463eeaf38SJesse Barnes 	}
250563eeaf38SJesse Barnes 
250663eeaf38SJesse Barnes 	I915_WRITE(EIR, eir);
25073143a2bfSChris Wilson 	POSTING_READ(EIR);
250863eeaf38SJesse Barnes 	eir = I915_READ(EIR);
250963eeaf38SJesse Barnes 	if (eir) {
251063eeaf38SJesse Barnes 		/*
251163eeaf38SJesse Barnes 		 * some errors might have become stuck,
251263eeaf38SJesse Barnes 		 * mask them.
251363eeaf38SJesse Barnes 		 */
251463eeaf38SJesse Barnes 		DRM_ERROR("EIR stuck: 0x%08x, masking\n", eir);
251563eeaf38SJesse Barnes 		I915_WRITE(EMR, I915_READ(EMR) | eir);
251663eeaf38SJesse Barnes 		I915_WRITE(IIR, I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
251763eeaf38SJesse Barnes 	}
251835aed2e6SChris Wilson }
251935aed2e6SChris Wilson 
252035aed2e6SChris Wilson /**
252135aed2e6SChris Wilson  * i915_handle_error - handle an error interrupt
252235aed2e6SChris Wilson  * @dev: drm device
252335aed2e6SChris Wilson  *
252435aed2e6SChris Wilson  * Do some basic checking of regsiter state at error interrupt time and
252535aed2e6SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
252635aed2e6SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
252735aed2e6SChris Wilson  * so userspace knows something bad happened (should trigger collection
252835aed2e6SChris Wilson  * of a ring dump etc.).
252935aed2e6SChris Wilson  */
253058174462SMika Kuoppala void i915_handle_error(struct drm_device *dev, bool wedged,
253158174462SMika Kuoppala 		       const char *fmt, ...)
253235aed2e6SChris Wilson {
253335aed2e6SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
253458174462SMika Kuoppala 	va_list args;
253558174462SMika Kuoppala 	char error_msg[80];
253635aed2e6SChris Wilson 
253758174462SMika Kuoppala 	va_start(args, fmt);
253858174462SMika Kuoppala 	vscnprintf(error_msg, sizeof(error_msg), fmt, args);
253958174462SMika Kuoppala 	va_end(args);
254058174462SMika Kuoppala 
254158174462SMika Kuoppala 	i915_capture_error_state(dev, wedged, error_msg);
254235aed2e6SChris Wilson 	i915_report_and_clear_eir(dev);
25438a905236SJesse Barnes 
2544ba1234d1SBen Gamari 	if (wedged) {
2545f69061beSDaniel Vetter 		atomic_set_mask(I915_RESET_IN_PROGRESS_FLAG,
2546f69061beSDaniel Vetter 				&dev_priv->gpu_error.reset_counter);
2547ba1234d1SBen Gamari 
254811ed50ecSBen Gamari 		/*
254917e1df07SDaniel Vetter 		 * Wakeup waiting processes so that the reset work function
255017e1df07SDaniel Vetter 		 * i915_error_work_func doesn't deadlock trying to grab various
255117e1df07SDaniel Vetter 		 * locks. By bumping the reset counter first, the woken
255217e1df07SDaniel Vetter 		 * processes will see a reset in progress and back off,
255317e1df07SDaniel Vetter 		 * releasing their locks and then wait for the reset completion.
255417e1df07SDaniel Vetter 		 * We must do this for _all_ gpu waiters that might hold locks
255517e1df07SDaniel Vetter 		 * that the reset work needs to acquire.
255617e1df07SDaniel Vetter 		 *
255717e1df07SDaniel Vetter 		 * Note: The wake_up serves as the required memory barrier to
255817e1df07SDaniel Vetter 		 * ensure that the waiters see the updated value of the reset
255917e1df07SDaniel Vetter 		 * counter atomic_t.
256011ed50ecSBen Gamari 		 */
256117e1df07SDaniel Vetter 		i915_error_wake_up(dev_priv, false);
256211ed50ecSBen Gamari 	}
256311ed50ecSBen Gamari 
2564122f46baSDaniel Vetter 	/*
2565122f46baSDaniel Vetter 	 * Our reset work can grab modeset locks (since it needs to reset the
2566122f46baSDaniel Vetter 	 * state of outstanding pagelips). Hence it must not be run on our own
2567122f46baSDaniel Vetter 	 * dev-priv->wq work queue for otherwise the flush_work in the pageflip
2568122f46baSDaniel Vetter 	 * code will deadlock.
2569122f46baSDaniel Vetter 	 */
2570122f46baSDaniel Vetter 	schedule_work(&dev_priv->gpu_error.work);
25718a905236SJesse Barnes }
25728a905236SJesse Barnes 
257321ad8330SVille Syrjälä static void __always_unused i915_pageflip_stall_check(struct drm_device *dev, int pipe)
25744e5359cdSSimon Farnsworth {
25752d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
25764e5359cdSSimon Farnsworth 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
25774e5359cdSSimon Farnsworth 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
257805394f39SChris Wilson 	struct drm_i915_gem_object *obj;
25794e5359cdSSimon Farnsworth 	struct intel_unpin_work *work;
25804e5359cdSSimon Farnsworth 	unsigned long flags;
25814e5359cdSSimon Farnsworth 	bool stall_detected;
25824e5359cdSSimon Farnsworth 
25834e5359cdSSimon Farnsworth 	/* Ignore early vblank irqs */
25844e5359cdSSimon Farnsworth 	if (intel_crtc == NULL)
25854e5359cdSSimon Farnsworth 		return;
25864e5359cdSSimon Farnsworth 
25874e5359cdSSimon Farnsworth 	spin_lock_irqsave(&dev->event_lock, flags);
25884e5359cdSSimon Farnsworth 	work = intel_crtc->unpin_work;
25894e5359cdSSimon Farnsworth 
2590e7d841caSChris Wilson 	if (work == NULL ||
2591e7d841caSChris Wilson 	    atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE ||
2592e7d841caSChris Wilson 	    !work->enable_stall_check) {
25934e5359cdSSimon Farnsworth 		/* Either the pending flip IRQ arrived, or we're too early. Don't check */
25944e5359cdSSimon Farnsworth 		spin_unlock_irqrestore(&dev->event_lock, flags);
25954e5359cdSSimon Farnsworth 		return;
25964e5359cdSSimon Farnsworth 	}
25974e5359cdSSimon Farnsworth 
25984e5359cdSSimon Farnsworth 	/* Potential stall - if we see that the flip has happened, assume a missed interrupt */
259905394f39SChris Wilson 	obj = work->pending_flip_obj;
2600a6c45cf0SChris Wilson 	if (INTEL_INFO(dev)->gen >= 4) {
26019db4a9c7SJesse Barnes 		int dspsurf = DSPSURF(intel_crtc->plane);
2602446f2545SArmin Reese 		stall_detected = I915_HI_DISPBASE(I915_READ(dspsurf)) ==
2603f343c5f6SBen Widawsky 					i915_gem_obj_ggtt_offset(obj);
26044e5359cdSSimon Farnsworth 	} else {
26059db4a9c7SJesse Barnes 		int dspaddr = DSPADDR(intel_crtc->plane);
2606f343c5f6SBen Widawsky 		stall_detected = I915_READ(dspaddr) == (i915_gem_obj_ggtt_offset(obj) +
2607f4510a27SMatt Roper 							crtc->y * crtc->primary->fb->pitches[0] +
2608f4510a27SMatt Roper 							crtc->x * crtc->primary->fb->bits_per_pixel/8);
26094e5359cdSSimon Farnsworth 	}
26104e5359cdSSimon Farnsworth 
26114e5359cdSSimon Farnsworth 	spin_unlock_irqrestore(&dev->event_lock, flags);
26124e5359cdSSimon Farnsworth 
26134e5359cdSSimon Farnsworth 	if (stall_detected) {
26144e5359cdSSimon Farnsworth 		DRM_DEBUG_DRIVER("Pageflip stall detected\n");
26154e5359cdSSimon Farnsworth 		intel_prepare_page_flip(dev, intel_crtc->plane);
26164e5359cdSSimon Farnsworth 	}
26174e5359cdSSimon Farnsworth }
26184e5359cdSSimon Farnsworth 
261942f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
262042f52ef8SKeith Packard  * we use as a pipe index
262142f52ef8SKeith Packard  */
2622f71d4af4SJesse Barnes static int i915_enable_vblank(struct drm_device *dev, int pipe)
26230a3e67a4SJesse Barnes {
26242d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2625e9d21d7fSKeith Packard 	unsigned long irqflags;
262671e0ffa5SJesse Barnes 
26275eddb70bSChris Wilson 	if (!i915_pipe_enabled(dev, pipe))
262871e0ffa5SJesse Barnes 		return -EINVAL;
26290a3e67a4SJesse Barnes 
26301ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2631f796cf8fSJesse Barnes 	if (INTEL_INFO(dev)->gen >= 4)
26327c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2633755e9019SImre Deak 				     PIPE_START_VBLANK_INTERRUPT_STATUS);
26340a3e67a4SJesse Barnes 	else
26357c463586SKeith Packard 		i915_enable_pipestat(dev_priv, pipe,
2636755e9019SImre Deak 				     PIPE_VBLANK_INTERRUPT_STATUS);
26378692d00eSChris Wilson 
26388692d00eSChris Wilson 	/* maintain vblank delivery even in deep C-states */
26393d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
26406b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_DISABLE(INSTPM_AGPBUSY_DIS));
26411ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26428692d00eSChris Wilson 
26430a3e67a4SJesse Barnes 	return 0;
26440a3e67a4SJesse Barnes }
26450a3e67a4SJesse Barnes 
2646f71d4af4SJesse Barnes static int ironlake_enable_vblank(struct drm_device *dev, int pipe)
2647f796cf8fSJesse Barnes {
26482d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2649f796cf8fSJesse Barnes 	unsigned long irqflags;
2650b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
265140da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2652f796cf8fSJesse Barnes 
2653f796cf8fSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
2654f796cf8fSJesse Barnes 		return -EINVAL;
2655f796cf8fSJesse Barnes 
2656f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2657b518421fSPaulo Zanoni 	ironlake_enable_display_irq(dev_priv, bit);
2658b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2659b1f14ad0SJesse Barnes 
2660b1f14ad0SJesse Barnes 	return 0;
2661b1f14ad0SJesse Barnes }
2662b1f14ad0SJesse Barnes 
26637e231dbeSJesse Barnes static int valleyview_enable_vblank(struct drm_device *dev, int pipe)
26647e231dbeSJesse Barnes {
26652d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
26667e231dbeSJesse Barnes 	unsigned long irqflags;
26677e231dbeSJesse Barnes 
26687e231dbeSJesse Barnes 	if (!i915_pipe_enabled(dev, pipe))
26697e231dbeSJesse Barnes 		return -EINVAL;
26707e231dbeSJesse Barnes 
26717e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
267231acc7f5SJesse Barnes 	i915_enable_pipestat(dev_priv, pipe,
2673755e9019SImre Deak 			     PIPE_START_VBLANK_INTERRUPT_STATUS);
26747e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
26757e231dbeSJesse Barnes 
26767e231dbeSJesse Barnes 	return 0;
26777e231dbeSJesse Barnes }
26787e231dbeSJesse Barnes 
2679abd58f01SBen Widawsky static int gen8_enable_vblank(struct drm_device *dev, int pipe)
2680abd58f01SBen Widawsky {
2681abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2682abd58f01SBen Widawsky 	unsigned long irqflags;
2683abd58f01SBen Widawsky 
2684abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2685abd58f01SBen Widawsky 		return -EINVAL;
2686abd58f01SBen Widawsky 
2687abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
26887167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] &= ~GEN8_PIPE_VBLANK;
26897167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2690abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2691abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2692abd58f01SBen Widawsky 	return 0;
2693abd58f01SBen Widawsky }
2694abd58f01SBen Widawsky 
269542f52ef8SKeith Packard /* Called from drm generic code, passed 'crtc' which
269642f52ef8SKeith Packard  * we use as a pipe index
269742f52ef8SKeith Packard  */
2698f71d4af4SJesse Barnes static void i915_disable_vblank(struct drm_device *dev, int pipe)
26990a3e67a4SJesse Barnes {
27002d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2701e9d21d7fSKeith Packard 	unsigned long irqflags;
27020a3e67a4SJesse Barnes 
27031ec14ad3SChris Wilson 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27043d13ef2eSDamien Lespiau 	if (INTEL_INFO(dev)->gen == 3)
27056b26c86dSDaniel Vetter 		I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_DIS));
27068692d00eSChris Wilson 
27077c463586SKeith Packard 	i915_disable_pipestat(dev_priv, pipe,
2708755e9019SImre Deak 			      PIPE_VBLANK_INTERRUPT_STATUS |
2709755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27101ec14ad3SChris Wilson 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27110a3e67a4SJesse Barnes }
27120a3e67a4SJesse Barnes 
2713f71d4af4SJesse Barnes static void ironlake_disable_vblank(struct drm_device *dev, int pipe)
2714f796cf8fSJesse Barnes {
27152d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2716f796cf8fSJesse Barnes 	unsigned long irqflags;
2717b518421fSPaulo Zanoni 	uint32_t bit = (INTEL_INFO(dev)->gen >= 7) ? DE_PIPE_VBLANK_IVB(pipe) :
271840da17c2SDaniel Vetter 						     DE_PIPE_VBLANK(pipe);
2719f796cf8fSJesse Barnes 
2720f796cf8fSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
2721b518421fSPaulo Zanoni 	ironlake_disable_display_irq(dev_priv, bit);
2722b1f14ad0SJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2723b1f14ad0SJesse Barnes }
2724b1f14ad0SJesse Barnes 
27257e231dbeSJesse Barnes static void valleyview_disable_vblank(struct drm_device *dev, int pipe)
27267e231dbeSJesse Barnes {
27272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
27287e231dbeSJesse Barnes 	unsigned long irqflags;
27297e231dbeSJesse Barnes 
27307e231dbeSJesse Barnes 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
273131acc7f5SJesse Barnes 	i915_disable_pipestat(dev_priv, pipe,
2732755e9019SImre Deak 			      PIPE_START_VBLANK_INTERRUPT_STATUS);
27337e231dbeSJesse Barnes 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
27347e231dbeSJesse Barnes }
27357e231dbeSJesse Barnes 
2736abd58f01SBen Widawsky static void gen8_disable_vblank(struct drm_device *dev, int pipe)
2737abd58f01SBen Widawsky {
2738abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
2739abd58f01SBen Widawsky 	unsigned long irqflags;
2740abd58f01SBen Widawsky 
2741abd58f01SBen Widawsky 	if (!i915_pipe_enabled(dev, pipe))
2742abd58f01SBen Widawsky 		return;
2743abd58f01SBen Widawsky 
2744abd58f01SBen Widawsky 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
27457167d7c6SDaniel Vetter 	dev_priv->de_irq_mask[pipe] |= GEN8_PIPE_VBLANK;
27467167d7c6SDaniel Vetter 	I915_WRITE(GEN8_DE_PIPE_IMR(pipe), dev_priv->de_irq_mask[pipe]);
2747abd58f01SBen Widawsky 	POSTING_READ(GEN8_DE_PIPE_IMR(pipe));
2748abd58f01SBen Widawsky 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
2749abd58f01SBen Widawsky }
2750abd58f01SBen Widawsky 
2751893eead0SChris Wilson static u32
2752*a4872ba6SOscar Mateo ring_last_seqno(struct intel_engine_cs *ring)
2753852835f3SZou Nan hai {
2754893eead0SChris Wilson 	return list_entry(ring->request_list.prev,
2755893eead0SChris Wilson 			  struct drm_i915_gem_request, list)->seqno;
2756893eead0SChris Wilson }
2757893eead0SChris Wilson 
27589107e9d2SChris Wilson static bool
2759*a4872ba6SOscar Mateo ring_idle(struct intel_engine_cs *ring, u32 seqno)
2760893eead0SChris Wilson {
27619107e9d2SChris Wilson 	return (list_empty(&ring->request_list) ||
27629107e9d2SChris Wilson 		i915_seqno_passed(seqno, ring_last_seqno(ring)));
2763f65d9421SBen Gamari }
2764f65d9421SBen Gamari 
2765a028c4b0SDaniel Vetter static bool
2766a028c4b0SDaniel Vetter ipehr_is_semaphore_wait(struct drm_device *dev, u32 ipehr)
2767a028c4b0SDaniel Vetter {
2768a028c4b0SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 8) {
2769a028c4b0SDaniel Vetter 		/*
2770a028c4b0SDaniel Vetter 		 * FIXME: gen8 semaphore support - currently we don't emit
2771a028c4b0SDaniel Vetter 		 * semaphores on bdw anyway, but this needs to be addressed when
2772a028c4b0SDaniel Vetter 		 * we merge that code.
2773a028c4b0SDaniel Vetter 		 */
2774a028c4b0SDaniel Vetter 		return false;
2775a028c4b0SDaniel Vetter 	} else {
2776a028c4b0SDaniel Vetter 		ipehr &= ~MI_SEMAPHORE_SYNC_MASK;
2777a028c4b0SDaniel Vetter 		return ipehr == (MI_SEMAPHORE_MBOX | MI_SEMAPHORE_COMPARE |
2778a028c4b0SDaniel Vetter 				 MI_SEMAPHORE_REGISTER);
2779a028c4b0SDaniel Vetter 	}
2780a028c4b0SDaniel Vetter }
2781a028c4b0SDaniel Vetter 
2782*a4872ba6SOscar Mateo static struct intel_engine_cs *
2783*a4872ba6SOscar Mateo semaphore_wait_to_signaller_ring(struct intel_engine_cs *ring, u32 ipehr)
2784921d42eaSDaniel Vetter {
2785921d42eaSDaniel Vetter 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2786*a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
2787921d42eaSDaniel Vetter 	int i;
2788921d42eaSDaniel Vetter 
2789921d42eaSDaniel Vetter 	if (INTEL_INFO(dev_priv->dev)->gen >= 8) {
2790921d42eaSDaniel Vetter 		/*
2791921d42eaSDaniel Vetter 		 * FIXME: gen8 semaphore support - currently we don't emit
2792921d42eaSDaniel Vetter 		 * semaphores on bdw anyway, but this needs to be addressed when
2793921d42eaSDaniel Vetter 		 * we merge that code.
2794921d42eaSDaniel Vetter 		 */
2795921d42eaSDaniel Vetter 		return NULL;
2796921d42eaSDaniel Vetter 	} else {
2797921d42eaSDaniel Vetter 		u32 sync_bits = ipehr & MI_SEMAPHORE_SYNC_MASK;
2798921d42eaSDaniel Vetter 
2799921d42eaSDaniel Vetter 		for_each_ring(signaller, dev_priv, i) {
2800921d42eaSDaniel Vetter 			if(ring == signaller)
2801921d42eaSDaniel Vetter 				continue;
2802921d42eaSDaniel Vetter 
2803ebc348b2SBen Widawsky 			if (sync_bits == signaller->semaphore.mbox.wait[ring->id])
2804921d42eaSDaniel Vetter 				return signaller;
2805921d42eaSDaniel Vetter 		}
2806921d42eaSDaniel Vetter 	}
2807921d42eaSDaniel Vetter 
2808921d42eaSDaniel Vetter 	DRM_ERROR("No signaller ring found for ring %i, ipehr 0x%08x\n",
2809921d42eaSDaniel Vetter 		  ring->id, ipehr);
2810921d42eaSDaniel Vetter 
2811921d42eaSDaniel Vetter 	return NULL;
2812921d42eaSDaniel Vetter }
2813921d42eaSDaniel Vetter 
2814*a4872ba6SOscar Mateo static struct intel_engine_cs *
2815*a4872ba6SOscar Mateo semaphore_waits_for(struct intel_engine_cs *ring, u32 *seqno)
2816a24a11e6SChris Wilson {
2817a24a11e6SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
281888fe429dSDaniel Vetter 	u32 cmd, ipehr, head;
281988fe429dSDaniel Vetter 	int i;
2820a24a11e6SChris Wilson 
2821a24a11e6SChris Wilson 	ipehr = I915_READ(RING_IPEHR(ring->mmio_base));
2822a028c4b0SDaniel Vetter 	if (!ipehr_is_semaphore_wait(ring->dev, ipehr))
28236274f212SChris Wilson 		return NULL;
2824a24a11e6SChris Wilson 
282588fe429dSDaniel Vetter 	/*
282688fe429dSDaniel Vetter 	 * HEAD is likely pointing to the dword after the actual command,
282788fe429dSDaniel Vetter 	 * so scan backwards until we find the MBOX. But limit it to just 3
282888fe429dSDaniel Vetter 	 * dwords. Note that we don't care about ACTHD here since that might
282988fe429dSDaniel Vetter 	 * point at at batch, and semaphores are always emitted into the
283088fe429dSDaniel Vetter 	 * ringbuffer itself.
2831a24a11e6SChris Wilson 	 */
283288fe429dSDaniel Vetter 	head = I915_READ_HEAD(ring) & HEAD_ADDR;
283388fe429dSDaniel Vetter 
283488fe429dSDaniel Vetter 	for (i = 4; i; --i) {
283588fe429dSDaniel Vetter 		/*
283688fe429dSDaniel Vetter 		 * Be paranoid and presume the hw has gone off into the wild -
283788fe429dSDaniel Vetter 		 * our ring is smaller than what the hardware (and hence
283888fe429dSDaniel Vetter 		 * HEAD_ADDR) allows. Also handles wrap-around.
283988fe429dSDaniel Vetter 		 */
284088fe429dSDaniel Vetter 		head &= ring->size - 1;
284188fe429dSDaniel Vetter 
284288fe429dSDaniel Vetter 		/* This here seems to blow up */
284388fe429dSDaniel Vetter 		cmd = ioread32(ring->virtual_start + head);
2844a24a11e6SChris Wilson 		if (cmd == ipehr)
2845a24a11e6SChris Wilson 			break;
2846a24a11e6SChris Wilson 
284788fe429dSDaniel Vetter 		head -= 4;
284888fe429dSDaniel Vetter 	}
2849a24a11e6SChris Wilson 
285088fe429dSDaniel Vetter 	if (!i)
285188fe429dSDaniel Vetter 		return NULL;
285288fe429dSDaniel Vetter 
285388fe429dSDaniel Vetter 	*seqno = ioread32(ring->virtual_start + head + 4) + 1;
2854921d42eaSDaniel Vetter 	return semaphore_wait_to_signaller_ring(ring, ipehr);
2855a24a11e6SChris Wilson }
2856a24a11e6SChris Wilson 
2857*a4872ba6SOscar Mateo static int semaphore_passed(struct intel_engine_cs *ring)
28586274f212SChris Wilson {
28596274f212SChris Wilson 	struct drm_i915_private *dev_priv = ring->dev->dev_private;
2860*a4872ba6SOscar Mateo 	struct intel_engine_cs *signaller;
28616274f212SChris Wilson 	u32 seqno, ctl;
28626274f212SChris Wilson 
28636274f212SChris Wilson 	ring->hangcheck.deadlock = true;
28646274f212SChris Wilson 
28656274f212SChris Wilson 	signaller = semaphore_waits_for(ring, &seqno);
28666274f212SChris Wilson 	if (signaller == NULL || signaller->hangcheck.deadlock)
28676274f212SChris Wilson 		return -1;
28686274f212SChris Wilson 
28696274f212SChris Wilson 	/* cursory check for an unkickable deadlock */
28706274f212SChris Wilson 	ctl = I915_READ_CTL(signaller);
28716274f212SChris Wilson 	if (ctl & RING_WAIT_SEMAPHORE && semaphore_passed(signaller) < 0)
28726274f212SChris Wilson 		return -1;
28736274f212SChris Wilson 
28746274f212SChris Wilson 	return i915_seqno_passed(signaller->get_seqno(signaller, false), seqno);
28756274f212SChris Wilson }
28766274f212SChris Wilson 
28776274f212SChris Wilson static void semaphore_clear_deadlocks(struct drm_i915_private *dev_priv)
28786274f212SChris Wilson {
2879*a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
28806274f212SChris Wilson 	int i;
28816274f212SChris Wilson 
28826274f212SChris Wilson 	for_each_ring(ring, dev_priv, i)
28836274f212SChris Wilson 		ring->hangcheck.deadlock = false;
28846274f212SChris Wilson }
28856274f212SChris Wilson 
2886ad8beaeaSMika Kuoppala static enum intel_ring_hangcheck_action
2887*a4872ba6SOscar Mateo ring_stuck(struct intel_engine_cs *ring, u64 acthd)
28881ec14ad3SChris Wilson {
28891ec14ad3SChris Wilson 	struct drm_device *dev = ring->dev;
28901ec14ad3SChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
28919107e9d2SChris Wilson 	u32 tmp;
28929107e9d2SChris Wilson 
28936274f212SChris Wilson 	if (ring->hangcheck.acthd != acthd)
2894f2f4d82fSJani Nikula 		return HANGCHECK_ACTIVE;
28956274f212SChris Wilson 
28969107e9d2SChris Wilson 	if (IS_GEN2(dev))
2897f2f4d82fSJani Nikula 		return HANGCHECK_HUNG;
28989107e9d2SChris Wilson 
28999107e9d2SChris Wilson 	/* Is the chip hanging on a WAIT_FOR_EVENT?
29009107e9d2SChris Wilson 	 * If so we can simply poke the RB_WAIT bit
29019107e9d2SChris Wilson 	 * and break the hang. This should work on
29029107e9d2SChris Wilson 	 * all but the second generation chipsets.
29039107e9d2SChris Wilson 	 */
29049107e9d2SChris Wilson 	tmp = I915_READ_CTL(ring);
29051ec14ad3SChris Wilson 	if (tmp & RING_WAIT) {
290658174462SMika Kuoppala 		i915_handle_error(dev, false,
290758174462SMika Kuoppala 				  "Kicking stuck wait on %s",
29081ec14ad3SChris Wilson 				  ring->name);
29091ec14ad3SChris Wilson 		I915_WRITE_CTL(ring, tmp);
2910f2f4d82fSJani Nikula 		return HANGCHECK_KICK;
29111ec14ad3SChris Wilson 	}
2912a24a11e6SChris Wilson 
29136274f212SChris Wilson 	if (INTEL_INFO(dev)->gen >= 6 && tmp & RING_WAIT_SEMAPHORE) {
29146274f212SChris Wilson 		switch (semaphore_passed(ring)) {
29156274f212SChris Wilson 		default:
2916f2f4d82fSJani Nikula 			return HANGCHECK_HUNG;
29176274f212SChris Wilson 		case 1:
291858174462SMika Kuoppala 			i915_handle_error(dev, false,
291958174462SMika Kuoppala 					  "Kicking stuck semaphore on %s",
2920a24a11e6SChris Wilson 					  ring->name);
2921a24a11e6SChris Wilson 			I915_WRITE_CTL(ring, tmp);
2922f2f4d82fSJani Nikula 			return HANGCHECK_KICK;
29236274f212SChris Wilson 		case 0:
2924f2f4d82fSJani Nikula 			return HANGCHECK_WAIT;
29256274f212SChris Wilson 		}
29269107e9d2SChris Wilson 	}
29279107e9d2SChris Wilson 
2928f2f4d82fSJani Nikula 	return HANGCHECK_HUNG;
2929a24a11e6SChris Wilson }
2930d1e61e7fSChris Wilson 
2931f65d9421SBen Gamari /**
2932f65d9421SBen Gamari  * This is called when the chip hasn't reported back with completed
293305407ff8SMika Kuoppala  * batchbuffers in a long time. We keep track per ring seqno progress and
293405407ff8SMika Kuoppala  * if there are no progress, hangcheck score for that ring is increased.
293505407ff8SMika Kuoppala  * Further, acthd is inspected to see if the ring is stuck. On stuck case
293605407ff8SMika Kuoppala  * we kick the ring. If we see no progress on three subsequent calls
293705407ff8SMika Kuoppala  * we assume chip is wedged and try to fix it by resetting the chip.
2938f65d9421SBen Gamari  */
2939a658b5d2SDamien Lespiau static void i915_hangcheck_elapsed(unsigned long data)
2940f65d9421SBen Gamari {
2941f65d9421SBen Gamari 	struct drm_device *dev = (struct drm_device *)data;
29422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
2943*a4872ba6SOscar Mateo 	struct intel_engine_cs *ring;
2944b4519513SChris Wilson 	int i;
294505407ff8SMika Kuoppala 	int busy_count = 0, rings_hung = 0;
29469107e9d2SChris Wilson 	bool stuck[I915_NUM_RINGS] = { 0 };
29479107e9d2SChris Wilson #define BUSY 1
29489107e9d2SChris Wilson #define KICK 5
29499107e9d2SChris Wilson #define HUNG 20
2950893eead0SChris Wilson 
2951d330a953SJani Nikula 	if (!i915.enable_hangcheck)
29523e0dc6b0SBen Widawsky 		return;
29533e0dc6b0SBen Widawsky 
2954b4519513SChris Wilson 	for_each_ring(ring, dev_priv, i) {
295550877445SChris Wilson 		u64 acthd;
295650877445SChris Wilson 		u32 seqno;
29579107e9d2SChris Wilson 		bool busy = true;
2958b4519513SChris Wilson 
29596274f212SChris Wilson 		semaphore_clear_deadlocks(dev_priv);
29606274f212SChris Wilson 
296105407ff8SMika Kuoppala 		seqno = ring->get_seqno(ring, false);
296205407ff8SMika Kuoppala 		acthd = intel_ring_get_active_head(ring);
296305407ff8SMika Kuoppala 
296405407ff8SMika Kuoppala 		if (ring->hangcheck.seqno == seqno) {
29659107e9d2SChris Wilson 			if (ring_idle(ring, seqno)) {
2966da661464SMika Kuoppala 				ring->hangcheck.action = HANGCHECK_IDLE;
2967da661464SMika Kuoppala 
29689107e9d2SChris Wilson 				if (waitqueue_active(&ring->irq_queue)) {
29699107e9d2SChris Wilson 					/* Issue a wake-up to catch stuck h/w. */
2970094f9a54SChris Wilson 					if (!test_and_set_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings)) {
2971f4adcd24SDaniel Vetter 						if (!(dev_priv->gpu_error.test_irq_rings & intel_ring_flag(ring)))
29729107e9d2SChris Wilson 							DRM_ERROR("Hangcheck timer elapsed... %s idle\n",
29739107e9d2SChris Wilson 								  ring->name);
2974f4adcd24SDaniel Vetter 						else
2975f4adcd24SDaniel Vetter 							DRM_INFO("Fake missed irq on %s\n",
2976f4adcd24SDaniel Vetter 								 ring->name);
29779107e9d2SChris Wilson 						wake_up_all(&ring->irq_queue);
2978094f9a54SChris Wilson 					}
2979094f9a54SChris Wilson 					/* Safeguard against driver failure */
2980094f9a54SChris Wilson 					ring->hangcheck.score += BUSY;
29819107e9d2SChris Wilson 				} else
29829107e9d2SChris Wilson 					busy = false;
298305407ff8SMika Kuoppala 			} else {
29846274f212SChris Wilson 				/* We always increment the hangcheck score
29856274f212SChris Wilson 				 * if the ring is busy and still processing
29866274f212SChris Wilson 				 * the same request, so that no single request
29876274f212SChris Wilson 				 * can run indefinitely (such as a chain of
29886274f212SChris Wilson 				 * batches). The only time we do not increment
29896274f212SChris Wilson 				 * the hangcheck score on this ring, if this
29906274f212SChris Wilson 				 * ring is in a legitimate wait for another
29916274f212SChris Wilson 				 * ring. In that case the waiting ring is a
29926274f212SChris Wilson 				 * victim and we want to be sure we catch the
29936274f212SChris Wilson 				 * right culprit. Then every time we do kick
29946274f212SChris Wilson 				 * the ring, add a small increment to the
29956274f212SChris Wilson 				 * score so that we can catch a batch that is
29966274f212SChris Wilson 				 * being repeatedly kicked and so responsible
29976274f212SChris Wilson 				 * for stalling the machine.
29989107e9d2SChris Wilson 				 */
2999ad8beaeaSMika Kuoppala 				ring->hangcheck.action = ring_stuck(ring,
3000ad8beaeaSMika Kuoppala 								    acthd);
3001ad8beaeaSMika Kuoppala 
3002ad8beaeaSMika Kuoppala 				switch (ring->hangcheck.action) {
3003da661464SMika Kuoppala 				case HANGCHECK_IDLE:
3004f2f4d82fSJani Nikula 				case HANGCHECK_WAIT:
30056274f212SChris Wilson 					break;
3006f2f4d82fSJani Nikula 				case HANGCHECK_ACTIVE:
3007ea04cb31SJani Nikula 					ring->hangcheck.score += BUSY;
30086274f212SChris Wilson 					break;
3009f2f4d82fSJani Nikula 				case HANGCHECK_KICK:
3010ea04cb31SJani Nikula 					ring->hangcheck.score += KICK;
30116274f212SChris Wilson 					break;
3012f2f4d82fSJani Nikula 				case HANGCHECK_HUNG:
3013ea04cb31SJani Nikula 					ring->hangcheck.score += HUNG;
30146274f212SChris Wilson 					stuck[i] = true;
30156274f212SChris Wilson 					break;
30166274f212SChris Wilson 				}
301705407ff8SMika Kuoppala 			}
30189107e9d2SChris Wilson 		} else {
3019da661464SMika Kuoppala 			ring->hangcheck.action = HANGCHECK_ACTIVE;
3020da661464SMika Kuoppala 
30219107e9d2SChris Wilson 			/* Gradually reduce the count so that we catch DoS
30229107e9d2SChris Wilson 			 * attempts across multiple batches.
30239107e9d2SChris Wilson 			 */
30249107e9d2SChris Wilson 			if (ring->hangcheck.score > 0)
30259107e9d2SChris Wilson 				ring->hangcheck.score--;
3026cbb465e7SChris Wilson 		}
3027f65d9421SBen Gamari 
302805407ff8SMika Kuoppala 		ring->hangcheck.seqno = seqno;
302905407ff8SMika Kuoppala 		ring->hangcheck.acthd = acthd;
30309107e9d2SChris Wilson 		busy_count += busy;
303105407ff8SMika Kuoppala 	}
303205407ff8SMika Kuoppala 
303305407ff8SMika Kuoppala 	for_each_ring(ring, dev_priv, i) {
3034b6b0fac0SMika Kuoppala 		if (ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG) {
3035b8d88d1dSDaniel Vetter 			DRM_INFO("%s on %s\n",
303605407ff8SMika Kuoppala 				 stuck[i] ? "stuck" : "no progress",
3037a43adf07SChris Wilson 				 ring->name);
3038a43adf07SChris Wilson 			rings_hung++;
303905407ff8SMika Kuoppala 		}
304005407ff8SMika Kuoppala 	}
304105407ff8SMika Kuoppala 
304205407ff8SMika Kuoppala 	if (rings_hung)
304358174462SMika Kuoppala 		return i915_handle_error(dev, true, "Ring hung");
304405407ff8SMika Kuoppala 
304505407ff8SMika Kuoppala 	if (busy_count)
304605407ff8SMika Kuoppala 		/* Reset timer case chip hangs without another request
304705407ff8SMika Kuoppala 		 * being added */
304810cd45b6SMika Kuoppala 		i915_queue_hangcheck(dev);
304910cd45b6SMika Kuoppala }
305010cd45b6SMika Kuoppala 
305110cd45b6SMika Kuoppala void i915_queue_hangcheck(struct drm_device *dev)
305210cd45b6SMika Kuoppala {
305310cd45b6SMika Kuoppala 	struct drm_i915_private *dev_priv = dev->dev_private;
3054d330a953SJani Nikula 	if (!i915.enable_hangcheck)
305510cd45b6SMika Kuoppala 		return;
305610cd45b6SMika Kuoppala 
305799584db3SDaniel Vetter 	mod_timer(&dev_priv->gpu_error.hangcheck_timer,
305810cd45b6SMika Kuoppala 		  round_jiffies_up(jiffies + DRM_I915_HANGCHECK_JIFFIES));
3059f65d9421SBen Gamari }
3060f65d9421SBen Gamari 
30611c69eb42SPaulo Zanoni static void ibx_irq_reset(struct drm_device *dev)
306291738a95SPaulo Zanoni {
306391738a95SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
306491738a95SPaulo Zanoni 
306591738a95SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
306691738a95SPaulo Zanoni 		return;
306791738a95SPaulo Zanoni 
3068f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(SDE);
3069105b122eSPaulo Zanoni 
3070105b122eSPaulo Zanoni 	if (HAS_PCH_CPT(dev) || HAS_PCH_LPT(dev))
3071105b122eSPaulo Zanoni 		I915_WRITE(SERR_INT, 0xffffffff);
3072622364b6SPaulo Zanoni }
3073105b122eSPaulo Zanoni 
307491738a95SPaulo Zanoni /*
3075622364b6SPaulo Zanoni  * SDEIER is also touched by the interrupt handler to work around missed PCH
3076622364b6SPaulo Zanoni  * interrupts. Hence we can't update it after the interrupt handler is enabled -
3077622364b6SPaulo Zanoni  * instead we unconditionally enable all PCH interrupt sources here, but then
3078622364b6SPaulo Zanoni  * only unmask them as needed with SDEIMR.
3079622364b6SPaulo Zanoni  *
3080622364b6SPaulo Zanoni  * This function needs to be called before interrupts are enabled.
308191738a95SPaulo Zanoni  */
3082622364b6SPaulo Zanoni static void ibx_irq_pre_postinstall(struct drm_device *dev)
3083622364b6SPaulo Zanoni {
3084622364b6SPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
3085622364b6SPaulo Zanoni 
3086622364b6SPaulo Zanoni 	if (HAS_PCH_NOP(dev))
3087622364b6SPaulo Zanoni 		return;
3088622364b6SPaulo Zanoni 
3089622364b6SPaulo Zanoni 	WARN_ON(I915_READ(SDEIER) != 0);
309091738a95SPaulo Zanoni 	I915_WRITE(SDEIER, 0xffffffff);
309191738a95SPaulo Zanoni 	POSTING_READ(SDEIER);
309291738a95SPaulo Zanoni }
309391738a95SPaulo Zanoni 
30947c4d664eSPaulo Zanoni static void gen5_gt_irq_reset(struct drm_device *dev)
3095d18ea1b5SDaniel Vetter {
3096d18ea1b5SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
3097d18ea1b5SDaniel Vetter 
3098f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GT);
3099a9d356a6SPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 6)
3100f86f3fb0SPaulo Zanoni 		GEN5_IRQ_RESET(GEN6_PM);
3101d18ea1b5SDaniel Vetter }
3102d18ea1b5SDaniel Vetter 
3103c0e09200SDave Airlie /* drm_dma.h hooks
3104c0e09200SDave Airlie */
3105be30b29fSPaulo Zanoni static void ironlake_irq_reset(struct drm_device *dev)
3106036a4a7dSZhenyu Wang {
31072d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3108036a4a7dSZhenyu Wang 
31090c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xffffffff);
3110bdfcdb63SDaniel Vetter 
3111f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(DE);
3112c6d954c1SPaulo Zanoni 	if (IS_GEN7(dev))
3113c6d954c1SPaulo Zanoni 		I915_WRITE(GEN7_ERR_INT, 0xffffffff);
3114036a4a7dSZhenyu Wang 
31157c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
3116c650156aSZhenyu Wang 
31171c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
31187d99163dSBen Widawsky }
31197d99163dSBen Widawsky 
3120be30b29fSPaulo Zanoni static void ironlake_irq_preinstall(struct drm_device *dev)
3121be30b29fSPaulo Zanoni {
3122be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
31237d99163dSBen Widawsky }
31247d99163dSBen Widawsky 
31257e231dbeSJesse Barnes static void valleyview_irq_preinstall(struct drm_device *dev)
31267e231dbeSJesse Barnes {
31272d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
31287e231dbeSJesse Barnes 	int pipe;
31297e231dbeSJesse Barnes 
31307e231dbeSJesse Barnes 	/* VLV magic */
31317e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0);
31327e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(RENDER_RING_BASE), 0);
31337e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(GEN6_BSD_RING_BASE), 0);
31347e231dbeSJesse Barnes 	I915_WRITE(RING_IMR(BLT_RING_BASE), 0);
31357e231dbeSJesse Barnes 
31367e231dbeSJesse Barnes 	/* and GT */
31377e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
31387e231dbeSJesse Barnes 	I915_WRITE(GTIIR, I915_READ(GTIIR));
3139d18ea1b5SDaniel Vetter 
31407c4d664eSPaulo Zanoni 	gen5_gt_irq_reset(dev);
31417e231dbeSJesse Barnes 
31427e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, 0xff);
31437e231dbeSJesse Barnes 
31447e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
31457e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
31467e231dbeSJesse Barnes 	for_each_pipe(pipe)
31477e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
31487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
31497e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
31507e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
31517e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
31527e231dbeSJesse Barnes }
31537e231dbeSJesse Barnes 
3154823f6b38SPaulo Zanoni static void gen8_irq_reset(struct drm_device *dev)
3155abd58f01SBen Widawsky {
3156abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3157abd58f01SBen Widawsky 	int pipe;
3158abd58f01SBen Widawsky 
3159abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, 0);
3160abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3161abd58f01SBen Widawsky 
3162f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 0);
3163f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 1);
3164f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 2);
3165f86f3fb0SPaulo Zanoni 	GEN8_IRQ_RESET_NDX(GT, 3);
3166abd58f01SBen Widawsky 
3167823f6b38SPaulo Zanoni 	for_each_pipe(pipe)
3168f86f3fb0SPaulo Zanoni 		GEN8_IRQ_RESET_NDX(DE_PIPE, pipe);
3169abd58f01SBen Widawsky 
3170f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_PORT_);
3171f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_DE_MISC_);
3172f86f3fb0SPaulo Zanoni 	GEN5_IRQ_RESET(GEN8_PCU_);
3173abd58f01SBen Widawsky 
31741c69eb42SPaulo Zanoni 	ibx_irq_reset(dev);
3175abd58f01SBen Widawsky }
3176abd58f01SBen Widawsky 
3177823f6b38SPaulo Zanoni static void gen8_irq_preinstall(struct drm_device *dev)
3178823f6b38SPaulo Zanoni {
3179823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3180abd58f01SBen Widawsky }
3181abd58f01SBen Widawsky 
318243f328d7SVille Syrjälä static void cherryview_irq_preinstall(struct drm_device *dev)
318343f328d7SVille Syrjälä {
318443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
318543f328d7SVille Syrjälä 	int pipe;
318643f328d7SVille Syrjälä 
318743f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
318843f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
318943f328d7SVille Syrjälä 
319043f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 0);
319143f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 1);
319243f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 2);
319343f328d7SVille Syrjälä 	GEN8_IRQ_RESET_NDX(GT, 3);
319443f328d7SVille Syrjälä 
319543f328d7SVille Syrjälä 	GEN5_IRQ_RESET(GEN8_PCU_);
319643f328d7SVille Syrjälä 
319743f328d7SVille Syrjälä 	POSTING_READ(GEN8_PCU_IIR);
319843f328d7SVille Syrjälä 
319943f328d7SVille Syrjälä 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK_CHV);
320043f328d7SVille Syrjälä 
320143f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
320243f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
320343f328d7SVille Syrjälä 
320443f328d7SVille Syrjälä 	for_each_pipe(pipe)
320543f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
320643f328d7SVille Syrjälä 
320743f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
320843f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
320943f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
321043f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
321143f328d7SVille Syrjälä }
321243f328d7SVille Syrjälä 
321382a28bcfSDaniel Vetter static void ibx_hpd_irq_setup(struct drm_device *dev)
321482a28bcfSDaniel Vetter {
32152d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
321682a28bcfSDaniel Vetter 	struct drm_mode_config *mode_config = &dev->mode_config;
321782a28bcfSDaniel Vetter 	struct intel_encoder *intel_encoder;
3218fee884edSDaniel Vetter 	u32 hotplug_irqs, hotplug, enabled_irqs = 0;
321982a28bcfSDaniel Vetter 
322082a28bcfSDaniel Vetter 	if (HAS_PCH_IBX(dev)) {
3221fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK;
322282a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3223cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3224fee884edSDaniel Vetter 				enabled_irqs |= hpd_ibx[intel_encoder->hpd_pin];
322582a28bcfSDaniel Vetter 	} else {
3226fee884edSDaniel Vetter 		hotplug_irqs = SDE_HOTPLUG_MASK_CPT;
322782a28bcfSDaniel Vetter 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
3228cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
3229fee884edSDaniel Vetter 				enabled_irqs |= hpd_cpt[intel_encoder->hpd_pin];
323082a28bcfSDaniel Vetter 	}
323182a28bcfSDaniel Vetter 
3232fee884edSDaniel Vetter 	ibx_display_interrupt_update(dev_priv, hotplug_irqs, enabled_irqs);
323382a28bcfSDaniel Vetter 
32347fe0b973SKeith Packard 	/*
32357fe0b973SKeith Packard 	 * Enable digital hotplug on the PCH, and configure the DP short pulse
32367fe0b973SKeith Packard 	 * duration to 2ms (which is the minimum in the Display Port spec)
32377fe0b973SKeith Packard 	 *
32387fe0b973SKeith Packard 	 * This register is the same on all known PCH chips.
32397fe0b973SKeith Packard 	 */
32407fe0b973SKeith Packard 	hotplug = I915_READ(PCH_PORT_HOTPLUG);
32417fe0b973SKeith Packard 	hotplug &= ~(PORTD_PULSE_DURATION_MASK|PORTC_PULSE_DURATION_MASK|PORTB_PULSE_DURATION_MASK);
32427fe0b973SKeith Packard 	hotplug |= PORTD_HOTPLUG_ENABLE | PORTD_PULSE_DURATION_2ms;
32437fe0b973SKeith Packard 	hotplug |= PORTC_HOTPLUG_ENABLE | PORTC_PULSE_DURATION_2ms;
32447fe0b973SKeith Packard 	hotplug |= PORTB_HOTPLUG_ENABLE | PORTB_PULSE_DURATION_2ms;
32457fe0b973SKeith Packard 	I915_WRITE(PCH_PORT_HOTPLUG, hotplug);
32467fe0b973SKeith Packard }
32477fe0b973SKeith Packard 
3248d46da437SPaulo Zanoni static void ibx_irq_postinstall(struct drm_device *dev)
3249d46da437SPaulo Zanoni {
32502d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
325182a28bcfSDaniel Vetter 	u32 mask;
3252d46da437SPaulo Zanoni 
3253692a04cfSDaniel Vetter 	if (HAS_PCH_NOP(dev))
3254692a04cfSDaniel Vetter 		return;
3255692a04cfSDaniel Vetter 
3256105b122eSPaulo Zanoni 	if (HAS_PCH_IBX(dev))
32575c673b60SDaniel Vetter 		mask = SDE_GMBUS | SDE_AUX_MASK | SDE_POISON;
3258105b122eSPaulo Zanoni 	else
32595c673b60SDaniel Vetter 		mask = SDE_GMBUS_CPT | SDE_AUX_MASK_CPT;
32608664281bSPaulo Zanoni 
3261337ba017SPaulo Zanoni 	GEN5_ASSERT_IIR_IS_ZERO(SDEIIR);
3262d46da437SPaulo Zanoni 	I915_WRITE(SDEIMR, ~mask);
3263d46da437SPaulo Zanoni }
3264d46da437SPaulo Zanoni 
32650a9a8c91SDaniel Vetter static void gen5_gt_irq_postinstall(struct drm_device *dev)
32660a9a8c91SDaniel Vetter {
32670a9a8c91SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
32680a9a8c91SDaniel Vetter 	u32 pm_irqs, gt_irqs;
32690a9a8c91SDaniel Vetter 
32700a9a8c91SDaniel Vetter 	pm_irqs = gt_irqs = 0;
32710a9a8c91SDaniel Vetter 
32720a9a8c91SDaniel Vetter 	dev_priv->gt_irq_mask = ~0;
3273040d2baaSBen Widawsky 	if (HAS_L3_DPF(dev)) {
32740a9a8c91SDaniel Vetter 		/* L3 parity interrupt is always unmasked. */
327535a85ac6SBen Widawsky 		dev_priv->gt_irq_mask = ~GT_PARITY_ERROR(dev);
327635a85ac6SBen Widawsky 		gt_irqs |= GT_PARITY_ERROR(dev);
32770a9a8c91SDaniel Vetter 	}
32780a9a8c91SDaniel Vetter 
32790a9a8c91SDaniel Vetter 	gt_irqs |= GT_RENDER_USER_INTERRUPT;
32800a9a8c91SDaniel Vetter 	if (IS_GEN5(dev)) {
32810a9a8c91SDaniel Vetter 		gt_irqs |= GT_RENDER_PIPECTL_NOTIFY_INTERRUPT |
32820a9a8c91SDaniel Vetter 			   ILK_BSD_USER_INTERRUPT;
32830a9a8c91SDaniel Vetter 	} else {
32840a9a8c91SDaniel Vetter 		gt_irqs |= GT_BLT_USER_INTERRUPT | GT_BSD_USER_INTERRUPT;
32850a9a8c91SDaniel Vetter 	}
32860a9a8c91SDaniel Vetter 
328735079899SPaulo Zanoni 	GEN5_IRQ_INIT(GT, dev_priv->gt_irq_mask, gt_irqs);
32880a9a8c91SDaniel Vetter 
32890a9a8c91SDaniel Vetter 	if (INTEL_INFO(dev)->gen >= 6) {
3290a6706b45SDeepak S 		pm_irqs |= dev_priv->pm_rps_events;
32910a9a8c91SDaniel Vetter 
32920a9a8c91SDaniel Vetter 		if (HAS_VEBOX(dev))
32930a9a8c91SDaniel Vetter 			pm_irqs |= PM_VEBOX_USER_INTERRUPT;
32940a9a8c91SDaniel Vetter 
3295605cd25bSPaulo Zanoni 		dev_priv->pm_irq_mask = 0xffffffff;
329635079899SPaulo Zanoni 		GEN5_IRQ_INIT(GEN6_PM, dev_priv->pm_irq_mask, pm_irqs);
32970a9a8c91SDaniel Vetter 	}
32980a9a8c91SDaniel Vetter }
32990a9a8c91SDaniel Vetter 
3300f71d4af4SJesse Barnes static int ironlake_irq_postinstall(struct drm_device *dev)
3301036a4a7dSZhenyu Wang {
33024bc9d430SDaniel Vetter 	unsigned long irqflags;
33032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
33048e76f8dcSPaulo Zanoni 	u32 display_mask, extra_mask;
33058e76f8dcSPaulo Zanoni 
33068e76f8dcSPaulo Zanoni 	if (INTEL_INFO(dev)->gen >= 7) {
33078e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE_IVB |
33088e76f8dcSPaulo Zanoni 				DE_PCH_EVENT_IVB | DE_PLANEC_FLIP_DONE_IVB |
33098e76f8dcSPaulo Zanoni 				DE_PLANEB_FLIP_DONE_IVB |
33105c673b60SDaniel Vetter 				DE_PLANEA_FLIP_DONE_IVB | DE_AUX_CHANNEL_A_IVB);
33118e76f8dcSPaulo Zanoni 		extra_mask = (DE_PIPEC_VBLANK_IVB | DE_PIPEB_VBLANK_IVB |
33125c673b60SDaniel Vetter 			      DE_PIPEA_VBLANK_IVB | DE_ERR_INT_IVB);
33138e76f8dcSPaulo Zanoni 	} else {
33148e76f8dcSPaulo Zanoni 		display_mask = (DE_MASTER_IRQ_CONTROL | DE_GSE | DE_PCH_EVENT |
3315ce99c256SDaniel Vetter 				DE_PLANEA_FLIP_DONE | DE_PLANEB_FLIP_DONE |
33165b3a856bSDaniel Vetter 				DE_AUX_CHANNEL_A |
33175b3a856bSDaniel Vetter 				DE_PIPEB_CRC_DONE | DE_PIPEA_CRC_DONE |
33185b3a856bSDaniel Vetter 				DE_POISON);
33195c673b60SDaniel Vetter 		extra_mask = DE_PIPEA_VBLANK | DE_PIPEB_VBLANK | DE_PCU_EVENT |
33205c673b60SDaniel Vetter 				DE_PIPEB_FIFO_UNDERRUN | DE_PIPEA_FIFO_UNDERRUN;
33218e76f8dcSPaulo Zanoni 	}
3322036a4a7dSZhenyu Wang 
33231ec14ad3SChris Wilson 	dev_priv->irq_mask = ~display_mask;
3324036a4a7dSZhenyu Wang 
33250c841212SPaulo Zanoni 	I915_WRITE(HWSTAM, 0xeffe);
33260c841212SPaulo Zanoni 
3327622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3328622364b6SPaulo Zanoni 
332935079899SPaulo Zanoni 	GEN5_IRQ_INIT(DE, dev_priv->irq_mask, display_mask | extra_mask);
3330036a4a7dSZhenyu Wang 
33310a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
3332036a4a7dSZhenyu Wang 
3333d46da437SPaulo Zanoni 	ibx_irq_postinstall(dev);
33347fe0b973SKeith Packard 
3335f97108d1SJesse Barnes 	if (IS_IRONLAKE_M(dev)) {
33366005ce42SDaniel Vetter 		/* Enable PCU event interrupts
33376005ce42SDaniel Vetter 		 *
33386005ce42SDaniel Vetter 		 * spinlocking not required here for correctness since interrupt
33394bc9d430SDaniel Vetter 		 * setup is guaranteed to run in single-threaded context. But we
33404bc9d430SDaniel Vetter 		 * need it to make the assert_spin_locked happy. */
33414bc9d430SDaniel Vetter 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3342f97108d1SJesse Barnes 		ironlake_enable_display_irq(dev_priv, DE_PCU_EVENT);
33434bc9d430SDaniel Vetter 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3344f97108d1SJesse Barnes 	}
3345f97108d1SJesse Barnes 
3346036a4a7dSZhenyu Wang 	return 0;
3347036a4a7dSZhenyu Wang }
3348036a4a7dSZhenyu Wang 
3349f8b79e58SImre Deak static void valleyview_display_irqs_install(struct drm_i915_private *dev_priv)
3350f8b79e58SImre Deak {
3351f8b79e58SImre Deak 	u32 pipestat_mask;
3352f8b79e58SImre Deak 	u32 iir_mask;
3353f8b79e58SImre Deak 
3354f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3355f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3356f8b79e58SImre Deak 
3357f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3358f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3359f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3360f8b79e58SImre Deak 
3361f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3362f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3363f8b79e58SImre Deak 
3364f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3365f8b79e58SImre Deak 					       PIPE_GMBUS_INTERRUPT_STATUS);
3366f8b79e58SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3367f8b79e58SImre Deak 
3368f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3369f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3370f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3371f8b79e58SImre Deak 	dev_priv->irq_mask &= ~iir_mask;
3372f8b79e58SImre Deak 
3373f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3374f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3375f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3376f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3377f8b79e58SImre Deak 	POSTING_READ(VLV_IER);
3378f8b79e58SImre Deak }
3379f8b79e58SImre Deak 
3380f8b79e58SImre Deak static void valleyview_display_irqs_uninstall(struct drm_i915_private *dev_priv)
3381f8b79e58SImre Deak {
3382f8b79e58SImre Deak 	u32 pipestat_mask;
3383f8b79e58SImre Deak 	u32 iir_mask;
3384f8b79e58SImre Deak 
3385f8b79e58SImre Deak 	iir_mask = I915_DISPLAY_PORT_INTERRUPT |
3386f8b79e58SImre Deak 		   I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
33876c7fba04SImre Deak 		   I915_DISPLAY_PIPE_B_EVENT_INTERRUPT;
3388f8b79e58SImre Deak 
3389f8b79e58SImre Deak 	dev_priv->irq_mask |= iir_mask;
3390f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
3391f8b79e58SImre Deak 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3392f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3393f8b79e58SImre Deak 	I915_WRITE(VLV_IIR, iir_mask);
3394f8b79e58SImre Deak 	POSTING_READ(VLV_IIR);
3395f8b79e58SImre Deak 
3396f8b79e58SImre Deak 	pipestat_mask = PLANE_FLIP_DONE_INT_STATUS_VLV |
3397f8b79e58SImre Deak 			PIPE_CRC_DONE_INTERRUPT_STATUS;
3398f8b79e58SImre Deak 
3399f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_A, pipestat_mask |
3400f8b79e58SImre Deak 					        PIPE_GMBUS_INTERRUPT_STATUS);
3401f8b79e58SImre Deak 	i915_disable_pipestat(dev_priv, PIPE_B, pipestat_mask);
3402f8b79e58SImre Deak 
3403f8b79e58SImre Deak 	pipestat_mask = PIPESTAT_INT_STATUS_MASK |
3404f8b79e58SImre Deak 			PIPE_FIFO_UNDERRUN_STATUS;
3405f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_A), pipestat_mask);
3406f8b79e58SImre Deak 	I915_WRITE(PIPESTAT(PIPE_B), pipestat_mask);
3407f8b79e58SImre Deak 	POSTING_READ(PIPESTAT(PIPE_A));
3408f8b79e58SImre Deak }
3409f8b79e58SImre Deak 
3410f8b79e58SImre Deak void valleyview_enable_display_irqs(struct drm_i915_private *dev_priv)
3411f8b79e58SImre Deak {
3412f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3413f8b79e58SImre Deak 
3414f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3415f8b79e58SImre Deak 		return;
3416f8b79e58SImre Deak 
3417f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = true;
3418f8b79e58SImre Deak 
3419f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3420f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3421f8b79e58SImre Deak }
3422f8b79e58SImre Deak 
3423f8b79e58SImre Deak void valleyview_disable_display_irqs(struct drm_i915_private *dev_priv)
3424f8b79e58SImre Deak {
3425f8b79e58SImre Deak 	assert_spin_locked(&dev_priv->irq_lock);
3426f8b79e58SImre Deak 
3427f8b79e58SImre Deak 	if (!dev_priv->display_irqs_enabled)
3428f8b79e58SImre Deak 		return;
3429f8b79e58SImre Deak 
3430f8b79e58SImre Deak 	dev_priv->display_irqs_enabled = false;
3431f8b79e58SImre Deak 
3432f8b79e58SImre Deak 	if (dev_priv->dev->irq_enabled)
3433f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3434f8b79e58SImre Deak }
3435f8b79e58SImre Deak 
34367e231dbeSJesse Barnes static int valleyview_irq_postinstall(struct drm_device *dev)
34377e231dbeSJesse Barnes {
34382d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3439b79480baSDaniel Vetter 	unsigned long irqflags;
34407e231dbeSJesse Barnes 
3441f8b79e58SImre Deak 	dev_priv->irq_mask = ~0;
34427e231dbeSJesse Barnes 
344320afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
344420afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
344520afbda2SDaniel Vetter 
34467e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
3447f8b79e58SImre Deak 	I915_WRITE(VLV_IER, ~dev_priv->irq_mask);
34487e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34497e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
34507e231dbeSJesse Barnes 
3451b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3452b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3453b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3454f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3455f8b79e58SImre Deak 		valleyview_display_irqs_install(dev_priv);
3456b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
345731acc7f5SJesse Barnes 
34587e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34597e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
34607e231dbeSJesse Barnes 
34610a9a8c91SDaniel Vetter 	gen5_gt_irq_postinstall(dev);
34627e231dbeSJesse Barnes 
34637e231dbeSJesse Barnes 	/* ack & enable invalid PTE error interrupts */
34647e231dbeSJesse Barnes #if 0 /* FIXME: add support to irq handler for checking these bits */
34657e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_STATUS_MASK);
34667e231dbeSJesse Barnes 	I915_WRITE(DPINVGTT, DPINVGTT_EN_MASK);
34677e231dbeSJesse Barnes #endif
34687e231dbeSJesse Barnes 
34697e231dbeSJesse Barnes 	I915_WRITE(VLV_MASTER_IER, MASTER_INTERRUPT_ENABLE);
347020afbda2SDaniel Vetter 
347120afbda2SDaniel Vetter 	return 0;
347220afbda2SDaniel Vetter }
347320afbda2SDaniel Vetter 
3474abd58f01SBen Widawsky static void gen8_gt_irq_postinstall(struct drm_i915_private *dev_priv)
3475abd58f01SBen Widawsky {
3476abd58f01SBen Widawsky 	int i;
3477abd58f01SBen Widawsky 
3478abd58f01SBen Widawsky 	/* These are interrupts we'll toggle with the ring mask register */
3479abd58f01SBen Widawsky 	uint32_t gt_interrupts[] = {
3480abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_RCS_IRQ_SHIFT |
3481abd58f01SBen Widawsky 			GT_RENDER_L3_PARITY_ERROR_INTERRUPT |
3482abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_BCS_IRQ_SHIFT,
3483abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VCS1_IRQ_SHIFT |
3484abd58f01SBen Widawsky 			GT_RENDER_USER_INTERRUPT << GEN8_VCS2_IRQ_SHIFT,
3485abd58f01SBen Widawsky 		0,
3486abd58f01SBen Widawsky 		GT_RENDER_USER_INTERRUPT << GEN8_VECS_IRQ_SHIFT
3487abd58f01SBen Widawsky 		};
3488abd58f01SBen Widawsky 
3489337ba017SPaulo Zanoni 	for (i = 0; i < ARRAY_SIZE(gt_interrupts); i++)
349035079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(GT, i, ~gt_interrupts[i], gt_interrupts[i]);
34910961021aSBen Widawsky 
34920961021aSBen Widawsky 	dev_priv->pm_irq_mask = 0xffffffff;
3493abd58f01SBen Widawsky }
3494abd58f01SBen Widawsky 
3495abd58f01SBen Widawsky static void gen8_de_irq_postinstall(struct drm_i915_private *dev_priv)
3496abd58f01SBen Widawsky {
3497abd58f01SBen Widawsky 	struct drm_device *dev = dev_priv->dev;
3498d0e1f1cbSDamien Lespiau 	uint32_t de_pipe_masked = GEN8_PIPE_PRIMARY_FLIP_DONE |
34990fbe7870SDaniel Vetter 		GEN8_PIPE_CDCLK_CRC_DONE |
350030100f2bSDaniel Vetter 		GEN8_DE_PIPE_IRQ_FAULT_ERRORS;
35015c673b60SDaniel Vetter 	uint32_t de_pipe_enables = de_pipe_masked | GEN8_PIPE_VBLANK |
35025c673b60SDaniel Vetter 		GEN8_PIPE_FIFO_UNDERRUN;
3503abd58f01SBen Widawsky 	int pipe;
350413b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_A] = ~de_pipe_masked;
350513b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_B] = ~de_pipe_masked;
350613b3a0a7SDaniel Vetter 	dev_priv->de_irq_mask[PIPE_C] = ~de_pipe_masked;
3507abd58f01SBen Widawsky 
3508337ba017SPaulo Zanoni 	for_each_pipe(pipe)
350935079899SPaulo Zanoni 		GEN8_IRQ_INIT_NDX(DE_PIPE, pipe, dev_priv->de_irq_mask[pipe],
351035079899SPaulo Zanoni 				  de_pipe_enables);
3511abd58f01SBen Widawsky 
351235079899SPaulo Zanoni 	GEN5_IRQ_INIT(GEN8_DE_PORT_, ~GEN8_AUX_CHANNEL_A, GEN8_AUX_CHANNEL_A);
3513abd58f01SBen Widawsky }
3514abd58f01SBen Widawsky 
3515abd58f01SBen Widawsky static int gen8_irq_postinstall(struct drm_device *dev)
3516abd58f01SBen Widawsky {
3517abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3518abd58f01SBen Widawsky 
3519622364b6SPaulo Zanoni 	ibx_irq_pre_postinstall(dev);
3520622364b6SPaulo Zanoni 
3521abd58f01SBen Widawsky 	gen8_gt_irq_postinstall(dev_priv);
3522abd58f01SBen Widawsky 	gen8_de_irq_postinstall(dev_priv);
3523abd58f01SBen Widawsky 
3524abd58f01SBen Widawsky 	ibx_irq_postinstall(dev);
3525abd58f01SBen Widawsky 
3526abd58f01SBen Widawsky 	I915_WRITE(GEN8_MASTER_IRQ, DE_MASTER_IRQ_CONTROL);
3527abd58f01SBen Widawsky 	POSTING_READ(GEN8_MASTER_IRQ);
3528abd58f01SBen Widawsky 
3529abd58f01SBen Widawsky 	return 0;
3530abd58f01SBen Widawsky }
3531abd58f01SBen Widawsky 
353243f328d7SVille Syrjälä static int cherryview_irq_postinstall(struct drm_device *dev)
353343f328d7SVille Syrjälä {
353443f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
353543f328d7SVille Syrjälä 	u32 enable_mask = I915_DISPLAY_PORT_INTERRUPT |
353643f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
353743f328d7SVille Syrjälä 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
35383278f67fSVille Syrjälä 		I915_DISPLAY_PIPE_C_EVENT_INTERRUPT;
35393278f67fSVille Syrjälä 	u32 pipestat_enable = PLANE_FLIP_DONE_INT_STATUS_VLV |
35403278f67fSVille Syrjälä 		PIPE_CRC_DONE_INTERRUPT_STATUS;
354143f328d7SVille Syrjälä 	unsigned long irqflags;
354243f328d7SVille Syrjälä 	int pipe;
354343f328d7SVille Syrjälä 
354443f328d7SVille Syrjälä 	/*
354543f328d7SVille Syrjälä 	 * Leave vblank interrupts masked initially.  enable/disable will
354643f328d7SVille Syrjälä 	 * toggle them based on usage.
354743f328d7SVille Syrjälä 	 */
35483278f67fSVille Syrjälä 	dev_priv->irq_mask = ~enable_mask;
354943f328d7SVille Syrjälä 
355043f328d7SVille Syrjälä 	for_each_pipe(pipe)
355143f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
355243f328d7SVille Syrjälä 
355343f328d7SVille Syrjälä 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
35543278f67fSVille Syrjälä 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
355543f328d7SVille Syrjälä 	for_each_pipe(pipe)
355643f328d7SVille Syrjälä 		i915_enable_pipestat(dev_priv, pipe, pipestat_enable);
355743f328d7SVille Syrjälä 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
355843f328d7SVille Syrjälä 
355943f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
356043f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, dev_priv->irq_mask);
356143f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, enable_mask);
356243f328d7SVille Syrjälä 
356343f328d7SVille Syrjälä 	gen8_gt_irq_postinstall(dev_priv);
356443f328d7SVille Syrjälä 
356543f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, MASTER_INTERRUPT_ENABLE);
356643f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
356743f328d7SVille Syrjälä 
356843f328d7SVille Syrjälä 	return 0;
356943f328d7SVille Syrjälä }
357043f328d7SVille Syrjälä 
3571abd58f01SBen Widawsky static void gen8_irq_uninstall(struct drm_device *dev)
3572abd58f01SBen Widawsky {
3573abd58f01SBen Widawsky 	struct drm_i915_private *dev_priv = dev->dev_private;
3574abd58f01SBen Widawsky 
3575abd58f01SBen Widawsky 	if (!dev_priv)
3576abd58f01SBen Widawsky 		return;
3577abd58f01SBen Widawsky 
3578d4eb6b10SPaulo Zanoni 	intel_hpd_irq_uninstall(dev_priv);
3579abd58f01SBen Widawsky 
3580823f6b38SPaulo Zanoni 	gen8_irq_reset(dev);
3581abd58f01SBen Widawsky }
3582abd58f01SBen Widawsky 
35837e231dbeSJesse Barnes static void valleyview_irq_uninstall(struct drm_device *dev)
35847e231dbeSJesse Barnes {
35852d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3586f8b79e58SImre Deak 	unsigned long irqflags;
35877e231dbeSJesse Barnes 	int pipe;
35887e231dbeSJesse Barnes 
35897e231dbeSJesse Barnes 	if (!dev_priv)
35907e231dbeSJesse Barnes 		return;
35917e231dbeSJesse Barnes 
3592843d0e7dSImre Deak 	I915_WRITE(VLV_MASTER_IER, 0);
3593843d0e7dSImre Deak 
35943ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3595ac4c16c5SEgbert Eich 
35967e231dbeSJesse Barnes 	for_each_pipe(pipe)
35977e231dbeSJesse Barnes 		I915_WRITE(PIPESTAT(pipe), 0xffff);
35987e231dbeSJesse Barnes 
35997e231dbeSJesse Barnes 	I915_WRITE(HWSTAM, 0xffffffff);
36007e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_EN, 0);
36017e231dbeSJesse Barnes 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3602f8b79e58SImre Deak 
3603f8b79e58SImre Deak 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3604f8b79e58SImre Deak 	if (dev_priv->display_irqs_enabled)
3605f8b79e58SImre Deak 		valleyview_display_irqs_uninstall(dev_priv);
3606f8b79e58SImre Deak 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3607f8b79e58SImre Deak 
3608f8b79e58SImre Deak 	dev_priv->irq_mask = 0;
3609f8b79e58SImre Deak 
36107e231dbeSJesse Barnes 	I915_WRITE(VLV_IIR, 0xffffffff);
36117e231dbeSJesse Barnes 	I915_WRITE(VLV_IMR, 0xffffffff);
36127e231dbeSJesse Barnes 	I915_WRITE(VLV_IER, 0x0);
36137e231dbeSJesse Barnes 	POSTING_READ(VLV_IER);
36147e231dbeSJesse Barnes }
36157e231dbeSJesse Barnes 
361643f328d7SVille Syrjälä static void cherryview_irq_uninstall(struct drm_device *dev)
361743f328d7SVille Syrjälä {
361843f328d7SVille Syrjälä 	struct drm_i915_private *dev_priv = dev->dev_private;
361943f328d7SVille Syrjälä 	int pipe;
362043f328d7SVille Syrjälä 
362143f328d7SVille Syrjälä 	if (!dev_priv)
362243f328d7SVille Syrjälä 		return;
362343f328d7SVille Syrjälä 
362443f328d7SVille Syrjälä 	I915_WRITE(GEN8_MASTER_IRQ, 0);
362543f328d7SVille Syrjälä 	POSTING_READ(GEN8_MASTER_IRQ);
362643f328d7SVille Syrjälä 
362743f328d7SVille Syrjälä #define GEN8_IRQ_FINI_NDX(type, which)				\
362843f328d7SVille Syrjälä do {								\
362943f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR(which), 0xffffffff);	\
363043f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER(which), 0);		\
363143f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
363243f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR(which));			\
363343f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR(which), 0xffffffff);	\
363443f328d7SVille Syrjälä } while (0)
363543f328d7SVille Syrjälä 
363643f328d7SVille Syrjälä #define GEN8_IRQ_FINI(type)				\
363743f328d7SVille Syrjälä do {							\
363843f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IMR, 0xffffffff);	\
363943f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IER, 0);		\
364043f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
364143f328d7SVille Syrjälä 	POSTING_READ(GEN8_##type##_IIR);		\
364243f328d7SVille Syrjälä 	I915_WRITE(GEN8_##type##_IIR, 0xffffffff);	\
364343f328d7SVille Syrjälä } while (0)
364443f328d7SVille Syrjälä 
364543f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 0);
364643f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 1);
364743f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 2);
364843f328d7SVille Syrjälä 	GEN8_IRQ_FINI_NDX(GT, 3);
364943f328d7SVille Syrjälä 
365043f328d7SVille Syrjälä 	GEN8_IRQ_FINI(PCU);
365143f328d7SVille Syrjälä 
365243f328d7SVille Syrjälä #undef GEN8_IRQ_FINI
365343f328d7SVille Syrjälä #undef GEN8_IRQ_FINI_NDX
365443f328d7SVille Syrjälä 
365543f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_EN, 0);
365643f328d7SVille Syrjälä 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
365743f328d7SVille Syrjälä 
365843f328d7SVille Syrjälä 	for_each_pipe(pipe)
365943f328d7SVille Syrjälä 		I915_WRITE(PIPESTAT(pipe), 0xffff);
366043f328d7SVille Syrjälä 
366143f328d7SVille Syrjälä 	I915_WRITE(VLV_IMR, 0xffffffff);
366243f328d7SVille Syrjälä 	I915_WRITE(VLV_IER, 0x0);
366343f328d7SVille Syrjälä 	I915_WRITE(VLV_IIR, 0xffffffff);
366443f328d7SVille Syrjälä 	POSTING_READ(VLV_IIR);
366543f328d7SVille Syrjälä }
366643f328d7SVille Syrjälä 
3667f71d4af4SJesse Barnes static void ironlake_irq_uninstall(struct drm_device *dev)
3668036a4a7dSZhenyu Wang {
36692d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
36704697995bSJesse Barnes 
36714697995bSJesse Barnes 	if (!dev_priv)
36724697995bSJesse Barnes 		return;
36734697995bSJesse Barnes 
36743ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
3675ac4c16c5SEgbert Eich 
3676be30b29fSPaulo Zanoni 	ironlake_irq_reset(dev);
3677036a4a7dSZhenyu Wang }
3678036a4a7dSZhenyu Wang 
3679c2798b19SChris Wilson static void i8xx_irq_preinstall(struct drm_device * dev)
3680c2798b19SChris Wilson {
36812d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3682c2798b19SChris Wilson 	int pipe;
3683c2798b19SChris Wilson 
3684c2798b19SChris Wilson 	for_each_pipe(pipe)
3685c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3686c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3687c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3688c2798b19SChris Wilson 	POSTING_READ16(IER);
3689c2798b19SChris Wilson }
3690c2798b19SChris Wilson 
3691c2798b19SChris Wilson static int i8xx_irq_postinstall(struct drm_device *dev)
3692c2798b19SChris Wilson {
36932d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3694379ef82dSDaniel Vetter 	unsigned long irqflags;
3695c2798b19SChris Wilson 
3696c2798b19SChris Wilson 	I915_WRITE16(EMR,
3697c2798b19SChris Wilson 		     ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
3698c2798b19SChris Wilson 
3699c2798b19SChris Wilson 	/* Unmask the interrupts that we always want on. */
3700c2798b19SChris Wilson 	dev_priv->irq_mask =
3701c2798b19SChris Wilson 		~(I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3702c2798b19SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3703c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3704c2798b19SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
3705c2798b19SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
3706c2798b19SChris Wilson 	I915_WRITE16(IMR, dev_priv->irq_mask);
3707c2798b19SChris Wilson 
3708c2798b19SChris Wilson 	I915_WRITE16(IER,
3709c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
3710c2798b19SChris Wilson 		     I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
3711c2798b19SChris Wilson 		     I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
3712c2798b19SChris Wilson 		     I915_USER_INTERRUPT);
3713c2798b19SChris Wilson 	POSTING_READ16(IER);
3714c2798b19SChris Wilson 
3715379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3716379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3717379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3718755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3719755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3720379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3721379ef82dSDaniel Vetter 
3722c2798b19SChris Wilson 	return 0;
3723c2798b19SChris Wilson }
3724c2798b19SChris Wilson 
372590a72f87SVille Syrjälä /*
372690a72f87SVille Syrjälä  * Returns true when a page flip has completed.
372790a72f87SVille Syrjälä  */
372890a72f87SVille Syrjälä static bool i8xx_handle_vblank(struct drm_device *dev,
37291f1c2e24SVille Syrjälä 			       int plane, int pipe, u32 iir)
373090a72f87SVille Syrjälä {
37312d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
37321f1c2e24SVille Syrjälä 	u16 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
373390a72f87SVille Syrjälä 
37348d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
373590a72f87SVille Syrjälä 		return false;
373690a72f87SVille Syrjälä 
373790a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
373890a72f87SVille Syrjälä 		return false;
373990a72f87SVille Syrjälä 
37401f1c2e24SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
374190a72f87SVille Syrjälä 
374290a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
374390a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
374490a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
374590a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
374690a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
374790a72f87SVille Syrjälä 	 */
374890a72f87SVille Syrjälä 	if (I915_READ16(ISR) & flip_pending)
374990a72f87SVille Syrjälä 		return false;
375090a72f87SVille Syrjälä 
375190a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
375290a72f87SVille Syrjälä 
375390a72f87SVille Syrjälä 	return true;
375490a72f87SVille Syrjälä }
375590a72f87SVille Syrjälä 
3756ff1f525eSDaniel Vetter static irqreturn_t i8xx_irq_handler(int irq, void *arg)
3757c2798b19SChris Wilson {
375845a83f84SDaniel Vetter 	struct drm_device *dev = arg;
37592d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3760c2798b19SChris Wilson 	u16 iir, new_iir;
3761c2798b19SChris Wilson 	u32 pipe_stats[2];
3762c2798b19SChris Wilson 	unsigned long irqflags;
3763c2798b19SChris Wilson 	int pipe;
3764c2798b19SChris Wilson 	u16 flip_mask =
3765c2798b19SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
3766c2798b19SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
3767c2798b19SChris Wilson 
3768c2798b19SChris Wilson 	iir = I915_READ16(IIR);
3769c2798b19SChris Wilson 	if (iir == 0)
3770c2798b19SChris Wilson 		return IRQ_NONE;
3771c2798b19SChris Wilson 
3772c2798b19SChris Wilson 	while (iir & ~flip_mask) {
3773c2798b19SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3774c2798b19SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3775c2798b19SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3776c2798b19SChris Wilson 		 * interrupts (for non-MSI).
3777c2798b19SChris Wilson 		 */
3778c2798b19SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3779c2798b19SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
378058174462SMika Kuoppala 			i915_handle_error(dev, false,
378158174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
378258174462SMika Kuoppala 					  iir);
3783c2798b19SChris Wilson 
3784c2798b19SChris Wilson 		for_each_pipe(pipe) {
3785c2798b19SChris Wilson 			int reg = PIPESTAT(pipe);
3786c2798b19SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3787c2798b19SChris Wilson 
3788c2798b19SChris Wilson 			/*
3789c2798b19SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
3790c2798b19SChris Wilson 			 */
37912d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & 0x8000ffff)
3792c2798b19SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
3793c2798b19SChris Wilson 		}
3794c2798b19SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3795c2798b19SChris Wilson 
3796c2798b19SChris Wilson 		I915_WRITE16(IIR, iir & ~flip_mask);
3797c2798b19SChris Wilson 		new_iir = I915_READ16(IIR); /* Flush posted writes */
3798c2798b19SChris Wilson 
3799d05c617eSDaniel Vetter 		i915_update_dri1_breadcrumb(dev);
3800c2798b19SChris Wilson 
3801c2798b19SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3802c2798b19SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3803c2798b19SChris Wilson 
38044356d586SDaniel Vetter 		for_each_pipe(pipe) {
38051f1c2e24SVille Syrjälä 			int plane = pipe;
38063a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
38071f1c2e24SVille Syrjälä 				plane = !plane;
38081f1c2e24SVille Syrjälä 
38094356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
38101f1c2e24SVille Syrjälä 			    i8xx_handle_vblank(dev, plane, pipe, iir))
38111f1c2e24SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
3812c2798b19SChris Wilson 
38134356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
3814277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
38152d9d2b0bSVille Syrjälä 
38162d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
38172d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
3818fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
38194356d586SDaniel Vetter 		}
3820c2798b19SChris Wilson 
3821c2798b19SChris Wilson 		iir = new_iir;
3822c2798b19SChris Wilson 	}
3823c2798b19SChris Wilson 
3824c2798b19SChris Wilson 	return IRQ_HANDLED;
3825c2798b19SChris Wilson }
3826c2798b19SChris Wilson 
3827c2798b19SChris Wilson static void i8xx_irq_uninstall(struct drm_device * dev)
3828c2798b19SChris Wilson {
38292d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3830c2798b19SChris Wilson 	int pipe;
3831c2798b19SChris Wilson 
3832c2798b19SChris Wilson 	for_each_pipe(pipe) {
3833c2798b19SChris Wilson 		/* Clear enable bits; then clear status bits */
3834c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3835c2798b19SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
3836c2798b19SChris Wilson 	}
3837c2798b19SChris Wilson 	I915_WRITE16(IMR, 0xffff);
3838c2798b19SChris Wilson 	I915_WRITE16(IER, 0x0);
3839c2798b19SChris Wilson 	I915_WRITE16(IIR, I915_READ16(IIR));
3840c2798b19SChris Wilson }
3841c2798b19SChris Wilson 
3842a266c7d5SChris Wilson static void i915_irq_preinstall(struct drm_device * dev)
3843a266c7d5SChris Wilson {
38442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
3845a266c7d5SChris Wilson 	int pipe;
3846a266c7d5SChris Wilson 
3847a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
3848a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
3849a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
3850a266c7d5SChris Wilson 	}
3851a266c7d5SChris Wilson 
385200d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xeffe);
3853a266c7d5SChris Wilson 	for_each_pipe(pipe)
3854a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
3855a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
3856a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
3857a266c7d5SChris Wilson 	POSTING_READ(IER);
3858a266c7d5SChris Wilson }
3859a266c7d5SChris Wilson 
3860a266c7d5SChris Wilson static int i915_irq_postinstall(struct drm_device *dev)
3861a266c7d5SChris Wilson {
38622d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
386338bde180SChris Wilson 	u32 enable_mask;
3864379ef82dSDaniel Vetter 	unsigned long irqflags;
3865a266c7d5SChris Wilson 
386638bde180SChris Wilson 	I915_WRITE(EMR, ~(I915_ERROR_PAGE_TABLE | I915_ERROR_MEMORY_REFRESH));
386738bde180SChris Wilson 
386838bde180SChris Wilson 	/* Unmask the interrupts that we always want on. */
386938bde180SChris Wilson 	dev_priv->irq_mask =
387038bde180SChris Wilson 		~(I915_ASLE_INTERRUPT |
387138bde180SChris Wilson 		  I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
387238bde180SChris Wilson 		  I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
387338bde180SChris Wilson 		  I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
387438bde180SChris Wilson 		  I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
387538bde180SChris Wilson 		  I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
387638bde180SChris Wilson 
387738bde180SChris Wilson 	enable_mask =
387838bde180SChris Wilson 		I915_ASLE_INTERRUPT |
387938bde180SChris Wilson 		I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
388038bde180SChris Wilson 		I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
388138bde180SChris Wilson 		I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT |
388238bde180SChris Wilson 		I915_USER_INTERRUPT;
388338bde180SChris Wilson 
3884a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
388520afbda2SDaniel Vetter 		I915_WRITE(PORT_HOTPLUG_EN, 0);
388620afbda2SDaniel Vetter 		POSTING_READ(PORT_HOTPLUG_EN);
388720afbda2SDaniel Vetter 
3888a266c7d5SChris Wilson 		/* Enable in IER... */
3889a266c7d5SChris Wilson 		enable_mask |= I915_DISPLAY_PORT_INTERRUPT;
3890a266c7d5SChris Wilson 		/* and unmask in IMR */
3891a266c7d5SChris Wilson 		dev_priv->irq_mask &= ~I915_DISPLAY_PORT_INTERRUPT;
3892a266c7d5SChris Wilson 	}
3893a266c7d5SChris Wilson 
3894a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
3895a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
3896a266c7d5SChris Wilson 	POSTING_READ(IER);
3897a266c7d5SChris Wilson 
3898f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
389920afbda2SDaniel Vetter 
3900379ef82dSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
3901379ef82dSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
3902379ef82dSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3903755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
3904755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
3905379ef82dSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3906379ef82dSDaniel Vetter 
390720afbda2SDaniel Vetter 	return 0;
390820afbda2SDaniel Vetter }
390920afbda2SDaniel Vetter 
391090a72f87SVille Syrjälä /*
391190a72f87SVille Syrjälä  * Returns true when a page flip has completed.
391290a72f87SVille Syrjälä  */
391390a72f87SVille Syrjälä static bool i915_handle_vblank(struct drm_device *dev,
391490a72f87SVille Syrjälä 			       int plane, int pipe, u32 iir)
391590a72f87SVille Syrjälä {
39162d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
391790a72f87SVille Syrjälä 	u32 flip_pending = DISPLAY_PLANE_FLIP_PENDING(plane);
391890a72f87SVille Syrjälä 
39198d7849dbSVille Syrjälä 	if (!intel_pipe_handle_vblank(dev, pipe))
392090a72f87SVille Syrjälä 		return false;
392190a72f87SVille Syrjälä 
392290a72f87SVille Syrjälä 	if ((iir & flip_pending) == 0)
392390a72f87SVille Syrjälä 		return false;
392490a72f87SVille Syrjälä 
392590a72f87SVille Syrjälä 	intel_prepare_page_flip(dev, plane);
392690a72f87SVille Syrjälä 
392790a72f87SVille Syrjälä 	/* We detect FlipDone by looking for the change in PendingFlip from '1'
392890a72f87SVille Syrjälä 	 * to '0' on the following vblank, i.e. IIR has the Pendingflip
392990a72f87SVille Syrjälä 	 * asserted following the MI_DISPLAY_FLIP, but ISR is deasserted, hence
393090a72f87SVille Syrjälä 	 * the flip is completed (no longer pending). Since this doesn't raise
393190a72f87SVille Syrjälä 	 * an interrupt per se, we watch for the change at vblank.
393290a72f87SVille Syrjälä 	 */
393390a72f87SVille Syrjälä 	if (I915_READ(ISR) & flip_pending)
393490a72f87SVille Syrjälä 		return false;
393590a72f87SVille Syrjälä 
393690a72f87SVille Syrjälä 	intel_finish_page_flip(dev, pipe);
393790a72f87SVille Syrjälä 
393890a72f87SVille Syrjälä 	return true;
393990a72f87SVille Syrjälä }
394090a72f87SVille Syrjälä 
3941ff1f525eSDaniel Vetter static irqreturn_t i915_irq_handler(int irq, void *arg)
3942a266c7d5SChris Wilson {
394345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
39442d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
39458291ee90SChris Wilson 	u32 iir, new_iir, pipe_stats[I915_MAX_PIPES];
3946a266c7d5SChris Wilson 	unsigned long irqflags;
394738bde180SChris Wilson 	u32 flip_mask =
394838bde180SChris Wilson 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
394938bde180SChris Wilson 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
395038bde180SChris Wilson 	int pipe, ret = IRQ_NONE;
3951a266c7d5SChris Wilson 
3952a266c7d5SChris Wilson 	iir = I915_READ(IIR);
395338bde180SChris Wilson 	do {
395438bde180SChris Wilson 		bool irq_received = (iir & ~flip_mask) != 0;
39558291ee90SChris Wilson 		bool blc_event = false;
3956a266c7d5SChris Wilson 
3957a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
3958a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
3959a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
3960a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
3961a266c7d5SChris Wilson 		 */
3962a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
3963a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
396458174462SMika Kuoppala 			i915_handle_error(dev, false,
396558174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
396658174462SMika Kuoppala 					  iir);
3967a266c7d5SChris Wilson 
3968a266c7d5SChris Wilson 		for_each_pipe(pipe) {
3969a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
3970a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
3971a266c7d5SChris Wilson 
397238bde180SChris Wilson 			/* Clear the PIPE*STAT regs before the IIR */
3973a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
3974a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
397538bde180SChris Wilson 				irq_received = true;
3976a266c7d5SChris Wilson 			}
3977a266c7d5SChris Wilson 		}
3978a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
3979a266c7d5SChris Wilson 
3980a266c7d5SChris Wilson 		if (!irq_received)
3981a266c7d5SChris Wilson 			break;
3982a266c7d5SChris Wilson 
3983a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
398416c6c56bSVille Syrjälä 		if (I915_HAS_HOTPLUG(dev) &&
398516c6c56bSVille Syrjälä 		    iir & I915_DISPLAY_PORT_INTERRUPT)
398616c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
3987a266c7d5SChris Wilson 
398838bde180SChris Wilson 		I915_WRITE(IIR, iir & ~flip_mask);
3989a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
3990a266c7d5SChris Wilson 
3991a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
3992a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
3993a266c7d5SChris Wilson 
3994a266c7d5SChris Wilson 		for_each_pipe(pipe) {
399538bde180SChris Wilson 			int plane = pipe;
39963a77c4c4SDaniel Vetter 			if (HAS_FBC(dev))
399738bde180SChris Wilson 				plane = !plane;
39985e2032d4SVille Syrjälä 
399990a72f87SVille Syrjälä 			if (pipe_stats[pipe] & PIPE_VBLANK_INTERRUPT_STATUS &&
400090a72f87SVille Syrjälä 			    i915_handle_vblank(dev, plane, pipe, iir))
400190a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(plane);
4002a266c7d5SChris Wilson 
4003a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4004a266c7d5SChris Wilson 				blc_event = true;
40054356d586SDaniel Vetter 
40064356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4007277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
40082d9d2b0bSVille Syrjälä 
40092d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
40102d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
4011fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
4012a266c7d5SChris Wilson 		}
4013a266c7d5SChris Wilson 
4014a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4015a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4016a266c7d5SChris Wilson 
4017a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4018a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4019a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4020a266c7d5SChris Wilson 		 * we would never get another interrupt.
4021a266c7d5SChris Wilson 		 *
4022a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4023a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4024a266c7d5SChris Wilson 		 * another one.
4025a266c7d5SChris Wilson 		 *
4026a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4027a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4028a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4029a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4030a266c7d5SChris Wilson 		 * stray interrupts.
4031a266c7d5SChris Wilson 		 */
403238bde180SChris Wilson 		ret = IRQ_HANDLED;
4033a266c7d5SChris Wilson 		iir = new_iir;
403438bde180SChris Wilson 	} while (iir & ~flip_mask);
4035a266c7d5SChris Wilson 
4036d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
40378291ee90SChris Wilson 
4038a266c7d5SChris Wilson 	return ret;
4039a266c7d5SChris Wilson }
4040a266c7d5SChris Wilson 
4041a266c7d5SChris Wilson static void i915_irq_uninstall(struct drm_device * dev)
4042a266c7d5SChris Wilson {
40432d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4044a266c7d5SChris Wilson 	int pipe;
4045a266c7d5SChris Wilson 
40463ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4047ac4c16c5SEgbert Eich 
4048a266c7d5SChris Wilson 	if (I915_HAS_HOTPLUG(dev)) {
4049a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, 0);
4050a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4051a266c7d5SChris Wilson 	}
4052a266c7d5SChris Wilson 
405300d98ebdSChris Wilson 	I915_WRITE16(HWSTAM, 0xffff);
405455b39755SChris Wilson 	for_each_pipe(pipe) {
405555b39755SChris Wilson 		/* Clear enable bits; then clear status bits */
4056a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
405755b39755SChris Wilson 		I915_WRITE(PIPESTAT(pipe), I915_READ(PIPESTAT(pipe)));
405855b39755SChris Wilson 	}
4059a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4060a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4061a266c7d5SChris Wilson 
4062a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4063a266c7d5SChris Wilson }
4064a266c7d5SChris Wilson 
4065a266c7d5SChris Wilson static void i965_irq_preinstall(struct drm_device * dev)
4066a266c7d5SChris Wilson {
40672d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4068a266c7d5SChris Wilson 	int pipe;
4069a266c7d5SChris Wilson 
4070a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4071a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4072a266c7d5SChris Wilson 
4073a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xeffe);
4074a266c7d5SChris Wilson 	for_each_pipe(pipe)
4075a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4076a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4077a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4078a266c7d5SChris Wilson 	POSTING_READ(IER);
4079a266c7d5SChris Wilson }
4080a266c7d5SChris Wilson 
4081a266c7d5SChris Wilson static int i965_irq_postinstall(struct drm_device *dev)
4082a266c7d5SChris Wilson {
40832d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4084bbba0a97SChris Wilson 	u32 enable_mask;
4085a266c7d5SChris Wilson 	u32 error_mask;
4086b79480baSDaniel Vetter 	unsigned long irqflags;
4087a266c7d5SChris Wilson 
4088a266c7d5SChris Wilson 	/* Unmask the interrupts that we always want on. */
4089bbba0a97SChris Wilson 	dev_priv->irq_mask = ~(I915_ASLE_INTERRUPT |
4090adca4730SChris Wilson 			       I915_DISPLAY_PORT_INTERRUPT |
4091bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_A_EVENT_INTERRUPT |
4092bbba0a97SChris Wilson 			       I915_DISPLAY_PIPE_B_EVENT_INTERRUPT |
4093bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
4094bbba0a97SChris Wilson 			       I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT |
4095bbba0a97SChris Wilson 			       I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT);
4096bbba0a97SChris Wilson 
4097bbba0a97SChris Wilson 	enable_mask = ~dev_priv->irq_mask;
409821ad8330SVille Syrjälä 	enable_mask &= ~(I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
409921ad8330SVille Syrjälä 			 I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT);
4100bbba0a97SChris Wilson 	enable_mask |= I915_USER_INTERRUPT;
4101bbba0a97SChris Wilson 
4102bbba0a97SChris Wilson 	if (IS_G4X(dev))
4103bbba0a97SChris Wilson 		enable_mask |= I915_BSD_USER_INTERRUPT;
4104a266c7d5SChris Wilson 
4105b79480baSDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4106b79480baSDaniel Vetter 	 * just to make the assert_spin_locked check happy. */
4107b79480baSDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4108755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_GMBUS_INTERRUPT_STATUS);
4109755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_A, PIPE_CRC_DONE_INTERRUPT_STATUS);
4110755e9019SImre Deak 	i915_enable_pipestat(dev_priv, PIPE_B, PIPE_CRC_DONE_INTERRUPT_STATUS);
4111b79480baSDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4112a266c7d5SChris Wilson 
4113a266c7d5SChris Wilson 	/*
4114a266c7d5SChris Wilson 	 * Enable some error detection, note the instruction error mask
4115a266c7d5SChris Wilson 	 * bit is reserved, so we leave it masked.
4116a266c7d5SChris Wilson 	 */
4117a266c7d5SChris Wilson 	if (IS_G4X(dev)) {
4118a266c7d5SChris Wilson 		error_mask = ~(GM45_ERROR_PAGE_TABLE |
4119a266c7d5SChris Wilson 			       GM45_ERROR_MEM_PRIV |
4120a266c7d5SChris Wilson 			       GM45_ERROR_CP_PRIV |
4121a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4122a266c7d5SChris Wilson 	} else {
4123a266c7d5SChris Wilson 		error_mask = ~(I915_ERROR_PAGE_TABLE |
4124a266c7d5SChris Wilson 			       I915_ERROR_MEMORY_REFRESH);
4125a266c7d5SChris Wilson 	}
4126a266c7d5SChris Wilson 	I915_WRITE(EMR, error_mask);
4127a266c7d5SChris Wilson 
4128a266c7d5SChris Wilson 	I915_WRITE(IMR, dev_priv->irq_mask);
4129a266c7d5SChris Wilson 	I915_WRITE(IER, enable_mask);
4130a266c7d5SChris Wilson 	POSTING_READ(IER);
4131a266c7d5SChris Wilson 
413220afbda2SDaniel Vetter 	I915_WRITE(PORT_HOTPLUG_EN, 0);
413320afbda2SDaniel Vetter 	POSTING_READ(PORT_HOTPLUG_EN);
413420afbda2SDaniel Vetter 
4135f49e38ddSJani Nikula 	i915_enable_asle_pipestat(dev);
413620afbda2SDaniel Vetter 
413720afbda2SDaniel Vetter 	return 0;
413820afbda2SDaniel Vetter }
413920afbda2SDaniel Vetter 
4140bac56d5bSEgbert Eich static void i915_hpd_irq_setup(struct drm_device *dev)
414120afbda2SDaniel Vetter {
41422d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4143e5868a31SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4144cd569aedSEgbert Eich 	struct intel_encoder *intel_encoder;
414520afbda2SDaniel Vetter 	u32 hotplug_en;
414620afbda2SDaniel Vetter 
4147b5ea2d56SDaniel Vetter 	assert_spin_locked(&dev_priv->irq_lock);
4148b5ea2d56SDaniel Vetter 
4149bac56d5bSEgbert Eich 	if (I915_HAS_HOTPLUG(dev)) {
4150bac56d5bSEgbert Eich 		hotplug_en = I915_READ(PORT_HOTPLUG_EN);
4151bac56d5bSEgbert Eich 		hotplug_en &= ~HOTPLUG_INT_EN_MASK;
4152adca4730SChris Wilson 		/* Note HDMI and DP share hotplug bits */
4153e5868a31SEgbert Eich 		/* enable bits are the same for all generations */
4154cd569aedSEgbert Eich 		list_for_each_entry(intel_encoder, &mode_config->encoder_list, base.head)
4155cd569aedSEgbert Eich 			if (dev_priv->hpd_stats[intel_encoder->hpd_pin].hpd_mark == HPD_ENABLED)
4156cd569aedSEgbert Eich 				hotplug_en |= hpd_mask_i915[intel_encoder->hpd_pin];
4157a266c7d5SChris Wilson 		/* Programming the CRT detection parameters tends
4158a266c7d5SChris Wilson 		   to generate a spurious hotplug event about three
4159a266c7d5SChris Wilson 		   seconds later.  So just do it once.
4160a266c7d5SChris Wilson 		*/
4161a266c7d5SChris Wilson 		if (IS_G4X(dev))
4162a266c7d5SChris Wilson 			hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
416385fc95baSDaniel Vetter 		hotplug_en &= ~CRT_HOTPLUG_VOLTAGE_COMPARE_MASK;
4164a266c7d5SChris Wilson 		hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
4165a266c7d5SChris Wilson 
4166a266c7d5SChris Wilson 		/* Ignore TV since it's buggy */
4167a266c7d5SChris Wilson 		I915_WRITE(PORT_HOTPLUG_EN, hotplug_en);
4168a266c7d5SChris Wilson 	}
4169bac56d5bSEgbert Eich }
4170a266c7d5SChris Wilson 
4171ff1f525eSDaniel Vetter static irqreturn_t i965_irq_handler(int irq, void *arg)
4172a266c7d5SChris Wilson {
417345a83f84SDaniel Vetter 	struct drm_device *dev = arg;
41742d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4175a266c7d5SChris Wilson 	u32 iir, new_iir;
4176a266c7d5SChris Wilson 	u32 pipe_stats[I915_MAX_PIPES];
4177a266c7d5SChris Wilson 	unsigned long irqflags;
4178a266c7d5SChris Wilson 	int ret = IRQ_NONE, pipe;
417921ad8330SVille Syrjälä 	u32 flip_mask =
418021ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT |
418121ad8330SVille Syrjälä 		I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
4182a266c7d5SChris Wilson 
4183a266c7d5SChris Wilson 	iir = I915_READ(IIR);
4184a266c7d5SChris Wilson 
4185a266c7d5SChris Wilson 	for (;;) {
4186501e01d7SVille Syrjälä 		bool irq_received = (iir & ~flip_mask) != 0;
41872c8ba29fSChris Wilson 		bool blc_event = false;
41882c8ba29fSChris Wilson 
4189a266c7d5SChris Wilson 		/* Can't rely on pipestat interrupt bit in iir as it might
4190a266c7d5SChris Wilson 		 * have been cleared after the pipestat interrupt was received.
4191a266c7d5SChris Wilson 		 * It doesn't set the bit in iir again, but it still produces
4192a266c7d5SChris Wilson 		 * interrupts (for non-MSI).
4193a266c7d5SChris Wilson 		 */
4194a266c7d5SChris Wilson 		spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4195a266c7d5SChris Wilson 		if (iir & I915_RENDER_COMMAND_PARSER_ERROR_INTERRUPT)
419658174462SMika Kuoppala 			i915_handle_error(dev, false,
419758174462SMika Kuoppala 					  "Command parser error, iir 0x%08x",
419858174462SMika Kuoppala 					  iir);
4199a266c7d5SChris Wilson 
4200a266c7d5SChris Wilson 		for_each_pipe(pipe) {
4201a266c7d5SChris Wilson 			int reg = PIPESTAT(pipe);
4202a266c7d5SChris Wilson 			pipe_stats[pipe] = I915_READ(reg);
4203a266c7d5SChris Wilson 
4204a266c7d5SChris Wilson 			/*
4205a266c7d5SChris Wilson 			 * Clear the PIPE*STAT regs before the IIR
4206a266c7d5SChris Wilson 			 */
4207a266c7d5SChris Wilson 			if (pipe_stats[pipe] & 0x8000ffff) {
4208a266c7d5SChris Wilson 				I915_WRITE(reg, pipe_stats[pipe]);
4209501e01d7SVille Syrjälä 				irq_received = true;
4210a266c7d5SChris Wilson 			}
4211a266c7d5SChris Wilson 		}
4212a266c7d5SChris Wilson 		spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4213a266c7d5SChris Wilson 
4214a266c7d5SChris Wilson 		if (!irq_received)
4215a266c7d5SChris Wilson 			break;
4216a266c7d5SChris Wilson 
4217a266c7d5SChris Wilson 		ret = IRQ_HANDLED;
4218a266c7d5SChris Wilson 
4219a266c7d5SChris Wilson 		/* Consume port.  Then clear IIR or we'll miss events */
422016c6c56bSVille Syrjälä 		if (iir & I915_DISPLAY_PORT_INTERRUPT)
422116c6c56bSVille Syrjälä 			i9xx_hpd_irq_handler(dev);
4222a266c7d5SChris Wilson 
422321ad8330SVille Syrjälä 		I915_WRITE(IIR, iir & ~flip_mask);
4224a266c7d5SChris Wilson 		new_iir = I915_READ(IIR); /* Flush posted writes */
4225a266c7d5SChris Wilson 
4226a266c7d5SChris Wilson 		if (iir & I915_USER_INTERRUPT)
4227a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[RCS]);
4228a266c7d5SChris Wilson 		if (iir & I915_BSD_USER_INTERRUPT)
4229a266c7d5SChris Wilson 			notify_ring(dev, &dev_priv->ring[VCS]);
4230a266c7d5SChris Wilson 
4231a266c7d5SChris Wilson 		for_each_pipe(pipe) {
42322c8ba29fSChris Wilson 			if (pipe_stats[pipe] & PIPE_START_VBLANK_INTERRUPT_STATUS &&
423390a72f87SVille Syrjälä 			    i915_handle_vblank(dev, pipe, pipe, iir))
423490a72f87SVille Syrjälä 				flip_mask &= ~DISPLAY_PLANE_FLIP_PENDING(pipe);
4235a266c7d5SChris Wilson 
4236a266c7d5SChris Wilson 			if (pipe_stats[pipe] & PIPE_LEGACY_BLC_EVENT_STATUS)
4237a266c7d5SChris Wilson 				blc_event = true;
42384356d586SDaniel Vetter 
42394356d586SDaniel Vetter 			if (pipe_stats[pipe] & PIPE_CRC_DONE_INTERRUPT_STATUS)
4240277de95eSDaniel Vetter 				i9xx_pipe_crc_irq_handler(dev, pipe);
4241a266c7d5SChris Wilson 
42422d9d2b0bSVille Syrjälä 			if (pipe_stats[pipe] & PIPE_FIFO_UNDERRUN_STATUS &&
42432d9d2b0bSVille Syrjälä 			    intel_set_cpu_fifo_underrun_reporting(dev, pipe, false))
4244fc2c807bSVille Syrjälä 				DRM_ERROR("pipe %c underrun\n", pipe_name(pipe));
42452d9d2b0bSVille Syrjälä 		}
4246a266c7d5SChris Wilson 
4247a266c7d5SChris Wilson 		if (blc_event || (iir & I915_ASLE_INTERRUPT))
4248a266c7d5SChris Wilson 			intel_opregion_asle_intr(dev);
4249a266c7d5SChris Wilson 
4250515ac2bbSDaniel Vetter 		if (pipe_stats[0] & PIPE_GMBUS_INTERRUPT_STATUS)
4251515ac2bbSDaniel Vetter 			gmbus_irq_handler(dev);
4252515ac2bbSDaniel Vetter 
4253a266c7d5SChris Wilson 		/* With MSI, interrupts are only generated when iir
4254a266c7d5SChris Wilson 		 * transitions from zero to nonzero.  If another bit got
4255a266c7d5SChris Wilson 		 * set while we were handling the existing iir bits, then
4256a266c7d5SChris Wilson 		 * we would never get another interrupt.
4257a266c7d5SChris Wilson 		 *
4258a266c7d5SChris Wilson 		 * This is fine on non-MSI as well, as if we hit this path
4259a266c7d5SChris Wilson 		 * we avoid exiting the interrupt handler only to generate
4260a266c7d5SChris Wilson 		 * another one.
4261a266c7d5SChris Wilson 		 *
4262a266c7d5SChris Wilson 		 * Note that for MSI this could cause a stray interrupt report
4263a266c7d5SChris Wilson 		 * if an interrupt landed in the time between writing IIR and
4264a266c7d5SChris Wilson 		 * the posting read.  This should be rare enough to never
4265a266c7d5SChris Wilson 		 * trigger the 99% of 100,000 interrupts test for disabling
4266a266c7d5SChris Wilson 		 * stray interrupts.
4267a266c7d5SChris Wilson 		 */
4268a266c7d5SChris Wilson 		iir = new_iir;
4269a266c7d5SChris Wilson 	}
4270a266c7d5SChris Wilson 
4271d05c617eSDaniel Vetter 	i915_update_dri1_breadcrumb(dev);
42722c8ba29fSChris Wilson 
4273a266c7d5SChris Wilson 	return ret;
4274a266c7d5SChris Wilson }
4275a266c7d5SChris Wilson 
4276a266c7d5SChris Wilson static void i965_irq_uninstall(struct drm_device * dev)
4277a266c7d5SChris Wilson {
42782d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = dev->dev_private;
4279a266c7d5SChris Wilson 	int pipe;
4280a266c7d5SChris Wilson 
4281a266c7d5SChris Wilson 	if (!dev_priv)
4282a266c7d5SChris Wilson 		return;
4283a266c7d5SChris Wilson 
42843ca1ccedSVille Syrjälä 	intel_hpd_irq_uninstall(dev_priv);
4285ac4c16c5SEgbert Eich 
4286a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_EN, 0);
4287a266c7d5SChris Wilson 	I915_WRITE(PORT_HOTPLUG_STAT, I915_READ(PORT_HOTPLUG_STAT));
4288a266c7d5SChris Wilson 
4289a266c7d5SChris Wilson 	I915_WRITE(HWSTAM, 0xffffffff);
4290a266c7d5SChris Wilson 	for_each_pipe(pipe)
4291a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe), 0);
4292a266c7d5SChris Wilson 	I915_WRITE(IMR, 0xffffffff);
4293a266c7d5SChris Wilson 	I915_WRITE(IER, 0x0);
4294a266c7d5SChris Wilson 
4295a266c7d5SChris Wilson 	for_each_pipe(pipe)
4296a266c7d5SChris Wilson 		I915_WRITE(PIPESTAT(pipe),
4297a266c7d5SChris Wilson 			   I915_READ(PIPESTAT(pipe)) & 0x8000ffff);
4298a266c7d5SChris Wilson 	I915_WRITE(IIR, I915_READ(IIR));
4299a266c7d5SChris Wilson }
4300a266c7d5SChris Wilson 
43013ca1ccedSVille Syrjälä static void intel_hpd_irq_reenable(unsigned long data)
4302ac4c16c5SEgbert Eich {
43032d1013ddSJani Nikula 	struct drm_i915_private *dev_priv = (struct drm_i915_private *)data;
4304ac4c16c5SEgbert Eich 	struct drm_device *dev = dev_priv->dev;
4305ac4c16c5SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4306ac4c16c5SEgbert Eich 	unsigned long irqflags;
4307ac4c16c5SEgbert Eich 	int i;
4308ac4c16c5SEgbert Eich 
4309ac4c16c5SEgbert Eich 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
4310ac4c16c5SEgbert Eich 	for (i = (HPD_NONE + 1); i < HPD_NUM_PINS; i++) {
4311ac4c16c5SEgbert Eich 		struct drm_connector *connector;
4312ac4c16c5SEgbert Eich 
4313ac4c16c5SEgbert Eich 		if (dev_priv->hpd_stats[i].hpd_mark != HPD_DISABLED)
4314ac4c16c5SEgbert Eich 			continue;
4315ac4c16c5SEgbert Eich 
4316ac4c16c5SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4317ac4c16c5SEgbert Eich 
4318ac4c16c5SEgbert Eich 		list_for_each_entry(connector, &mode_config->connector_list, head) {
4319ac4c16c5SEgbert Eich 			struct intel_connector *intel_connector = to_intel_connector(connector);
4320ac4c16c5SEgbert Eich 
4321ac4c16c5SEgbert Eich 			if (intel_connector->encoder->hpd_pin == i) {
4322ac4c16c5SEgbert Eich 				if (connector->polled != intel_connector->polled)
4323ac4c16c5SEgbert Eich 					DRM_DEBUG_DRIVER("Reenabling HPD on connector %s\n",
4324ac4c16c5SEgbert Eich 							 drm_get_connector_name(connector));
4325ac4c16c5SEgbert Eich 				connector->polled = intel_connector->polled;
4326ac4c16c5SEgbert Eich 				if (!connector->polled)
4327ac4c16c5SEgbert Eich 					connector->polled = DRM_CONNECTOR_POLL_HPD;
4328ac4c16c5SEgbert Eich 			}
4329ac4c16c5SEgbert Eich 		}
4330ac4c16c5SEgbert Eich 	}
4331ac4c16c5SEgbert Eich 	if (dev_priv->display.hpd_irq_setup)
4332ac4c16c5SEgbert Eich 		dev_priv->display.hpd_irq_setup(dev);
4333ac4c16c5SEgbert Eich 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
4334ac4c16c5SEgbert Eich }
4335ac4c16c5SEgbert Eich 
4336f71d4af4SJesse Barnes void intel_irq_init(struct drm_device *dev)
4337f71d4af4SJesse Barnes {
43388b2e326dSChris Wilson 	struct drm_i915_private *dev_priv = dev->dev_private;
43398b2e326dSChris Wilson 
43408b2e326dSChris Wilson 	INIT_WORK(&dev_priv->hotplug_work, i915_hotplug_work_func);
434199584db3SDaniel Vetter 	INIT_WORK(&dev_priv->gpu_error.work, i915_error_work_func);
4342c6a828d3SDaniel Vetter 	INIT_WORK(&dev_priv->rps.work, gen6_pm_rps_work);
4343a4da4fa4SDaniel Vetter 	INIT_WORK(&dev_priv->l3_parity.error_work, ivybridge_parity_work);
43448b2e326dSChris Wilson 
4345a6706b45SDeepak S 	/* Let's track the enabled rps events */
4346a6706b45SDeepak S 	dev_priv->pm_rps_events = GEN6_PM_RPS_EVENTS;
4347a6706b45SDeepak S 
434899584db3SDaniel Vetter 	setup_timer(&dev_priv->gpu_error.hangcheck_timer,
434999584db3SDaniel Vetter 		    i915_hangcheck_elapsed,
435061bac78eSDaniel Vetter 		    (unsigned long) dev);
43513ca1ccedSVille Syrjälä 	setup_timer(&dev_priv->hotplug_reenable_timer, intel_hpd_irq_reenable,
4352ac4c16c5SEgbert Eich 		    (unsigned long) dev_priv);
435361bac78eSDaniel Vetter 
435497a19a24STomas Janousek 	pm_qos_add_request(&dev_priv->pm_qos, PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
43559ee32feaSDaniel Vetter 
43564cdb83ecSVille Syrjälä 	if (IS_GEN2(dev)) {
43574cdb83ecSVille Syrjälä 		dev->max_vblank_count = 0;
43584cdb83ecSVille Syrjälä 		dev->driver->get_vblank_counter = i8xx_get_vblank_counter;
43594cdb83ecSVille Syrjälä 	} else if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
4360f71d4af4SJesse Barnes 		dev->max_vblank_count = 0xffffffff; /* full 32 bit counter */
4361f71d4af4SJesse Barnes 		dev->driver->get_vblank_counter = gm45_get_vblank_counter;
4362391f75e2SVille Syrjälä 	} else {
4363391f75e2SVille Syrjälä 		dev->driver->get_vblank_counter = i915_get_vblank_counter;
4364391f75e2SVille Syrjälä 		dev->max_vblank_count = 0xffffff; /* only 24 bits of frame count */
4365f71d4af4SJesse Barnes 	}
4366f71d4af4SJesse Barnes 
4367c2baf4b7SVille Syrjälä 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
4368f71d4af4SJesse Barnes 		dev->driver->get_vblank_timestamp = i915_get_vblank_timestamp;
4369f71d4af4SJesse Barnes 		dev->driver->get_scanout_position = i915_get_crtc_scanoutpos;
4370c2baf4b7SVille Syrjälä 	}
4371f71d4af4SJesse Barnes 
437243f328d7SVille Syrjälä 	if (IS_CHERRYVIEW(dev)) {
437343f328d7SVille Syrjälä 		dev->driver->irq_handler = cherryview_irq_handler;
437443f328d7SVille Syrjälä 		dev->driver->irq_preinstall = cherryview_irq_preinstall;
437543f328d7SVille Syrjälä 		dev->driver->irq_postinstall = cherryview_irq_postinstall;
437643f328d7SVille Syrjälä 		dev->driver->irq_uninstall = cherryview_irq_uninstall;
437743f328d7SVille Syrjälä 		dev->driver->enable_vblank = valleyview_enable_vblank;
437843f328d7SVille Syrjälä 		dev->driver->disable_vblank = valleyview_disable_vblank;
437943f328d7SVille Syrjälä 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
438043f328d7SVille Syrjälä 	} else if (IS_VALLEYVIEW(dev)) {
43817e231dbeSJesse Barnes 		dev->driver->irq_handler = valleyview_irq_handler;
43827e231dbeSJesse Barnes 		dev->driver->irq_preinstall = valleyview_irq_preinstall;
43837e231dbeSJesse Barnes 		dev->driver->irq_postinstall = valleyview_irq_postinstall;
43847e231dbeSJesse Barnes 		dev->driver->irq_uninstall = valleyview_irq_uninstall;
43857e231dbeSJesse Barnes 		dev->driver->enable_vblank = valleyview_enable_vblank;
43867e231dbeSJesse Barnes 		dev->driver->disable_vblank = valleyview_disable_vblank;
4387fa00abe0SEgbert Eich 		dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4388abd58f01SBen Widawsky 	} else if (IS_GEN8(dev)) {
4389abd58f01SBen Widawsky 		dev->driver->irq_handler = gen8_irq_handler;
4390abd58f01SBen Widawsky 		dev->driver->irq_preinstall = gen8_irq_preinstall;
4391abd58f01SBen Widawsky 		dev->driver->irq_postinstall = gen8_irq_postinstall;
4392abd58f01SBen Widawsky 		dev->driver->irq_uninstall = gen8_irq_uninstall;
4393abd58f01SBen Widawsky 		dev->driver->enable_vblank = gen8_enable_vblank;
4394abd58f01SBen Widawsky 		dev->driver->disable_vblank = gen8_disable_vblank;
4395abd58f01SBen Widawsky 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4396f71d4af4SJesse Barnes 	} else if (HAS_PCH_SPLIT(dev)) {
4397f71d4af4SJesse Barnes 		dev->driver->irq_handler = ironlake_irq_handler;
4398f71d4af4SJesse Barnes 		dev->driver->irq_preinstall = ironlake_irq_preinstall;
4399f71d4af4SJesse Barnes 		dev->driver->irq_postinstall = ironlake_irq_postinstall;
4400f71d4af4SJesse Barnes 		dev->driver->irq_uninstall = ironlake_irq_uninstall;
4401f71d4af4SJesse Barnes 		dev->driver->enable_vblank = ironlake_enable_vblank;
4402f71d4af4SJesse Barnes 		dev->driver->disable_vblank = ironlake_disable_vblank;
440382a28bcfSDaniel Vetter 		dev_priv->display.hpd_irq_setup = ibx_hpd_irq_setup;
4404f71d4af4SJesse Barnes 	} else {
4405c2798b19SChris Wilson 		if (INTEL_INFO(dev)->gen == 2) {
4406c2798b19SChris Wilson 			dev->driver->irq_preinstall = i8xx_irq_preinstall;
4407c2798b19SChris Wilson 			dev->driver->irq_postinstall = i8xx_irq_postinstall;
4408c2798b19SChris Wilson 			dev->driver->irq_handler = i8xx_irq_handler;
4409c2798b19SChris Wilson 			dev->driver->irq_uninstall = i8xx_irq_uninstall;
4410a266c7d5SChris Wilson 		} else if (INTEL_INFO(dev)->gen == 3) {
4411a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i915_irq_preinstall;
4412a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i915_irq_postinstall;
4413a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i915_irq_uninstall;
4414a266c7d5SChris Wilson 			dev->driver->irq_handler = i915_irq_handler;
441520afbda2SDaniel Vetter 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4416c2798b19SChris Wilson 		} else {
4417a266c7d5SChris Wilson 			dev->driver->irq_preinstall = i965_irq_preinstall;
4418a266c7d5SChris Wilson 			dev->driver->irq_postinstall = i965_irq_postinstall;
4419a266c7d5SChris Wilson 			dev->driver->irq_uninstall = i965_irq_uninstall;
4420a266c7d5SChris Wilson 			dev->driver->irq_handler = i965_irq_handler;
4421bac56d5bSEgbert Eich 			dev_priv->display.hpd_irq_setup = i915_hpd_irq_setup;
4422c2798b19SChris Wilson 		}
4423f71d4af4SJesse Barnes 		dev->driver->enable_vblank = i915_enable_vblank;
4424f71d4af4SJesse Barnes 		dev->driver->disable_vblank = i915_disable_vblank;
4425f71d4af4SJesse Barnes 	}
4426f71d4af4SJesse Barnes }
442720afbda2SDaniel Vetter 
442820afbda2SDaniel Vetter void intel_hpd_init(struct drm_device *dev)
442920afbda2SDaniel Vetter {
443020afbda2SDaniel Vetter 	struct drm_i915_private *dev_priv = dev->dev_private;
4431821450c6SEgbert Eich 	struct drm_mode_config *mode_config = &dev->mode_config;
4432821450c6SEgbert Eich 	struct drm_connector *connector;
4433b5ea2d56SDaniel Vetter 	unsigned long irqflags;
4434821450c6SEgbert Eich 	int i;
443520afbda2SDaniel Vetter 
4436821450c6SEgbert Eich 	for (i = 1; i < HPD_NUM_PINS; i++) {
4437821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_cnt = 0;
4438821450c6SEgbert Eich 		dev_priv->hpd_stats[i].hpd_mark = HPD_ENABLED;
4439821450c6SEgbert Eich 	}
4440821450c6SEgbert Eich 	list_for_each_entry(connector, &mode_config->connector_list, head) {
4441821450c6SEgbert Eich 		struct intel_connector *intel_connector = to_intel_connector(connector);
4442821450c6SEgbert Eich 		connector->polled = intel_connector->polled;
4443821450c6SEgbert Eich 		if (!connector->polled && I915_HAS_HOTPLUG(dev) && intel_connector->encoder->hpd_pin > HPD_NONE)
4444821450c6SEgbert Eich 			connector->polled = DRM_CONNECTOR_POLL_HPD;
4445821450c6SEgbert Eich 	}
4446b5ea2d56SDaniel Vetter 
4447b5ea2d56SDaniel Vetter 	/* Interrupt setup is already guaranteed to be single-threaded, this is
4448b5ea2d56SDaniel Vetter 	 * just to make the assert_spin_locked checks happy. */
4449b5ea2d56SDaniel Vetter 	spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
445020afbda2SDaniel Vetter 	if (dev_priv->display.hpd_irq_setup)
445120afbda2SDaniel Vetter 		dev_priv->display.hpd_irq_setup(dev);
4452b5ea2d56SDaniel Vetter 	spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
445320afbda2SDaniel Vetter }
4454c67a470bSPaulo Zanoni 
44555d584b2eSPaulo Zanoni /* Disable interrupts so we can allow runtime PM. */
4456730488b2SPaulo Zanoni void intel_runtime_pm_disable_interrupts(struct drm_device *dev)
4457c67a470bSPaulo Zanoni {
4458c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4459c67a470bSPaulo Zanoni 
4460730488b2SPaulo Zanoni 	dev->driver->irq_uninstall(dev);
44615d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = true;
4462c67a470bSPaulo Zanoni }
4463c67a470bSPaulo Zanoni 
44645d584b2eSPaulo Zanoni /* Restore interrupts so we can recover from runtime PM. */
4465730488b2SPaulo Zanoni void intel_runtime_pm_restore_interrupts(struct drm_device *dev)
4466c67a470bSPaulo Zanoni {
4467c67a470bSPaulo Zanoni 	struct drm_i915_private *dev_priv = dev->dev_private;
4468c67a470bSPaulo Zanoni 
44695d584b2eSPaulo Zanoni 	dev_priv->pm.irqs_disabled = false;
4470730488b2SPaulo Zanoni 	dev->driver->irq_preinstall(dev);
4471730488b2SPaulo Zanoni 	dev->driver->irq_postinstall(dev);
4472c67a470bSPaulo Zanoni }
4473